1*c6479af2Sbcallah /* $OpenBSD: io.c,v 1.38 2019/07/24 14:33:16 bcallah Exp $ */
27cb960a2Sdownsj
37cb960a2Sdownsj /*
47cb960a2Sdownsj * shell buffered IO and formatted output
57cb960a2Sdownsj */
67cb960a2Sdownsj
769b9f96bSmillert #include <sys/stat.h>
87cb960a2Sdownsj
9b608f594Smmcc #include <ctype.h>
104a010e0cStb #include <errno.h>
114a010e0cStb #include <fcntl.h>
12*c6479af2Sbcallah #include <stdlib.h>
1356018212Smmcc #include <string.h>
144a010e0cStb #include <unistd.h>
15b608f594Smmcc
16b608f594Smmcc #include "sh.h"
17b608f594Smmcc
183b015934Smillert static int initio_done;
193b015934Smillert
207cb960a2Sdownsj /*
217cb960a2Sdownsj * formatted output functions
227cb960a2Sdownsj */
237cb960a2Sdownsj
247cb960a2Sdownsj
2561e6c220Smpech /* A shell error occurred (eg, syntax error, etc.) */
267cb960a2Sdownsj void
errorf(const char * fmt,...)277cb960a2Sdownsj errorf(const char *fmt, ...)
287cb960a2Sdownsj {
297cb960a2Sdownsj va_list va;
307cb960a2Sdownsj
317cb960a2Sdownsj shl_stdout_ok = 0; /* debugging: note that stdout not valid */
327cb960a2Sdownsj exstat = 1;
3363ca93eaSmillert if (fmt != NULL && *fmt != '\0') {
340e7d3a01Smillert error_prefix(true);
3569b9f96bSmillert va_start(va, fmt);
367cb960a2Sdownsj shf_vfprintf(shl_out, fmt, va);
377cb960a2Sdownsj va_end(va);
387cb960a2Sdownsj shf_putchar('\n', shl_out);
397cb960a2Sdownsj }
407cb960a2Sdownsj shf_flush(shl_out);
417cb960a2Sdownsj unwind(LERROR);
427cb960a2Sdownsj }
437cb960a2Sdownsj
447cb960a2Sdownsj /* like errorf(), but no unwind is done */
457cb960a2Sdownsj void
warningf(bool show_lineno,const char * fmt,...)46504796f7Smmcc warningf(bool show_lineno, const char *fmt, ...)
477cb960a2Sdownsj {
487cb960a2Sdownsj va_list va;
497cb960a2Sdownsj
50504796f7Smmcc error_prefix(show_lineno);
5169b9f96bSmillert va_start(va, fmt);
527cb960a2Sdownsj shf_vfprintf(shl_out, fmt, va);
537cb960a2Sdownsj va_end(va);
547cb960a2Sdownsj shf_putchar('\n', shl_out);
557cb960a2Sdownsj shf_flush(shl_out);
567cb960a2Sdownsj }
577cb960a2Sdownsj
587cb960a2Sdownsj /* Used by built-in utilities to prefix shell and utility name to message
597cb960a2Sdownsj * (also unwinds environments for special builtins).
607cb960a2Sdownsj */
617cb960a2Sdownsj void
bi_errorf(const char * fmt,...)627cb960a2Sdownsj bi_errorf(const char *fmt, ...)
637cb960a2Sdownsj {
647cb960a2Sdownsj va_list va;
657cb960a2Sdownsj
667cb960a2Sdownsj shl_stdout_ok = 0; /* debugging: note that stdout not valid */
677cb960a2Sdownsj exstat = 1;
6863ca93eaSmillert if (fmt != NULL && *fmt != '\0') {
690e7d3a01Smillert error_prefix(true);
707cb960a2Sdownsj /* not set when main() calls parse_args() */
717cb960a2Sdownsj if (builtin_argv0)
727cb960a2Sdownsj shf_fprintf(shl_out, "%s: ", builtin_argv0);
7369b9f96bSmillert va_start(va, fmt);
747cb960a2Sdownsj shf_vfprintf(shl_out, fmt, va);
757cb960a2Sdownsj va_end(va);
767cb960a2Sdownsj shf_putchar('\n', shl_out);
777cb960a2Sdownsj }
787cb960a2Sdownsj shf_flush(shl_out);
797cb960a2Sdownsj /* POSIX special builtins and ksh special builtins cause
807cb960a2Sdownsj * non-interactive shells to exit.
817cb960a2Sdownsj * XXX odd use of KEEPASN; also may not want LERROR here
827cb960a2Sdownsj */
837a8124d8Sderaadt if ((builtin_flag & SPEC_BI) ||
847a8124d8Sderaadt (Flag(FPOSIX) && (builtin_flag & KEEPASN))) {
85355ffa75Stedu builtin_argv0 = NULL;
867cb960a2Sdownsj unwind(LERROR);
877cb960a2Sdownsj }
887cb960a2Sdownsj }
897cb960a2Sdownsj
906c72b531Sjca static void
internal_error_vwarn(const char * fmt,va_list va)916c72b531Sjca internal_error_vwarn(const char *fmt, va_list va)
926c72b531Sjca {
936c72b531Sjca error_prefix(true);
946c72b531Sjca shf_fprintf(shl_out, "internal error: ");
956c72b531Sjca shf_vfprintf(shl_out, fmt, va);
966c72b531Sjca shf_putchar('\n', shl_out);
976c72b531Sjca shf_flush(shl_out);
986c72b531Sjca }
996c72b531Sjca
1006c72b531Sjca /* Warn when something that shouldn't happen does */
1017cb960a2Sdownsj void
internal_warningf(const char * fmt,...)1026c72b531Sjca internal_warningf(const char *fmt, ...)
1037cb960a2Sdownsj {
1047cb960a2Sdownsj va_list va;
1057cb960a2Sdownsj
10669b9f96bSmillert va_start(va, fmt);
1076c72b531Sjca internal_error_vwarn(fmt, va);
1087cb960a2Sdownsj va_end(va);
1096c72b531Sjca }
1106c72b531Sjca
1116c72b531Sjca /* Warn and unwind when something that shouldn't happen does */
1126c72b531Sjca __dead void
internal_errorf(const char * fmt,...)1136c72b531Sjca internal_errorf(const char *fmt, ...)
1146c72b531Sjca {
1156c72b531Sjca va_list va;
1166c72b531Sjca
1176c72b531Sjca va_start(va, fmt);
1186c72b531Sjca internal_error_vwarn(fmt, va);
1196c72b531Sjca va_end(va);
1207cb960a2Sdownsj unwind(LERROR);
1217cb960a2Sdownsj }
1227cb960a2Sdownsj
1237cb960a2Sdownsj /* used by error reporting functions to print "ksh: .kshrc[25]: " */
1247cb960a2Sdownsj void
error_prefix(int fileline)125c5d5393cSotto error_prefix(int fileline)
1267cb960a2Sdownsj {
1273b015934Smillert /* Avoid foo: foo[2]: ... */
1287a8124d8Sderaadt if (!fileline || !source || !source->file ||
1297a8124d8Sderaadt strcmp(source->file, kshname) != 0)
1307cb960a2Sdownsj shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
1317cb960a2Sdownsj if (fileline && source && source->file != NULL) {
1327cb960a2Sdownsj shf_fprintf(shl_out, "%s[%d]: ", source->file,
1337cb960a2Sdownsj source->errline > 0 ? source->errline : source->line);
1347cb960a2Sdownsj source->errline = 0;
1357cb960a2Sdownsj }
1367cb960a2Sdownsj }
1377cb960a2Sdownsj
1387cb960a2Sdownsj /* printf to shl_out (stderr) with flush */
1397cb960a2Sdownsj void
shellf(const char * fmt,...)1407cb960a2Sdownsj shellf(const char *fmt, ...)
1417cb960a2Sdownsj {
1427cb960a2Sdownsj va_list va;
1437cb960a2Sdownsj
1443b015934Smillert if (!initio_done) /* shl_out may not be set up yet... */
1453b015934Smillert return;
14669b9f96bSmillert va_start(va, fmt);
1477cb960a2Sdownsj shf_vfprintf(shl_out, fmt, va);
1487cb960a2Sdownsj va_end(va);
1497cb960a2Sdownsj shf_flush(shl_out);
1507cb960a2Sdownsj }
1517cb960a2Sdownsj
1527cb960a2Sdownsj /* printf to shl_stdout (stdout) */
1537cb960a2Sdownsj void
shprintf(const char * fmt,...)1547cb960a2Sdownsj shprintf(const char *fmt, ...)
1557cb960a2Sdownsj {
1567cb960a2Sdownsj va_list va;
1577cb960a2Sdownsj
1587cb960a2Sdownsj if (!shl_stdout_ok)
1596c72b531Sjca internal_errorf("shl_stdout not valid");
16069b9f96bSmillert va_start(va, fmt);
1617cb960a2Sdownsj shf_vfprintf(shl_stdout, fmt, va);
1627cb960a2Sdownsj va_end(va);
1637cb960a2Sdownsj }
1647cb960a2Sdownsj
165945abdecSmillert #ifdef KSH_DEBUG
166945abdecSmillert static struct shf *kshdebug_shf;
167945abdecSmillert
168945abdecSmillert void
kshdebug_init_(void)169c5d5393cSotto kshdebug_init_(void)
170945abdecSmillert {
171945abdecSmillert if (kshdebug_shf)
172945abdecSmillert shf_close(kshdebug_shf);
173945abdecSmillert kshdebug_shf = shf_open("/tmp/ksh-debug.log",
1747a8124d8Sderaadt O_WRONLY|O_APPEND|O_CREAT, 0600, SHF_WR|SHF_MAPHI);
175945abdecSmillert if (kshdebug_shf) {
176945abdecSmillert shf_fprintf(kshdebug_shf, "\nNew shell[pid %d]\n", getpid());
177945abdecSmillert shf_flush(kshdebug_shf);
178945abdecSmillert }
179945abdecSmillert }
180945abdecSmillert
181945abdecSmillert /* print to debugging log */
182945abdecSmillert void
kshdebug_printf_(const char * fmt,...)183945abdecSmillert kshdebug_printf_(const char *fmt, ...)
184945abdecSmillert {
185945abdecSmillert va_list va;
186945abdecSmillert
187945abdecSmillert if (!kshdebug_shf)
188945abdecSmillert return;
18969b9f96bSmillert va_start(va, fmt);
190945abdecSmillert shf_fprintf(kshdebug_shf, "[%d] ", getpid());
191945abdecSmillert shf_vfprintf(kshdebug_shf, fmt, va);
192945abdecSmillert va_end(va);
193945abdecSmillert shf_flush(kshdebug_shf);
194945abdecSmillert }
195945abdecSmillert
196945abdecSmillert void
kshdebug_dump_(const char * str,const void * mem,int nbytes)197c5d5393cSotto kshdebug_dump_(const char *str, const void *mem, int nbytes)
198945abdecSmillert {
199945abdecSmillert int i, j;
200945abdecSmillert int nprow = 16;
201945abdecSmillert
202945abdecSmillert if (!kshdebug_shf)
203945abdecSmillert return;
204945abdecSmillert shf_fprintf(kshdebug_shf, "[%d] %s:\n", getpid(), str);
205945abdecSmillert for (i = 0; i < nbytes; i += nprow) {
206945abdecSmillert char c = '\t';
2077a8124d8Sderaadt
208945abdecSmillert for (j = 0; j < nprow && i + j < nbytes; j++) {
2097a8124d8Sderaadt shf_fprintf(kshdebug_shf, "%c%02x", c,
2107a8124d8Sderaadt ((const unsigned char *) mem)[i + j]);
211945abdecSmillert c = ' ';
212945abdecSmillert }
213945abdecSmillert shf_fprintf(kshdebug_shf, "\n");
214945abdecSmillert }
215945abdecSmillert shf_flush(kshdebug_shf);
216945abdecSmillert }
217945abdecSmillert #endif /* KSH_DEBUG */
218945abdecSmillert
2197cb960a2Sdownsj /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
2207cb960a2Sdownsj int
can_seek(int fd)221c5d5393cSotto can_seek(int fd)
2227cb960a2Sdownsj {
2237cb960a2Sdownsj struct stat statb;
2247cb960a2Sdownsj
2257cb960a2Sdownsj return fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
2267cb960a2Sdownsj SHF_UNBUF : 0;
2277cb960a2Sdownsj }
2287cb960a2Sdownsj
2297cb960a2Sdownsj struct shf shf_iob[3];
2307cb960a2Sdownsj
2317cb960a2Sdownsj void
initio(void)232c5d5393cSotto initio(void)
2337cb960a2Sdownsj {
2347cb960a2Sdownsj shf_fdopen(1, SHF_WR, shl_stdout); /* force buffer allocation */
2357cb960a2Sdownsj shf_fdopen(2, SHF_WR, shl_out);
2367cb960a2Sdownsj shf_fdopen(2, SHF_WR, shl_spare); /* force buffer allocation */
2373b015934Smillert initio_done = 1;
238945abdecSmillert kshdebug_init();
2397cb960a2Sdownsj }
2407cb960a2Sdownsj
2417cb960a2Sdownsj /* A dup2() with error checking */
2427cb960a2Sdownsj int
ksh_dup2(int ofd,int nfd,int errok)243c5d5393cSotto ksh_dup2(int ofd, int nfd, int errok)
2447cb960a2Sdownsj {
2457cb960a2Sdownsj int ret = dup2(ofd, nfd);
2467cb960a2Sdownsj
2473aaa63ebSderaadt if (ret == -1 && errno != EBADF && !errok)
2487cb960a2Sdownsj errorf("too many files open in shell");
2497cb960a2Sdownsj
2507cb960a2Sdownsj return ret;
2517cb960a2Sdownsj }
2527cb960a2Sdownsj
2537cb960a2Sdownsj /*
2547cb960a2Sdownsj * move fd from user space (0<=fd<10) to shell space (fd>=10),
2557cb960a2Sdownsj * set close-on-exec flag.
2567cb960a2Sdownsj */
2577cb960a2Sdownsj int
savefd(int fd)258afe13ccaSmillert savefd(int fd)
2597cb960a2Sdownsj {
2607cb960a2Sdownsj int nfd;
2617cb960a2Sdownsj
2627cb960a2Sdownsj if (fd < FDBASE) {
263bf50f917Sguenther nfd = fcntl(fd, F_DUPFD_CLOEXEC, FDBASE);
2643aaa63ebSderaadt if (nfd == -1) {
2657cb960a2Sdownsj if (errno == EBADF)
2667cb960a2Sdownsj return -1;
2677cb960a2Sdownsj else
2687cb960a2Sdownsj errorf("too many files open in shell");
26918bbba6bSmillert }
270b703bf84Sguenther } else {
2717cb960a2Sdownsj nfd = fd;
272b703bf84Sguenther fcntl(nfd, F_SETFD, FD_CLOEXEC);
273b703bf84Sguenther }
2747cb960a2Sdownsj return nfd;
2757cb960a2Sdownsj }
2767cb960a2Sdownsj
2777cb960a2Sdownsj void
restfd(int fd,int ofd)278c5d5393cSotto restfd(int fd, int ofd)
2797cb960a2Sdownsj {
2807cb960a2Sdownsj if (fd == 2)
2817cb960a2Sdownsj shf_flush(&shf_iob[fd]);
2827cb960a2Sdownsj if (ofd < 0) /* original fd closed */
2837cb960a2Sdownsj close(fd);
284c06a3dc9Smillert else if (fd != ofd) {
2850e7d3a01Smillert ksh_dup2(ofd, fd, true); /* XXX: what to do if this fails? */
2867cb960a2Sdownsj close(ofd);
2877cb960a2Sdownsj }
2887cb960a2Sdownsj }
2897cb960a2Sdownsj
2907cb960a2Sdownsj void
openpipe(int * pv)291c5d5393cSotto openpipe(int *pv)
2927cb960a2Sdownsj {
293afe13ccaSmillert int lpv[2];
294afe13ccaSmillert
2953aaa63ebSderaadt if (pipe(lpv) == -1)
2967cb960a2Sdownsj errorf("can't create pipe - try again");
297afe13ccaSmillert pv[0] = savefd(lpv[0]);
298afe13ccaSmillert if (pv[0] != lpv[0])
299afe13ccaSmillert close(lpv[0]);
300afe13ccaSmillert pv[1] = savefd(lpv[1]);
301afe13ccaSmillert if (pv[1] != lpv[1])
302afe13ccaSmillert close(lpv[1]);
3037cb960a2Sdownsj }
3047cb960a2Sdownsj
3057cb960a2Sdownsj void
closepipe(int * pv)306c5d5393cSotto closepipe(int *pv)
3077cb960a2Sdownsj {
3087cb960a2Sdownsj close(pv[0]);
3097cb960a2Sdownsj close(pv[1]);
3107cb960a2Sdownsj }
3117cb960a2Sdownsj
3127cb960a2Sdownsj /* Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
3137cb960a2Sdownsj * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
3147cb960a2Sdownsj */
3157cb960a2Sdownsj int
check_fd(char * name,int mode,const char ** emsgp)316c5d5393cSotto check_fd(char *name, int mode, const char **emsgp)
3177cb960a2Sdownsj {
3187cb960a2Sdownsj int fd, fl;
3197cb960a2Sdownsj
320e569fc7cSderaadt if (isdigit((unsigned char)name[0]) && !name[1]) {
3217cb960a2Sdownsj fd = name[0] - '0';
3223aaa63ebSderaadt if ((fl = fcntl(fd, F_GETFL)) == -1) {
3237cb960a2Sdownsj if (emsgp)
3247cb960a2Sdownsj *emsgp = "bad file descriptor";
3257cb960a2Sdownsj return -1;
3267cb960a2Sdownsj }
3277cb960a2Sdownsj fl &= O_ACCMODE;
3287cb960a2Sdownsj /* X_OK is a kludge to disable this check for dups (x<&1):
3297cb960a2Sdownsj * historical shells never did this check (XXX don't know what
3307cb960a2Sdownsj * posix has to say).
3317cb960a2Sdownsj */
3327a8124d8Sderaadt if (!(mode & X_OK) && fl != O_RDWR &&
3337a8124d8Sderaadt (((mode & R_OK) && fl != O_RDONLY) ||
3347a8124d8Sderaadt ((mode & W_OK) && fl != O_WRONLY))) {
3357cb960a2Sdownsj if (emsgp)
3367cb960a2Sdownsj *emsgp = (fl == O_WRONLY) ?
3377a8124d8Sderaadt "fd not open for reading" :
3387a8124d8Sderaadt "fd not open for writing";
3397cb960a2Sdownsj return -1;
3407cb960a2Sdownsj }
3417cb960a2Sdownsj return fd;
34294e42df6Smillert } else if (name[0] == 'p' && !name[1])
343dcacb757Sdownsj return coproc_getfd(mode, emsgp);
3447cb960a2Sdownsj if (emsgp)
3457cb960a2Sdownsj *emsgp = "illegal file descriptor name";
3467cb960a2Sdownsj return -1;
3477cb960a2Sdownsj }
3487cb960a2Sdownsj
3497cb960a2Sdownsj /* Called once from main */
3507cb960a2Sdownsj void
coproc_init(void)351c5d5393cSotto coproc_init(void)
3527cb960a2Sdownsj {
3537cb960a2Sdownsj coproc.read = coproc.readw = coproc.write = -1;
354dcacb757Sdownsj coproc.njobs = 0;
355dcacb757Sdownsj coproc.id = 0;
3567cb960a2Sdownsj }
3577cb960a2Sdownsj
3587cb960a2Sdownsj /* Called by c_read() when eof is read - close fd if it is the co-process fd */
3597cb960a2Sdownsj void
coproc_read_close(int fd)360c5d5393cSotto coproc_read_close(int fd)
3617cb960a2Sdownsj {
3627cb960a2Sdownsj if (coproc.read >= 0 && fd == coproc.read) {
363dcacb757Sdownsj coproc_readw_close(fd);
3647cb960a2Sdownsj close(coproc.read);
3657cb960a2Sdownsj coproc.read = -1;
3667cb960a2Sdownsj }
3677cb960a2Sdownsj }
3687cb960a2Sdownsj
3697cb960a2Sdownsj /* Called by c_read() and by iosetup() to close the other side of the
3707cb960a2Sdownsj * read pipe, so reads will actually terminate.
3717cb960a2Sdownsj */
3727cb960a2Sdownsj void
coproc_readw_close(int fd)373c5d5393cSotto coproc_readw_close(int fd)
3747cb960a2Sdownsj {
375dcacb757Sdownsj if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
3767cb960a2Sdownsj close(coproc.readw);
3777cb960a2Sdownsj coproc.readw = -1;
3787cb960a2Sdownsj }
3797cb960a2Sdownsj }
3807cb960a2Sdownsj
3817cb960a2Sdownsj /* Called by c_print when a write to a fd fails with EPIPE and by iosetup
3827cb960a2Sdownsj * when co-process input is dup'd
3837cb960a2Sdownsj */
3847cb960a2Sdownsj void
coproc_write_close(int fd)385c5d5393cSotto coproc_write_close(int fd)
3867cb960a2Sdownsj {
3877cb960a2Sdownsj if (coproc.write >= 0 && fd == coproc.write) {
3887cb960a2Sdownsj close(coproc.write);
3897cb960a2Sdownsj coproc.write = -1;
3907cb960a2Sdownsj }
3917cb960a2Sdownsj }
3927cb960a2Sdownsj
3932e25baf5Sdavid /* Called to check for existence of/value of the co-process file descriptor.
3947cb960a2Sdownsj * (Used by check_fd() and by c_read/c_print to deal with -p option).
3957cb960a2Sdownsj */
3967cb960a2Sdownsj int
coproc_getfd(int mode,const char ** emsgp)397c5d5393cSotto coproc_getfd(int mode, const char **emsgp)
3987cb960a2Sdownsj {
3997cb960a2Sdownsj int fd = (mode & R_OK) ? coproc.read : coproc.write;
4007cb960a2Sdownsj
4017cb960a2Sdownsj if (fd >= 0)
4027cb960a2Sdownsj return fd;
4037cb960a2Sdownsj if (emsgp)
4047cb960a2Sdownsj *emsgp = "no coprocess";
4057cb960a2Sdownsj return -1;
4067cb960a2Sdownsj }
4077cb960a2Sdownsj
408dcacb757Sdownsj /* called to close file descriptors related to the coprocess (if any)
409dcacb757Sdownsj * Should be called with SIGCHLD blocked.
410dcacb757Sdownsj */
4117cb960a2Sdownsj void
coproc_cleanup(int reuse)412c5d5393cSotto coproc_cleanup(int reuse)
4137cb960a2Sdownsj {
4147cb960a2Sdownsj /* This to allow co-processes to share output pipe */
4157cb960a2Sdownsj if (!reuse || coproc.readw < 0 || coproc.read < 0) {
4167cb960a2Sdownsj if (coproc.read >= 0) {
4177cb960a2Sdownsj close(coproc.read);
4187cb960a2Sdownsj coproc.read = -1;
4197cb960a2Sdownsj }
4207cb960a2Sdownsj if (coproc.readw >= 0) {
4217cb960a2Sdownsj close(coproc.readw);
4227cb960a2Sdownsj coproc.readw = -1;
4237cb960a2Sdownsj }
4247cb960a2Sdownsj }
4257cb960a2Sdownsj if (coproc.write >= 0) {
4267cb960a2Sdownsj close(coproc.write);
4277cb960a2Sdownsj coproc.write = -1;
4287cb960a2Sdownsj }
4297cb960a2Sdownsj }
4307cb960a2Sdownsj
431f00c5086Smillert
4327cb960a2Sdownsj /*
4337cb960a2Sdownsj * temporary files
4347cb960a2Sdownsj */
4357cb960a2Sdownsj
4367cb960a2Sdownsj struct temp *
maketemp(Area * ap,Temp_type type,struct temp ** tlist)437c5d5393cSotto maketemp(Area *ap, Temp_type type, struct temp **tlist)
4387cb960a2Sdownsj {
4397cb960a2Sdownsj struct temp *tp;
4407cb960a2Sdownsj int len;
4417cb960a2Sdownsj int fd;
4427cb960a2Sdownsj char *path;
443f00c5086Smillert const char *dir;
4447cb960a2Sdownsj
445f00c5086Smillert dir = tmpdir ? tmpdir : "/tmp";
4467cb960a2Sdownsj /* The 20 + 20 is a paranoid worst case for pid/inc */
447f00c5086Smillert len = strlen(dir) + 3 + 20 + 20 + 1;
4488c046d24Snicm tp = alloc(sizeof(struct temp) + len, ap);
4497cb960a2Sdownsj tp->name = path = (char *) &tp[1];
450f7654a50Snicm tp->shf = NULL;
451f00c5086Smillert tp->type = type;
4524a5d53a7Smillert shf_snprintf(path, len, "%s/shXXXXXXXX", dir);
4534a5d53a7Smillert fd = mkstemp(path);
4544a5d53a7Smillert if (fd >= 0)
455f7654a50Snicm tp->shf = shf_fdopen(fd, SHF_WR, NULL);
4567cb960a2Sdownsj tp->pid = procpid;
457f00c5086Smillert
458f00c5086Smillert tp->next = *tlist;
459f00c5086Smillert *tlist = tp;
4607cb960a2Sdownsj return tp;
4617cb960a2Sdownsj }
462