Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!zaphod.mps.ohio-state.edu!unix.cis.pitt.edu!pitt!amanue!oglvee!norm From: norm@oglvee.UUCP (Norman Joseph) Newsgroups: comp.unix.programmer Subject: Re: How to provide Shell Escape from a C program? Message-ID: <622@oglvee.UUCP> Date: 21 Nov 90 14:16:14 GMT References: <349@clbull.cl.bull.fr> Organization: Oglevee Computer Systems, Connellsville, Pa Lines: 34 In <349@clbull.cl.bull.fr> rudrak@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@dsi.com, or Oglevee Computer Systems, Inc. ditka!oglvee!norm@daver.bungi.com ---<*>--- "Shucking Usenet oysters in pursuit of a pearl." -- Bill Kennedy