This is just a suggestion on how to approach the proposed solution. For two specific actions you are asking about:
touch : you can achieve a similar effect by opening and closing the file you want to touch- be careful not to use the
O_TRUNC flag on open
ls : this is a bit more painful because you will need to use dirent.h on POSIX systems and go dirent.h resultsopendirreaddir- Be sure to call
closedir when you are done.
If you want to replace system and popen calls with something equivalent using vfork , there is some caution. Calling vfork little more difficult to use, because if the child does anything other than exec right after the call, you can corrupt the state of the parent's memory.
To replace the system :
- create a helper program to execute the string provided in the arguments
- a helper program can either call
system on the supplied argument, or parse the command line and call exec
- in your
system replacement function, create an arg vector to call the helper program and pass it to the program line that you really want to execute as an argument to that program. - after calling
vfork , you immediately exec helper program in child - parent is waiting for completion for the child
In your popen replacement:
- create a helper program that takes
stdout and stdin file descriptors as arguments and the command line you want to execute- the helper program will duplicate the characters
0 or 1 (or both) passed in the descriptors, as indicated by the arguments - a helper program can execute a line with
popen and proxy data between the parent and the child, or call exec after parsing the command line
- in your
popen replacement function, use pipe to create a stdout or stdin communication channel (according to the second parameter of the popen function) and create an arg vector to invoke the helper program, passing in the corresponding file descriptor number and command line as arguments - after calling
vfork , you immediately exec helper program in child - you'll need
pclose replacement to use child process
source share