How to provide Shell Escape from a C program?

Norman Joseph norm at oglvee.UUCP
Thu Nov 22 01:16:14 AEST 1990


In <349 at clbull.cl.bull.fr> rudrak at saphir.cl.bull.fr (Rudrakshala Purushotham)
writes:

>I want to provide shell escape feature from a C program. But I am having 
>some problems with the following code:

You are doing more work than you have to.  Your "shell_escape( command )"
has already been written for you.  It is called "system()".  Look for it
in your manual.  As for your code:

>	char *args [MAX_ARGS];
>	args [0] = "/bin/sh";
>	args [1] = "-c";

>	for (i = 2; i < MAX_ARGS && (s = strtok (command, " \t")); i++)
>		args [i] = strsave (s);

>	args [i] = NULL;

To properly use strtok() you only pass it the string you want to parse
once.  To return successive tokens from the string, the first argument
should be a null pointer in all successive calls.  The idiom goes
something like this:

	s = strtok( command, " \t" );
	args[2] = strsave( s );
	for ( i = 3;  i < MAX_ARGS && ( s = strtok( NULL, " \t" ));  i++ )
		args[i] = strsave( s );
	args[i] = NULL;
-- 
Norm Joseph - (amanue!oglvee!norm)           cgh!amanue!oglvee!norm at dsi.com, or
  Oglevee Computer Systems, Inc.             ditka!oglvee!norm at daver.bungi.com
                                   ---<*>---
      "Shucking Usenet oysters in pursuit of a pearl."  --  Bill Kennedy



More information about the Comp.unix.programmer mailing list