Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!amdcad!ames!lll-tis!ptsfa!rtech!cpsc6a!cpsc6b!crs From: crs@cpsc6b.cpsc6a.att.com (Chris (I'm Outta Here!) Seaman) Newsgroups: comp.lang.c Subject: Re: system (and mkdir) Message-ID: <447@cpsc6b.cpsc6a.att.com> Date: 22 Jan 88 21:42:02 GMT References: <127@dcrbg1.UUCP> <9472@ccicpg.UUCP> <2771@cbdkc1.ATT.COM> Organization: AT&T (CPSC), Oakland, CA Lines: 42 Keywords: c program Summary: What I use it for... In article <2771@cbdkc1.ATT.COM>, pmd@cbdkc1.ATT.COM (Paul Dubuc) writes: < < In article <9472@ccicpg.UUCP> miket@ccicpg.UUCP (Mike Tracy) writes: < }In article <127@dcrbg1.UUCP> bcf2303@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@cpsc6a.att.com | || See "Attack of the Killer Smiley"! ..!ihnp4!cpsc6a!crs | \vvvvvv/ Coming Soon to a newsgroup near you! | \____/