Hi,
I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any help at all would be greatly appreciated. - #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <sys/types.h>
-
#include <sys/wait.h>
-
#include <signal.h>
-
-
int make_tokenlist(char *buf, char *tokens[])
-
{
-
char *line;
-
int i;
-
-
i = 0;
-
-
line = buf;
-
tokens[i] = strtok(line, " ");
-
do {
-
i++;
-
line = NULL;
-
tokens[i] = strtok(line," ");
-
} while(tokens[i] != NULL);
-
-
-
return i;
-
}
-
int make_cmdlist(char *buf, char *commands[])
-
{
-
char the_input_line[256];
-
char *the_line;
-
int j;
-
-
j = 0;
-
-
the_line = buf;
-
commands[j] = strtok(the_line, "|");
-
do {
-
j++;
-
the_line = NULL;
-
commands[j] = strtok(the_line,"|");
-
} while(commands[j] != NULL);
-
-
-
return j;
-
}
-
int count_pipes(char *tokens[]){
-
int f = 0;
-
int numPipes = 0;
-
do {
-
-
if (strcmp(tokens[f]," | ") == 0)
-
{
-
numPipes++;
-
}
-
f++;
-
-
}while (tokens[f] != NULL);
-
return numPipes;
-
}
-
-
-
main(int argc, char **argv, char **envp)
-
{
-
char input_line[256], *tokens[10], *commands[10];
-
-
int pid, firstpid, child_status,grandchild_status,q,l, c,z,h,howManyPipes,g,otherpid, cmdIterator,numCmds;
-
int fd[5][2];
-
char *theArgs[10];
-
char *temp;
-
int quit = 0;
-
-
printf("James> ");
-
if(gets(input_line) != NULL)
-
{
-
z = make_cmdlist(input_line,commands);
-
printf("%d",z);
-
-
-
-
}else
-
{
-
printf("huh?\n");
-
}
-
for(q = 0; q < z; q++){
-
if (pipe(fd[q])<0)
-
{
-
perror("fatal error.");
-
-
}
-
}
-
pid = fork();
-
if (pid < 0)
-
{
-
perror("fork()");
-
}
-
if (pid > 0)
-
{
-
c = wait(&child_status);
-
-
}else
-
{
-
-
-
for (g = 0; g < (z-1); g++)
-
{
-
-
-
otherpid = fork();
-
if (otherpid < 0)
-
{
-
perror("fork()");
-
}
-
if (otherpid > 0)
-
{
-
/*wait(&grandchild_status);*/
-
}else{
-
if (g == 0)
-
{
-
-
if (dup2(fd[0][1],STDOUT_FILENO)<0)
-
{
-
perror("can't dup1");
-
}
-
for (l = 0; l < 5; l++)
-
{
-
close(fd[l][0]);
-
close(fd[l][1]);
-
}
-
-
make_tokenlist(commands[g],theArgs);
-
printf("1Executing %s",theArgs);
-
execvp(theArgs[0],theArgs);
-
-
}else{
-
-
if (dup2(fd[g-1][0],STDIN_FILENO)<0)
-
{
-
perror("can't dup2");
-
}
-
if (dup2(fd[g][1],STDOUT_FILENO)<0)
-
{
-
perror("can't dup3");
-
}
-
for (l = 0; l < 5; l++)
-
{
-
close(fd[l][0]);
-
close(fd[l][1]);
-
}
-
make_tokenlist(commands[g],theArgs);
-
printf("2Executing %s",theArgs);
-
execvp(theArgs[0],theArgs);
-
}
-
-
}
-
-
}
-
if (dup2(fd[0][0],STDIN_FILENO)<0){
-
perror("no worky");
-
}
-
for (l = 0; l < 5; l++)
-
{
-
close(fd[l][0]);
-
close(fd[l][1]);
-
}
-
make_tokenlist(commands[z-1],theArgs);
-
printf("3Executing %s", theArgs);
-
execvp(theArgs[0],theArgs);
-
}
-
return 0;
-
}
Thanks Again,
James
0 1464 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: DPhelps |
last post by:
I have a multithreaded Python shell server (base on the sample code
'pysvr.py') that uses a C-based extension class. The trouble I'm having
is that I cannot figure out a way to create a Python object from the C
code. I'll detail the methods I've looked at and the troubles I'm
having:
Method 1)
Ideally, I could get PyRun_String to work, however I cannot figure out
|
by: Bill |
last post by:
I used to be able to run the following ASP code on our corp machine (W2K
Server Edition and IIS-5)
and successfully send a net-msg to anyone on our intranet.
Last week it stopped working... and I'm not sure what changed.
(I had applied ALL the W2K update patches... but... I'm not sure if the
problem started before or after that.)
Did any recent W2K patches change the way createObject, wScript.shell, or
"NET SEND" works?
|
by: Andrew Chanter |
last post by:
I have a VB app that has a routine that runs macros in a (password
protected) Access 97 db. (I have attached the code below.) Most of these
macros end up opening a form in the Access db for the user to interract
with.When the user finishes in the Access session they quit and are returned
to the VB app. Problem is that in many cases, when the user quits the
Access session, an Access 'shell' remains in the taskbar that cannot be
removed...
|
by: Susan Bricker |
last post by:
Greetings. As a relative newcomer to Access, I am having trouble
deciding on how to design the form flow for updating and creating
related records. I'm looking for a variety of suggestions so that I can
decide what's best for the user and what's best for screen flow.
Here's the structure: I have what's called "an Event". Each Event can
have multiple "Trials". Each "Trial" can multiple "Classes".
(This is the structure for a dog...
|
by: Tommy Lu |
last post by:
Hi, wondering if there is a way to interact the shell command with the
C# program? For example, if I type
c:\>ver
it then suppose to return the version of the OS I am currently using...
or
c:\>systeminfo
| |
by: Vladimir Nesterovsky |
last post by:
Hello,
I'm having a trouble with a Shell.Folder object when I'm trying to use it
from a C++ application, but at the same time, the same object works fine
from within html script.
The effect is that, I cannot put data into a compressed folder in C++, in
spite of the fact that all operations succeed.
Note: all this have to work under XP and above, because of integrated
|
by: Adam Tilghman |
last post by:
Hi all,
I have found that IE doesn't seem to respect the <SELECT> "multiple"
attribute when set using DOM methods, although the attribute/property
seems to exist and is updated properly. Those changes just don't make
it onto the screen.
Am I doing something wrong here?
If not, is there a better feature test I can use than
"appName.match()"?
|
by: =?Utf-8?B?TGlhbSBNYWM=?= |
last post by:
Hi Folks,
I have embeded WMI scripting within a Visual Basic application to create
remote shares and set permissions, I'm now moving to vb.net environment and
having trouble getting my scripting to work, I have search the net for vb.net
code to create shared folders and set permsission but no joy, if anyone can
help or recommend good web sites on this or is there anyway I can get my
exisiting code to work in vb.net please see code below...
|
by: Alex K |
last post by:
Hello,
Does anyone know if the python shell supports paging or if I should
look into iPython? Thank you so much.
Alex
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
| | |