--- ray/src/common/popen.c 1992/09/21 12:02:16 2.2 +++ ray/src/common/popen.c 2003/02/25 02:47:21 2.6 @@ -1,14 +1,15 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: popen.c,v 2.6 2003/02/25 02:47:21 greg Exp $"; #endif - /* * popen() and pclose() calls for systems without pipe facilities */ +#include "copyright.h" + #include +#include +#include #include #include "paths.h" @@ -27,16 +28,17 @@ popen(cmd, mode) /* open command for reading or writi register char *cmd; char *mode; { - extern FILE *fopen(); - extern char *malloc(), *mktemp(), *strcpy(); - static int fnum = 0; FILE *fp; char *newcmd, *fname; register char *cp, *cp2 = NULL; int quote = '\0', paren = 0; - fname = malloc(TEMPLEN+1); - newcmd = malloc(TEMPLEN+6+strlen(cmd)); + while (isspace(*cmd)) + cmd++; + if (!*cmd) + return(NULL); + fname = (char *)malloc(TEMPLEN+1); + newcmd = (char *)malloc(TEMPLEN+6+strlen(cmd)); if (fname == NULL | newcmd == NULL) return(NULL); mktemp(strcpy(fname,TEMPLATE)); @@ -54,9 +56,19 @@ char *mode; break; *cp++ = '"'; /* double quotes only */ continue; + case '\\': + if (!quote && cmd[1] == '\n') { + *cp++ = ' '; + cmd++; + continue; + } + cmd++; + break; #else break; -#endif + case '\\': + *cp++ = *cmd++; + break; case '(': if (!quote) paren++; @@ -65,13 +77,11 @@ char *mode; if (!quote && paren) paren--; break; - case '\\': - if (!quote && cmd[1] == '\n') { - *cp++ = ' '; - cmd++; - continue; - } - *cp++ = *cmd++; + case ';': +#endif + case '|': + if (!quote && !paren && cp2 == NULL) + cp2 = fname; break; case ' ': case '\t': @@ -79,11 +89,6 @@ char *mode; while (isspace(cmd[1])) cmd++; break; - case '|': - case ';': - if (!quote && !paren && cp2 == NULL) - cp2 = fname; - break; case '\n': case '\0': if (cp2 == NULL) @@ -91,7 +96,7 @@ char *mode; break; } if (cp2 == fname && *mode == 'w') { /* add input file */ - *cp++ = ' '; *cp++ = '<'; *cp++ = ' '; + *cp++ = ' '; *cp++ = '<'; while (*cp2) *cp++ = *cp2++; *cp++ = ' '; @@ -103,7 +108,7 @@ char *mode; if (*mode == 'r') { /* add output file */ while (isspace(cp[-1]) || cp[-1] == ';') cp--; - *cp++ = ' '; *cp++ = '>'; *cp++ = ' '; + *cp++ = ' '; *cp++ = '>'; while (*cp2) *cp++ = *cp2++; *cp = '\0'; @@ -115,8 +120,8 @@ char *mode; return(NULL); } if (fileno(fp) >= NFILE) { - fputs("popen: too many open files\n", stderr); - exit(1); + eputs("popen: too many open files\n"); + quit(1); } pips[fileno(fp)].f = fname; if (*mode == 'r') {