1 /* $RCSfile: popen.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 11:22:52 $
3 * (C) Copyright 1988, 1990 Diomidis Spinellis.
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
9 * Revision 4.0.1.1 91/06/07 11:22:52 lwall
10 * patch4: new copyright notice
12 * Revision 4.0 91/03/20 01:34:50 lwall
15 * Revision 3.0.1.2 90/08/09 04:04:42 lwall
16 * patch19: various MSDOS and OS/2 patches folded in
18 * Revision 3.0.1.1 90/03/27 16:11:57 lwall
19 * patch16: MSDOS support
21 * Revision 1.1 90/03/18 20:32:20 dds
27 * Popen and pclose for MS-DOS
35 * Possible actions on an popened file
38 delete, /* Used for "r". Delete the tmp file */
39 execute /* Used for "w". Execute the command. */
43 * Linked list of things to do at the end of the program execution.
46 FILE *f; /* File we are working on (to fclose) */
47 const char *name; /* Name of the file (to unlink) */
48 const char *command; /* Command to execute */
49 enum action what; /* What to do (execute or delete) */
50 struct todo *next; /* Next structure */
54 /* Clean up function */
55 static int close_pipes(void);
58 * Add a file f running the command command on file name to the list
59 * of actions to be done at the end. The action is specified in what.
60 * Return -1 on failure, 0 if ok.
63 add(FILE *f, const char *command, const char *name, enum action what)
67 if ((p = (struct todo *) malloc(sizeof(struct todo))) == NULL)
79 mypopen(const char *command, const char *t)
87 if (onexit(close_pipes) == NULL)
92 if ((name = tempnam((char*)NULL, "pp")) == NULL)
97 sprintf(buff, "%s >%s", command, name);
98 if (system(buff) || (f = fopen(name, "r")) == NULL) {
102 if (add(f, command, name, delete)) {
110 if ((f = fopen(name, "w")) == NULL) {
114 if (add(f, command, name, execute)) {
130 struct todo *p, **prev;
135 for (p = todolist, prev = &todolist; p; prev = &(p->next), p = p->next)
142 if (fclose(f) == EOF) {
145 } else if (unlink(name) < 0)
152 (void)sprintf(buff, "%s <%s", p->command, p->name);
154 if (fclose(f) == EOF) {
157 } else if (system(buff)) {
160 } else if (unlink(name) < 0)
174 * Clean up at the end. Called by the onexit handler.
181 for (p = todolist; p; p = p->next)
182 (void)mypclose(p->f);