--- ray/src/common/popen.c 1992/09/29 09:25:54 2.3 +++ 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,10 +56,19 @@ char *mode; break; *cp++ = '"'; /* double quotes only */ continue; + case '\\': + if (!quote && cmd[1] == '\n') { + *cp++ = ' '; + cmd++; + continue; + } + cmd++; + break; #else break; -#endif -#ifndef MSDOS + case '\\': + *cp++ = *cmd++; + break; case '(': if (!quote) paren++; @@ -66,26 +77,18 @@ char *mode; if (!quote && paren) paren--; break; - case '\\': - if (!quote && cmd[1] == '\n') { - *cp++ = ' '; - cmd++; - continue; - } - *cp++ = *cmd++; - break; + case ';': #endif + case '|': + if (!quote && !paren && cp2 == NULL) + cp2 = fname; + break; case ' ': case '\t': if (!quote) while (isspace(cmd[1])) cmd++; break; - case '|': - case ';': - if (!quote && !paren && cp2 == NULL) - cp2 = fname; - break; case '\n': case '\0': if (cp2 == NULL) @@ -93,7 +96,7 @@ char *mode; break; } if (cp2 == fname && *mode == 'w') { /* add input file */ - *cp++ = ' '; *cp++ = '<'; *cp++ = ' '; + *cp++ = ' '; *cp++ = '<'; while (*cp2) *cp++ = *cp2++; *cp++ = ' '; @@ -105,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';