home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 4
/
DATAFILE_PDCD4.iso
/
unix
/
unixtools
/
util
/
c
/
popen
< prev
next >
Wrap
Text File
|
1992-07-21
|
3KB
|
188 lines
/* C.Popen: Simulation of pipes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#define MAX_PIPE 20
typedef enum
{
unopened = 0,
reading,
writing
}
pipemode;
static struct pipe
{
char *command; /* The command being executed */
char *name; /* The name of the pipe file */
FILE *fd; /* The file used as a pipe */
pipemode pmode; /* The open mode of the pipe */
}
pipes[MAX_PIPE];
FILE *popen (char *command, char *mode)
{
FILE *current;
char *name;
int i;
pipemode curmode;
char tmp[11];
/* decide on mode */
if ( mode[1] != 0 )
return NULL;
else if ( *mode == 'r' )
curmode = reading;
else if ( *mode == 'w' )
curmode = writing;
else
return NULL;
/* Get a slot in thbe pipes structure */
for ( i = 0; i < MAX_PIPE; ++i )
{
if ( pipes[i].pmode == unopened )
break;
}
if ( i >= MAX_PIPE )
return NULL;
/* Get a file name to use */
sprintf(tmp, "Pipe%.2d", i);
name = mktemp(tmp);
if ( name == NULL )
return NULL;
/*
* If we're reading, just call system() to get a file filled
* with output.
*/
if ( curmode == reading )
{
char cmd[256];
char *p;
char *q;
char *p1 = 0;
for (p = command, q = cmd; *p;)
{
/* Replace '%o' with output filename */
if (*p == '%' && p[1] == 'o')
{
for (p1 = name; *p1;)
*q++ = *p1++;
p += 2;
}
else
*q++ = *p++;
}
/* If there was no '%o', append '>pipe' */
if (p1 == 0)
{
*q++ = ' ';
*q++ = '>';
for (p1 = name; *p1;)
*q++ = *p1++;
}
/* Terminate the string */
*q = 0;
system(cmd);
if ( (current = fopen(name,"r")) == NULL )
return NULL;
}
else
{
if ( (current = fopen(name,"w")) == NULL )
return NULL;
}
pipes[i].command = strdup(command);
pipes[i].name = name;
pipes[i].fd = current;
pipes[i].pmode = curmode;
return current;
}
int pclose (FILE *current)
{
int rval;
int i;
/* Get the appropriate slot in thbe pipes structure */
for ( i = 0; i < MAX_PIPE; ++i )
{
if ( pipes[i].fd == current )
break;
}
if ( i >= MAX_PIPE )
return -1;
if ( pipes[i].pmode == reading )
{
/* Input pipes are just files we're done with */
rval = fclose(current);
remove(pipes[i].name);
}
else
{
/*
* Output pipes are temporary files we have
* to cram down the throats of programs.
*/
char command[256];
char *p1 = 0;
char *p;
char *q;
/* Close the pipe file */
fclose(current);
/* Create the required command string */
for (p = pipes[i].command, q = command; *p;)
{
/* Replace '%i' with the pipe filename */
if (*p == '%' && p[1] == 'i')
{
for (p1 = pipes[i].name; *p1;)
*q++ = *p1++;
p += 2;
}
else
*q++ = *p++;
}
/* If there was no '%i', append '<pipe' */
if (p1 == 0)
{
*q++ = ' ';
*q++ = '<';
for (p1 = pipes[i].name; *p1;)
*q++ = *p1++;
}
/* Terminate the string */
*q = 0;
rval = system(command);
remove(pipes[i].name);
}
/* clean up current pipe */
pipes[i].pmode = unopened;
free(pipes[i].name);
free(pipes[i].command);
return rval;
}