1*6d38604fSBaptiste Daroussin /* $Id: compat_mkstemps.c,v 1.1 2021/09/19 15:05:39 schwarze Exp $ */
2*6d38604fSBaptiste Daroussin /*
3*6d38604fSBaptiste Daroussin * Copyright (c) 2015, 2021 Ingo Schwarze <schwarze@openbsd.org>
4*6d38604fSBaptiste Daroussin *
5*6d38604fSBaptiste Daroussin * Permission to use, copy, modify, and distribute this software for any
6*6d38604fSBaptiste Daroussin * purpose with or without fee is hereby granted, provided that the above
7*6d38604fSBaptiste Daroussin * copyright notice and this permission notice appear in all copies.
8*6d38604fSBaptiste Daroussin *
9*6d38604fSBaptiste Daroussin * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*6d38604fSBaptiste Daroussin * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*6d38604fSBaptiste Daroussin * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*6d38604fSBaptiste Daroussin * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*6d38604fSBaptiste Daroussin * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*6d38604fSBaptiste Daroussin * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*6d38604fSBaptiste Daroussin * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*6d38604fSBaptiste Daroussin *
17*6d38604fSBaptiste Daroussin * Parts of the algorithm of this function are inspired by OpenBSD
18*6d38604fSBaptiste Daroussin * mkdtemp(3) by Theo de Raadt and Todd Miller, but the code differs.
19*6d38604fSBaptiste Daroussin */
20*6d38604fSBaptiste Daroussin #include "config.h"
21*6d38604fSBaptiste Daroussin
22*6d38604fSBaptiste Daroussin #include <sys/stat.h>
23*6d38604fSBaptiste Daroussin #include <errno.h>
24*6d38604fSBaptiste Daroussin #include <fcntl.h>
25*6d38604fSBaptiste Daroussin #include <limits.h>
26*6d38604fSBaptiste Daroussin #include <stdlib.h>
27*6d38604fSBaptiste Daroussin #include <string.h>
28*6d38604fSBaptiste Daroussin
29*6d38604fSBaptiste Daroussin int
mkstemps(char * path,int suffixlen)30*6d38604fSBaptiste Daroussin mkstemps(char *path, int suffixlen)
31*6d38604fSBaptiste Daroussin {
32*6d38604fSBaptiste Daroussin char *start, *end, *cp;
33*6d38604fSBaptiste Daroussin int fd, tries;
34*6d38604fSBaptiste Daroussin char backup;
35*6d38604fSBaptiste Daroussin
36*6d38604fSBaptiste Daroussin end = strchr(path, '\0');
37*6d38604fSBaptiste Daroussin if (suffixlen < 0 || suffixlen > end - path - 6) {
38*6d38604fSBaptiste Daroussin errno = EINVAL;
39*6d38604fSBaptiste Daroussin return -1;
40*6d38604fSBaptiste Daroussin }
41*6d38604fSBaptiste Daroussin end -= suffixlen;
42*6d38604fSBaptiste Daroussin for (start = end; start > path; start--)
43*6d38604fSBaptiste Daroussin if (start[-1] != 'X')
44*6d38604fSBaptiste Daroussin break;
45*6d38604fSBaptiste Daroussin
46*6d38604fSBaptiste Daroussin backup = *end;
47*6d38604fSBaptiste Daroussin for (tries = INT_MAX; tries; tries--) {
48*6d38604fSBaptiste Daroussin *end = '\0';
49*6d38604fSBaptiste Daroussin cp = mktemp(path);
50*6d38604fSBaptiste Daroussin *end = backup;
51*6d38604fSBaptiste Daroussin if (cp == NULL)
52*6d38604fSBaptiste Daroussin return -1;
53*6d38604fSBaptiste Daroussin fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
54*6d38604fSBaptiste Daroussin if (fd != -1)
55*6d38604fSBaptiste Daroussin return fd;
56*6d38604fSBaptiste Daroussin for (cp = start; cp < end; cp++)
57*6d38604fSBaptiste Daroussin *cp = 'X';
58*6d38604fSBaptiste Daroussin if (errno != EEXIST)
59*6d38604fSBaptiste Daroussin return -1;
60*6d38604fSBaptiste Daroussin }
61*6d38604fSBaptiste Daroussin errno = EEXIST;
62*6d38604fSBaptiste Daroussin return -1;
63*6d38604fSBaptiste Daroussin }
64