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??????????????? -
#include <stdio.h>
-
#include <sys/types.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <fcntl.h>
-
void parse(char *line, char **argv)
-
{
-
while (*line != '\0')
-
{
-
while (*line == ' ' || *line == '\t' || *line == '\n')
-
*line++ = '\0';
-
*argv++ = line;
-
while (*line != '\0' && *line != ' ' &&
-
*line != '\t' && *line != '\n')
-
line++;
-
}
-
*argv = '\0';
-
}
-
-
-
main(void)
-
{
-
char line[1024];
-
char *argv[64];
-
char **envp;
-
char command[50][20];
-
int inredir=0,outredir=0; //FLAGS FOR CHECKING WHETHER REDIRECTIONS EXIST OR NOT
-
char in_redir[100],out_redir[100];
-
while (1)
-
{
-
printf("Shell -> ");
-
gets(line);
-
printf("\n");
-
parse(line, argv);
-
if (strcmp(argv[0], "exit") == 0)
-
exit(0);
-
//execute(argv);
-
int i=0,j=0;
-
while(argv[i])
-
{
-
//printf("%s\n",argv[i]);
-
if(strcmp(argv[i],"<")==0)
-
{
-
inredir=1;
-
strcpy(in_redir,argv[++i]);
-
}
-
else if(strcmp(argv[i],">")==0)
-
{
-
outredir=1;
-
strcpy(out_redir,argv[++i]);
-
}
-
else
-
strcpy(command[j++],argv[i]);
-
i++;
-
}
-
strcpy(command[j],"\0");
-
pid_t pid;
-
int status,fd1,fd2;
-
if ((pid = fork()) < 0)
-
{
-
printf("*** ERROR: forking child process failed\n");
-
exit(1);
-
}
-
else if (pid == 0)
-
{
-
if(inredir)
-
{
-
fd1 = open(in_redir, O_RDONLY);
-
if (fd1 < 0)
-
{
-
perror("catf1f2: f1");
-
exit(1);
-
}
-
-
if (dup2(fd1, 0) != 0)
-
{
-
perror("catf1f2: dup2(f1, 0)");
-
exit(1);
-
}
-
close(fd1);
-
}
-
if(outredir)
-
{
-
fd2 = open(out_redir, O_WRONLY | O_TRUNC | O_CREAT, 0644);
-
if (fd2 < 0)
-
{
-
perror("catf1f2: f2");
-
exit(2);
-
}
-
if (dup2(fd2, 1) != 1)
-
{
-
perror("catf1f2: dup2(f2, 1)");
-
exit(1);
-
}
-
close(fd2);
-
}
-
if (execvp(command[0],command))
-
{
-
printf("*** ERROR: exec failed\n");
-
exit(1);
-
}
-
}
-
}
-
im using a gcc compiler on fc6... 0 2962 Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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
|
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"
|
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
...
|
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,...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |