[comp.unix.programmer] system

jik@athena.mit.edu (Jonathan I. Kamens) (10/23/90)

  I fail to see how this has anything to do with Unix internals.  It is
clearly a question about programming under Unix, which means that it clearly
belongs in comp.unix.programmer.

  I've cross-posted to comp.unix.programmer and directed followups there.

In article <24826@adm.BRL.MIL>, vilva@csvax.csc.lsu.edu (Vilva Natarajan) writes:
|> I am trying to do system("man cat | wc -c") in my program, and i need
|> to store the value returned by "wc" in a variable declared in my program.

  Use popen("man cat | wc -c", "r"), which will return a FILE *, and you can
read the output of wc from the FILE *.  See popen(3).

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

6600bori@ucsbuxa.ucsb.edu (Boris Burtin) (02/22/91)

Can anyone tell me why I get a core dump
when I try to do :

 system("last [username] > ~/filename");

Thanks...
	- Boris Burtin

jik@athena.mit.edu (Jonathan I. Kamens) (02/22/91)

In article <9321@hub.ucsb.edu>, 6600bori@ucsbuxa.ucsb.edu (Boris Burtin) writes:
|> Can anyone tell me why I get a core dump
|> when I try to do :
|> 
|>  system("last [username] > ~/filename");

  I have no idea why you get a coredump, and there's no way really for us to
tell.  Have you attempted to analyze the coredump with a debugger to see what
it's from?  Is it your process coredumping, or is it the shell that's being
started?  I don't get a coredump on my machine.  What type of machine are you
using, running what version of Unix?

  In any case, I suspect that at least part of the problem is that the bourne
shell, which is what system uses, doesn't understand "~".  Change the "~" to
"$HOME".  Observe:

	1 -> extern int system(char *);
	2 -> system("last jik > ~/lastjik");
	Linking from '/lib/libc.a' ... Linking completed.
	sh: ~/lastjik: cannot create
	(int) 256
	3 -> system("last jik > $HOME/lastjik");
	(int) 0

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

emmonsl@athena.ecs.csus.edu (L. Scott Emmons) (03/03/91)

In article <9321@hub.ucsb.edu> 6600bori@ucsbuxa.ucsb.edu (Boris Burtin) writes:
>Can anyone tell me why I get a core dump
>when I try to do :
>
> system("last [username] > ~/filename");

News has been stacked up here, so there's probably already been a reply
to this (since it's been a week since your original post), but
anyway...

system() uses /bin/sh to execute the command, and sh doesn't know ~.

Also, you are _much_ better off doing a fork() and then an exec()
instead of a system()...system just does the equivalent of a fork() and
an exec(), just _much_ less efficiently...If you need some help on
using fork() and exec() please send me some mail and I'll be happy to
give you a hand. (The reason that this is more efficient is because with
exec() you don't fork a shell, you just execute the program directly).

There is a story (well, a truth) around campus here.  Someone needed to
delete a file via a program.  Instead of using unlink() they
system("rm") the file...  rm is just a program which calls unlink(),
but with millions of times the overhead, because they used system()
(ok, well not quite that much)...Anyway, avoid system() where exec() or
a direct system call will do.

Hope this offers you some help...

			L. Scott Emmons
			---------------
	emmons@csus.csus.edu  <or>  ...[ucbvax]!ucdavis!csus!emmons
		Packet: kc6nfp@kg6xx.#nocal.ca.usa.na

emmonsl@athena.ecs.csus.edu (L. Scott Emmons) (03/04/91)

In article <1991Mar2.181423.13981@csusac.csus.edu> I wrote:
>Also, you are _much_ better off doing a fork() and then an exec()
>instead of a system()...system just does the equivalent of a fork()

[...]

Ack, I made a bit of an error in this post.  You will also need to set
up a pipe() to properly handle the redirection.  I assume you were going
to take the file then and read it back into the program?  If so, a pipe()
works well for this.

Sorry for the mistake...

			L. Scott Emmons
			---------------
	emmons@csus.csus.edu  <or>  ...[ucbvax]!ucdavis!csus!emmons
		Packet: kc6nfp@kg6xx.#nocal.ca.usa.na