hi all,
im tring to implement a bash shell with some operations like(pipe,redir ection 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".....n y 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 2967 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 does not print the file to the printer. It
displays the following as the return from Runtime...:
java.lang.UNIXProcess@1034bb5
|
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 callback. It's designed for
simple GUI dialog or layout, with the emphasis on getting the user data
back into shell.
For the moment, the shell variable and command are disabled. It just
prints to stdout, instead. But, you can change it easily.
|
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 short i want to provide input to
a program using some (or all) of the functionality found in bash.
It's mainly the format of the file I'm having trouble with. I wanted to be
able to write something like this:
#!/bin/bash
for x in xs
|
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 in advance..
With Regards,
|
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 the normal bash
commands (e.g. print instead of echo). My main reason for asking is that
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.
Thanks.
| |
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 create a shell script in Bash for the game Hangman. I am sure everyone is well aware of the game. The down side is that i can not use "sed" or "awk". FUN FUN
I am at my ends wit now and truley utterly lost, so any help would go a long way to stop...
|
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
I need to write a program that creates real application/FTP accounts
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |