Advertisement
kmb180

UNIX Shell

Feb 12th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. //Kevin Brough
  2. //UNIX Shell
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <dirent.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. void parse(char *line, char *argv[]){ //we'll go through and parse our command line
  13.   argv[0] = strtok(line, " ()|&;\t\n");
  14.   int i;
  15.   for(i = 1; i < 19; i = i++) {
  16.     argv[i] = strtok(NULL, " ()|&;\t\n");
  17.     if (argv[i] == NULL){
  18.         break;
  19.     }  
  20.   }
  21.   argv[19] = NULL;//making sure to set the end to null
  22. }
  23. int main(void){
  24.     char buffer[100];
  25.     int argc;
  26.     char *argv[20];
  27.     int status, pid;
  28.     int redirectOut, redirectIn, appendOut;
  29.     while(1){
  30.         redirectOut = redirectIn = appendOut = 0;//resetting the booleans for cleanliness
  31.         printf("> ");
  32.         fgets(buffer, 100, stdin);
  33.         parse(buffer, argv);
  34.         if ((strcmp("exit", argv[0])) == 0){//first two built in functions
  35.             exit(0);
  36.         }  
  37.         else if(!strcmp("cd", argv[0])){
  38.             if(chdir(argv[1]) != 0) {
  39.                 printf("Cannot change dir\n");
  40.             }  
  41.         }
  42.         else{
  43.             for (argc=1; argv[argc] != 0; argc++){//checking the commandline arguments to see if there's redirection etc.
  44.                 if (!strcmp(argv[argc], ">>")){//the checking
  45.                     appendOut = 1;//making sure we set the boolean value
  46.                     argv[argc] = 0;//saving the args then breaking out
  47.                     break;
  48.                 }
  49.                 else if(!strcmp(argv[argc], ">")){
  50.                     redirectOut  = 1;
  51.                     argv[argc] = 0;
  52.                     break;
  53.                 }
  54.                 else if(!strcmp(argv[argc], "<")){
  55.                     redirectIn  = 1;
  56.                     argv[argc] = 0;
  57.                     break;
  58.                 }
  59.             }
  60.             pid = fork();
  61.             if (pid == 0){//child process
  62.                 if (appendOut){//first we check which redirect/append it is
  63.                     if (!freopen(argv[argc+1], "ab", stdout)){//do the coresponding freopen and check if it's null
  64.                         fprintf(stderr, "Error: Failed to append output to: ");//and do the error handling
  65.                         perror(argv[argc+1]);
  66.                         exit(-1);
  67.                     }
  68.                 }//repeat for the next two
  69.                 else if (redirectOut){
  70.                     if (!freopen(argv[argc+1], "wb", stdout)){
  71.                         fprintf(stderr, "Error. Failed to redirect output to: ");
  72.                         perror(argv[argc+1]);
  73.                         exit(-1);
  74.                     }
  75.                 }
  76.                 else if (redirectIn){
  77.                     if (!freopen(argv[argc+1], "rb", stdin)){
  78.                         fprintf(stderr, "Error. Failed to redirect input from: ");
  79.                         perror(argv[argc+1]);
  80.                         exit(-1);
  81.                     }
  82.                 }
  83.                 execvp(argv[0], argv);//where all the work is done
  84.                 perror(argv[0]);
  85.                 exit(1);
  86.             }
  87.             else if (pid > 0){//parent process
  88.                 wait(&status);
  89.             }
  90.             else{//should the unimaginable happen
  91.                 perror("fork");
  92.             }  
  93.         }
  94.     }
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement