xref: /netbsd-src/bin/ksh/io.c (revision 0f2b5450fbfc5f23c50e3d6158d2810fa603cc92)
1*0f2b5450Skamil /*	$NetBSD: io.c,v 1.18 2018/05/08 16:37:59 kamil Exp $	*/
22ab2e20cStls 
3e1b2664cSjtc /*
4e1b2664cSjtc  * shell buffered IO and formatted output
5e1b2664cSjtc  */
66377cac7Sagc #include <sys/cdefs.h>
76377cac7Sagc 
86377cac7Sagc #ifndef lint
9*0f2b5450Skamil __RCSID("$NetBSD: io.c,v 1.18 2018/05/08 16:37:59 kamil Exp $");
106377cac7Sagc #endif
116377cac7Sagc 
12fac4b394Skamil #include <sys/stat.h>
13e1b2664cSjtc #include <ctype.h>
14e1b2664cSjtc #include "sh.h"
15e1b2664cSjtc 
1648ee8d12Shubertf static int initio_done;
1748ee8d12Shubertf 
18e1b2664cSjtc /*
19e1b2664cSjtc  * formatted output functions
20e1b2664cSjtc  */
21e1b2664cSjtc 
22e1b2664cSjtc 
23456dff6cSwiz /* A shell error occurred (eg, syntax error, etc.) */
24e1b2664cSjtc void
errorf(const char * fmt,...)25e1b2664cSjtc errorf(const char *fmt, ...)
26e1b2664cSjtc {
27e1b2664cSjtc 	va_list va;
28e1b2664cSjtc 
29e1b2664cSjtc 	shl_stdout_ok = 0;	/* debugging: note that stdout not valid */
30e1b2664cSjtc 	exstat = 1;
31e1b2664cSjtc 	if (*fmt) {
32dd8a75d5Skamil 		error_prefix(true);
33acc2fa79Skamil 		va_start(va, fmt);
34e1b2664cSjtc 		shf_vfprintf(shl_out, fmt, va);
35e1b2664cSjtc 		va_end(va);
36e1b2664cSjtc 		shf_putchar('\n', shl_out);
37e1b2664cSjtc 	}
38e1b2664cSjtc 	shf_flush(shl_out);
39e1b2664cSjtc 	unwind(LERROR);
40e1b2664cSjtc }
41e1b2664cSjtc 
42e1b2664cSjtc /* like errorf(), but no unwind is done */
43e1b2664cSjtc void
warningf(int fileline,const char * fmt,...)44e1b2664cSjtc warningf(int fileline, const char *fmt, ...)
45e1b2664cSjtc {
46e1b2664cSjtc 	va_list va;
47e1b2664cSjtc 
48e1b2664cSjtc 	error_prefix(fileline);
49acc2fa79Skamil 	va_start(va, fmt);
50e1b2664cSjtc 	shf_vfprintf(shl_out, fmt, va);
51e1b2664cSjtc 	va_end(va);
52e1b2664cSjtc 	shf_putchar('\n', shl_out);
53e1b2664cSjtc 	shf_flush(shl_out);
54e1b2664cSjtc }
55e1b2664cSjtc 
56e1b2664cSjtc /* Used by built-in utilities to prefix shell and utility name to message
57e1b2664cSjtc  * (also unwinds environments for special builtins).
58e1b2664cSjtc  */
59e1b2664cSjtc void
bi_errorf(const char * fmt,...)60e1b2664cSjtc bi_errorf(const char *fmt, ...)
61e1b2664cSjtc {
62e1b2664cSjtc 	va_list va;
63e1b2664cSjtc 
64e1b2664cSjtc 	shl_stdout_ok = 0;	/* debugging: note that stdout not valid */
65e1b2664cSjtc 	exstat = 1;
66e1b2664cSjtc 	if (*fmt) {
67dd8a75d5Skamil 		error_prefix(true);
68e1b2664cSjtc 		/* not set when main() calls parse_args() */
69e1b2664cSjtc 		if (builtin_argv0)
70e1b2664cSjtc 			shf_fprintf(shl_out, "%s: ", builtin_argv0);
71acc2fa79Skamil 		va_start(va, fmt);
72e1b2664cSjtc 		shf_vfprintf(shl_out, fmt, va);
73e1b2664cSjtc 		va_end(va);
74e1b2664cSjtc 		shf_putchar('\n', shl_out);
75e1b2664cSjtc 	}
76e1b2664cSjtc 	shf_flush(shl_out);
77e1b2664cSjtc 	/* POSIX special builtins and ksh special builtins cause
78e1b2664cSjtc 	 * non-interactive shells to exit.
79e1b2664cSjtc 	 * XXX odd use of KEEPASN; also may not want LERROR here
80e1b2664cSjtc 	 */
81e1b2664cSjtc 	if ((builtin_flag & SPEC_BI)
82e1b2664cSjtc 	    || (Flag(FPOSIX) && (builtin_flag & KEEPASN)))
83e1b2664cSjtc 	{
84e1b2664cSjtc 		builtin_argv0 = (char *) 0;
85e1b2664cSjtc 		unwind(LERROR);
86e1b2664cSjtc 	}
87e1b2664cSjtc }
88e1b2664cSjtc 
89e1b2664cSjtc /* Called when something that shouldn't happen does */
90e1b2664cSjtc void
internal_errorf(int jump,const char * fmt,...)91e1b2664cSjtc internal_errorf(int jump, const char *fmt, ...)
92e1b2664cSjtc {
93e1b2664cSjtc 	va_list va;
94e1b2664cSjtc 
95dd8a75d5Skamil 	error_prefix(true);
96e1b2664cSjtc 	shf_fprintf(shl_out, "internal error: ");
97acc2fa79Skamil 	va_start(va, fmt);
98e1b2664cSjtc 	shf_vfprintf(shl_out, fmt, va);
99e1b2664cSjtc 	va_end(va);
100e1b2664cSjtc 	shf_putchar('\n', shl_out);
101e1b2664cSjtc 	shf_flush(shl_out);
102e1b2664cSjtc 	if (jump)
103e1b2664cSjtc 		unwind(LERROR);
104e1b2664cSjtc }
105e1b2664cSjtc 
106e1b2664cSjtc /* used by error reporting functions to print "ksh: .kshrc[25]: " */
107e1b2664cSjtc void
error_prefix(fileline)108e1b2664cSjtc error_prefix(fileline)
109e1b2664cSjtc 	int fileline;
110e1b2664cSjtc {
11148ee8d12Shubertf 	/* Avoid foo: foo[2]: ... */
11248ee8d12Shubertf 	if (!fileline || !source || !source->file
11348ee8d12Shubertf 	    || strcmp(source->file, kshname) != 0)
114e1b2664cSjtc 		shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
115e1b2664cSjtc 	if (fileline && source && source->file != NULL) {
116e1b2664cSjtc 		shf_fprintf(shl_out, "%s[%d]: ", source->file,
117e1b2664cSjtc 			source->errline > 0 ? source->errline : source->line);
118e1b2664cSjtc 		source->errline = 0;
119e1b2664cSjtc 	}
120e1b2664cSjtc }
121e1b2664cSjtc 
122e1b2664cSjtc /* printf to shl_out (stderr) with flush */
123e1b2664cSjtc void
shellf(const char * fmt,...)124e1b2664cSjtc shellf(const char *fmt, ...)
125e1b2664cSjtc {
126e1b2664cSjtc 	va_list va;
127e1b2664cSjtc 
12848ee8d12Shubertf 	if (!initio_done) /* shl_out may not be set up yet... */
12948ee8d12Shubertf 		return;
130acc2fa79Skamil 	va_start(va, fmt);
131e1b2664cSjtc 	shf_vfprintf(shl_out, fmt, va);
132e1b2664cSjtc 	va_end(va);
133e1b2664cSjtc 	shf_flush(shl_out);
134e1b2664cSjtc }
135e1b2664cSjtc 
136e1b2664cSjtc /* printf to shl_stdout (stdout) */
137e1b2664cSjtc void
shprintf(const char * fmt,...)138e1b2664cSjtc shprintf(const char *fmt, ...)
139e1b2664cSjtc {
140e1b2664cSjtc 	va_list va;
141e1b2664cSjtc 
142e1b2664cSjtc 	if (!shl_stdout_ok)
143e1b2664cSjtc 		internal_errorf(1, "shl_stdout not valid");
144acc2fa79Skamil 	va_start(va, fmt);
145e1b2664cSjtc 	shf_vfprintf(shl_stdout, fmt, va);
146e1b2664cSjtc 	va_end(va);
147e1b2664cSjtc }
148e1b2664cSjtc 
14948ee8d12Shubertf #ifdef KSH_DEBUG
15048ee8d12Shubertf static struct shf *kshdebug_shf;
15148ee8d12Shubertf 
15248ee8d12Shubertf void
kshdebug_init_()15348ee8d12Shubertf kshdebug_init_()
15448ee8d12Shubertf {
15548ee8d12Shubertf 	if (kshdebug_shf)
15648ee8d12Shubertf 		shf_close(kshdebug_shf);
15748ee8d12Shubertf 	kshdebug_shf = shf_open("/tmp/ksh-debug.log",
15848ee8d12Shubertf 				O_WRONLY|O_APPEND|O_CREAT, 0600,
15948ee8d12Shubertf 				SHF_WR|SHF_MAPHI);
16048ee8d12Shubertf 	if (kshdebug_shf) {
16148ee8d12Shubertf 		shf_fprintf(kshdebug_shf, "\nNew shell[pid %d]\n", getpid());
16248ee8d12Shubertf 		shf_flush(kshdebug_shf);
16348ee8d12Shubertf 	}
16448ee8d12Shubertf }
16548ee8d12Shubertf 
16648ee8d12Shubertf /* print to debugging log */
16748ee8d12Shubertf void
kshdebug_printf_(const char * fmt,...)16848ee8d12Shubertf kshdebug_printf_(const char *fmt, ...)
16948ee8d12Shubertf {
17048ee8d12Shubertf 	va_list va;
17148ee8d12Shubertf 
17248ee8d12Shubertf 	if (!kshdebug_shf)
17348ee8d12Shubertf 		return;
174acc2fa79Skamil 	va_start(va, fmt);
17548ee8d12Shubertf 	shf_fprintf(kshdebug_shf, "[%d] ", getpid());
17648ee8d12Shubertf 	shf_vfprintf(kshdebug_shf, fmt, va);
17748ee8d12Shubertf 	va_end(va);
17848ee8d12Shubertf 	shf_flush(kshdebug_shf);
17948ee8d12Shubertf }
18048ee8d12Shubertf 
18148ee8d12Shubertf void
kshdebug_dump_(str,mem,nbytes)18248ee8d12Shubertf kshdebug_dump_(str, mem, nbytes)
18348ee8d12Shubertf 	const char *str;
18448ee8d12Shubertf 	const void *mem;
18548ee8d12Shubertf 	int nbytes;
18648ee8d12Shubertf {
18748ee8d12Shubertf 	int i, j;
18848ee8d12Shubertf 	int nprow = 16;
18948ee8d12Shubertf 
19048ee8d12Shubertf 	if (!kshdebug_shf)
19148ee8d12Shubertf 		return;
19248ee8d12Shubertf 	shf_fprintf(kshdebug_shf, "[%d] %s:\n", getpid(), str);
19348ee8d12Shubertf 	for (i = 0; i < nbytes; i += nprow) {
19448ee8d12Shubertf 		char c = '\t';
19548ee8d12Shubertf 		for (j = 0; j < nprow && i + j < nbytes; j++) {
19648ee8d12Shubertf 			shf_fprintf(kshdebug_shf, "%c%02x",
19748ee8d12Shubertf 				c, ((const unsigned char *) mem)[i + j]);
19848ee8d12Shubertf 			c = ' ';
19948ee8d12Shubertf 		}
20048ee8d12Shubertf 		shf_fprintf(kshdebug_shf, "\n");
20148ee8d12Shubertf 	}
20248ee8d12Shubertf 	shf_flush(kshdebug_shf);
20348ee8d12Shubertf }
20448ee8d12Shubertf #endif /* KSH_DEBUG */
20548ee8d12Shubertf 
206e1b2664cSjtc /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
207e1b2664cSjtc int
can_seek(fd)208e1b2664cSjtc can_seek(fd)
209e1b2664cSjtc 	int fd;
210e1b2664cSjtc {
211e1b2664cSjtc 	struct stat statb;
212e1b2664cSjtc 
213e1b2664cSjtc 	return fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
214e1b2664cSjtc 		SHF_UNBUF : 0;
215e1b2664cSjtc }
216e1b2664cSjtc 
217e1b2664cSjtc struct shf	shf_iob[3];
218e1b2664cSjtc 
219e1b2664cSjtc void
initio()220e1b2664cSjtc initio()
221e1b2664cSjtc {
222e1b2664cSjtc 	shf_fdopen(1, SHF_WR, shl_stdout);	/* force buffer allocation */
223e1b2664cSjtc 	shf_fdopen(2, SHF_WR, shl_out);
224e1b2664cSjtc 	shf_fdopen(2, SHF_WR, shl_spare);	/* force buffer allocation */
22548ee8d12Shubertf 	initio_done = 1;
22648ee8d12Shubertf 	kshdebug_init();
227e1b2664cSjtc }
228e1b2664cSjtc 
229e1b2664cSjtc /* A dup2() with error checking */
230e1b2664cSjtc int
ksh_dup2(ofd,nfd,errok)231e1b2664cSjtc ksh_dup2(ofd, nfd, errok)
232e1b2664cSjtc 	int ofd;
233e1b2664cSjtc 	int nfd;
234e1b2664cSjtc 	int errok;
235e1b2664cSjtc {
236e1b2664cSjtc 	int ret = dup2(ofd, nfd);
237e1b2664cSjtc 
238e1b2664cSjtc 	if (ret < 0 && errno != EBADF && !errok)
239e1b2664cSjtc 		errorf("too many files open in shell");
240e1b2664cSjtc 
241e1b2664cSjtc 	return ret;
242e1b2664cSjtc }
243e1b2664cSjtc 
244e1b2664cSjtc /*
245e1b2664cSjtc  * move fd from user space (0<=fd<10) to shell space (fd>=10),
246e1b2664cSjtc  * set close-on-exec flag.
247e1b2664cSjtc  */
248e1b2664cSjtc int
savefd(fd,noclose)249e1b2664cSjtc savefd(fd, noclose)
250e1b2664cSjtc 	int fd;
251e1b2664cSjtc 	int noclose;
252e1b2664cSjtc {
253e1b2664cSjtc 	int nfd;
254e1b2664cSjtc 
255e1b2664cSjtc 	if (fd < FDBASE) {
256e1b2664cSjtc 		nfd = ksh_dupbase(fd, FDBASE);
25745e5a869Sthorpej 		if (nfd < 0) {
258e1b2664cSjtc 			if (errno == EBADF)
259e1b2664cSjtc 				return -1;
260e1b2664cSjtc 			else
261e1b2664cSjtc 				errorf("too many files open in shell");
26245e5a869Sthorpej 		}
263e1b2664cSjtc 		if (!noclose)
264e1b2664cSjtc 			close(fd);
265e1b2664cSjtc 	} else
266e1b2664cSjtc 		nfd = fd;
267e1b2664cSjtc 	fd_clexec(nfd);
268e1b2664cSjtc 	return nfd;
269e1b2664cSjtc }
270e1b2664cSjtc 
271e1b2664cSjtc void
restfd(fd,ofd)272e1b2664cSjtc restfd(fd, ofd)
273e1b2664cSjtc 	int fd, ofd;
274e1b2664cSjtc {
275e1b2664cSjtc 	if (fd == 2)
276e1b2664cSjtc 		shf_flush(&shf_iob[fd]);
277e1b2664cSjtc 	if (ofd < 0)		/* original fd closed */
278e1b2664cSjtc 		close(fd);
279f662a744Smycroft 	else if (fd != ofd) {
280dd8a75d5Skamil 		ksh_dup2(ofd, fd, true); /* XXX: what to do if this fails? */
281e1b2664cSjtc 		close(ofd);
282e1b2664cSjtc 	}
283e1b2664cSjtc }
284e1b2664cSjtc 
285e1b2664cSjtc void
openpipe(pv)286e1b2664cSjtc openpipe(pv)
287*0f2b5450Skamil 	int *pv;
288e1b2664cSjtc {
289e1b2664cSjtc 	if (pipe(pv) < 0)
290e1b2664cSjtc 		errorf("can't create pipe - try again");
291e1b2664cSjtc 	pv[0] = savefd(pv[0], 0);
292e1b2664cSjtc 	pv[1] = savefd(pv[1], 0);
293e1b2664cSjtc }
294e1b2664cSjtc 
295e1b2664cSjtc void
closepipe(pv)296e1b2664cSjtc closepipe(pv)
297*0f2b5450Skamil 	int *pv;
298e1b2664cSjtc {
299e1b2664cSjtc 	close(pv[0]);
300e1b2664cSjtc 	close(pv[1]);
301e1b2664cSjtc }
302e1b2664cSjtc 
303e1b2664cSjtc /* Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
304e1b2664cSjtc  * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
305e1b2664cSjtc  */
306e1b2664cSjtc int
check_fd(name,mode,emsgp)307e1b2664cSjtc check_fd(name, mode, emsgp)
308e1b2664cSjtc 	char *name;
309e1b2664cSjtc 	int mode;
310e1b2664cSjtc 	const char **emsgp;
311e1b2664cSjtc {
312e1b2664cSjtc 	int fd, fl;
313e1b2664cSjtc 
3141f3392afSchristos 	if (isdigit((unsigned char)name[0]) && !name[1]) {
315e1b2664cSjtc 		fd = name[0] - '0';
316e1b2664cSjtc 		if ((fl = fcntl(fd = name[0] - '0', F_GETFL, 0)) < 0) {
317e1b2664cSjtc 			if (emsgp)
318e1b2664cSjtc 				*emsgp = "bad file descriptor";
319e1b2664cSjtc 			return -1;
320e1b2664cSjtc 		}
321e1b2664cSjtc 		fl &= O_ACCMODE;
322eb13ce8cSkamil 
323e1b2664cSjtc 		/* X_OK is a kludge to disable this check for dups (x<&1):
324e1b2664cSjtc 		 * historical shells never did this check (XXX don't know what
325e1b2664cSjtc 		 * posix has to say).
326e1b2664cSjtc 		 */
327e1b2664cSjtc 		if (!(mode & X_OK) && fl != O_RDWR
328e1b2664cSjtc 		    && (((mode & R_OK) && fl != O_RDONLY)
329e1b2664cSjtc 			|| ((mode & W_OK) && fl != O_WRONLY)))
330e1b2664cSjtc 		{
331e1b2664cSjtc 			if (emsgp)
332e1b2664cSjtc 				*emsgp = (fl == O_WRONLY) ?
333e1b2664cSjtc 						"fd not open for reading"
334e1b2664cSjtc 					      : "fd not open for writing";
335e1b2664cSjtc 			return -1;
336e1b2664cSjtc 		}
337e1b2664cSjtc 		return fd;
338e1b2664cSjtc 	}
339e1b2664cSjtc #ifdef KSH
340e1b2664cSjtc 	else if (name[0] == 'p' && !name[1])
341e1b2664cSjtc 		return coproc_getfd(mode, emsgp);
342e1b2664cSjtc #endif /* KSH */
343e1b2664cSjtc 	if (emsgp)
344e1b2664cSjtc 		*emsgp = "illegal file descriptor name";
345e1b2664cSjtc 	return -1;
346e1b2664cSjtc }
347e1b2664cSjtc 
348e1b2664cSjtc #ifdef KSH
349e1b2664cSjtc /* Called once from main */
350e1b2664cSjtc void
coproc_init()351e1b2664cSjtc coproc_init()
352e1b2664cSjtc {
353e1b2664cSjtc 	coproc.read = coproc.readw = coproc.write = -1;
354e1b2664cSjtc 	coproc.njobs = 0;
355e1b2664cSjtc 	coproc.id = 0;
356e1b2664cSjtc }
357e1b2664cSjtc 
358e1b2664cSjtc /* Called by c_read() when eof is read - close fd if it is the co-process fd */
359e1b2664cSjtc void
coproc_read_close(fd)360e1b2664cSjtc coproc_read_close(fd)
361e1b2664cSjtc 	int fd;
362e1b2664cSjtc {
363e1b2664cSjtc 	if (coproc.read >= 0 && fd == coproc.read) {
364e1b2664cSjtc 		coproc_readw_close(fd);
365e1b2664cSjtc 		close(coproc.read);
366e1b2664cSjtc 		coproc.read = -1;
367e1b2664cSjtc 	}
368e1b2664cSjtc }
369e1b2664cSjtc 
370e1b2664cSjtc /* Called by c_read() and by iosetup() to close the other side of the
371e1b2664cSjtc  * read pipe, so reads will actually terminate.
372e1b2664cSjtc  */
373e1b2664cSjtc void
coproc_readw_close(fd)374e1b2664cSjtc coproc_readw_close(fd)
375e1b2664cSjtc 	int fd;
376e1b2664cSjtc {
377e1b2664cSjtc 	if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
378e1b2664cSjtc 		close(coproc.readw);
379e1b2664cSjtc 		coproc.readw = -1;
380e1b2664cSjtc 	}
381e1b2664cSjtc }
382e1b2664cSjtc 
383e1b2664cSjtc /* Called by c_print when a write to a fd fails with EPIPE and by iosetup
384e1b2664cSjtc  * when co-process input is dup'd
385e1b2664cSjtc  */
386e1b2664cSjtc void
coproc_write_close(fd)387e1b2664cSjtc coproc_write_close(fd)
388e1b2664cSjtc 	int fd;
389e1b2664cSjtc {
390e1b2664cSjtc 	if (coproc.write >= 0 && fd == coproc.write) {
391e1b2664cSjtc 		close(coproc.write);
392e1b2664cSjtc 		coproc.write = -1;
393e1b2664cSjtc 	}
394e1b2664cSjtc }
395e1b2664cSjtc 
396f662a744Smycroft /* Called to check for existence of/value of the co-process file descriptor.
397e1b2664cSjtc  * (Used by check_fd() and by c_read/c_print to deal with -p option).
398e1b2664cSjtc  */
399e1b2664cSjtc int
coproc_getfd(mode,emsgp)400e1b2664cSjtc coproc_getfd(mode, emsgp)
401e1b2664cSjtc 	int mode;
402e1b2664cSjtc 	const char **emsgp;
403e1b2664cSjtc {
404e1b2664cSjtc 	int fd = (mode & R_OK) ? coproc.read : coproc.write;
405e1b2664cSjtc 
406e1b2664cSjtc 	if (fd >= 0)
407e1b2664cSjtc 		return fd;
408e1b2664cSjtc 	if (emsgp)
409e1b2664cSjtc 		*emsgp = "no coprocess";
410e1b2664cSjtc 	return -1;
411e1b2664cSjtc }
412e1b2664cSjtc 
413e1b2664cSjtc /* called to close file descriptors related to the coprocess (if any)
414e1b2664cSjtc  * Should be called with SIGCHLD blocked.
415e1b2664cSjtc  */
416e1b2664cSjtc void
coproc_cleanup(reuse)417e1b2664cSjtc coproc_cleanup(reuse)
418e1b2664cSjtc 	int reuse;
419e1b2664cSjtc {
420e1b2664cSjtc 	/* This to allow co-processes to share output pipe */
421e1b2664cSjtc 	if (!reuse || coproc.readw < 0 || coproc.read < 0) {
422e1b2664cSjtc 		if (coproc.read >= 0) {
423e1b2664cSjtc 			close(coproc.read);
424e1b2664cSjtc 			coproc.read = -1;
425e1b2664cSjtc 		}
426e1b2664cSjtc 		if (coproc.readw >= 0) {
427e1b2664cSjtc 			close(coproc.readw);
428e1b2664cSjtc 			coproc.readw = -1;
429e1b2664cSjtc 		}
430e1b2664cSjtc 	}
431e1b2664cSjtc 	if (coproc.write >= 0) {
432e1b2664cSjtc 		close(coproc.write);
433e1b2664cSjtc 		coproc.write = -1;
434e1b2664cSjtc 	}
435e1b2664cSjtc }
436e1b2664cSjtc #endif /* KSH */
437e1b2664cSjtc 
43848ee8d12Shubertf 
439e1b2664cSjtc /*
440e1b2664cSjtc  * temporary files
441e1b2664cSjtc  */
442e1b2664cSjtc 
443e1b2664cSjtc struct temp *
maketemp(ap,type,tlist)44448ee8d12Shubertf maketemp(ap, type, tlist)
445e1b2664cSjtc 	Area *ap;
44648ee8d12Shubertf 	Temp_type type;
44748ee8d12Shubertf 	struct temp **tlist;
448e1b2664cSjtc {
449f662a744Smycroft #ifndef __NetBSD__
450e1b2664cSjtc 	static unsigned int inc;
451f662a744Smycroft #endif
452e1b2664cSjtc 	struct temp *tp;
453e1b2664cSjtc 	int len;
454e1b2664cSjtc 	int fd;
455a397ec1fSchristos 	char *pathx;
45648ee8d12Shubertf 	const char *dir;
457e1b2664cSjtc 
45848ee8d12Shubertf 	dir = tmpdir ? tmpdir : "/tmp";
459e1b2664cSjtc 	/* The 20 + 20 is a paranoid worst case for pid/inc */
46048ee8d12Shubertf 	len = strlen(dir) + 3 + 20 + 20 + 1;
461e1b2664cSjtc 	tp = (struct temp *) alloc(sizeof(struct temp) + len, ap);
462a397ec1fSchristos 	tp->name = pathx = (char *) &tp[1];
463e1b2664cSjtc 	tp->shf = (struct shf *) 0;
46448ee8d12Shubertf 	tp->type = type;
465f662a744Smycroft #ifdef __NetBSD__
466a397ec1fSchristos 	shf_snprintf(pathx, len, "%s/shXXXXXXXX", dir);
467a397ec1fSchristos 	fd = mkstemp(pathx);
468f662a744Smycroft 	if (fd >= 0)
469f662a744Smycroft 		tp->shf = shf_fdopen(fd, SHF_WR, (struct shf *) 0);
470f662a744Smycroft #else
471e1b2664cSjtc 	while (1) {
472e1b2664cSjtc 		/* Note that temp files need to fit 8.3 DOS limits */
473a397ec1fSchristos 		shf_snprintf(pathx, len, "%s/sh%05u.%03x",
47448ee8d12Shubertf 			     dir, (unsigned) procpid, inc++);
475e1b2664cSjtc 		/* Mode 0600 to be paranoid, O_TRUNC in case O_EXCL isn't
476e1b2664cSjtc 		 * really there.
477e1b2664cSjtc 		 */
478a397ec1fSchristos 		fd = open(pathx, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600);
479e1b2664cSjtc 		if (fd >= 0) {
480e1b2664cSjtc 			tp->shf = shf_fdopen(fd, SHF_WR, (struct shf *) 0);
481e1b2664cSjtc 			break;
482e1b2664cSjtc 		}
483e1b2664cSjtc 		if (errno != EINTR
484e1b2664cSjtc #ifdef EEXIST
485e1b2664cSjtc 		    && errno != EEXIST
486e1b2664cSjtc #endif /* EEXIST */
487e1b2664cSjtc #ifdef EISDIR
488e1b2664cSjtc 		    && errno != EISDIR
489e1b2664cSjtc #endif /* EISDIR */
490e1b2664cSjtc 			)
49148ee8d12Shubertf 			/* Error must be printed by caller: don't know here if
492e1b2664cSjtc 			 * errorf() or bi_errorf() should be used.
493e1b2664cSjtc 			 */
494e1b2664cSjtc 			break;
495e1b2664cSjtc 	}
496f662a744Smycroft #endif /* __NetBSD__ */
497e1b2664cSjtc 	tp->pid = procpid;
49848ee8d12Shubertf 
49948ee8d12Shubertf 	tp->next = *tlist;
50048ee8d12Shubertf 	*tlist = tp;
50148ee8d12Shubertf 
502e1b2664cSjtc 	return tp;
503e1b2664cSjtc }
504