tags:

views:

254

answers:

2
+3  Q: 

The exec family

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?

+4  A: 

doing

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

gets the output to the screen.

Jose Vega
+2  A: 

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.

Jay Conrod