Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- char **parse_cmdline( const char *cmdline ){
- int command_count = 0;
- char * pch;
- char *copy = malloc(100);
- strcpy(copy, cmdline);
- pch = strtok(copy, " ");
- char **commands = malloc(sizeof(char)*500);
- while (pch != NULL){
- commands[command_count++] = pch;
- pch = strtok (NULL, " ,.-");
- }
- commands[command_count] = NULL;
- return commands;
- }
- int main(){
- char *buffer = malloc(100);
- int length = 0;
- int processes = 0;
- pid_t pid;
- int c;
- char **commands = malloc(sizeof(char)*500);
- while ((c = getchar()) != EOF){
- if(c == '\n'){
- commands = parse_cmdline(buffer);
- processes++;
- pid = fork();
- if(pid == 0){
- execv(commands[0] ,commands);
- }
- memset(buffer,0,strlen(buffer));
- length = 0;
- }
- else buffer[length++] = c;
- }
- for(int i = 0; i < processes; i++){
- wait(NULL);
- }
- free(buffer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement