[comp.lang.c] Calling system processes

shepherd@mentor.cc.purdue.edu (Mike Shepherd) (10/25/90)

	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')?  I am
most interest in the ability to change directories though.

	Also, is there a way to call other system programs like 'vi' or
'mail'?  Is it used different than simple system calls?

Thanks,
Mike
-- 
*   Mike Shepherd	(shepherd@mentor.cc.purdue.edu)			      *
*******************************************************************************
* Sometimes I think the surest sign that intelligent life exists elsewhere in *
* the universe is that none of it has tried to contact us.    --Calvin of C&H *

karl@haddock.ima.isc.com (Karl Heuer) (10/25/90)

In article <15657@mentor.cc.purdue.edu> shepherd@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@ima.isc.com or uunet!ima!karl), The Walking Lint

jh4o+@andrew.cmu.edu (Jeffrey T. Hutzelman) (10/26/90)

Use system(3).  This should be able to run most anything the user
running your program can run.
-----------------
Jeffrey Hutzelman
America Online: JeffreyH11
Internet/BITNET:jh4o+@andrew.cmu.edu, jhutz@drycas.club.cc.cmu.edu,
                jh4o@cmuccvma

>> Apple // Forever!!! <<

george@hls0.hls.oz (George Turczynski) (10/29/90)

In article <15657@mentor.cc.purdue.edu>, shepherd@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')?  I am
> most interest in the ability to change directories though.

Just to quickly answer this last one, though perhaps countless others will also,
use the `chdir()' system call to change directories.

As for the rest, read the manual entries on system(), fork(), execve(), wait()
and so on...


-- 
George P. J. Turczynski,   Computer Systems Engineer. Highland Logic Pty Ltd.
ACSnet: george@highland.oz |^^^^^^^^^^^^^^^^^^^^^^^^| Suite 1, 348-354 Argyle St
Phone:  +61 48 683490      |  Witty remarks are as  | Moss Vale, NSW. 2577
Fax:    +61 48 683474      |  hard to come by as is | Australia.
---------------------------   space to put them !    ---------------------------