Calling system processes

Karl Heuer karl at haddock.ima.isc.com
Thu Oct 25 13:16:29 AEST 1990


In article <15657 at mentor.cc.purdue.edu> shepherd at mentor.cc.purdue.edu (Mike Shepherd) writes:
>	I was wondering if there is a way to call system processes from a 
>C program, and how it would be done (this like 'ls','cd','more')?

Depends on your operating system; check your manual to see which ones exist.
Likely possibilities are functions named (or starting with) "system", "popen",
"spawn", "exec".  If the command you want to execute is fixed (rather than
something entered by the user), there may already be a C interface to it, and
you should probably use that instead of a subprocess.  It's usually simpler to
do opendir(), readdir(), closedir() than to try to parse the output of "ls".

>I am most interest in the ability to change directories though.

Use chdir().  Assuming a Unix system, you *must* use the C interface in this
case, since the current directory is local to a process.  If you were to do
system("cd foo"), the subshell would change directories and then exit, which
doesn't do you any good.

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list