Xref: utzoo comp.lang.c:26516 comp.sys.ibm.pc.programmer:227 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!van-bc!dbinette From: dbinette@van-bc.UUCP (Dave Binette) Newsgroups: comp.lang.c,comp.sys.ibm.pc.programmer Subject: Re: popen() Message-ID: <218@van-bc.UUCP> Date: 3 Mar 90 07:40:15 GMT References: <1503@loria.crin.fr> Reply-To: dbinette@van-bc.UUCP (Dave Binette) Organization: Wimsey Associates Lines: 99 In article <1503@loria.crin.fr> anigbogu@loria.crin.fr (Julian Anigbogu) writes: >Can some kind soul point me to where I can get a PC implementation of >the Unix C popen() /* for handle to a pipe */. I manipulate huge >rasterfiles(from a scanner) on a Sun and I adopted the policy of >compressing all files to save disk space. I open them with >popen("uncompress -c filename","r") so that they remain compressed on >disk. Following this tirade on the inadequacies of DOS is a code fragment that should help. To start with... here is an excerpt from a help utility for C Defines: Modes used by the spawn function. P_WAIT child runs separately, parent waits until exit. P_NOWAIT child and parent run concurrently. (not implemented) P_OVERLAY child replaces parent so that parent no longer exists. Defined in process.h The critical issue here is P_NOWAIT. Because under "DOS" processes are sequential there will be no "pipe" (as we know it in *IX) to waylay disk usage that you are trying to avoid. {normally *IX uses a fixed size pipe to transfer data from concurrent procs, this allows large amounts of data to be passed without consuming disk space} Under "DOS" the system will actually create a temporary file somewhere. When the "parent" process terminates the "child" process is started with its stdin reading from the temporary file. With that horrible scenario in mind.... here is an implimentation of the popen/pclose commands. hardly perfect but it might do the trick for READING OUTPUT from a program. #ifndef M_XENIX /* ---------------------------------------------------------------- */ /* Compatability */ /* ---------------------------------------------------------------- */ char Popentemp[MAXARGLINE+1]; FILE * popen(char *s, char * fmode) { char * tpath; char cmd[MAXARGLINE+1]; FILE * fp; if((tpath = getenv("TMP")) == (char *)0) if((tpath = getenv("TEMP")) == (char *)0) tpath = ""; strcpy(Popentemp, tpath); strcat(Popentemp, mktemp("\\$PICK_XXXXXX")); strcpy(cmd, s); strcat(cmd, " > "); strcat(cmd, Popentemp); system(cmd); fp = fopen(Popentemp, fmode); return(fp); } /* ---------------------------------------------------------------- */ int pclose(FILE * fp) { int r = -1; if(fp) if(fclose(fp) == 0); r = unlink(Popentemp); return(r); } #endif