bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / unix / mktemp.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /*
17  * Copyright (c) 1987, 1993
18  * The Regents of the University of California.  All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 3. All advertising materials mentioning features or use of this software
29  *    must display the following acknowledgement:
30  *    This product includes software developed by the University of
31  *    California, Berkeley and its contributors.
32  * 4. Neither the name of the University nor the names of its contributors
33  *    may be used to endorse or promote products derived from this software
34  *    without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  */
48
49 #include "apr_private.h"
50 #include "apr_file_io.h" /* prototype of apr_mkstemp() */
51 #include "apr_strings.h" /* prototype of apr_mkstemp() */
52 #include "apr_arch_file_io.h" /* prototype of apr_mkstemp() */
53 #include "apr_portable.h" /* for apr_os_file_put() */
54
55 #ifndef HAVE_MKSTEMP
56
57 #if defined(SVR4) || defined(WIN32) || defined(NETWARE)
58 #ifdef SVR4
59 #if HAVE_INTTYPES_H
60 #include <inttypes.h>
61 #endif
62 #endif
63 #define arc4random() rand()
64 #define seedrandom(a) srand(a)
65 #else
66 #if APR_HAVE_STDINT_H
67 #include <stdint.h>
68 #endif
69 #define arc4random() random()
70 #define seedrandom(a) srandom(a)
71 #endif
72
73 #if APR_HAVE_SYS_TYPES_H
74 #include <sys/types.h>
75 #endif
76 #if APR_HAVE_SYS_STAT_H
77 #include <sys/stat.h>
78 #endif
79 #if APR_HAVE_FCNTL_H
80 #include <fcntl.h>
81 #endif
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <ctype.h>
86 #ifdef HAVE_TIME_H
87 #include <time.h>
88 #endif
89
90 static const unsigned char padchar[] =
91 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
92 static apr_uint32_t randseed=0;
93
94 static int gettemp(char *path, apr_file_t **doopen, apr_int32_t flags, apr_pool_t *p)
95 {
96     register char *start, *trv, *suffp;
97     char *pad;
98     apr_finfo_t sbuf;
99     apr_status_t rv;
100     apr_uint32_t randnum;
101
102     if (randseed==0) {
103         randseed = (int)apr_time_now();
104         seedrandom(randseed);
105     }
106
107     for (trv = path; *trv; ++trv)
108         ;
109     suffp = trv;
110     --trv;
111     if (trv < path) {
112         return APR_EINVAL;
113     }
114
115     /* Fill space with random characters */
116     while (*trv == 'X') {
117         randnum = arc4random() % (sizeof(padchar) - 1);
118         *trv-- = padchar[randnum];
119     }
120     start = trv + 1;
121
122     /*
123      * check the target directory.
124      */
125     for (;; --trv) {
126         if (trv <= path)
127             break;
128         if (*trv == '/') {
129             *trv = '\0';
130             rv = apr_stat(&sbuf, path, APR_FINFO_TYPE, p);
131             *trv = '/';
132             if (rv != APR_SUCCESS)
133                 return rv;
134             if (sbuf.filetype != APR_DIR) {
135                 return APR_ENOTDIR;
136             }
137             break;
138         }
139     }
140
141     for (;;) {
142         if ((rv = apr_file_open(doopen, path, flags,
143                                 APR_UREAD | APR_UWRITE, p)) == APR_SUCCESS)
144             return APR_SUCCESS;
145         if (!APR_STATUS_IS_EEXIST(rv))
146             return rv;
147
148         /* If we have a collision, cycle through the space of filenames */
149         for (trv = start;;) {
150             if (*trv == '\0' || trv == suffp)
151                 return APR_EINVAL; /* XXX: is this the correct return code? */
152             pad = strchr((char *)padchar, *trv);
153             if (pad == NULL || !*++pad) {
154                 *trv++ = padchar[0];
155             }
156             else {
157                 *trv++ = *pad;
158                 break;
159             }
160         }
161     }
162     /*NOTREACHED*/
163 }
164
165 #else
166
167 #if APR_HAVE_STDLIB_H
168 #include <stdlib.h> /* for mkstemp() - Single Unix */
169 #endif
170 #if APR_HAVE_UNISTD_H
171 #include <unistd.h> /* for mkstemp() - FreeBSD */
172 #endif
173 #endif /* !defined(HAVE_MKSTEMP) */
174
175 APR_DECLARE(apr_status_t) apr_file_mktemp(apr_file_t **fp, char *template, apr_int32_t flags, apr_pool_t *p)
176 {
177 #ifdef HAVE_MKSTEMP
178     int fd;
179 #endif
180     flags = (!flags) ? APR_CREATE | APR_READ | APR_WRITE | APR_EXCL | 
181                        APR_DELONCLOSE : flags;
182 #ifndef HAVE_MKSTEMP
183     return gettemp(template, fp, flags, p);
184 #else
185
186     fd = mkstemp(template);
187     if (fd == -1) {
188         return errno;
189     }
190     /* XXX: We must reset several flags values as passed-in, since
191      * mkstemp didn't subscribe to our preference flags.
192      *
193      * We either have to unset the flags, or fix up the fd and other
194      * xthread and inherit bits appropriately.  Since gettemp() above
195      * calls apr_file_open, our flags are respected in that code path.
196      */
197     apr_os_file_put(fp, &fd, flags, p);
198     (*fp)->fname = apr_pstrdup(p, template);
199
200     if (!(flags & APR_FILE_NOCLEANUP)) {
201         apr_pool_cleanup_register((*fp)->pool, (void *)(*fp),
202                                   apr_unix_file_cleanup,
203                                   apr_unix_child_file_cleanup);
204     }
205 #endif
206     return APR_SUCCESS;
207 }
208