pastebin - collaborative debugging tool
513.kpaste.net RSS


UPDATED UPON10/14/09:04PM [BY:VIN]
Posted by Anonymous on Fri 14th Oct 2011 09:14
raw | new post
view followups (newest first): UPDATED UPON10/14/09:14PM [BY:VIN] by Anonymous
modification of post by Anonymous (view diff)

  1. /* CODE WILL TAKE A COMMAND, PASS IT TO CREATEPROCESS(),
  2.  * FORK A CHILD AND EXECUTE COMMAND
  3.  * By John
  4.  *
  5.  */
  6.  
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. char* buffer;
  13. pid_t pid;
  14.  
  15. void startProcess( char* );
  16.  
  17. int main ()
  18. {
  19.         char* input = (char*)malloc( 128 );
  20.         buffer = NULL;
  21.  
  22.         while( 1 )
  23.         {
  24.                 printf( "EECE315_Shell - %s$ ", getcwd(NULL, 0) );
  25.                 gets( input );
  26.                 if( strcmp(input, "exit") == 0 )
  27.                 {
  28.                         printf( "Terminating shell...\n" );
  29.                         return 0;
  30.                 }
  31.                 else if( strcmp(input, "") != 0 )
  32.                 {
  33.                         startProcess( input );
  34.                 }
  35.         }
  36. }
  37.  
  38. void startProcess( char* input )
  39. {
  40.         pid = fork();
  41.  
  42.         if( pid == 0 )
  43.         {
  44.                 execvp( input, NULL );
  45.         }
  46.         else if( pid > 0 )
  47.         {
  48.                 //parent wait  <---- [vin: wtf where is the wait code?]
  49.         }
  50.         else
  51.         {
  52.                 printf( "Fork returned error code, no child" );
  53.         }
  54. }
  55.  
  56. =========================================================================================== BY VIN
  57. /* try running this on it's own to see whats going on. i used a bunch of print statements to show whats going on
  58.  
  59. 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.
  60.  
  61. 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 */
  62.  
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <unistd.h>
  66. #include <string.h>
  67.  
  68. int main ()
  69. {
  70.   char * basepath[100];
  71.   char * pathtoken;
  72.   char * pPath;
  73.   char * temppath;
  74.   int basepathindex;
  75.   int pathfound = 0;  
  76.   pPath = getenv ("PATH");
  77.   temppath = (char *)malloc(strlen(pPath)+1); //allocate memory for the path tokens
  78.   if (temppath!=NULL)
  79.     printf ("The current path is: %s\n",pPath);
  80.     printf ("Splitting string \"%s\" into tokens:\n",pPath);
  81.     strcpy(temppath, pPath);
  82.     pathtoken = strtok (temppath,":"); // string token the PATH by ':'
  83.   while (pathtoken != NULL) // this loops through all the path tokens
  84.   {
  85.     printf ("storing %s in basepath\n",pathtoken);
  86.     basepath[basepathindex] = pathtoken;
  87.     printf ("the value added to the basepath is %s\n", basepath[basepathindex]);
  88.     pathtoken = strtok (NULL, ":");    
  89.     strcpy (temppath, basepath[basepathindex]);
  90.     strcat (temppath, "/");
  91.     strcat (temppath, "ls"); // the "ls" is just a test, this will be replaced with the arguement later on
  92.     printf("the full path of the command is %s \n",temppath);
  93.    if (pathfound == access(temppath, X_OK))  //check if the fullpath if the command can be found
  94.         {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"
  95.         else
  96.         {printf("notfound\n\n");}      
  97.     basepathindex++;
  98.   }  
  99.   return 0;
  100. }
  101. ==============================================================================================

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at