Advertisement
lamiastella

redirection almost working

Sep 26th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <assert.h>
  8. #include <signal.h>  //for kill
  9. #include <fcntl.h>
  10.  
  11. void execute(char **argv)
  12. {
  13.  
  14.   int status;
  15.   int pid = fork();
  16.   if (pid  <0)  //fork error has happened
  17.      {
  18.        perror("Can't fork a child process\n");
  19.        exit(EXIT_FAILURE);
  20.      }
  21.   if (pid==0)   //It's the child process and can run his task
  22.      {
  23.     execvp(argv[0],argv);
  24.         perror("error");  
  25.      }
  26.   else  //pid>0 it's the parent and should wait for the child
  27.      {
  28.         int status;
  29.        // int wc = wait(&status);
  30.        // assert(wc != -1);
  31.  
  32.       //while(wait(&status)!=pid)
  33.       //   ;
  34.         wait(NULL);  //the way taught in the class
  35.      }
  36.  
  37.  
  38. }
  39.  
  40. int main (int argc, char **argv)
  41.  
  42. {  char cwd[100];
  43.    char input[512];
  44.    char *args[512];
  45.    char **next = args;
  46.    char *temp;
  47.    getcwd(cwd,sizeof(cwd));
  48.    while (1)
  49.    {  
  50.       chdir(cwd);
  51.       printf("mysh> ");
  52.       fgets(input,512,stdin);
  53.       input[strlen(input) - 1] = 0;
  54.       temp = strtok(input, " ");
  55.       while(temp != NULL)
  56.              {
  57.                      //printf("%s\n", temp);
  58.                      *next++ = temp;
  59.                      temp = strtok(NULL, " ");
  60.                    
  61.               }      
  62.             *next = NULL;
  63.             next=args;
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. ///////////////////////////////////
  71.  
  72.  int i;    
  73.      for (i=1; args[i];i++)
  74.          {
  75.            if (strcmp(args[i],">")==0)
  76.              {
  77.                printf("argv[i] %s %d \n", args[i], i);
  78.                int out = open(args[i+1], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
  79.                close(1);
  80.                int fdl=dup2(out,1);                
  81.                close(out);  
  82.                execvp(args[0],args);
  83. //               open(STDOUT, ">>", \$out);
  84.              }
  85.  
  86.          }
  87.  
  88.  
  89.  
  90.  
  91. ////////////////////////////
  92.  
  93.    
  94.      if (strcmp(args[0], "exit")==0)
  95.             // exit(EXIT_SUCCESS);
  96.             exit(0);
  97.      if (strcmp(args[0], "cd")==0)
  98.           {
  99.            printf("argv[1] is  %s \n", args[1]);
  100.                if ((!args[1]) || (strcmp(args[1],"~")==0))
  101.                  {
  102.                     chdir(getenv("HOME"));
  103.                     getcwd(cwd,sizeof(cwd));
  104.                  }
  105.                else
  106.                 {
  107.                    chdir(args[1]);
  108.                    getcwd(cwd,sizeof(cwd));
  109.                 }
  110.  
  111.  
  112.           }
  113.       else
  114.        {
  115.  
  116.         execute(args);
  117.       }
  118.      
  119.    }
  120.  
  121.    return 0;
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement