27 #define MAX_CHILDREN 100
58 char *prog_name, *file_name, *mode;
66 #ifdef STDIO_NEEDS_BINARY
70 mode_string[0] = mode[0];
72 strcpy( mode_string + 2, mode + 1 );
76 if ( *mode ==
'w' || *mode ==
'a' )
81 if ( file_name != NULL && strcmp( file_name,
"-" ) != 0 )
96 if (kill(
pids[i], 0) < 0) {
97 int opid =
pids[i], pid = 0;
103 while (pid != opid) {
110 fprintf( stderr,
"Reaping %d at %d for %d at %d\n",
134 cp = file_name + strlen( file_name ) - 2;
136 if ( *file_name ==
'|' )
139 if ( (fp =
my_popen( file_name + 1
, mode
, &thepid
)) == NULL )
141 err_str =
"%s: can't invoke <<%s>> for %s: ";
147 fprintf( stderr,
"Forking %d at %d\n",
148 thepid, catching_children );
156 else if ( cp > file_name && *cp ==
'.' && *(cp + 1) ==
'Z' )
159 combuf = (
char *)malloc( 20 + strlen( file_name ) );
160 if ( combuf == NULL )
162 err_str =
"%s: out of memory opening (compressed) %s for %s";
167 sprintf( combuf,
"compress > %s", file_name );
168 else if ( *mode ==
'a' )
169 sprintf( combuf,
"compress >> %s", file_name );
171 sprintf( combuf,
"compress -d < %s", file_name );
179 "%s: can't invoke 'compress' program, trying to open %s for %s";
185 fprintf( stderr,
"Forking %d at %d\n", thepid, catching_children );
195 if ( (fp = fopen(file_name, mode)) == NULL )
197 err_str =
"%s: can't open %s for %s: ";
205 fprintf( stderr, err_str,
206 prog_name, file_name,
207 (*mode ==
'w') ?
"output" :
208 (*mode ==
'a') ?
"append" :
217 char *prog_name, *file_name, *mode;
247 if ( fd == NULL || fd == stdin || fd == stdout )
254 #ifndef NO_OPEN_PIPES
267 if ( *mode !=
'r' && *mode !=
'w' )
273 if ( pipe(pipefd) < 0 )
279 if ( (thepid = fork()) < 0 )
285 else if (thepid == 0) {
299 for ( i = 3; i < 64; i++ )
303 if ( execl(
"/bin/sh",
"sh",
"-c", cmd, NULL) < 0 )
313 retfile = fdopen( pipefd[0], mode );
317 retfile = fdopen( pipefd[1], mode );
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
static int catching_children
static FILE * my_popen(char *cmd, char *mode, int *pid)
void rle_close_f(FILE *fd)
FILE * rle_open_f_noexit(char *prog_name, char *file_name, char *mode)