1 /* $OpenBSD: mktemp.c,v 1.19 2005/08/08 08:05:36 espie Exp $ */
4 * Copyright (c) 2000, 2001, 2005 Todd C. Miller <Todd.Miller@courtesan.com>
5 * Copyright (c) 1987, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
37 #if defined(TIME_WITH_SYS_TIME) || defined(HAVE_SYS_TIME_H)
38 # include <sys/time.h>
45 #endif /* HAVE_STDLIB_H */
49 #endif /* HAVE_UNISTD_H */
54 static const char rcsid[] = "$Sudo: mkstemp.c,v 1.1.2.1 2007/06/12 17:06:20 millert Exp $";
57 static unsigned int get_random __P((void));
58 static void seed_random __P((void));
68 char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
72 return (-1); /* zero length path */
75 for (trv = path; *trv; ++trv)
78 while (trv >= path && *trv == 'X' && pid != 0) {
79 *trv-- = (pid % 10) + '0';
82 while (trv >= path && *trv == 'X') {
85 /* assumes pid_t is at least 16 bits */
86 pid = (get_random() & 0xffff) % (26 + 26);
93 * check the target directory; if you have six X's and it
94 * doesn't exist this runs for a *very* long time.
101 rval = stat(path, &sbuf);
105 if (!S_ISDIR(sbuf.st_mode)) {
114 if ((fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
119 /* tricky little algorithm for backward compatibility */
120 for (trv = start;;) {
126 if (isdigit((unsigned char)(*trv)))
128 else if (*trv == 'z') /* wrap from z to A */
162 # define SRAND srandom
163 # define SEED_T unsigned int
166 # define RAND lrand48
167 # define SRAND srand48
172 # define SEED_T unsigned int
183 * Seed from time of day and process id multiplied by small primes.
186 seed = (ts.tv_sec % 10000) * 523 + ts.tv_nsec / 1000 * 13 +
187 (getpid() % 1000) * 983;
194 static int initialized;
201 return(RAND() & 0xffffffff);