473,503 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implementing a bash shell

1 New Member
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
0 2962

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

Similar topics

3
8237
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
3
2659
by: William Park | last post by:
I'm very excited to announce shell interface to GTK+2 (2.6.1) for Bash. It read XML syntax describing the widget layout, and returns user selection as shell variable or runs shell command as...
11
2977
by: Magnus Jonneryd | last post by:
Hi, I'm planning on writing a program that interactively is fed input via a shell (bash). I also want to be able to write a shell script that executes various commands related to my program. In...
7
5555
by: chakkaradeepcc | last post by:
HI all, How to execute bash scripts from python (other than using os.popen) and get the values that those bash scripts return. I would be happy if someone could help me out in this.. thanks...
16
3919
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of...
14
4869
keyvanrahmadi
by: keyvanrahmadi | last post by:
Sorry if this post is rather long but hopefully you wont get bored half way through. I have a project in hand which i have started and have a deadline of 1 week. Basically what i need to do is to...
10
4414
by: Isaac Gouy | last post by:
$ ./test_fail 1 Floating point exception - but this leaves 'somefile' zero size $ ./test_fail 1>somefile Floating point exception
1
3246
by: Fredrik Lundh | last post by:
John Lawrence wrote: doesn't exactly work for Python scripts, though: $ cat env.py #!/usr/bin/env python import os os.environ = "hello"
6
1862
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions ...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.