473,500 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a shell in C, having trouble with multiple piping

1 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <signal.h>
  8.  
  9. int make_tokenlist(char *buf, char *tokens[])
  10. {
  11.     char *line;
  12.     int i;
  13.  
  14.     i = 0;
  15.  
  16.     line = buf;
  17.     tokens[i] = strtok(line, " ");
  18.     do {
  19.         i++;
  20.         line = NULL;
  21.          tokens[i] = strtok(line," ");
  22.     } while(tokens[i] != NULL);
  23.  
  24.  
  25.     return i;
  26. }
  27. int make_cmdlist(char *buf, char *commands[])
  28. {
  29.     char the_input_line[256];
  30.     char *the_line;
  31.     int j;
  32.  
  33.     j = 0;
  34.  
  35.     the_line = buf;
  36.     commands[j] = strtok(the_line, "|");
  37.     do {
  38.         j++;
  39.         the_line = NULL;
  40.          commands[j] = strtok(the_line,"|");
  41.     } while(commands[j] != NULL);
  42.  
  43.  
  44.     return j;
  45. }
  46. int count_pipes(char *tokens[]){
  47.     int f = 0;
  48.     int numPipes = 0;
  49.     do {
  50.  
  51.         if (strcmp(tokens[f]," | ") == 0)
  52.         {
  53.             numPipes++;
  54.         }
  55.         f++;
  56.  
  57.     }while (tokens[f] != NULL);
  58.     return numPipes;
  59. }
  60.  
  61.  
  62. main(int argc, char **argv, char **envp)
  63. {
  64.     char input_line[256], *tokens[10], *commands[10];
  65.  
  66.     int pid, firstpid, child_status,grandchild_status,q,l, c,z,h,howManyPipes,g,otherpid, cmdIterator,numCmds;
  67.     int fd[5][2];
  68.     char *theArgs[10];
  69.     char *temp;
  70.     int quit = 0;
  71.  
  72.         printf("James> ");
  73.         if(gets(input_line) != NULL)
  74.         {
  75.             z = make_cmdlist(input_line,commands);
  76.             printf("%d",z);
  77.  
  78.  
  79.  
  80.         }else
  81.         {
  82.             printf("huh?\n");
  83.         }
  84.         for(q = 0; q < z; q++){
  85.         if (pipe(fd[q])<0)
  86.         {
  87.             perror("fatal error.");
  88.  
  89.         }
  90.         }        
  91.         pid = fork();
  92.         if (pid < 0)
  93.         {
  94.             perror("fork()");
  95.         }
  96.         if (pid > 0)
  97.         {
  98.             c = wait(&child_status);
  99.  
  100.         }else
  101.         {
  102.  
  103.  
  104.             for (g = 0; g < (z-1); g++)
  105.             {
  106.  
  107.  
  108.                 otherpid = fork();
  109.                 if (otherpid < 0)
  110.                 {
  111.                     perror("fork()");
  112.                 }
  113.                 if (otherpid > 0)
  114.                 {
  115.                     /*wait(&grandchild_status);*/
  116.                 }else{
  117.                     if (g == 0)
  118.                     {
  119.  
  120.                         if (dup2(fd[0][1],STDOUT_FILENO)<0)
  121.                         {
  122.                         perror("can't dup1");
  123.                         }
  124.                         for (l = 0; l < 5; l++)
  125.                         {
  126.                             close(fd[l][0]);
  127.                             close(fd[l][1]);
  128.                         }
  129.  
  130.                     make_tokenlist(commands[g],theArgs);
  131.                     printf("1Executing %s",theArgs);
  132.                     execvp(theArgs[0],theArgs);
  133.  
  134.                     }else{
  135.  
  136.                         if (dup2(fd[g-1][0],STDIN_FILENO)<0)
  137.                         {
  138.                             perror("can't dup2");
  139.                         }
  140.                         if (dup2(fd[g][1],STDOUT_FILENO)<0)
  141.                         {
  142.                             perror("can't dup3");
  143.                         }
  144.                         for (l = 0; l < 5; l++)
  145.                         {
  146.                             close(fd[l][0]);
  147.                             close(fd[l][1]);
  148.                         }
  149.                         make_tokenlist(commands[g],theArgs);
  150.                         printf("2Executing %s",theArgs);
  151.                         execvp(theArgs[0],theArgs);
  152.                     }
  153.  
  154.                 }
  155.  
  156.             }
  157.             if (dup2(fd[0][0],STDIN_FILENO)<0){
  158.             perror("no worky");
  159.             }
  160.             for (l = 0; l < 5; l++)
  161.             {
  162.                 close(fd[l][0]);
  163.                 close(fd[l][1]);
  164.             }
  165.             make_tokenlist(commands[z-1],theArgs);
  166.             printf("3Executing %s", theArgs);
  167.             execvp(theArgs[0],theArgs);        
  168.         }
  169.     return 0;
  170. }
Thanks Again,
James
Sep 30 '08 #1
0 1441

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

Similar topics

0
1298
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...
5
24123
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...
4
2614
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...
7
1832
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...
9
5023
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 ...
0
393
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...
6
7234
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...
0
2092
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...
7
3956
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
0
7136
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,...
1
6906
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...
0
7397
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
5490
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
4923
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
3110
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
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
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
316
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.