Article View: harvard.comp.unix
Article #3Re: C system command
From: Kevin Quitt
Date: Wed, 29 Nov 1995 02:39
Date: Wed, 29 Nov 1995 02:39
60 lines
2078 bytes
2078 bytes
On 27 Nov 1995 12:27:00 +0100, tony@Austria.EU.net (Tony Curtis) wrote:
>In article <49b2cg$4e3@decaxp.harvard.edu> sarmad@scunix4.harvard.edu (Shaikh Sarmad) writes:
> *** Joev's suggestion ***
> : main (int argc, char **argv)
> : {
> : char cmd[100];
> : sprintf(cmd, "finger -m %s", argv[1]);
> : system(cmd);
> : }
>
> *** Eugene's suggestion ***
> : main (int argc, char **argv)
> : { char cmd [80];
> : strcpy (cmd, "finger -m ");
> : strcat (cmd, argv[1]);
> : system (cmd);
> : }
>
>There is a problem with both of these though.
>An execv() with dynamically allocated space would be the
>better solution (I believe one was posted sometime).
It's way too big. Ug. How about this then:
#include <stdio.h>
#include <stdlib.h>
char *finger = "finger -m ";
main( int argc, char *argv[] )
{
char *command;
int arg = 0;
while (--argc > 0)
{
command = malloc( 2 + strlen( finger ) + strlen( argv[++arg] ));
if ( !command )
{
fputs( "Can't allocate memory for command\n", stderr );
exit( EXIT_FAILURE );
}
strcpy( command, finger );
strcat( command, argv[arg] );
system( command );
free( command );
}
if ( arg )
exit( EXIT_SUCCESS );
else
fputs( "I need to know who to finger!\n", stderr );
return EXIT_FAILURE; /* For the picky but stupid compilers */
}
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91351-4454 96.37% of all statistics are made up
Technical Details
Message-ID:
<30bbc4b3.595472243@llyene.jpl.nasa.gov>Path:
arc1-ca.pugleaf.net!in2.uu.net!spcuna!wlbr!llyene!newsReferences:
<4997nh$sjj@decaxp.harvard.edu> <uufd9afhhri.fsf@scws27.harvard.edu> <49b2cg$4e3@decaxp.harvard.edu> <k6zqdis3gc.fsf@www.Austria.EU.net>Show Full Headers
Complete Article Headers
Newsgroups: harvard.comp.unix,comp.unix.questions,comp.lang.c Path: arc1-ca.pugleaf.net!in2.uu.net!spcuna!wlbr!llyene!news From: kdq@emoryi.jpl.nasa.gov (Kevin Quitt) Subject: Re: C system command Message-ID: <30bbc4b3.595472243@llyene.jpl.nasa.gov> Sender: news@llyene.jpl.nasa.gov Organization: Jet Propulsion Laboratory X-Newsreader: Forte Agent .99c/16.141 References: <4997nh$sjj@decaxp.harvard.edu> <uufd9afhhri.fsf@scws27.harvard.edu> <49b2cg$4e3@decaxp.harvard.edu> <k6zqdis3gc.fsf@www.Austria.EU.net> Date: Wed, 29 Nov 1995 02:39:46 GMT