Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

shell program errors!

Posted on 2002-03-04
4
Medium Priority
?
294 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Suggested Courses

604 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