473,803 Members | 3,833 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 1464

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

Similar topics

0
1318
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
5
24158
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?
4
2635
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...
7
1855
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...
9
5044
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
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 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
6
7258
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()"?
0
2130
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...
7
3975
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
9562
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,...
0
10542
Oralloy
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...
0
10068
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 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...
0
9119
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, 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...
1
7600
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 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...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
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
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
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 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...

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.