Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* simple shell, parses batches of commands separated by pipes and executes them in separate processes */
- /* useful characters: [] # */
- # include <stdio.h>
- # include <string.h>
- # include <fcntl.h> // for O_RDONLY
- # include <stdlib.h> // for exit()
- # include <unistd.h> // for execlp()
- # define errormsg(x) { puts(x); exit(1); }
- # define SIZE 1024
- int main(int argc, char * argv[])
- {
- char *cmd[SIZE]; // stores the current command
- int index = 0; // read index
- int quit = 0;
- int processes = 0; // active processes
- int status; // child or parent process
- char c;
- printf("Type command: ");
- while(!quit && c != EOF)
- {
- index++;
- c = getchar();
- if(c != '\0' && c != '|')
- cmd[index] = c;
- if(c == '|') // executes command and empties buffer
- {
- int pid = fork();
- processes++; // process added
- if(pid != 0) wait(&status);
- execlp(cmd, cmd, 0);
- cmd = ""; // COMPILATION ERROR here
- }
- if(strcmp(cmd, "quit\n") == 0) quit = 1;
- }
- printf("Batch executed\n");
- int i;
- for(i = 0; i < processes; i++) // waits until all processes have terminated
- wait(&status);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement