Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!dciem!nrcaer!cognos!geovision!alastair From: alastair@geovision.UUCP (Alastair Mayer) Newsgroups: comp.lang.c Subject: Re: Accessing argc & argv from a functi Message-ID: <144@geovision.UUCP> Date: Fri, 14-Aug-87 11:05:34 EDT Article-I.D.: geovisio.144 Posted: Fri Aug 14 11:05:34 1987 Date-Received: Sun, 16-Aug-87 08:53:32 EDT References: <22@flmis06.ATT.COM> <28700019@ccvaxa> <274@spar.SPAR.SLB.COM> Reply-To: alastair@geovision.UUCP (Alastair Mayer) Organization: Geovision Corporation, Ottawa, Canada Lines: 41 In article <274@spar.SPAR.SLB.COM> hunt@spar.UUCP (Neil Hunt) writes: >I have thought of another (rather silly) way to obtain the >arguments to a program without having access to main: :-) > > [.. do a ps and grep out the PID.. ] >args, and stick the whole lot into a file: > > sprintf(str, "ps -uwww | egrep %d | sed -f com > /tmp/args", getpid()); > >Execute the command pipe using system: > > system(str); > >Now open and read the file, and obtain the original args (which will not >change even if your program screws up its stack and scribbles over everything). This is similar to a method I suggested to a co-worker who needed to do the same thing. The refinement you missed avoids the use of a temporary file and the need to open that file. Just use popen(3), which is analgous to fopen(3) except that a command is given instead of the filename. E.g: your example: sprintf(str, ..etc..) system(str); /* following implied by your msg */ fp = fopen("/tmp/args","r"); fscanf(fp, ..etc..); fclose(fp); using popen: sprintf(str, ..etc..) fp = popen(str,"r"); /* combines system and fopen, sort of*/ fscanf(fp, ..etc..); pclose(fp); This implies Unix (or some other implementation that supports popen(3)) but so does your version. And of course, that [fp]open succeeded should be checked before attempting the fscanf in real code. -- Alastair JW Mayer BIX: al UUCP: ...!utzoo!dciem!nrcaer!cognos!geovision!alastair Let's build a base on the Moon on our way to the asteroids - forget Mars.