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