1 /* $OpenBSD: mkstemp.c,v 1.2 2025年08月04日 04:59:31 guenther Exp $ */ 2 /* 3 * Copyright (c) 2024 Todd C. Miller 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18#include <sys/stat.h> 19#include <errno.h> 20#include <fcntl.h> 21#include <stdlib.h> 22 23#define MKOSTEMP_FLAGS \ 24 (O_APPEND | O_CLOEXEC | O_CLOFORK | O_DSYNC | O_RSYNC | O_SYNC) 25 26 static int 27 mkstemp_cb(const char *path, int flags) 28{ 29 flags |= O_CREAT | O_EXCL | O_RDWR; 30 return open(path, flags, S_IRUSR|S_IWUSR); 31} 32 33 int 34 mkostemps(char *path, int slen, int flags) 35{ 36 if (flags & ~MKOSTEMP_FLAGS) { 37 errno = EINVAL; 38 return -1; 39 } 40 return __mktemp4(path, slen, flags, mkstemp_cb); 41} 42 43 int 44 mkostemp(char *path, int flags) 45{ 46 if (flags & ~MKOSTEMP_FLAGS) { 47 errno = EINVAL; 48 return -1; 49 } 50 return __mktemp4(path, 0, flags, mkstemp_cb); 51} 52 DEF_WEAK(mkostemp); 53 54 int 55 mkstemp(char *path) 56{ 57 return __mktemp4(path, 0, 0, mkstemp_cb); 58} 59 DEF_WEAK(mkstemp); 60 61 int 62 mkstemps(char *path, int slen) 63{ 64 return __mktemp4(path, slen, 0, mkstemp_cb); 65} 66