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
|
/* $Header: popen.c,v 3.0.1.2 90/08/09 04:04:42 lwall Locked $
*
* (C) Copyright 1988, 1990 Diomidis Spinellis.
*
* You may distribute under the terms of the GNU General Public License
* as specified in the README file that comes with the perl 3.0 kit.
*
* $Log: popen.c,v $
* Revision 3.0.1.2 90/08/09 04:04:42 lwall
* patch19: various MSDOS and OS/2 patches folded in
*
* Revision 3.0.1.1 90/03/27 16:11:57 lwall
* patch16: MSDOS support
*
* Revision 1.1 90/03/18 20:32:20 dds
* Initial revision
*
*/
/*
* Popen and pclose for MS-DOS
*/
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
/*
* Possible actions on an popened file
*/
enum action {
delete, /* Used for "r". Delete the tmp file */
execute /* Used for "w". Execute the command. */
};
/*
* Linked list of things to do at the end of the program execution.
*/
static struct todo {
FILE *f; /* File we are working on (to fclose) */
const char *name; /* Name of the file (to unlink) */
const char *command; /* Command to execute */
enum action what; /* What to do (execute or delete) */
struct todo *next; /* Next structure */
} *todolist;
/* Clean up function */
static int close_pipes(void);
/*
* Add a file f running the command command on file name to the list
* of actions to be done at the end. The action is specified in what.
* Return -1 on failure, 0 if ok.
*/
static int
add(FILE *f, const char *command, const char *name, enum action what)
{
struct todo *p;
if ((p = (struct todo *) malloc(sizeof(struct todo))) == NULL)
return -1;
p->f = f;
p->command = command;
p->name = name;
p->what = what;
p->next = todolist;
todolist = p;
return 0;
}
FILE *
mypopen(const char *command, const char *t)
{
char buff[256];
char *name;
FILE *f;
static init = 0;
if (!init)
if (onexit(close_pipes) == NULL)
return NULL;
else
init++;
if ((name = tempnam(getenv("TMP"), "pp")) == NULL)
return NULL;
switch (*t) {
case 'r':
sprintf(buff, "%s>%s", command, name);
if (system(buff) || (f = fopen(name, "r")) == NULL) {
free(name);
return NULL;
}
if (add(f, command, name, delete)) {
(void)fclose(f);
(void)unlink(name);
free(name);
return NULL;
}
return f;
case 'w':
if ((f = fopen(name, "w")) == NULL) {
free(name);
return NULL;
}
if (add(f, command, name, execute)) {
(void)fclose(f);
(void)unlink(name);
free(name);
return NULL;
}
return f;
default:
free(name);
return NULL;
}
}
int
mypclose(FILE *f)
{
struct todo *p, **prev;
char buff[256];
const char *name;
int status;
for (p = todolist, prev = &todolist; p; prev = &(p->next), p = p->next)
if (p->f == f) {
*prev = p->next;
name = p->name;
switch (p->what) {
case delete:
free(p);
if (fclose(f) == EOF) {
(void)unlink(name);
status = EOF;
} else if (unlink(name) < 0)
status = EOF;
else
status = 0;
free(name);
return status;
case execute:
(void)sprintf(buff, "%s <%s", p->command, p->name);
free(p);
if (system(buff)) {
(void)unlink(name);
status = EOF;
} else if (fclose(f) == EOF) {
(void)unlink(name);
status = EOF;
} else if (unlink(name) < 0)
status = EOF;
else
status = 0;
free(name);
return status;
default:
return EOF;
}
}
return EOF;
}
/*
* Clean up at the end. Called by the onexit handler.
*/
static int
close_pipes(void)
{
struct todo *p;
for (p = todolist; p; p = p->next)
(void)mypclose(p->f);
return 0;
}
|