Question

I have a project the requires the use of the exec family. My project consist of making an interactive shell. The shell will implement a few basic commands like cd, ls, echo, etc. I have been researching the use of exec, but have not found a useful site. Any suggested links would help.

int ret;
ret = execl ("/bin/ls", "ls", "-1", (char *)0);

How would i get the output of this operation to show on the screen?

Was it helpful?

Solution

The code you wrote works for me in a simple test program that does nothing else. Remember, when you call execl, the process retains all of the old file handles. So whatever stdout was when you call execl, it will be the same when the new binary is loaded. If you just want the output to go to the terminal, just make sure stdout goes to the terminal.

If you want to do I/O with another program, popen is good for this (as mgb mentioned). It will fork a new process, set up plumbing for you, call some variant of exec, and return a file handle you can use for communication.

OTHER TIPS

doing

int fd = 1;
dup(fd);
close(fd);

gets the output to the screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top