Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include "doCommand.h"
  2.  
  3. int countComm(char * line)
  4. {
  5.     int count=0;
  6.     char *token=strdup(line);
  7.     strtok(token, " ");
  8.     while(token!=NULL)
  9.     {
  10.        token=strtok(NULL," " );
  11.         count++;
  12.     }
  13.     return count;
  14. }
  15.  
  16. char **argVconst(char *line)
  17. {
  18.  
  19.   char** arrCommands=malloc(sizeof(char*)*(countComm(line)+1));
  20.   char* token=strdup(line);
  21.   token=token+1;
  22.   strtok(token,"\n");
  23.   char* nextArg=token;
  24.   int i=0;
  25.   nextArg=strtok(token," ");
  26.   while(nextArg!=NULL)
  27.   {
  28.     //printf("##%s##\n", nextArg);
  29.     arrCommands[i]=strdup(nextArg);
  30.     i++;
  31.     nextArg=strtok(NULL, " ");
  32.   }
  33.   return arrCommands;
  34. }
  35.  
  36.  
  37. bool doCommand(char *line)
  38. {
  39.     char** command=argVconst(line);
  40.     //printf("%s\n", "testCommand");
  41.     pid_t process = fork();
  42.     if(process == 0) //child process
  43.     {
  44.         //printf("%s\n", "Hello child");
  45.         fclose(file); //close file to avoid infinite loop
  46.         execvp(command[0],command);
  47.     }
  48.     else if(process > 0) //for the parent process
  49.     {
  50.         //printf("%s\n", "Are we here?");
  51.  
  52.         wait(NULL);
  53.     }
  54.     else if(process<0) //Process DNE
  55.     {
  56.         //printf("%s\n", "before you do");
  57.         exit(1);
  58.     }
  59.  
  60.  
  61.   return true;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement