diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2010-07-16 13:09:56 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2010-07-16 13:09:56 +0300 |
commit | bc70de7b3302d5a81515b901cae376b8b51d2004 (patch) | |
tree | d36d6743e65697f6923b79d0ea8f9f9bf4ef7398 /pc/popen.c | |
parent | b9e4a1fd4c8c8753ab8a9887bab55f03efe1e3e2 (diff) | |
download | egawk-bc70de7b3302d5a81515b901cae376b8b51d2004.tar.gz egawk-bc70de7b3302d5a81515b901cae376b8b51d2004.tar.bz2 egawk-bc70de7b3302d5a81515b901cae376b8b51d2004.zip |
Move to gawk-3.1.0.
Diffstat (limited to 'pc/popen.c')
-rw-r--r-- | pc/popen.c | 95 |
1 files changed, 46 insertions, 49 deletions
@@ -21,12 +21,10 @@ #endif static char template[] = "piXXXXXX"; -typedef enum { unopened = 0, reading, writing } pipemode; -static -struct { - char *command; - char *name; - pipemode pmode; +static struct { + char *command; + char *name; + char pmode[4]; } pipes[_NFILE]; @@ -41,8 +39,7 @@ struct { #if defined(_MSC_VER) || defined(__MINGW32__) static int -unixshell(p) -char *p; +unixshell(char *p) { static char *shell[] = {"sh", "bash", "csh", "tcsh", "sh32", "sh16", "ksh", NULL}; char **shellp = shell, *s, *q; @@ -64,8 +61,7 @@ char *p; } static char * -slashify(p, s) -char *p, *s; +slashify(char *p, char *s) { if (unixshell(s)) while (s = strchr(p, '\\')) *s = '/'; @@ -75,8 +71,7 @@ char *p, *s; } static char * -scriptify(command) -char *command; +scriptify(char *command) { FILE *fp; char *cmd, *name, *s, *p; @@ -90,25 +85,26 @@ char *command; p = s; } slashify(name, p); - if (! unixshell(p)) { + if (! (i = unixshell(p))) { realloc(name, strlen(name) + 5); strcat(name, ".bat"); } if (s) sprintf(cmd + strlen(cmd), " %cc ", unixshell(s) ? '-' : '/'); strcpy(p = cmd + strlen(cmd), name); free(name); - i = strlen(command); - if ( ((fp = fopen(p, "wb")) == NULL) || (fwrite(command, 1, i, fp) < i) - || (fputc('\n', fp) == EOF)) { - cmd = NULL; - } + if ((fp = fopen(p, i ? "wb" : "w")) != NULL) { + if (! i) fputs("@echo off\n", fp); + i = strlen(command); + if ((fwrite(command, 1, i, fp) < i) || (fputc('\n', fp) == EOF)) + cmd = NULL; + } else + cmd = NULL; if (fp) fclose(fp); return(cmd); } static void -unlink_and_free(cmd) -char *cmd; +unlink_and_free(char *cmd) { char *s; @@ -120,8 +116,7 @@ char *cmd; } int -os_system(cmd) -char *cmd; +os_system(char *cmd) { char *s; int i; @@ -145,31 +140,26 @@ char *cmd; FILE * -os_popen( char *command, char *mode ) { +os_popen( char *command, char *mode ) +{ FILE *current; char *name; int cur; - pipemode curmode; + char curmode[4]; #if defined(OS2) && (_MSC_VER != 510) if (_osmode == OS2_MODE) return(popen(command, mode)); #endif - /* - ** decide on mode. - */ - if(strcmp(mode,"r") == 0) - curmode = reading; - else if(strcmp(mode,"w") == 0) - curmode = writing; - else - return NULL; + if (*mode != 'r' && *mode != 'w') + return NULL; + strncpy(curmode, mode, 3); curmode[3] = '\0'; #if defined(__MINGW32__) || (defined(_MSC_VER) && defined(WIN32)) current = popen(command = scriptify(command), mode); cur = fileno(current); - pipes[cur].pmode = curmode; + strcpy(pipes[cur].pmode, curmode); pipes[cur].command = command; return(current); #endif @@ -183,32 +173,36 @@ os_popen( char *command, char *mode ) { ** If we're reading, just call system to get a file filled with ** output. */ - if(curmode == reading) { + if (*curmode == 'r') { FILE *fp; if ((cur = dup(fileno(stdout))) == -1) return NULL; - if ((current = freopen(name, "w", stdout)) == NULL) + *curmode = 'w'; + if ((current = freopen(name, curmode, stdout)) == NULL) return NULL; os_system(command); if (dup2(cur, fileno(stdout)) == -1) return NULL; close(cur); - if((current = fopen(name,"r")) == NULL) + *curmode = 'r'; + if ((current = fopen(name, curmode)) == NULL) return NULL; } else { - if((current = fopen(name,"w")) == NULL) + if ((current = fopen(name, curmode)) == NULL) return NULL; } cur = fileno(current); pipes[cur].name = name; - pipes[cur].pmode = curmode; + strcpy(pipes[cur].pmode, curmode); pipes[cur].command = strdup(command); return current; } int -os_pclose( FILE * current) { - int cur = fileno(current),rval; +os_pclose( FILE * current) +{ + int cur = fileno(current); + int fd, rval; #if defined(OS2) && (_MSC_VER != 510) if (_osmode == OS2_MODE) @@ -217,7 +211,7 @@ os_pclose( FILE * current) { #if defined(__MINGW32__) || (defined(_MSC_VER) && defined(WIN32)) rval = pclose(current); - pipes[cur].pmode = unopened; + *pipes[cur].pmode = '\0'; unlink_and_free(pipes[cur].command); return rval; #endif @@ -225,24 +219,24 @@ os_pclose( FILE * current) { /* ** check for an open file. */ - if(pipes[cur].pmode == unopened) - return -1; - if(pipes[cur].pmode == reading) { + switch (*pipes[cur].pmode) { + case 'r': /* ** input pipes are just files we're done with. */ rval = fclose(current); unlink(pipes[cur].name); - } else { + break; + case 'w': /* ** output pipes are temporary files we have ** to cram down the throats of programs. */ - int fd; fclose(current); rval = -1; if ((fd = dup(fileno(stdin))) != -1) { - if (current = freopen(pipes[cur].name, "r", stdin)) { + char *mode = pipes[cur].pmode; *mode = 'r'; + if (current = freopen(pipes[cur].name, mode, stdin)) { rval = os_system(pipes[cur].command); fclose(current); if (dup2(fd, fileno(stdin)) == -1) rval = -1; @@ -250,11 +244,14 @@ os_pclose( FILE * current) { } } unlink(pipes[cur].name); + break; + default: + return -1; } /* ** clean up current pipe. */ - pipes[cur].pmode = unopened; + *pipes[cur].pmode = '\0'; free(pipes[cur].name); free(pipes[cur].command); return rval; |