1 /* mktemp.cc: mktemp functions
3 This file is adapted for Cygwin from FreeBSD and newlib.
5 See the copyright at the bottom of this file. */
13 static int _gettemp(char *, int *, int, size_t, int);
15 static const char padchar
[] =
16 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
22 return _gettemp(path
, &fd
, 0, 0, O_BINARY
) ? fd
: -1;
28 return _gettemp(path
, NULL
, 1, 0, 0) ? path
: NULL
;
32 mkstemps(char *path
, int len
)
35 return _gettemp(path
, &fd
, 0, len
, O_BINARY
) ? fd
: -1;
39 mkostemp(char *path
, int flags
)
42 return _gettemp(path
, &fd
, 0, 0, flags
& ~O_ACCMODE
) ? fd
: -1;
46 mkostemps(char *path
, int len
, int flags
)
49 return _gettemp(path
, &fd
, 0, len
, flags
& ~O_ACCMODE
) ? fd
: -1;
55 return _gettemp(path
, NULL
, 0, 0, 0) ? path
: (char *) NULL
;
59 _gettemp(char *path
, int *doopen
, int domkdir
, size_t suffixlen
, int flags
)
61 char *start
, *trv
, *suffp
;
64 if (doopen
&& domkdir
)
70 trv
= strchr (path
, '\0');
71 if ((size_t) (trv
- path
) < suffixlen
)
79 /* Fill space with random characters */
80 while (trv
>= path
&& *trv
== 'X')
82 uint32_t rand
= arc4random () % (sizeof (padchar
) - 1);
83 *trv
-- = padchar
[rand
];
93 * check the target directory.
96 if (doopen
!= NULL
|| domkdir
)
98 for (; trv
> path
; trv
--)
103 int rval
= stat (path
, &sbuf
);
107 if (!S_ISDIR (sbuf
.st_mode
))
121 if ((*doopen
= open (path
, O_CREAT
| O_EXCL
| O_RDWR
| flags
,
122 S_IRUSR
| S_IWUSR
)) >= 0)
129 if (mkdir (path
, 0700) == 0)
134 else if (lstat (path
, &sbuf
))
135 return errno
== ENOENT
;
137 /* If we have a collision, cycle through the space of filenames */
140 if (*trv
== '\0' || trv
== suffp
)
142 pad
= strchr (padchar
, *trv
);
143 if (pad
== NULL
|| *++pad
== '\0')
156 * Copyright (c) 1987, 1993
157 * The Regents of the University of California. All rights reserved.
159 * Redistribution and use in source and binary forms, with or without
160 * modification, are permitted provided that the following conditions
162 * 1. Redistributions of source code must retain the above copyright
163 * notice, this list of conditions and the following disclaimer.
164 * 2. Redistributions in binary form must reproduce the above copyright
165 * notice, this list of conditions and the following disclaimer in the
166 * documentation and/or other materials provided with the distribution.
167 * 4. Neither the name of the University nor the names of its contributors
168 * may be used to endorse or promote products derived from this software
169 * without specific prior written permission.
171 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
172 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
175 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
176 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
177 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
178 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
179 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
180 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF