Family exec

I have a project that requires the use of the exec family. My project consists of creating an interactive shell. The shell will execute several basic commands, such as cd, ls, echo, etc. I am studying using exec, but have not found a useful site. Any suggested links will help.

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

How can I get the output of this operation to be displayed on the screen?

+4
source share
2 answers

The code you wrote works for me in a simple test program that does nothing. Remember that when you call execl, the process saves all old file descriptors. So no matter what you do when you call execl, it will be the same when loading a new binary. If you just want the output to go to the terminal, just make sure stdout is sent to the terminal.

If you want to do I / O with another program, it is useful to use popen (as mentioned in mgb). It will open a new process, configure the plumbing for you, call some version of exec and return a file descriptor that you can use for communication.

+2
source

make

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

displays output.

+4
source

Source: https://habr.com/ru/post/1277277/


All Articles