Advertisement
lamiastella

bg ok python multiopt ok

Sep 28th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 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. int bg_flag=0;
  12. int py_flag=0;
  13. int arg_count=0;
  14. void execute(char **argv)
  15. {
  16.   printf("%d \n",bg_flag);
  17.   int status;
  18.   int pid = fork();
  19.   if (pid  <0)  //fork error has happened
  20.      {
  21.        perror("Can't fork a child process\n");
  22.        exit(EXIT_FAILURE);
  23.      }
  24.   if (pid==0)   //It's the child process and can run his task
  25.      {
  26.      
  27.       if (py_flag==1)
  28.          {
  29.            int i;
  30.            char *new_argv[arg_count];
  31.            new_argv[0]="/usr/bin/python";
  32.            for (i=1; i<=arg_count;i++)
  33.             {
  34.               new_argv[i]=argv[i-1];http://pastebin.com/SMJkgSJH  bg job working, python multi-opt working
  35.               printf("%s new argv[%d] is \n",new_argv[i],i);
  36.            }
  37.            new_argv[arg_count+1]=0;
  38.            //new_argv[1]=argv[0];
  39.            //new_argv[2]=0;
  40.            execvp(new_argv[0],new_argv);
  41.          }
  42.        
  43.       printf("%d \n", bg_flag);
  44.       if (bg_flag==1)
  45.         {
  46.           printf("bg \n");
  47.           execvp(argv[0],argv);
  48.         }
  49.       else
  50.        {
  51.        
  52.         execvp(argv[0],argv);
  53.         perror("error");  
  54.        }
  55.      }
  56.   else  //pid>0 it's the parent and should wait for the child
  57.      {
  58.         if (bg_flag==1)
  59.           {
  60.           }
  61.         else
  62.            wait(NULL);
  63.      }
  64. }
  65.  
  66.  
  67. int main (int argc, char **argv)
  68.  
  69. {  
  70.    FILE *bfd=NULL;  //batch file descriptor
  71.    char cwd[100];  //current working directory
  72.    int batch_mode=0;  
  73.    char input[512];
  74.    char *args[512];
  75.    char **next = args;
  76.    char *temp;
  77.    
  78.    getcwd(cwd,sizeof(cwd));
  79.    if (argv[1]!=0)
  80.       {
  81.        batch_mode=1;
  82.        if((bfd = fopen(argv[1], "r")) == NULL)
  83.             {
  84.                     printf("Error Opening File.\n");
  85.                     exit(1);
  86.             }
  87.       }  
  88.  
  89.      
  90.     chdir(cwd);
  91.     if (batch_mode)
  92.          {
  93.            while( fgets(input, sizeof(input), bfd) != NULL )
  94.             {
  95.                  arg_count=0;
  96.                  write(STDOUT_FILENO,input, strlen(input));
  97.                  input[strlen(input) - 1] = 0;
  98.                  temp = strtok(input, " ");
  99.                  while(temp != NULL)
  100.                   {
  101.                      *next++ = temp;
  102.                      temp = strtok(NULL, " ");
  103.                      arg_count++;
  104.  
  105.                   }
  106.                  *next = NULL;
  107.                  next=args;
  108.                  execute(args);
  109.  
  110.             }
  111.              fclose(bfd);
  112.          }
  113.    else  //in shell mode
  114.    while (1)
  115.       {
  116.         {
  117.            arg_count=0;
  118.            printf("mysh> ");
  119.            fgets(input,512,stdin);
  120.            input[strlen(input) - 1] = 0;
  121.            temp = strtok(input, " ");
  122.            while(temp != NULL)
  123.              {
  124.                      //printf("%s\n", temp);
  125.                      *next++ = temp;
  126.                      temp = strtok(NULL, " ");
  127.                      arg_count++;
  128.                    
  129.               }      
  130.             *next = NULL;
  131.             next=args;
  132.  
  133.             if (strstr(args[0],".py") !=NULL) //is our first argument a python program?
  134.                py_flag=1; //if yes set the py_flag
  135.             if (strcmp(args[0], "exit")==0)  //is our first argument "exit" command?
  136.                  exit(0);
  137.             if (strcmp(args[0], "cd")==0)  //is our first argument "cd" command?
  138.              {
  139.                  if ((!args[1]) || (strcmp(args[1],"~")==0)) //is the second argument for "cd" command NULL or ~?
  140.                   {
  141.                     chdir(getenv("HOME")); //change the current directory to home
  142.                     getcwd(cwd,sizeof(cwd));
  143.                   }
  144.                  else
  145.                   {
  146.                    chdir(args[1]);  //otherwise change the current directory to the given directory
  147.                    getcwd(cwd,sizeof(cwd));
  148.                   }
  149.              }
  150.             if (strcmp(args[0],"wait")==0)
  151.                   wait(NULL);
  152.             if (strstr(args[arg_count-1],"&")!=NULL)
  153.                   {
  154.                    printf("%s", args[arg_count-1]);
  155.                    bg_flag=1;
  156.                    args[arg_count-1]=NULL;
  157.                    execute(args);
  158.                    printf("bg_flag is %d \n",bg_flag);
  159.                
  160.                   }
  161.  
  162.             else
  163.              {
  164.                 execute(args);
  165.              }
  166.  
  167.            }
  168.        }
  169.  
  170.   return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement