tmpfile.c (d201fe46e355212750b727061e6a7ac005267852) tmpfile.c (0bada8603d59cf4f15ff6b20a27c4137b9c0abe6)
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 32 unchanged lines hidden (view full) ---

41#endif /* LIBC_SCCS and not lint */
42
43#include "namespace.h"
44#include <sys/types.h>
45#include <signal.h>
46#include <unistd.h>
47#include <errno.h>
48#include <stdio.h>
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 32 unchanged lines hidden (view full) ---

41#endif /* LIBC_SCCS and not lint */
42
43#include "namespace.h"
44#include <sys/types.h>
45#include <signal.h>
46#include <unistd.h>
47#include <errno.h>
48#include <stdio.h>
49#include <stdlib.h>
49#include <string.h>
50#include <paths.h>
51#include "un-namespace.h"
52
53FILE *
54tmpfile()
55{
56 sigset_t set, oset;
57 FILE *fp;
58 int fd, sverrno;
59#define TRAILER "tmp.XXXXXX"
50#include <string.h>
51#include <paths.h>
52#include "un-namespace.h"
53
54FILE *
55tmpfile()
56{
57 sigset_t set, oset;
58 FILE *fp;
59 int fd, sverrno;
60#define TRAILER "tmp.XXXXXX"
60 char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)];
61 char *buf;
62 const char *tmpdir;
61
63
62 (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1);
63 (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER));
64 tmpdir = getenv("TMPDIR");
65 if (tmpdir == NULL)
66 tmpdir = _PATH_TMP;
64
67
68 (void)asprintf(&buf, "%s%s%s", tmpdir,
69 (tmpdir[strlen(tmpdir) - 1] == '/') ? "" : "/", TRAILER);
70 if (buf == NULL)
71 return (NULL);
72
65 sigfillset(&set);
66 (void)_sigprocmask(SIG_BLOCK, &set, &oset);
67
68 fd = mkstemp(buf);
69 if (fd != -1)
70 (void)unlink(buf);
71
73 sigfillset(&set);
74 (void)_sigprocmask(SIG_BLOCK, &set, &oset);
75
76 fd = mkstemp(buf);
77 if (fd != -1)
78 (void)unlink(buf);
79
80 free(buf);
81
72 (void)_sigprocmask(SIG_SETMASK, &oset, NULL);
73
74 if (fd == -1)
75 return (NULL);
76
77 if ((fp = fdopen(fd, "w+")) == NULL) {
78 sverrno = errno;
79 (void)_close(fd);
80 errno = sverrno;
81 return (NULL);
82 }
83 return (fp);
84}
82 (void)_sigprocmask(SIG_SETMASK, &oset, NULL);
83
84 if (fd == -1)
85 return (NULL);
86
87 if ((fp = fdopen(fd, "w+")) == NULL) {
88 sverrno = errno;
89 (void)_close(fd);
90 errno = sverrno;
91 return (NULL);
92 }
93 return (fp);
94}