1 /* $Header: popen.c,v 4.0 91/03/20 01:34:50 lwall Locked $
3 * (C) Copyright 1988, 1990 Diomidis Spinellis.
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 4.0 91/03/20 01:34:50 lwall
12 * Revision 3.0.1.2 90/08/09 04:04:42 lwall
13 * patch19: various MSDOS and OS/2 patches folded in
15 * Revision 3.0.1.1 90/03/27 16:11:57 lwall
16 * patch16: MSDOS support
18 * Revision 1.1 90/03/18 20:32:20 dds
24 * Popen and pclose for MS-DOS
32 * Possible actions on an popened file
35 delete, /* Used for "r". Delete the tmp file */
36 execute /* Used for "w". Execute the command. */
40 * Linked list of things to do at the end of the program execution.
43 FILE *f; /* File we are working on (to fclose) */
44 const char *name; /* Name of the file (to unlink) */
45 const char *command; /* Command to execute */
46 enum action what; /* What to do (execute or delete) */
47 struct todo *next; /* Next structure */
51 /* Clean up function */
52 static int close_pipes(void);
55 * Add a file f running the command command on file name to the list
56 * of actions to be done at the end. The action is specified in what.
57 * Return -1 on failure, 0 if ok.
60 add(FILE *f, const char *command, const char *name, enum action what)
64 if ((p = (struct todo *) malloc(sizeof(struct todo))) == NULL)
76 mypopen(const char *command, const char *t)
84 if (onexit(close_pipes) == NULL)
89 if ((name = tempnam((char*)NULL, "pp")) == NULL)
94 sprintf(buff, "%s >%s", command, name);
95 if (system(buff) || (f = fopen(name, "r")) == NULL) {
99 if (add(f, command, name, delete)) {
107 if ((f = fopen(name, "w")) == NULL) {
111 if (add(f, command, name, execute)) {
127 struct todo *p, **prev;
132 for (p = todolist, prev = &todolist; p; prev = &(p->next), p = p->next)
139 if (fclose(f) == EOF) {
142 } else if (unlink(name) < 0)
149 (void)sprintf(buff, "%s <%s", p->command, p->name);
151 if (fclose(f) == EOF) {
154 } else if (system(buff)) {
157 } else if (unlink(name) < 0)
171 * Clean up at the end. Called by the onexit handler.
178 for (p = todolist; p; p = p->next)
179 (void)mypclose(p->f);