/**** * CS 518 : Assignment 4 * Submitted By: Debzani Deb * programmed on Red Hat linux 9 kernel version 2.4.20-8 ***/ #include #include #include #include #include #include #include /* Structure to hold user typed commands */ typedef struct command_struct{ char name[100]; /* Command's maximum length */ int argc; /* Number of arguments */ char args[10][30]; /* Argument array, Each aurgemnet can be 30 character long */ }command; command cmd; /** * Parse the command line and break it down * to appropriate command structure * Acknowledge : M. Muztaba Fuad for this function **/ command parse_command(char *line){ command result; char temp[30],temp1[256]; int i=0,j=0,args=-1; strcpy(temp1,line); /* Tokenize the line */ for(i=0;id_name,file_name)==0){ result=0; break; } return result; } /** * Determines if string 'line' starts with string 'with' **/ int starts_with(char *line, char *with){ size_t len=(size_t) strlen(with); if(strncmp(line,with,len)==0) return 0; /* Yes, 'line' starts 'with' */ else return -1; /* Nope!! */ } /** * Determine if string 'line' ends with character 'with' **/ int ends_with(char *line, char with){ if (line[strlen(line)-1]==with) return 0; /* Yes, 'line' ends 'with' */ else return -1; /* Nope!! */ } /** * Main program **/ int main (int argc, char *argv[]){ char *path_val,t[100],*prompt; int done=1,found_command=0,path_item=0,i=0,j=0,status; char command_line[251],path[MAXPATHLEN],paths[256][256],cmd_to_exe[256]; /* Find the host name, The following three lines are commented */ /* is commented to run in esus */ // prompt=strtok(getenv("HOSTNAME"),"."); /* Form the shell prompt*/ // if (prompt==NULL) // strcpy(prompt, "[TEST]>"); /* Find the value of environment variable 'PATH' */ strcpy(path,getenv("PATH")); /* To look for a command, the program should */ /* look at the current folder and all the */ /* folders listed in the PATH environment variable */ path_val=getenv("PWD"); /* Find path to the current folder */ strcpy(paths[path_item++],path_val); /* copy the path to current folder in paths*/ /* Parse each path from 'PATH' and copy that to paths*/ path_val=strtok(path,": "); while(path_val!=NULL){ strcpy(paths[path_item++],path_val); path_val=strtok(NULL,": "); } /* Run as long as user don't type the 'done' */ while(done){ printf("[Test]>"); /* Read the command and parse it */ fgets(command_line,251,stdin); if (!strcmp(command_line, "done\n")) exit(0); cmd = parse_command(command_line); /* If the path name user gave is not absolute */ /* then construct the absolute path from relative path names */ if ((starts_with(cmd.name,"/")==0) ||(starts_with(cmd.name,"./")==0)|| (starts_with(cmd.name,"../")==0)) found_command = 1; else { for(i=0;i