943,740 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 975
  • C++ RSS
Oct 18th, 2008
0

getting command into Unix Shell

Expand Post »
Hi guys. I'm doing a programming, whereby I have to display a prompt of my own on the linux terminal. Example commandprompt>.

The program will then wait for user's input, example "ls -ali | more" or "ls -ali" and etc.

I will be using the following system call in my program namely, fork(), execlp(), wait().

I'm having problem now waiting the wait and execlp. I've no idea how to use the function wait, since i have no idea how i could get the status of my child process.

About using execlp, how do i make it robust? as in the user could be keying any amount of command. They could be piping a few things. like maybe... "ls -ali | more | ... | ..." or it could be simply be one command.

my codes:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. char cmd[100];
  4. int num=1;
  5. int counter=1;
  6. int status;
  7. pid_t pid;
  8.  
  9. do
  10. {
  11. cout<<"UOWUnix>";
  12. cin.getline(cmd,100);
  13.  
  14. //strcmp will return o if the word quit is found in the system.
  15. counter = strcmp(cmd, "QUIT");
  16.  
  17. //when the counter is 0, quits the system.
  18. if(counter==0)
  19. {
  20. cout<<"Good bye"<<endl;
  21. }//end if
  22.  
  23. //execute all the command here.
  24. else
  25. {
  26. pid=fork();
  27.  
  28. //tells the parent to wait here. How to get the status of the child!?!?
  29. if(pid>0)
  30. {
  31. //wait((int *) 0)
  32. cout<<"in parent!!! child id is "<<pid<<endl;
  33. }
  34. //This is the child. start to do the work here. For now can only do one command, ls only.
  35. else if(pid==0)
  36. {
  37. execlp(cmd, cmd,(char *)0);
  38. exit(0);
  39. }
  40. }
  41. }//end do
  42. while (counter !=0);
  43. }//end main
Last edited by Ancient Dragon; Oct 18th, 2008 at 12:33 pm. Reason: add code tags
Similar Threads
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Airline Reservation Portal
Next Thread in C++ Forum Timeline: Error using recursion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC