[comp.unix.questions] easy, basic, question...

wolfe@neon.stanford.edu (Michael R. Wolfe) (08/19/89)

I know how to call Unix commands from C:  system(string), but
I'm having trouble coming up with the commands in string form
from inside of a program.  That is, I'm being forced to do
multiple strcat's to form a string to send to system.  Is there a
way around this?

In addition, is there any way to find the result of a unix command
called from C without having to send it to a file and then open
the file.  For example, I'm trying to use grep from C, which forces
me to make a string such as "grep file >> anotherfile" using strcat,
use "system" to call it, and then go and open "anotherfile" afterwards
to find the result.  Has anyone come up with a better way?

-Mike

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/19/89)

In article <11072@neon.Stanford.EDU> wolfe@neon.stanford.edu (Michael R. Wolfe) writes:
-... That is, I'm being forced to do multiple strcat's to form a
-string to send to system.  Is there a way around this?

Yes:  Use a single sprintf() into a character array of sufficnet size,
then call system with that array name as its argument.

-In addition, is there any way to find the result of a unix command
-called from C without having to send it to a file and then open
-the file.

See popen(3S) in your UNIX Programmer's Reference Manual.

bzs@bu-cs.BU.EDU (Barry Shein) (08/21/89)

From: wolfe@neon.stanford.edu (Michael R. Wolfe)
>I know how to call Unix commands from C:  system(string), but
>I'm having trouble coming up with the commands in string form
>from inside of a program.  That is, I'm being forced to do
>multiple strcat's to form a string to send to system.  Is there a
>way around this?

sprintf() should help a lot with this.

>In addition, is there any way to find the result of a unix command
>called from C without having to send it to a file and then open
>the file.  For example, I'm trying to use grep from C, which forces
>me to make a string such as "grep file >> anotherfile" using strcat,
>use "system" to call it, and then go and open "anotherfile" afterwards
>to find the result.  Has anyone come up with a better way?

See popen(3) which returns a pipe to/from the command.
-- 
	-Barry Shein

Software Tool & Die, Purveyors to the Trade
1330 Beacon Street, Brookline, MA 02146, (617) 739-0202
Internet: bzs@skuld.std.com
UUCP:     encore!xylogics!skuld!bzs or uunet!skuld!bzs