/*
 * Exercise 3-8
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#define MAX 256
#define CMD_MAX 10

int validate(char  *candidate);
void execute(char **cmd_input);
char *promt_cmd();

char *valid_cmds=" ls  ps  df  cd  lo  pwd  ";
int main()
{
        char line_input[MAX], the_cmd[CMD_MAX];
        char *new_args[CMD_MAX], *cp;
/*        char *promt_cmd();*/
        int i,j;
        int child_wait_status;

        while(1)
        {
	       prompt_cmd();	
             //  if(gets(line_input) != NULL)
                {
                        cp=line_input;
                        i=0;
                        if((new_args[i]=strtok(cp, " ")) != NULL)
                        {
                                sprintf(the_cmd, "%s ", new_args[i]);
                                if(validate(the_cmd) == 0)
                                {
                                        do
                                        {
                                                ++i;
                                                cp=NULL;
                                                new_args[i]=strtok(cp, " ");
                                        } while(i<CMD_MAX-1 && new_args[i] != NULL);

                                        new_args[i]=NULL;
					if(strcmp(new_args[0], "lo") == 0)
						exit(0);
					else if(strcmp(new_args[0], "cd") == 0)
						chdir(new_args[1]);
					else 
						execute(new_args);

                                }
                                else
                                        printf("%s was not understood?\n", the_cmd);
  
                      }
                }
        }
}

int validate(char *command)
{
        if((strstr(valid_cmds, command) - valid_cmds) % 4 == 1)
                return(0);
	else
	{
		printf("%s was not understood?\n", command);
        	return(1);
	}
}

void execute(char **cmdline)
{
	int child_stat;
	switch(fork())
        {
	        case 0:
                	execvp(cmdline[0], cmdline);
                        perror("execvp failure");
                        exit(1);
                case -1:
                        perror("fork failure");
                        break;
                default:
                        wait(&child_stat);
        }                           
}

char *prompt_cmd()
{
	char *line_in;
	printf("cmd> ");
	if(gets(line_in) != NULL)
	return line_in;
	else 
	return (char *) NULL;
}


	
/*adimitro@alef:/student/adimitro/cwork/ch3> ./a.out
cmd> ls
10.c             child            ex3-6.c          parent
11.c             ex3-10_child.c   ex3-8.c          test.c
a.out            ex3-10_parent.c  ex3-9.c
cmd> ps
   PID TTY      TIME CMD
 20278 pts/5    0:00 bash
 20296 pts/5    0:00 a.out
cmd> df
/                  (/dev/dsk/c0t0d0s0 ):13242292 blocks   823978 files
/usr               (/dev/dsk/c0t0d0s3 ):16420178 blocks  1088998 files
/proc              (/proc             ):       0 blocks      431 files
/dev/fd            (fd                ):       0 blocks        0 files
/etc/mnttab        (mnttab            ):       0 blocks        0 files
/var/run           (swap              ): 5345296 blocks   222368 files
/tmp               (swap              ): 5345296 blocks   222368 files
/staff             (/dev/dsk/c0t8d0s7 ):69622974 blocks  4330946 files
/work              (/dev/dsk/c0t0d0s7 ): 1093218 blocks   331183 files
/opt               (/dev/dsk/c0t0d0s6 ): 1395674 blocks   481671 files
/student           (/dev/dsk/c0t10d0s7):63455424 blocks  4233998 files
/usr/local         (/dev/dsk/c0t0d0s5 ):11205152 blocks  1087605 files
/var/mail          (/dev/dsk/c0t0d0s4 ):13331162 blocks   827720 files
cmd> pwd
/student/adimitro/cwork/ch3
cmd> cd ..
cmd> pwd
/student/adimitro/cwork
cmd> cd ch3
cmd> pwd
/student/adimitro/cwork/ch3
cmd> lo
adimitro@alef:/student/adimitro/cwork/ch3>
*/
