Debian Codegraph @

msdos/dosexec.c

raw
1 /********************************************
2 dosexec.c
3 copyright 2009-2014,2023, Thomas E. Dickey
4 copyright 1991-1994,1995, Michael D. Brennan
5
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
8
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
12
13 /*
14 * $MawkId: dosexec.c,v 1.6 2023/07/25 21:27:16 tom Exp $
15 */
16
17 /* system() and pipes() for MSDOS and Win32 console */
18
19 #include "mawk.h"
20
21 #if defined(MSDOS) || defined(_WINNT)
22 #include "memory.h"
23 #include "files.h"
24 #include "fin.h"
25
26 #undef _POSIX_
27
28 #include <process.h>
29
30 #ifndef P_WAIT
31 #define P_WAIT _P_WAIT /* works with VS2008 */
32 #endif
33
34 static char *my_shell; /* e.g. "c:\\sys\\command.com" */
35 static char *command_opt; /* " /c" */
36
37 static void
38 get_shell(void)
39 {
40 char *s, *p;
41 int len;
42
43 if ((s = getenv("MAWKSHELL")) != 0) {
44 /* break into shell part and option part */
45 p = s;
46 while (*p != ' ' && *p != '\t')
47 p++;
48 len = p - s;
49 my_shell = (char *) zmalloc(len + 1);
50 memcpy(my_shell, s, len);
51 my_shell[len] = 0;
52 command_opt = p;
53 } else if ((s = getenv("COMSPEC")) != 0) {
54 my_shell = s;
55 command_opt = " /c";
56 /* leading space needed because of bug in command.com */
57 } else {
58 errmsg(0,
59 "cannot exec(), must set MAWKSHELL or COMSPEC in environment");
60 exit(2);
61 }
62 }
63
64 int
65 DOSexec(char *command)
66 {
67 char xbuff[256];
68
69 if (!my_shell)
70 get_shell();
71
72 sprintf(xbuff, "%s %s", command_opt, command);
73
74 fflush(stderr);
75 fflush(stdout);
76
77 return spawnl(P_WAIT, my_shell, my_shell, xbuff, (char *) 0);
78 }
79
80 static int next_tmp; /* index for naming temp files */
81 static char *tmpdir; /* directory to hold temp files */
82 static unsigned mawkid; /* unique to this mawk process */
83
84 /* compute the unique temp file name associated with id */
85 char *
86 tmp_file_name(int id,
87 char *buffer)
88 {
89 if (mawkid == 0) {
90 /* first time */
91 union {
92 void *ptr;
93 unsigned w[2];
94 } xptr;
95
96 memset(&xptr, 0, sizeof(xptr));
97 xptr.ptr = (void *) &mawkid;
98 mawkid = xptr.w[1];
99
100 tmpdir = getenv("MAWKTMPDIR");
101 if (!tmpdir || strlen(tmpdir) > 80)
102 tmpdir = "";
103 }
104
105 (void) sprintf(buffer, "%sMAWK%04X.%03X", tmpdir, mawkid, id);
106 return buffer;
107 }
108
109 /* open a pipe, returning a temp file identifier by
110 reference
111 */
112
113 PTR
114 get_pipe(char *command,
115 int type, int *tmp_idp)
116 {
117 PTR retval;
118 char xbuff[256];
119 char *tmpname;
120
121 *tmp_idp = next_tmp;
122 tmpname = tmp_file_name(next_tmp, xbuff + 163);
123
124 if (type == PIPE_OUT) {
125 retval = (PTR) fopen(tmpname, (binmode() & 2) ? "wb" : "w");
126 } else {
127 sprintf(xbuff, "%s > %s", command, tmpname);
128 tmp_idp[1] = DOSexec(xbuff);
129 retval = (PTR) FINopen(tmpname, 0);
130 }
131
132 next_tmp++;
133 return retval;
134 }
135
136 /* closing a fake pipes involves running the out pipe
137 command
138 */
139
140 int
141 close_fake_outpipe(char *command,
142 int tid) /* identifies the temp file */
143 {
144 char xbuff[256];
145 char *tmpname = tmp_file_name(tid, xbuff + 163);
146 int retval;
147
148 sprintf(xbuff, "%s < %s", command, tmpname);
149 retval = DOSexec(xbuff);
150 (void) unlink(tmpname);
151 return retval;
152 }
153
154 #endif /* MSDOS */