Solved

Need someone for linux C code compiler & Fork() Code Problem

Posted on 2004-05-01
7
322 Views
Last Modified: 2010-04-02
1) I dont have a linux C compiler..so i need someone to compile the code for me and let me know if & where the errors are.

2) This is what im trying to do, and ive done a bit, but not sure if its right. Ive not done the commands part as yet. The 3 commands are List / Count / Quit

1st Function:
- Fork()
- Wait for child to finish if should_wait = true, if its false it should not wait

2nd Function:
- Read line from stdin
- Does commands:
    If the user typed LIST_FILES_COMMAND then it passes "ls" to do_command.
    If the user typed COUNT_COMMAND then it passes "./count" to do_command.
    If the user typed QUIT_COMMAND then it returns true.
    Otherwise it prints "I don't know what \"%s\" means.\n"
    Returns false.
- If user types in WAIT_COMMAND then sets should_wait to true & prints "Baby shell will wait for command to finish."
- If the user types in DONT_WAIT_COMMAND then sets should_wait to false & prints "Baby shell will NOT wait for command to finish."
- Passes should_wait to do_command.

Main function:
- Call 2nd Function until it returns false
- Returns 0 back to the OS

*************************************
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

using namespace      std;

const      int      MAX_LINE_LEN                  = 256;

#define            WAIT_COMMAND                  "wait"
#define            DONT_WAIT_COMMAND            "dont_wait"
#define            LIST_FILES_COMMAND            "list"
#define            COUNT_COMMAND                  "count"
#define            QUIT_COMMAND                  "quit"

static bool should_wait = false;

void do_command (const char* command, bool should_wait = false)
{
      int p;
      p = fork();
      
      if (p==-1)
      {
            printf("Fork Failed");
            exit(0);
      }

      else if (p==0)
      {
            execlp(command,command,NULL);
            printf("Whoa! the child could do %s!\n",command);
            exit(-1);
      }
      
      if (should_wait == true)
      {
            wait(NULL);
            printf("Baby shell will wait for command to finish");
      }
      else if  (should_wait == false)
      {
            printf("Baby shell will NOT wait for command to finish");
      }
      
      printf("Child %d worked on it!\n", getpid());
}


bool get_and_do_commands_until_should_quit()
{
      char line[MAX_LINE_LEN] ;
      
      printf("What next? ");
      
      fgets(line,MAX_LINE_LEN,stdin);

      char* ptr = strchr(line,'\n');

      if  (ptr != NULL)
      *ptr = '\0';
      do_command (line,should_wait);
      return false;
}

int main()
{
      while(1)
      {
            get_and_do_commands_until_should_quit();
      }
      return 0;
}
**********************************************************      

0
Comment
Question by:newguy2004
  • 3
  • 3
7 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10970822
test.c:8: parse error before `namespace'
test.c:8: warning: data definition has no type or storage class
test.c:18: parse error before `should_wait'
test.c:18: `false' undeclared here (not in a function)
test.c:18: warning: data definition has no type or storage class
test.c:20: parse error before `bool'
test.c: In function `do_command':
test.c:33: `command' undeclared (first use in this function)
test.c:33: (Each undeclared identifier is reported only once
test.c:33: for each function it appears in.)
test.c:38: `true' undeclared (first use in this function)
test.c:43: `false' undeclared (first use in this function)
test.c: At top level:
test.c:52: parse error before `get_and_do_commands_until_should_quit'
test.c: In function `get_and_do_commands_until_should_quit':
test.c:60: parse error before `char'
test.c:62: `ptr' undeclared (first use in this function)
test.c:65: `false' undeclared (first use in this function)
0
 

Author Comment

by:newguy2004
ID: 10973737
Well, ok so how to complete the function so i can get rid for the errors related with function 1 and 2?
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10981099
This is a C++ program :)

compiled with g++ -Wall yourprog.cpp

No error, no warning

Congratulations :)
0
Technology Partners: 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!

 
LVL 10

Expert Comment

by:Mercantilum
ID: 10981126
Oh by the way, it works :)
0
 

Author Comment

by:newguy2004
ID: 10982497
Yea but it doesnt do what its suppose to do. If you see function 2 on my first post, and compare it with the code, thats is the bool get_and_do_commands_until_should_quit() command, it is incomplete, and im lost there.
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 110 total points
ID: 10982558
I think you did the most difficult part,

In get_and_do_commands_until_should_quit()

you just have to compare the input string with your commands

e.g.
#include <string.h>
static char *commands[] = {     "LIST_FILES_COMMAND",
    "COUNT_COMMAND",
    "QUIT_COMMAND",
   "WAIT_COMMAND",
"DONT_WAIT_COMMAND" };

{....    
     fgets(line,MAX_LINE_LEN,stdin);

     char* ptr = strchr(line,'\n');

     if  (ptr != NULL)  *ptr = '\0';


   int i;
   for (i=0 ; i<5 && strcmp(line, commands[i]) ; i++) /* */ ;
  else switch(i)
  {
    case 0: do the list files case ; break;
    case 1: do the count ; break;
    ...
    case 3: set should_wait variable to true ; break
...
    default: printf ("dont understand");
   }  

     do_command (line,should_wait);
     return false;
}
0
 

Author Comment

by:newguy2004
ID: 10992436
I cant compile it, so i dont know it it will work 100%, but i have to complete this week, so whatever u helped me with, i'll try to put it together..& give u the points..
0

Featured Post

Technology Partners: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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.
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.

733 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