1 /* $Header: popen.c,v 3.0.1.1 90/03/27 16:11:57 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 3.0.1.1 90/03/27 16:11:57 lwall
10 * patch16: MSDOS support
12 * Revision 1.1 90/03/18 20:32:20 dds
18 * Popen and pclose for MS-DOS
26 * Possible actions on an popened file
29 delete, /* Used for "r". Delete the tmp file */
30 execute /* Used for "w". Execute the command. */
34 * Linked list of things to do at the end of the program execution.
37 FILE *f; /* File we are working on (to fclose) */
38 const char *name; /* Name of the file (to unlink) */
39 const char *command; /* Command to execute */
40 enum action what; /* What to do (execute or delete) */
41 struct todo *next; /* Next structure */
45 /* Clean up function */
46 static int close_pipes(void);
49 * Add a file f running the command command on file name to the list
50 * of actions to be done at the end. The action is specified in what.
51 * Return -1 on failure, 0 if ok.
54 add(FILE *f, const char *command, const char *name, enum action what)
58 if ((p = (struct todo *) malloc(sizeof(struct todo))) == NULL)
70 mypopen(const char *command, const char *t)
78 if (onexit(close_pipes) == NULL)
83 if ((name = tempnam(getenv("TMP"), "pp")) == NULL)
88 sprintf(buff, "%s >%s", command, name);
89 if (system(buff) || (f = fopen(name, "r")) == NULL) {
93 if (add(f, command, name, delete)) {
101 if ((f = fopen(name, "w")) == NULL) {
105 if (add(f, command, name, execute)) {
121 struct todo *p, **prev;
126 for (p = todolist, prev = &todolist; p; prev = &(p->next), p = p->next)
133 if (fclose(f) == EOF) {
136 } else if (unlink(name) < 0)
143 (void)sprintf(buff, "%s <%s", p->command, p->name);
148 } else if (fclose(f) == EOF) {
151 } else if (unlink(name) < 0)
165 * Clean up at the end. Called by the onexit handler.
172 for (p = todolist; p; p = p->next)
173 (void)mypclose(p->f);