Solved

shell program errors!

Posted on 2002-03-04
4
288 Views
Last Modified: 2010-04-15
Dear experts,

I am implement a simplified shell program that will accept '@' as the filter character. When @ is used, I should call my own routine to create a pipeline to connect two process. This program is support to accept more than one filter like:

ls -l @ sort @ wc @....

I have completely written the codes and the program is compile fine but when I run it gives me the following errors:

here is the codes:

#include <stdio.h>
#define TRUE 1
#define STD_INPUT 0
#define STD_OUTPUT 1
   
int main (int argc, char** argv) {
   char command[80] = "";
   
   while (!(strcmp (command, "quit") == 0)) {
      printf ("SHELL$ ");
      gets (command);
      if (!(strcmp (command, "") == 0)) {
         if (!(strcmp (command, "quit") == 0)) {
            parseCommand (command);
         }
      }
   }
   printf ("Good, bye!\n");
     
   return 0;
}
parseCommand (char c[]) {
   char c1[80] = "", c2[80] = "";
   int i = 0, j = 0;
   
   while ((c[i] != '@') && (c[i] != '\n') && (i < 80)) {
      c1[i] = c[i];
      i++;  
   }
   if (c1[i - 1] == ' ') {
      c1[i - 1] = NULL;
   }
     
   i = i + 2;
   for (; i < 80; i++) {
      c2[j] = c[i];
      j++;
   }
       if (c2[0] != NULL) {
      pipeLine (c1, c2);
   }
}  
   
   
pipeLine (char c1[], char c2[]) {
   int fd[2];
   pipe (fd);
   if (fork () == 0) {          // Child
      close (fd[0]);
      close (STD_OUTPUT);
      dup (fd[1]);
      close (fd[1]);
      execlp ("sh", "sh", "-c", c1, NULL);
   }else {
      close (fd[1]);
      close (STD_INPUT);
      dup (fd[0]);
      close (fd[0]);  
      execlp ("sh", "sh", "-c", c2, NULL);
      parseCommand (c2);
   }
}  
   

Here is what I have when run the program:

SHELL$ ls -l @ sort wc
sort: can't stat wc: No such file or directory

0
Comment
Question by:kavvang
  • 3
4 Comments
 

Author Comment

by:kavvang
ID: 6840338
also like this:

SHELL$ ls -l @ sort @ wc
sort: can't stat @: No such file or directory
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 50 total points
ID: 6840639
You should swap the parseCommand(c2) and the execlp() function in your Pipeline(), since you need to parse the remaining command before you execute each one. Here is the working version of your code:

pipeLine (char c1[], char c2[]) {
  int fd[2];
  pipe (fd);
  if (fork () == 0) {          // Child
     close (fd[0]);
     close (STD_OUTPUT);
     dup (fd[1]);
     close (fd[1]);
     execlp ("sh", "sh", "-c", c1, NULL);
  }else {
     close (fd[1]);
     close (STD_INPUT);
     dup (fd[0]);
     close (fd[0]);  
     parseCommand (c2);
     execlp ("sh", "sh", "-c", c2, NULL);
  }
}  
0
 

Author Comment

by:kavvang
ID: 6840718
pkwan,

Thank you very much....I will test my code again and will give the points soon!

kavvang
0
 

Author Comment

by:kavvang
ID: 6843597
Thank you very much!!!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now