For an assignment, I am to write a program that will work as a command line interpreter, accepting any command, creating a new process with fork()
, and using the child process to execute the command with exec()
. The problem is I don't know how to use the exec()
family of commands, and searching online has proven to be of little help. I am working in C++ on a Linux server at my school.
From this post, I think I want execlp()
, but I am really not sure, so please correct me if I'm wrong. When I try this:
string s = "/bin/" + command;
execlp(s, command, NULL);
just to see if it works with a simple command and no arguments, like ls
, I cannot even get it to compile, receiving this error:
shell.cpp:52: error: cannot convert âstd::stringâ to âconst char*â for argument â1â to âint execlp(const char*, const char*, ...)â
when attempting to pass arguments as strings. Annoyingly, our textbook does the exact same thing with string literals, and works successfully, with the statement execlp("/bin/ls", "ls", NULL);
.
Clearly, I don't know how the function is meant to be used, so what I am asking is can someone offer advice or provide a resource as to which exec()
I should use and how I can use it for this purpose?