/* Tanel Toova * 7-1 * * */ #include #include #include #include #include #define TRUE 1 #define FALSE 0 char command[80]; char prg[80]; char prgarg[80]; char *commands[]={"list","help","quit","users","calender",NULL}; char *real_commands[]={"ls","","","w","cal",NULL}; char *arguments[]={"-l","","","-s","-m",NULL}; pid_t fork_result; int i; int wcommand; int abi; //proccess to show help on screen int help() { fprintf(stdout, "\nLegaalsed käsklused on:\n" "CALENDER - Näitab kalendrit, kus nädala algust arvestatakse esmaspäevast\n" "USERS - kõigi sisse loginud kasutajate kuvamine\n" "LIST - kõigi kataloogis olevate failide kuvamine\n" "HELP - abiinfo (st. see mida Sa praegu loed)\n" "QUIT - programmi töö lõpetamine\n\n"); } int main(void) { while(strcasecmp(command,"quit")!=0){ //while given command is not "quit" fprintf(stdout,"Anna käsklus: \n"); fprintf(stdout,">"); scanf("%s",command); wcommand=TRUE; //command is assumed to be wrong abi=FALSE; //it's assumed that help is not asked //compares given commands whith known ones for(i=0;commands[i]!=NULL;i++){ if(strcasecmp(command,commands[i])==0){ strcpy(prg,real_commands[i]); strcpy(prgarg,arguments[i]); wcommand=FALSE; //since command was recognised it's not wrong if(i==1){ //if the command was "help" wcommand=TRUE; abi=TRUE; help(); } } } //if wrong command was given if(wcommand==TRUE && abi==FALSE){fprintf(stdout,"VIGA! Käsklus %s on tundmatu!\n",command);} //if right command and not command "help" was given if(wcommand==FALSE && abi==FALSE){fork_result=fork();} if(fork_result==-1 && wcommand==FALSE){ fprintf(stderr,"VIGA! Child protsessi ei õnnestunud luua ...\n"); exit(1); } //if fork was success if(fork_result==0 && wcommand==FALSE){ execlp(prg,prg,prgarg,NULL); }else{ wait(NULL); //parent wait's for child to end } } return(0); }