while(true){
cout<<"Shell$ ";
getline(cin,cmd);
strcpy(argv[0], cmd.c_str());
if(cmd=="exit" ){
exit(EXIT_SUCCESS);
}
pid_t pid=fork();
if(pid==0){
//exec
execlp("/usr/bin/", argv[0]);//here it does not display the out
}
//wait for child
else{
wait(pid);
}
}
Isn't the first argument of execlp usually the executable to run rather than a directory? (as ls is a shell comment the exe is the shell, which means you arg vector needs to be sorted out, too.)
Why do you want to copy the command to argv[0] and then pass the copy? Pass it directly.
Read the man page for execlp. In particular:
The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
The fact an exec function returns at all indicates an error.
As andywestken mentioned the first argument to execlp is not a directory but the executable, for example execlp("/bin/ls", "/bin/ls", "-al", 0);
no, I am to use execlp but for example pipes, and chdir I have to use by system calls...so for example the user types cd /usr/bin it must use chdir in cpp code to change directory....but with bash as you shown, using cd only does the job
thanks, I used that already...but now I need to implement a functionality in my shell that knows how to launch new programs in the foreground and the background.