Solved

shell program errors!

Posted on 2002-03-04
4
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

730 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