/* CODE WILL TAKE A COMMAND, PASS IT TO CREATEPROCESS(), * FORK A CHILD AND EXECUTE COMMAND * By John * */ #include #include #include #include char* buffer; pid_t pid; void startProcess( char* ); int main () { char* input = (char*)malloc( 128 ); buffer = NULL; while( 1 ) { printf( "EECE315_Shell - %s$ ", getcwd(NULL, 0) ); gets( input ); if( strcmp(input, "exit") == 0 ) { printf( "Terminating shell...\n" ); return 0; } else if( strcmp(input, "") != 0 ) { startProcess( input ); } } } void startProcess( char* input ) { pid = fork(); if( pid == 0 ) { execvp( input, NULL ); } else if( pid > 0 ) { //parent wait <---- [vin: wtf where is the wait code?] } else { printf( "Fork returned error code, no child" ); } } =========================================================================================== BY VIN /* try running this on it's own to see whats going on. i used a bunch of print statements to show whats going on This will be part of the main and it take in the COMMAND NAME from the argument passed from the PARSER and it will search through PATH to find if that command exists, if it exists it will RUN that command with EXECV else it fails and prints a message. temppath serves two purposes 1) it allocates area for the tokens to be stored in and is passed down to the pathtoken which holds the tokens and each token is passed to the basepath[basepathindex] where each adress of the path is stored. 2) the temppath acts as the full path containing the basepath from PATH and the COMMAND */ #include #include #include #include int main () { char * basepath[100]; char * pathtoken; char * pPath; char * temppath; int basepathindex; int pathfound = 0; pPath = getenv ("PATH"); temppath = (char *)malloc(strlen(pPath)+1); //allocate memory for the path tokens if (temppath!=NULL) printf ("The current path is: %s\n",pPath); printf ("Splitting string \"%s\" into tokens:\n",pPath); strcpy(temppath, pPath); pathtoken = strtok (temppath,":"); // string token the PATH by ':' while (pathtoken != NULL) // this loops through all the path tokens { printf ("storing %s in basepath\n",pathtoken); basepath[basepathindex] = pathtoken; printf ("the value added to the basepath is %s\n", basepath[basepathindex]); pathtoken = strtok (NULL, ":"); strcpy (temppath, basepath[basepathindex]); strcat (temppath, "/"); strcat (temppath, "ls"); // the "ls" is just a test, this will be replaced with the arguement later on printf("the full path of the command is %s \n",temppath); if (pathfound == access(temppath, X_OK)) //check if the fullpath if the command can be found {printf("!!YAY!! command found in this path\n\n");} //fork, execv, wait function will be placed in here as the command is found (john's "start process" most-likely goes in this block" else {printf("notfound\n\n");} basepathindex++; } return 0; } ==============================================================================================BY VIN //This is a basic shell wrapper, run it and check out what it does. #include #include #include int main(int argc, char *argv[], char *envp[]) { char c = '\0'; printf("\n[315 NINJA TURTLE SHELLS] "); while(c != EOF) { c = getchar(); if(c == '\n') printf("[315 NINJA TURTLE SHELLS ] "); } printf("\n"); return 0; }