add doc about interaction with RAMRUN to README.Debian in response to #581393
[debian/sudo] / mkstemp.c
1 /*
2  * Copyright (c) 2001, 2003, 2008 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
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.
7  *
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.
15  */
16
17 #include "config.h"
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #ifdef HAVE_STDLIB_H
25 # include <stdlib.h>
26 #endif /* HAVE_STDLIB_H */
27 #include <ctype.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif /* HAVE_UNISTD_H */
31
32 #include "sudo.h"
33
34 static unsigned int get_random __P((void));
35 static void seed_random __P((void));
36
37 int
38 mkstemp(path)
39         char *path;
40 {
41         char *start, *cp;
42         int fd, r;
43         char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
44
45         if (*path == '\0') {
46                 errno = EINVAL;
47                 return(0);
48         }
49
50         for (cp = path; *cp; cp++)
51                 ;
52         do {
53                 cp--;
54         } while (cp >= path && *cp == 'X');
55         start = cp + 1;
56
57         for (;;) {
58                 for (cp = start; *cp; cp++) {
59                         r = get_random % (26 + 26);
60                         *cp = alphabet[r];
61                 }
62
63                 fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
64                 if (fd != -1 || errno != EEXIST)
65                         return(fd);
66         }
67         /*NOTREACHED*/
68 }
69
70 #ifdef HAVE_RANDOM
71 # define RAND           random
72 # define SRAND          srandom
73 # define SEED_T         unsigned int
74 #else
75 # ifdef HAVE_LRAND48
76 #  define RAND          lrand48
77 #  define SRAND         srand48
78 #  define SEED_T        long
79 # else
80 #  define RAND          rand
81 #  define SRAND         srand
82 #  define SEED_T        unsigned int
83 # endif
84 #endif
85
86 static void
87 seed_random()
88 {
89         SEED_T seed;
90         struct timespec ts;
91
92         /*
93          * Seed from time of day and process id multiplied by small primes.
94          */
95         (void) gettime(&ts);
96         seed = (ts.tv_sec % 10000) * 523 + ts.tv_nsec / 1000 * 13 +
97             (getpid() % 1000) * 983;
98         SRAND(seed);
99 }
100
101 static unsigned int
102 get_random()
103 {
104         static int initialized;
105
106         if (!initialized) {
107                 seed_random();
108                 initialized = 1;
109         }
110
111         return(RAND() & 0xffffffff);
112 }