Solved

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

Posted on 2004-05-01
7
320 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
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…
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 opening and reading files in the C programming language.

786 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