DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   getting command into Unix Shell (http://www.daniweb.com/forums/thread151957.html)

AcidG3rm5 Oct 18th, 2008 11:25 am
getting command into Unix Shell
 
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:
int main()
{
        char cmd[100];
        int num=1;
        int counter=1;
        int status;
        pid_t pid;       

        do
          {
                cout<<"UOWUnix>";
                cin.getline(cmd,100);
               
                //strcmp will return o if the word quit is found in the system.               
                counter = strcmp(cmd, "QUIT");
               
                //when the counter is 0, quits the system.               
                if(counter==0)
                {
                  cout<<"Good bye"<<endl;
                }//end if

                //execute all the command here.               
                else
                {
                        pid=fork();
                       
                        //tells the parent to wait here. How to get the status of the child!?!?                       
                        if(pid>0)
                        {
                                //wait((int *) 0)
                                cout<<"in parent!!! child id is "<<pid<<endl;       
                        }
                        //This is the child. start to do the work here. For now can only do one command, ls only.
                        else if(pid==0)
                        {       
                        execlp(cmd, cmd,(char *)0);
                        exit(0);
                        }       
                }               
          }//end do
        while (counter !=0);
}//end main


All times are GMT -4. The time now is 2:26 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC