2 /********************************************
4 copyright 1991, Michael D. Brennan
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
14 * Revision 1.3 1995/08/20 16:37:22 mike
17 * Revision 1.2 1994/10/08 18:50:03 mike
20 * Revision 1.1.1.1 1993/07/03 18:58:47 mike
23 * Revision 1.4 1992/12/05 22:29:43 mike
25 * don't use string_buff
28 * Revision 1.3 1992/07/10 16:21:57 brennan
29 * store exit code of input pipes
31 * Revision 1.2 1991/11/16 10:27:18 brennan
34 * Revision 1.1 91/10/29 09:45:56 brennan
39 /* system() and pipes() for MSDOS */
50 static void PROTO(get_shell, (void)) ;
52 static char *shell ; /* e.g. "c:\\sys\\command.com" */
53 static char *command_opt ; /* " /c" */
55 static void get_shell()
59 if ( s = getenv("MAWKSHELL") )
61 /* break into shell part and option part */
63 while ( *p != ' ' && *p != '\t' ) p++ ;
65 shell = (char *) zmalloc(len+1) ;
66 memcpy(shell, s, len) ; shell[len] = 0 ;
70 if ( s = getenv("COMSPEC") )
74 /* leading space needed because of bug in command.com */
79 "cannot exec(), must set MAWKSHELL or COMSPEC in environment" ) ;
85 int DOSexec( command )
90 if ( ! shell ) get_shell() ;
92 sprintf(xbuff, "%s %s", command_opt, command) ;
94 fflush(stderr) ; fflush(stdout) ;
96 return spawnl(P_WAIT, shell, shell, xbuff, (char *) 0 ) ;
100 static int next_tmp ; /* index for naming temp files */
101 static char *tmpdir ; /* directory to hold temp files */
102 static unsigned mawkid ; /* unique to this mawk process */
105 /* compute the unique temp file name associated with id */
106 char *tmp_file_name(id, buffer )
118 xptr.ptr = (void far*)&mawkid ;
121 tmpdir = getenv("MAWKTMPDIR") ;
122 if ( ! tmpdir || strlen(tmpdir) > 80 ) tmpdir = "" ;
125 (void) sprintf(buffer, "%sMAWK%04X.%03X",tmpdir, mawkid, id) ;
129 /* open a pipe, returning a temp file identifier by
133 PTR get_pipe( command, type, tmp_idp)
142 *tmp_idp = next_tmp ;
143 tmpname = tmp_file_name(next_tmp, xbuff+163) ;
145 if ( type == PIPE_OUT )
147 retval = (PTR) fopen(tmpname, (binmode()&2)? "wb":"w") ;
151 sprintf(xbuff, "%s > %s" , command, tmpname) ;
152 tmp_idp[1] = DOSexec(xbuff) ;
153 retval = (PTR) FINopen(tmpname, 0) ;
160 /* closing a fake pipes involves running the out pipe
164 int close_fake_outpipe(command, tid)
166 int tid ; /* identifies the temp file */
169 char *tmpname = tmp_file_name(tid, xbuff+163) ;
172 sprintf(xbuff, "%s < %s", command, tmpname) ;
173 retval = DOSexec(xbuff) ;
174 (void) unlink(tmpname) ;