system (and mkdir)

Chris I'm Outta Here! Seaman crs at cpsc6b.cpsc6a.att.com
Sat Jan 23 07:42:02 AEST 1988


In article <2771 at cbdkc1.ATT.COM>, pmd at cbdkc1.ATT.COM (Paul Dubuc) writes:
< 
< In article <9472 at ccicpg.UUCP> miket at ccicpg.UUCP (Mike Tracy) writes:
< }In article <127 at dcrbg1.UUCP> bcf2303 at dcrbg1.UUCP (Wing Chow) writes:
< }>
< }>can someone give me an example of how to use 'system' in a c program?
< }
< }The best example I have is the mkdir command...
< }
< }	system( "mkdir mydir" );
< No, you don't have to you system() for this. You can use fork()/exec().
< 
< Paul Dubuc	{ihnp4,cbosgd}!cbdkc1!pmd

You are correct about using fork()/exec() to call mkdir, at least in
System V.

As for what system is good for, try this one:

You have a program, which accepts input from a user (or a user created
file), then wants to 'fork and exec' their input as a command.  If
their command is a binary executable, no problem.  You can parse
their input to determine command and arguments, initialize appropriate
arrays, and call fork()/exec().  But, if their command is a shell
script, exec() will fail.  A script must be exec'ed as an argument
to the appropriate shell (/bin/sh, /bin/csh, /bin/ksh, etc.).  You
can make a few tests in your program, to see what kind of file
it is (assuming you can find it), and then appropriately call their
program directly, or as an argument to the shell.  However, it is
sometimes easier (when you aren't sure), and involves less coding
(if you are in a hurry), to use system().  Some benchmarks I have
tried showed little performance improvement of a program that used
fork()/exec(), versus one that used system().

Don't get me wrong.  I *do* use fork()/exec()/wait() whenever possible,
but sometimes...

-- 
Chris Seaman            |    o\  /o
crs at cpsc6a.att.com <or> |      ||         See "Attack of the Killer Smiley"!
..!ihnp4!cpsc6a!crs     |   \vvvvvv/     Coming Soon to a newsgroup near you!
                        |    \____/ 



More information about the Comp.lang.c mailing list