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 1441 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...
|
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...
|
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...
|
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...
|
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
...
| |
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...
|
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...
|
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...
|
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: 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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
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...
| | |