반응형
FILE * tmpfile(void)
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <signal.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <paths.h> FILE * tmpfile(void) { sigset_t set, oset; FILE *fp; int fd, sverrno; #define TRAILER "tmp.XXXXXXXXXX" char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)]; (void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1); (void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER)); sigfillset(&set); (void)sigprocmask(SIG_BLOCK, &set, &oset); fd = mkstemp(buf); if (fd != -1) { mode_t u; (void)unlink(buf); u = umask(0); (void)umask(u); (void)fchmod(fd, 0666 & ~u); } (void)sigprocmask(SIG_SETMASK, &oset, NULL); if (fd == -1) return (NULL); if ((fp = fdopen(fd, "w+")) == NULL) { sverrno = errno; (void)close(fd); errno = sverrno; return (NULL); } return (fp); }
반응형