getting command into Unix Shell

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

getting command into Unix Shell

 
0
  #1
Oct 18th, 2008
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:
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC