1 /* $OpenBSD: mkstemp.c,v 1.1 2024/01/19 19:45:02 millert 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 (O_APPEND | O_CLOEXEC | O_DSYNC | O_RSYNC | O_SYNC) 24 25 static int 26 mkstemp_cb(const char *path, int flags) 27 { 28 flags |= O_CREAT | O_EXCL | O_RDWR; 29 return open(path, flags, S_IRUSR|S_IWUSR); 30 } 31 32 int 33 mkostemps(char *path, int slen, int flags) 34 { 35 if (flags & ~MKOSTEMP_FLAGS) { 36 errno = EINVAL; 37 return -1; 38 } 39 return __mktemp4(path, slen, flags, mkstemp_cb); 40 } 41 42 int 43 mkostemp(char *path, int flags) 44 { 45 if (flags & ~MKOSTEMP_FLAGS) { 46 errno = EINVAL; 47 return -1; 48 } 49 return __mktemp4(path, 0, flags, mkstemp_cb); 50 } 51 DEF_WEAK(mkostemp); 52 53 int 54 mkstemp(char *path) 55 { 56 return __mktemp4(path, 0, 0, mkstemp_cb); 57 } 58 DEF_WEAK(mkstemp); 59 60 int 61 mkstemps(char *path, int slen) 62 { 63 return __mktemp4(path, slen, 0, mkstemp_cb); 64 } 65