Solved

shell program errors!

Posted on 2002-03-04
4
289 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

777 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