AcidG3rm5 -4 Light Poster

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.