Home | History | Annotate | Line # | Download | only in ksh
io.c revision 1.6
      1  1.6       wiz /*	$NetBSD: io.c,v 1.6 2001/09/16 16:34:23 wiz Exp $	*/
      2  1.2       tls 
      3  1.1       jtc /*
      4  1.1       jtc  * shell buffered IO and formatted output
      5  1.1       jtc  */
      6  1.1       jtc 
      7  1.1       jtc #include <ctype.h>
      8  1.1       jtc #include "sh.h"
      9  1.1       jtc #include "ksh_stat.h"
     10  1.1       jtc 
     11  1.5   hubertf static int initio_done;
     12  1.5   hubertf 
     13  1.1       jtc /*
     14  1.1       jtc  * formatted output functions
     15  1.1       jtc  */
     16  1.1       jtc 
     17  1.1       jtc 
     18  1.6       wiz /* A shell error occurred (eg, syntax error, etc.) */
     19  1.1       jtc void
     20  1.1       jtc #ifdef HAVE_PROTOTYPES
     21  1.1       jtc errorf(const char *fmt, ...)
     22  1.1       jtc #else
     23  1.1       jtc errorf(fmt, va_alist)
     24  1.1       jtc 	const char *fmt;
     25  1.1       jtc 	va_dcl
     26  1.1       jtc #endif
     27  1.1       jtc {
     28  1.1       jtc 	va_list va;
     29  1.1       jtc 
     30  1.1       jtc 	shl_stdout_ok = 0;	/* debugging: note that stdout not valid */
     31  1.1       jtc 	exstat = 1;
     32  1.1       jtc 	if (*fmt) {
     33  1.1       jtc 		error_prefix(TRUE);
     34  1.1       jtc 		SH_VA_START(va, fmt);
     35  1.1       jtc 		shf_vfprintf(shl_out, fmt, va);
     36  1.1       jtc 		va_end(va);
     37  1.1       jtc 		shf_putchar('\n', shl_out);
     38  1.1       jtc 	}
     39  1.1       jtc 	shf_flush(shl_out);
     40  1.1       jtc 	unwind(LERROR);
     41  1.1       jtc }
     42  1.1       jtc 
     43  1.1       jtc /* like errorf(), but no unwind is done */
     44  1.1       jtc void
     45  1.1       jtc #ifdef HAVE_PROTOTYPES
     46  1.1       jtc warningf(int fileline, const char *fmt, ...)
     47  1.1       jtc #else
     48  1.1       jtc warningf(fileline, fmt, va_alist)
     49  1.1       jtc 	int fileline;
     50  1.1       jtc 	const char *fmt;
     51  1.1       jtc 	va_dcl
     52  1.1       jtc #endif
     53  1.1       jtc {
     54  1.1       jtc 	va_list va;
     55  1.1       jtc 
     56  1.1       jtc 	error_prefix(fileline);
     57  1.1       jtc 	SH_VA_START(va, fmt);
     58  1.1       jtc 	shf_vfprintf(shl_out, fmt, va);
     59  1.1       jtc 	va_end(va);
     60  1.1       jtc 	shf_putchar('\n', shl_out);
     61  1.1       jtc 	shf_flush(shl_out);
     62  1.1       jtc }
     63  1.1       jtc 
     64  1.1       jtc /* Used by built-in utilities to prefix shell and utility name to message
     65  1.1       jtc  * (also unwinds environments for special builtins).
     66  1.1       jtc  */
     67  1.1       jtc void
     68  1.1       jtc #ifdef HAVE_PROTOTYPES
     69  1.1       jtc bi_errorf(const char *fmt, ...)
     70  1.1       jtc #else
     71  1.1       jtc bi_errorf(fmt, va_alist)
     72  1.1       jtc 	const char *fmt;
     73  1.1       jtc 	va_dcl
     74  1.1       jtc #endif
     75  1.1       jtc {
     76  1.1       jtc 	va_list va;
     77  1.1       jtc 
     78  1.1       jtc 	shl_stdout_ok = 0;	/* debugging: note that stdout not valid */
     79  1.1       jtc 	exstat = 1;
     80  1.1       jtc 	if (*fmt) {
     81  1.1       jtc 		error_prefix(TRUE);
     82  1.1       jtc 		/* not set when main() calls parse_args() */
     83  1.1       jtc 		if (builtin_argv0)
     84  1.1       jtc 			shf_fprintf(shl_out, "%s: ", builtin_argv0);
     85  1.1       jtc 		SH_VA_START(va, fmt);
     86  1.1       jtc 		shf_vfprintf(shl_out, fmt, va);
     87  1.1       jtc 		va_end(va);
     88  1.1       jtc 		shf_putchar('\n', shl_out);
     89  1.1       jtc 	}
     90  1.1       jtc 	shf_flush(shl_out);
     91  1.1       jtc 	/* POSIX special builtins and ksh special builtins cause
     92  1.1       jtc 	 * non-interactive shells to exit.
     93  1.1       jtc 	 * XXX odd use of KEEPASN; also may not want LERROR here
     94  1.1       jtc 	 */
     95  1.1       jtc 	if ((builtin_flag & SPEC_BI)
     96  1.1       jtc 	    || (Flag(FPOSIX) && (builtin_flag & KEEPASN)))
     97  1.1       jtc 	{
     98  1.1       jtc 		builtin_argv0 = (char *) 0;
     99  1.1       jtc 		unwind(LERROR);
    100  1.1       jtc 	}
    101  1.1       jtc }
    102  1.1       jtc 
    103  1.1       jtc /* Called when something that shouldn't happen does */
    104  1.1       jtc void
    105  1.1       jtc #ifdef HAVE_PROTOTYPES
    106  1.1       jtc internal_errorf(int jump, const char *fmt, ...)
    107  1.1       jtc #else
    108  1.1       jtc internal_errorf(jump, fmt, va_alist)
    109  1.1       jtc 	int jump;
    110  1.1       jtc 	const char *fmt;
    111  1.1       jtc 	va_dcl
    112  1.1       jtc #endif
    113  1.1       jtc {
    114  1.1       jtc 	va_list va;
    115  1.1       jtc 
    116  1.1       jtc 	error_prefix(TRUE);
    117  1.1       jtc 	shf_fprintf(shl_out, "internal error: ");
    118  1.1       jtc 	SH_VA_START(va, fmt);
    119  1.1       jtc 	shf_vfprintf(shl_out, fmt, va);
    120  1.1       jtc 	va_end(va);
    121  1.1       jtc 	shf_putchar('\n', shl_out);
    122  1.1       jtc 	shf_flush(shl_out);
    123  1.1       jtc 	if (jump)
    124  1.1       jtc 		unwind(LERROR);
    125  1.1       jtc }
    126  1.1       jtc 
    127  1.1       jtc /* used by error reporting functions to print "ksh: .kshrc[25]: " */
    128  1.1       jtc void
    129  1.1       jtc error_prefix(fileline)
    130  1.1       jtc 	int fileline;
    131  1.1       jtc {
    132  1.5   hubertf 	/* Avoid foo: foo[2]: ... */
    133  1.5   hubertf 	if (!fileline || !source || !source->file
    134  1.5   hubertf 	    || strcmp(source->file, kshname) != 0)
    135  1.5   hubertf 		shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
    136  1.1       jtc 	if (fileline && source && source->file != NULL) {
    137  1.1       jtc 		shf_fprintf(shl_out, "%s[%d]: ", source->file,
    138  1.1       jtc 			source->errline > 0 ? source->errline : source->line);
    139  1.1       jtc 		source->errline = 0;
    140  1.1       jtc 	}
    141  1.1       jtc }
    142  1.1       jtc 
    143  1.1       jtc /* printf to shl_out (stderr) with flush */
    144  1.1       jtc void
    145  1.1       jtc #ifdef HAVE_PROTOTYPES
    146  1.1       jtc shellf(const char *fmt, ...)
    147  1.1       jtc #else
    148  1.1       jtc shellf(fmt, va_alist)
    149  1.1       jtc 	const char *fmt;
    150  1.1       jtc 	va_dcl
    151  1.1       jtc #endif
    152  1.1       jtc {
    153  1.1       jtc 	va_list va;
    154  1.1       jtc 
    155  1.5   hubertf 	if (!initio_done) /* shl_out may not be set up yet... */
    156  1.5   hubertf 		return;
    157  1.1       jtc 	SH_VA_START(va, fmt);
    158  1.1       jtc 	shf_vfprintf(shl_out, fmt, va);
    159  1.1       jtc 	va_end(va);
    160  1.1       jtc 	shf_flush(shl_out);
    161  1.1       jtc }
    162  1.1       jtc 
    163  1.1       jtc /* printf to shl_stdout (stdout) */
    164  1.1       jtc void
    165  1.1       jtc #ifdef HAVE_PROTOTYPES
    166  1.1       jtc shprintf(const char *fmt, ...)
    167  1.1       jtc #else
    168  1.1       jtc shprintf(fmt, va_alist)
    169  1.1       jtc 	const char *fmt;
    170  1.1       jtc 	va_dcl
    171  1.1       jtc #endif
    172  1.1       jtc {
    173  1.1       jtc 	va_list va;
    174  1.1       jtc 
    175  1.1       jtc 	if (!shl_stdout_ok)
    176  1.1       jtc 		internal_errorf(1, "shl_stdout not valid");
    177  1.1       jtc 	SH_VA_START(va, fmt);
    178  1.1       jtc 	shf_vfprintf(shl_stdout, fmt, va);
    179  1.1       jtc 	va_end(va);
    180  1.1       jtc }
    181  1.1       jtc 
    182  1.5   hubertf #ifdef KSH_DEBUG
    183  1.5   hubertf static struct shf *kshdebug_shf;
    184  1.5   hubertf 
    185  1.5   hubertf void
    186  1.5   hubertf kshdebug_init_()
    187  1.5   hubertf {
    188  1.5   hubertf 	if (kshdebug_shf)
    189  1.5   hubertf 		shf_close(kshdebug_shf);
    190  1.5   hubertf 	kshdebug_shf = shf_open("/tmp/ksh-debug.log",
    191  1.5   hubertf 				O_WRONLY|O_APPEND|O_CREAT, 0600,
    192  1.5   hubertf 				SHF_WR|SHF_MAPHI);
    193  1.5   hubertf 	if (kshdebug_shf) {
    194  1.5   hubertf 		shf_fprintf(kshdebug_shf, "\nNew shell[pid %d]\n", getpid());
    195  1.5   hubertf 		shf_flush(kshdebug_shf);
    196  1.5   hubertf 	}
    197  1.5   hubertf }
    198  1.5   hubertf 
    199  1.5   hubertf /* print to debugging log */
    200  1.5   hubertf void
    201  1.5   hubertf # ifdef HAVE_PROTOTYPES
    202  1.5   hubertf kshdebug_printf_(const char *fmt, ...)
    203  1.5   hubertf # else
    204  1.5   hubertf kshdebug_printf_(fmt, va_alist)
    205  1.5   hubertf 	const char *fmt;
    206  1.5   hubertf 	va_dcl
    207  1.5   hubertf # endif
    208  1.5   hubertf {
    209  1.5   hubertf 	va_list va;
    210  1.5   hubertf 
    211  1.5   hubertf 	if (!kshdebug_shf)
    212  1.5   hubertf 		return;
    213  1.5   hubertf 	SH_VA_START(va, fmt);
    214  1.5   hubertf 	shf_fprintf(kshdebug_shf, "[%d] ", getpid());
    215  1.5   hubertf 	shf_vfprintf(kshdebug_shf, fmt, va);
    216  1.5   hubertf 	va_end(va);
    217  1.5   hubertf 	shf_flush(kshdebug_shf);
    218  1.5   hubertf }
    219  1.5   hubertf 
    220  1.5   hubertf void
    221  1.5   hubertf kshdebug_dump_(str, mem, nbytes)
    222  1.5   hubertf 	const char *str;
    223  1.5   hubertf 	const void *mem;
    224  1.5   hubertf 	int nbytes;
    225  1.5   hubertf {
    226  1.5   hubertf 	int i, j;
    227  1.5   hubertf 	int nprow = 16;
    228  1.5   hubertf 
    229  1.5   hubertf 	if (!kshdebug_shf)
    230  1.5   hubertf 		return;
    231  1.5   hubertf 	shf_fprintf(kshdebug_shf, "[%d] %s:\n", getpid(), str);
    232  1.5   hubertf 	for (i = 0; i < nbytes; i += nprow) {
    233  1.5   hubertf 		char c = '\t';
    234  1.5   hubertf 		for (j = 0; j < nprow && i + j < nbytes; j++) {
    235  1.5   hubertf 			shf_fprintf(kshdebug_shf, "%c%02x",
    236  1.5   hubertf 				c, ((const unsigned char *) mem)[i + j]);
    237  1.5   hubertf 			c = ' ';
    238  1.5   hubertf 		}
    239  1.5   hubertf 		shf_fprintf(kshdebug_shf, "\n");
    240  1.5   hubertf 	}
    241  1.5   hubertf 	shf_flush(kshdebug_shf);
    242  1.5   hubertf }
    243  1.5   hubertf #endif /* KSH_DEBUG */
    244  1.5   hubertf 
    245  1.1       jtc /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
    246  1.1       jtc int
    247  1.1       jtc can_seek(fd)
    248  1.1       jtc 	int fd;
    249  1.1       jtc {
    250  1.1       jtc 	struct stat statb;
    251  1.1       jtc 
    252  1.1       jtc 	return fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
    253  1.1       jtc 		SHF_UNBUF : 0;
    254  1.1       jtc }
    255  1.1       jtc 
    256  1.1       jtc struct shf	shf_iob[3];
    257  1.1       jtc 
    258  1.1       jtc void
    259  1.1       jtc initio()
    260  1.1       jtc {
    261  1.1       jtc 	shf_fdopen(1, SHF_WR, shl_stdout);	/* force buffer allocation */
    262  1.1       jtc 	shf_fdopen(2, SHF_WR, shl_out);
    263  1.1       jtc 	shf_fdopen(2, SHF_WR, shl_spare);	/* force buffer allocation */
    264  1.5   hubertf 	initio_done = 1;
    265  1.5   hubertf 	kshdebug_init();
    266  1.1       jtc }
    267  1.1       jtc 
    268  1.1       jtc /* A dup2() with error checking */
    269  1.1       jtc int
    270  1.1       jtc ksh_dup2(ofd, nfd, errok)
    271  1.1       jtc 	int ofd;
    272  1.1       jtc 	int nfd;
    273  1.1       jtc 	int errok;
    274  1.1       jtc {
    275  1.1       jtc 	int ret = dup2(ofd, nfd);
    276  1.1       jtc 
    277  1.1       jtc 	if (ret < 0 && errno != EBADF && !errok)
    278  1.1       jtc 		errorf("too many files open in shell");
    279  1.1       jtc 
    280  1.1       jtc #ifdef DUP2_BROKEN
    281  1.1       jtc 	/* Ultrix systems like to preserve the close-on-exec flag */
    282  1.1       jtc 	if (ret >= 0)
    283  1.1       jtc 		(void) fcntl(nfd, F_SETFD, 0);
    284  1.1       jtc #endif /* DUP2_BROKEN */
    285  1.1       jtc 
    286  1.1       jtc 	return ret;
    287  1.1       jtc }
    288  1.1       jtc 
    289  1.1       jtc /*
    290  1.1       jtc  * move fd from user space (0<=fd<10) to shell space (fd>=10),
    291  1.1       jtc  * set close-on-exec flag.
    292  1.1       jtc  */
    293  1.1       jtc int
    294  1.1       jtc savefd(fd, noclose)
    295  1.1       jtc 	int fd;
    296  1.1       jtc 	int noclose;
    297  1.1       jtc {
    298  1.1       jtc 	int nfd;
    299  1.1       jtc 
    300  1.1       jtc 	if (fd < FDBASE) {
    301  1.1       jtc 		nfd = ksh_dupbase(fd, FDBASE);
    302  1.3   thorpej 		if (nfd < 0) {
    303  1.1       jtc 			if (errno == EBADF)
    304  1.1       jtc 				return -1;
    305  1.1       jtc 			else
    306  1.1       jtc 				errorf("too many files open in shell");
    307  1.3   thorpej 		}
    308  1.1       jtc 		if (!noclose)
    309  1.1       jtc 			close(fd);
    310  1.1       jtc 	} else
    311  1.1       jtc 		nfd = fd;
    312  1.1       jtc 	fd_clexec(nfd);
    313  1.1       jtc 	return nfd;
    314  1.1       jtc }
    315  1.1       jtc 
    316  1.1       jtc void
    317  1.1       jtc restfd(fd, ofd)
    318  1.1       jtc 	int fd, ofd;
    319  1.1       jtc {
    320  1.1       jtc 	if (fd == 2)
    321  1.1       jtc 		shf_flush(&shf_iob[fd]);
    322  1.1       jtc 	if (ofd < 0)		/* original fd closed */
    323  1.1       jtc 		close(fd);
    324  1.1       jtc 	else {
    325  1.1       jtc 		ksh_dup2(ofd, fd, TRUE); /* XXX: what to do if this fails? */
    326  1.1       jtc 		close(ofd);
    327  1.1       jtc 	}
    328  1.1       jtc }
    329  1.1       jtc 
    330  1.1       jtc void
    331  1.1       jtc openpipe(pv)
    332  1.1       jtc 	register int *pv;
    333  1.1       jtc {
    334  1.1       jtc 	if (pipe(pv) < 0)
    335  1.1       jtc 		errorf("can't create pipe - try again");
    336  1.1       jtc 	pv[0] = savefd(pv[0], 0);
    337  1.1       jtc 	pv[1] = savefd(pv[1], 0);
    338  1.1       jtc }
    339  1.1       jtc 
    340  1.1       jtc void
    341  1.1       jtc closepipe(pv)
    342  1.1       jtc 	register int *pv;
    343  1.1       jtc {
    344  1.1       jtc 	close(pv[0]);
    345  1.1       jtc 	close(pv[1]);
    346  1.1       jtc }
    347  1.1       jtc 
    348  1.1       jtc /* Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
    349  1.1       jtc  * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
    350  1.1       jtc  */
    351  1.1       jtc int
    352  1.1       jtc check_fd(name, mode, emsgp)
    353  1.1       jtc 	char *name;
    354  1.1       jtc 	int mode;
    355  1.1       jtc 	const char **emsgp;
    356  1.1       jtc {
    357  1.1       jtc 	int fd, fl;
    358  1.1       jtc 
    359  1.4  christos 	if (isdigit((unsigned char)name[0]) && !name[1]) {
    360  1.1       jtc 		fd = name[0] - '0';
    361  1.1       jtc 		if ((fl = fcntl(fd = name[0] - '0', F_GETFL, 0)) < 0) {
    362  1.1       jtc 			if (emsgp)
    363  1.1       jtc 				*emsgp = "bad file descriptor";
    364  1.1       jtc 			return -1;
    365  1.1       jtc 		}
    366  1.1       jtc 		fl &= O_ACCMODE;
    367  1.1       jtc #ifdef OS2
    368  1.1       jtc 		if (mode == W_OK ) {
    369  1.1       jtc 		       if (setmode(fd, O_TEXT) == -1) {
    370  1.1       jtc 				if (emsgp)
    371  1.1       jtc 					*emsgp = "couldn't set write mode";
    372  1.1       jtc 				return -1;
    373  1.1       jtc 			}
    374  1.1       jtc 		 } else if (mode == R_OK)
    375  1.1       jtc 	      		if (setmode(fd, O_BINARY) == -1) {
    376  1.1       jtc 				if (emsgp)
    377  1.1       jtc 					*emsgp = "couldn't set read mode";
    378  1.1       jtc 				return -1;
    379  1.1       jtc 			}
    380  1.1       jtc #else /* OS2 */
    381  1.1       jtc 		/* X_OK is a kludge to disable this check for dups (x<&1):
    382  1.1       jtc 		 * historical shells never did this check (XXX don't know what
    383  1.1       jtc 		 * posix has to say).
    384  1.1       jtc 		 */
    385  1.1       jtc 		if (!(mode & X_OK) && fl != O_RDWR
    386  1.1       jtc 		    && (((mode & R_OK) && fl != O_RDONLY)
    387  1.1       jtc 			|| ((mode & W_OK) && fl != O_WRONLY)))
    388  1.1       jtc 		{
    389  1.1       jtc 			if (emsgp)
    390  1.1       jtc 				*emsgp = (fl == O_WRONLY) ?
    391  1.1       jtc 						"fd not open for reading"
    392  1.1       jtc 					      : "fd not open for writing";
    393  1.1       jtc 			return -1;
    394  1.1       jtc 		}
    395  1.1       jtc #endif /* OS2 */
    396  1.1       jtc 		return fd;
    397  1.1       jtc 	}
    398  1.1       jtc #ifdef KSH
    399  1.1       jtc 	else if (name[0] == 'p' && !name[1])
    400  1.1       jtc 		return coproc_getfd(mode, emsgp);
    401  1.1       jtc #endif /* KSH */
    402  1.1       jtc 	if (emsgp)
    403  1.1       jtc 		*emsgp = "illegal file descriptor name";
    404  1.1       jtc 	return -1;
    405  1.1       jtc }
    406  1.1       jtc 
    407  1.1       jtc #ifdef KSH
    408  1.1       jtc /* Called once from main */
    409  1.1       jtc void
    410  1.1       jtc coproc_init()
    411  1.1       jtc {
    412  1.1       jtc 	coproc.read = coproc.readw = coproc.write = -1;
    413  1.1       jtc 	coproc.njobs = 0;
    414  1.1       jtc 	coproc.id = 0;
    415  1.1       jtc }
    416  1.1       jtc 
    417  1.1       jtc /* Called by c_read() when eof is read - close fd if it is the co-process fd */
    418  1.1       jtc void
    419  1.1       jtc coproc_read_close(fd)
    420  1.1       jtc 	int fd;
    421  1.1       jtc {
    422  1.1       jtc 	if (coproc.read >= 0 && fd == coproc.read) {
    423  1.1       jtc 		coproc_readw_close(fd);
    424  1.1       jtc 		close(coproc.read);
    425  1.1       jtc 		coproc.read = -1;
    426  1.1       jtc 	}
    427  1.1       jtc }
    428  1.1       jtc 
    429  1.1       jtc /* Called by c_read() and by iosetup() to close the other side of the
    430  1.1       jtc  * read pipe, so reads will actually terminate.
    431  1.1       jtc  */
    432  1.1       jtc void
    433  1.1       jtc coproc_readw_close(fd)
    434  1.1       jtc 	int fd;
    435  1.1       jtc {
    436  1.1       jtc 	if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
    437  1.1       jtc 		close(coproc.readw);
    438  1.1       jtc 		coproc.readw = -1;
    439  1.1       jtc 	}
    440  1.1       jtc }
    441  1.1       jtc 
    442  1.1       jtc /* Called by c_print when a write to a fd fails with EPIPE and by iosetup
    443  1.1       jtc  * when co-process input is dup'd
    444  1.1       jtc  */
    445  1.1       jtc void
    446  1.1       jtc coproc_write_close(fd)
    447  1.1       jtc 	int fd;
    448  1.1       jtc {
    449  1.1       jtc 	if (coproc.write >= 0 && fd == coproc.write) {
    450  1.1       jtc 		close(coproc.write);
    451  1.1       jtc 		coproc.write = -1;
    452  1.1       jtc 	}
    453  1.1       jtc }
    454  1.1       jtc 
    455  1.1       jtc /* Called to check for existance of/value of the co-process file descriptor.
    456  1.1       jtc  * (Used by check_fd() and by c_read/c_print to deal with -p option).
    457  1.1       jtc  */
    458  1.1       jtc int
    459  1.1       jtc coproc_getfd(mode, emsgp)
    460  1.1       jtc 	int mode;
    461  1.1       jtc 	const char **emsgp;
    462  1.1       jtc {
    463  1.1       jtc 	int fd = (mode & R_OK) ? coproc.read : coproc.write;
    464  1.1       jtc 
    465  1.1       jtc 	if (fd >= 0)
    466  1.1       jtc 		return fd;
    467  1.1       jtc 	if (emsgp)
    468  1.1       jtc 		*emsgp = "no coprocess";
    469  1.1       jtc 	return -1;
    470  1.1       jtc }
    471  1.1       jtc 
    472  1.1       jtc /* called to close file descriptors related to the coprocess (if any)
    473  1.1       jtc  * Should be called with SIGCHLD blocked.
    474  1.1       jtc  */
    475  1.1       jtc void
    476  1.1       jtc coproc_cleanup(reuse)
    477  1.1       jtc 	int reuse;
    478  1.1       jtc {
    479  1.1       jtc 	/* This to allow co-processes to share output pipe */
    480  1.1       jtc 	if (!reuse || coproc.readw < 0 || coproc.read < 0) {
    481  1.1       jtc 		if (coproc.read >= 0) {
    482  1.1       jtc 			close(coproc.read);
    483  1.1       jtc 			coproc.read = -1;
    484  1.1       jtc 		}
    485  1.1       jtc 		if (coproc.readw >= 0) {
    486  1.1       jtc 			close(coproc.readw);
    487  1.1       jtc 			coproc.readw = -1;
    488  1.1       jtc 		}
    489  1.1       jtc 	}
    490  1.1       jtc 	if (coproc.write >= 0) {
    491  1.1       jtc 		close(coproc.write);
    492  1.1       jtc 		coproc.write = -1;
    493  1.1       jtc 	}
    494  1.1       jtc }
    495  1.1       jtc #endif /* KSH */
    496  1.1       jtc 
    497  1.5   hubertf 
    498  1.1       jtc /*
    499  1.1       jtc  * temporary files
    500  1.1       jtc  */
    501  1.1       jtc 
    502  1.1       jtc struct temp *
    503  1.5   hubertf maketemp(ap, type, tlist)
    504  1.1       jtc 	Area *ap;
    505  1.5   hubertf 	Temp_type type;
    506  1.5   hubertf 	struct temp **tlist;
    507  1.1       jtc {
    508  1.1       jtc 	static unsigned int inc;
    509  1.1       jtc 	struct temp *tp;
    510  1.1       jtc 	int len;
    511  1.1       jtc 	int fd;
    512  1.1       jtc 	char *path;
    513  1.5   hubertf 	const char *dir;
    514  1.1       jtc 
    515  1.5   hubertf 	dir = tmpdir ? tmpdir : "/tmp";
    516  1.1       jtc 	/* The 20 + 20 is a paranoid worst case for pid/inc */
    517  1.5   hubertf 	len = strlen(dir) + 3 + 20 + 20 + 1;
    518  1.1       jtc 	tp = (struct temp *) alloc(sizeof(struct temp) + len, ap);
    519  1.1       jtc 	tp->name = path = (char *) &tp[1];
    520  1.1       jtc 	tp->shf = (struct shf *) 0;
    521  1.5   hubertf 	tp->type = type;
    522  1.1       jtc 	while (1) {
    523  1.1       jtc 		/* Note that temp files need to fit 8.3 DOS limits */
    524  1.1       jtc 		shf_snprintf(path, len, "%s/sh%05u.%03x",
    525  1.5   hubertf 			     dir, (unsigned) procpid, inc++);
    526  1.1       jtc 		/* Mode 0600 to be paranoid, O_TRUNC in case O_EXCL isn't
    527  1.1       jtc 		 * really there.
    528  1.1       jtc 		 */
    529  1.1       jtc 		fd = open(path, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600);
    530  1.1       jtc 		if (fd >= 0) {
    531  1.1       jtc 			tp->shf = shf_fdopen(fd, SHF_WR, (struct shf *) 0);
    532  1.1       jtc 			break;
    533  1.1       jtc 		}
    534  1.1       jtc 		if (errno != EINTR
    535  1.1       jtc #ifdef EEXIST
    536  1.1       jtc 		    && errno != EEXIST
    537  1.1       jtc #endif /* EEXIST */
    538  1.1       jtc #ifdef EISDIR
    539  1.1       jtc 		    && errno != EISDIR
    540  1.1       jtc #endif /* EISDIR */
    541  1.1       jtc 			)
    542  1.5   hubertf 			/* Error must be printed by caller: don't know here if
    543  1.1       jtc 			 * errorf() or bi_errorf() should be used.
    544  1.1       jtc 			 */
    545  1.1       jtc 			break;
    546  1.1       jtc 	}
    547  1.1       jtc 	tp->next = NULL;
    548  1.1       jtc 	tp->pid = procpid;
    549  1.5   hubertf 
    550  1.5   hubertf 	tp->next = *tlist;
    551  1.5   hubertf 	*tlist = tp;
    552  1.5   hubertf 
    553  1.1       jtc 	return tp;
    554  1.1       jtc }
    555