2 * Copyright (c) 2001, 2003, 2008 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
26 #endif /* HAVE_STDLIB_H */
30 #endif /* HAVE_UNISTD_H */
35 static const char rcsid[] = "$Sudo: mkstemp.c,v 1.2 2008/08/20 11:40:15 millert Exp $";
38 static unsigned int get_random __P((void));
39 static void seed_random __P((void));
47 char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
54 for (cp = path; *cp; cp++)
58 } while (cp >= path && *cp == 'X');
62 for (cp = start; *cp; cp++) {
63 r = get_random % (26 + 26);
67 fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
68 if (fd != -1 || errno != EEXIST)
76 # define SRAND srandom
77 # define SEED_T unsigned int
81 # define SRAND srand48
86 # define SEED_T unsigned int
97 * Seed from time of day and process id multiplied by small primes.
100 seed = (ts.tv_sec % 10000) * 523 + ts.tv_nsec / 1000 * 13 +
101 (getpid() % 1000) * 983;
108 static int initialized;
115 return(RAND() & 0xffffffff);