system() question
L. Scott Emmons
emmonsl at athena.ecs.csus.edu
Sun Mar 3 04:14:23 AEST 1991
In article <9321 at hub.ucsb.edu> 6600bori at 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 at csus.csus.edu <or> ...[ucbvax]!ucdavis!csus!emmons
Packet: kc6nfp at kg6xx.#nocal.ca.usa.na
More information about the Comp.unix.programmer
mailing list