bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / unix / tempdir.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 #include "apr_private.h"
17 #include "apr_file_io.h"
18 #include "apr_strings.h"
19 #include "apr_env.h"
20
21 /*
22  * FIXME
23  * Currently, this variable is a bit of misnomer.
24  * The intention is to have this in APR's global pool so that we don't have 
25  * to go through this every time.
26  */
27 static char global_temp_dir[APR_PATH_MAX+1] = { 0 };
28
29 /* Try to open a temporary file in the temporary dir, write to it,
30    and then close it. */
31 static int test_tempdir(const char *temp_dir, apr_pool_t *p)
32 {
33     apr_file_t *dummy_file;
34     const char *path = apr_pstrcat(p, temp_dir, "/apr-tmp.XXXXXX", NULL);
35
36     if (apr_file_mktemp(&dummy_file, (char *)path, 0, p) == APR_SUCCESS) {
37         if (apr_file_putc('!', dummy_file) == APR_SUCCESS) {
38             if (apr_file_close(dummy_file) == APR_SUCCESS) {
39                 return 1;
40             }
41         }
42     }
43     return 0;
44 }
45
46
47 APR_DECLARE(apr_status_t) apr_temp_dir_get(const char **temp_dir, 
48                                            apr_pool_t *p)
49 {
50     apr_status_t apr_err;
51     const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" };
52     const char *try_envs[] = { "TMP", "TEMP", "TMPDIR" };
53     char *cwd;
54     int i;
55
56     /* Our goal is to find a temporary directory suitable for writing
57        into.  We'll only pay the price once if we're successful -- we
58        cache our successful find.  Here's the order in which we'll try
59        various paths:
60
61           $TMP
62           $TEMP
63           $TMPDIR
64           "C:\TEMP"     (windows only)
65           "SYS:\TMP"    (netware only)
66           "/tmp"
67           "/var/tmp"
68           "/usr/tmp"
69           P_tmpdir      (POSIX define)
70           `pwd` 
71
72        NOTE: This algorithm is basically the same one used by Python
73        2.2's tempfile.py module.  */
74
75     /* Try the environment first. */
76     for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) {
77         char *value;
78         apr_err = apr_env_get(&value, try_envs[i], p);
79         if ((apr_err == APR_SUCCESS) && value) {
80             apr_size_t len = strlen(value);
81             if (len && (len < APR_PATH_MAX) && test_tempdir(value, p)) {
82                 memcpy(global_temp_dir, value, len + 1);
83                 goto end;
84             }
85         }
86     }
87
88 #ifdef WIN32
89     /* Next, on Win32, try the C:\TEMP directory. */
90     if (test_tempdir("C:\\TEMP", p)) {
91         memcpy(global_temp_dir, "C:\\TEMP", 7 + 1);
92         goto end;
93     }
94 #endif
95 #ifdef NETWARE
96     /* Next, on NetWare, try the SYS:/TMP directory. */
97     if (test_tempdir("SYS:/TMP", p)) {
98         memcpy(global_temp_dir, "SYS:/TMP", 8 + 1);
99         goto end;
100     }
101 #endif
102
103     /* Next, try a set of hard-coded paths. */
104     for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) {
105         if (test_tempdir(try_dirs[i], p)) {
106             memcpy(global_temp_dir, try_dirs[i], strlen(try_dirs[i]) + 1);
107             goto end;
108         }
109     }
110
111 #ifdef P_tmpdir
112     /* 
113      * If we have it, use the POSIX definition of where 
114      * the tmpdir should be 
115      */
116     if (test_tempdir(P_tmpdir, p)) {
117         memcpy(global_temp_dir, P_tmpdir, strlen(P_tmpdir) +1);
118         goto end;
119     }
120 #endif
121     
122     /* Finally, try the current working directory. */
123     if (APR_SUCCESS == apr_filepath_get(&cwd, APR_FILEPATH_NATIVE, p)) {
124         if (test_tempdir(cwd, p)) {
125             memcpy(global_temp_dir, cwd, strlen(cwd) + 1);
126             goto end;
127         }
128     }
129
130 end:
131     if (global_temp_dir[0]) {
132         *temp_dir = apr_pstrdup(p, global_temp_dir);
133         return APR_SUCCESS;
134     }
135     return APR_EGENERAL;
136 }