1*60913Sbostic /*- 2*60913Sbostic * Copyright (c) 1993 The Regents of the University of California. 3*60913Sbostic * All rights reserved. 4*60913Sbostic * 5*60913Sbostic * This code is derived from software contributed to Berkeley by 6*60913Sbostic * Peter McIlroy. 7*60913Sbostic * 8*60913Sbostic * %sccs.include.redist.c% 9*60913Sbostic */ 10*60913Sbostic 11*60913Sbostic #ifndef lint 12*60913Sbostic static char sccsid[] = "@(#)tmp.c 5.1 (Berkeley) 06/01/93"; 13*60913Sbostic #endif /* not lint */ 14*60913Sbostic 15*60913Sbostic #include <sys/param.h> 16*60913Sbostic 17*60913Sbostic #include <err.h> 18*60913Sbostic #include <errno.h> 19*60913Sbostic #include <limits.h> 20*60913Sbostic #include <signal.h> 21*60913Sbostic #include <stdio.h> 22*60913Sbostic #include <stdlib.h> 23*60913Sbostic #include <string.h> 24*60913Sbostic #include <unistd.h> 25*60913Sbostic 26*60913Sbostic #include "pathnames.h" 27*60913Sbostic 28*60913Sbostic #define _NAME_TMP "sort.XXXXXXXX" 29*60913Sbostic 30*60913Sbostic FILE * 31*60913Sbostic ftmp() 32*60913Sbostic { 33*60913Sbostic static char *envtmp; 34*60913Sbostic sigset_t set, oset; 35*60913Sbostic static int first = 0; 36*60913Sbostic FILE *fd; 37*60913Sbostic char pathb[_POSIX_PATH_MAX], *path; 38*60913Sbostic 39*60913Sbostic path = pathb; 40*60913Sbostic if (!first && !envtmp) { 41*60913Sbostic envtmp = getenv("TMPDIR"); 42*60913Sbostic first = 1; 43*60913Sbostic } 44*60913Sbostic if (envtmp) 45*60913Sbostic (void)snprintf(path, 46*60913Sbostic sizeof(pathb), "%s/%s", envtmp, _NAME_TMP); 47*60913Sbostic else { 48*60913Sbostic memmove(path, _PATH_SORTTMP, sizeof(_PATH_SORTTMP)); 49*60913Sbostic } 50*60913Sbostic sigfillset(&set); 51*60913Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 52*60913Sbostic path = mktemp(path); 53*60913Sbostic if (!path) 54*60913Sbostic err(2, "%s"); 55*60913Sbostic if (!(fd = fopen(path, "w+"))) 56*60913Sbostic err(2, "%s", path); 57*60913Sbostic (void)unlink(path); 58*60913Sbostic 59*60913Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 60*60913Sbostic return (fd); 61*60913Sbostic }; 62