Solved

shell program errors!

Posted on 2002-03-04
4
287 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
Comment Utility
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
Comment Utility
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
Comment Utility
pkwan,

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

kavvang
0
 

Author Comment

by:kavvang
Comment Utility
Thank you very much!!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now