By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

implementing a bash shell

P: 1
hi all,
im tring to implement a bash shell with some operations like(pipe,redirection etc).im using execvp system call to achieve this.im stuck at redirecting the outut of a command to a file.the output im getting for the follwoing code is "*** ERROR: exec failed\n".....ny help???????????????
Expand|Select|Wrap|Line Numbers
  1. #include  <stdio.h>
  2. #include  <sys/types.h>
  3. #include  <stdlib.h>
  4. #include  <string.h>
  5. #include  <fcntl.h>
  6. void  parse(char *line, char **argv)
  7. {
  8.         while (*line != '\0')
  9.         {
  10.                 while (*line == ' ' || *line == '\t' || *line == '\n')
  11.                         *line++ = '\0';
  12.                 *argv++ = line;
  13.                 while (*line != '\0' && *line != ' ' &&
  14.                                 *line != '\t' && *line != '\n')
  15.                         line++;
  16.         }
  17.         *argv = '\0';
  18. }
  19.  
  20.  
  21. main(void)
  22. {
  23.         char  line[1024];
  24.         char  *argv[64];
  25.         char **envp;
  26.         char command[50][20];
  27.         int inredir=0,outredir=0; //FLAGS FOR CHECKING WHETHER REDIRECTIONS EXIST OR NOT
  28.         char in_redir[100],out_redir[100];
  29.         while (1)
  30.         {
  31.                 printf("Shell -> ");
  32.                 gets(line);
  33.                 printf("\n");
  34.                 parse(line, argv);
  35.                 if (strcmp(argv[0], "exit") == 0)
  36.                         exit(0);
  37.                 //execute(argv);
  38.                 int i=0,j=0;
  39.                 while(argv[i])
  40.                 {
  41.                         //printf("%s\n",argv[i]);
  42.                         if(strcmp(argv[i],"<")==0)
  43.                         {
  44.                                 inredir=1;
  45.                                 strcpy(in_redir,argv[++i]);
  46.                         }
  47.                         else if(strcmp(argv[i],">")==0)
  48.                         {
  49.                                 outredir=1;
  50.                                 strcpy(out_redir,argv[++i]);
  51.                         }
  52.                         else
  53.                                 strcpy(command[j++],argv[i]);
  54.                         i++;
  55.                 }
  56.                 strcpy(command[j],"\0");
  57.                 pid_t  pid;
  58.                 int    status,fd1,fd2;
  59.                 if ((pid = fork()) < 0)
  60.                 {
  61.                         printf("*** ERROR: forking child process failed\n");
  62.                         exit(1);
  63.                 }
  64.                 else if (pid == 0)
  65.                 {
  66.                         if(inredir)
  67.                         {
  68.                                 fd1 = open(in_redir, O_RDONLY);
  69.                                 if (fd1 < 0)
  70.                                 {
  71.                                         perror("catf1f2: f1");
  72.                                         exit(1);
  73.                                 }
  74.  
  75.                                 if (dup2(fd1, 0) != 0)
  76.                                 {
  77.                                         perror("catf1f2: dup2(f1, 0)");
  78.                                         exit(1);
  79.                                 }
  80.                                 close(fd1);
  81.                         }
  82.                         if(outredir)
  83.                         {
  84.                                 fd2 = open(out_redir, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  85.                                 if (fd2 < 0)
  86.                                 {
  87.                                         perror("catf1f2: f2");
  88.                                         exit(2);
  89.                                 }
  90.                                 if (dup2(fd2, 1) != 1)
  91.                                 {
  92.                                         perror("catf1f2: dup2(f2, 1)");
  93.                                         exit(1);
  94.                                 }
  95.                                 close(fd2);
  96.                         }
  97.                         if (execvp(command[0],command))
  98.                         {
  99.                                 printf("*** ERROR: exec failed\n");
  100.                                 exit(1);
  101.                         }
  102.                 }
  103.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
  104.  
im using a gcc compiler on fc6...
Aug 11 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.