popen()

Dave Binette dbinette at van-bc.UUCP
Sat Mar 3 18:40:15 AEST 1990


In article <1503 at loria.crin.fr> anigbogu at 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



More information about the Comp.lang.c mailing list