File: popen.c

package info (click to toggle)
trn4 4.0-test77-12
  • links: PTS, VCS
  • area: non-free
  • in suites: buster
  • size: 3,644 kB
  • sloc: ansic: 48,331; sh: 6,817; tcl: 1,696; yacc: 660; perl: 108; makefile: 24
file content (237 lines) | stat: -rw-r--r-- 6,461 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* popen/pclose:
 *
 * simple MS-DOS piping scheme to imitate UNIX pipes
 */

#include "EXTERN.h"
#include "common.h"
#include <setjmp.h>
#include "util2.h"
#include "util3.h"

#ifndef	_NFILE
# define _NFILE 5			/* Number of open files */
#endif	_NFILE

#define READIT	1			/* Read pipe */
#define WRITEIT	2			/* Write pipe */

static char* prgname[_NFILE];		/* program name if write pipe */
static int pipetype[_NFILE];		/* 1=read 2=write */
static char* pipename[_NFILE];		/* pipe file name */

/*
 *------------------------------------------------------------------------
 * run: Execute command via SHELL or COMSPEC
 *------------------------------------------------------------------------
 */

static int
run(char* command)
{
    jmp_buf panic;			/* How to recover from errors */
    char* shell;			/* Command processor */
    char* s = NULL;			/* Holds the command */
    int s_is_malloced = 0;		/* True if need to free 's' */
    static char* command_com = "COMMAND.COM";
    int status;				/* Return codes */
    char* shellpath;			/* Full command processor path */
    char* bp;				/* Generic string pointer */
    static char dash_c[] = "/c";

    s = savestr(command);
    /* Determine the command processor */
    if ((shell = getenv("SHELL")) == NULL
     && (shell = getenv("COMSPEC")) == NULL)
	shell = command_com;
    strupr(shell);
    shellpath = shell;
    /* Strip off any leading backslash directories */
    shell = rindex(shellpath, '\\');
    if (shell != NULL)
	shell++;
    else
	shell = shellpath;
    /* Strip off any leading slash directories */
    bp = rindex(shell, '/');
    if (bp != NULL)
	shell = ++bp;
    if (strcmp(shell, command_com) != 0) {
	/* MKS Shell needs quoted argument */
	char* bp;
	bp = s = safemalloc(strlen(command) + 3);
	*bp++ = '\'';
	while ((*bp++ = *command++) != '\0') ;
	*(bp - 1) = '\'';
	*bp = '\0';
	s_is_malloced = 1;
    } else
	s = command;
    /* Run the program */
    status = spawnl(P_WAIT, shellpath, shell, dash_c, s, NULL);
    if (s_is_malloced)
	free(s);
    return status;
}

/* 
 *------------------------------------------------------------------------
 * uniquepipe: returns a unique file name 
 *------------------------------------------------------------------------
 */

static char*
uniquepipe(void)
{ 
    static char name[14];
    static short int num = 0;
    (void) sprintf(name, "pipe%04d.tmp", num++);
    return name;
}

/*
 *------------------------------------------------------------------------
 * resetpipe: Private routine to cancel a pipe
 *------------------------------------------------------------------------
 */
static void
resetpipe(int fd)
{
    char* bp;
    if (fd >= 0 && fd < _NFILE) {
	pipetype[fd] = 0;
	if ((bp = pipename[fd]) != NULL) {
	    (void) unlink(bp);
	    free(bp);
	    pipename[fd] = NULL;
	}
	if ((bp = prgname[fd]) != NULL) {
	    free(bp);
	    prgname[fd] = NULL;
	}
    }
}

/* 
 *------------------------------------------------------------------------
 * popen: open a pipe 
 *------------------------------------------------------------------------
 */
FILE* popen(prg, type)
char* prg;			/* The command to be run */
char* type;			/* "w" or "r" */
{ 
    FILE* p = NULL;		/* Where we open the pipe */
    int ostdin;			/* Where our stdin is now */
    int pipefd = -1;		/* fileno(p) -- for convenience */
    char* tmpfile;		/* Holds name of pipe file */
    jmp_buf panic;		/* Where to go if there's an error */
    int lineno;			/* Line number where panic happened */

    /* Get a unique pipe file name */
    tmpfile = filexp("%Y/");
    strcat(tmpfile, uniquepipe());
    if ((lineno = setjmp(panic)) != 0) {
	/* An error has occurred, so clean up */
	int E = errno;
	if (p != NULL)
	    (void) fclose(p);
	resetpipe(pipefd);
	errno = E;
	lineno = lineno;
	return NULL;
    }
    if (strcmp(type, "w") == 0) {
	/* for write style pipe, pclose handles program execution */
	if ((p = fopen(tmpfile, "w")) != NULL) {
	    pipefd = fileno(p);
	    pipetype[pipefd] = WRITEIT;
	    pipename[pipefd] = savestr(tmpfile);
	    prgname[pipefd]  = savestr(prg);
	}
    } else if (strcmp(type, "r") == 0) {
	/* read pipe must create tmp file, set up stdout to point to the temp
	 * file, and run the program.  note that if the pipe file cannot be
	 * opened, it'll return a condition indicating pipe failure, which is
	 * fine. */
	if ((p = fopen(tmpfile, "w")) != NULL) {
	    int ostdout;
	    pipefd = fileno(p);
	    pipetype[pipefd]= READIT;
	    pipename[pipefd] = savestr(tmpfile);
	    /* Redirect stdin for the new command */
	    ostdout = dup(fileno(stdout));
	    if (dup2(fileno(stdout), pipefd) < 0) {
		int E = errno;
		(void) dup2(fileno(stdout), ostdout);
		errno = E;
		longjmp(panic, __LINE__);
	    }
	    if (run(prg) != 0)
		longjmp(panic, __LINE__);
	    if (dup2(fileno(stdout), ostdout) < 0)
		longjmp(panic, __LINE__);
	    if (fclose(p) < 0)
		longjmp(panic, __LINE__);
	    if ((p = fopen(tmpfile, "r")) == NULL)
		longjmp(panic, __LINE__);
	}
    } else {
	/* screwy call or unsupported type */
	errno = EINVFNC;
	longjmp(panic, __LINE__);
    }
    return p;
}

/* close a pipe */
int
pclose(p)
FILE* p;
{
    int pipefd = -1;		/* Fildes where pipe is opened */
    int ostdout;		/* Where our stdout points now */
    int ostdin;			/* Where our stdin points now */
    jmp_buf panic;		/* Context to return to if error */
    int lineno;			/* Line number where panic happened */
    if ((lineno = setjmp(panic)) != 0) {
	/* An error has occurred, so clean up and return */
	int E = errno;
	if (p != NULL)
	    (void) fclose(p);
	resetpipe(pipefd);
	errno = E;
	lineno = lineno;
	return -1;
    }
    pipefd = fileno(p);
    if (fclose(p) < 0)
	longjmp(panic, __LINE__);
    switch (pipetype[pipefd]) {
    case WRITEIT:
	/* open the temp file again as read, redirect stdin from that
	 * file, run the program, then clean up. */
	if ((p = fopen(pipename[pipefd],"r")) == NULL) 
	    longjmp(panic, __LINE__);
	ostdin = dup(fileno(stdin));
	if (dup2(fileno(stdin), fileno(p)) < 0)
	    longjmp(panic, __LINE__);
	if (run(prgname[pipefd]) != 0)
	    longjmp(panic, __LINE__);
	if (dup2(fileno(stdin), ostdin) < 0)
	    longjmp(panic, __LINE__);
	if (fclose(p) < 0)
	    longjmp(panic, __LINE__);
	resetpipe(pipefd);
	break;
    case READIT:
	/* close the temp file and remove it */
	resetpipe(pipefd);
	break;
    default:
	errno = EINVFNC;
	longjmp(panic, __LINE__);
	/*NOTREACHED*/
    }
    return 0;
}