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