Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "doCommand.h"
- int countComm(char * line)
- {
- int count=0;
- char *token=strdup(line);
- strtok(token, " ");
- while(token!=NULL)
- {
- token=strtok(NULL," " );
- count++;
- }
- return count;
- }
- char **argVconst(char *line)
- {
- char** arrCommands=malloc(sizeof(char*)*(countComm(line)+1));
- char* token=strdup(line);
- token=token+1;
- strtok(token,"\n");
- char* nextArg=token;
- int i=0;
- nextArg=strtok(token," ");
- while(nextArg!=NULL)
- {
- //printf("##%s##\n", nextArg);
- arrCommands[i]=strdup(nextArg);
- i++;
- nextArg=strtok(NULL, " ");
- }
- return arrCommands;
- }
- bool doCommand(char *line)
- {
- char** command=argVconst(line);
- //printf("%s\n", "testCommand");
- pid_t process = fork();
- if(process == 0) //child process
- {
- //printf("%s\n", "Hello child");
- fclose(file); //close file to avoid infinite loop
- execvp(command[0],command);
- }
- else if(process > 0) //for the parent process
- {
- //printf("%s\n", "Are we here?");
- wait(NULL);
- }
- else if(process<0) //Process DNE
- {
- //printf("%s\n", "before you do");
- exit(1);
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement