Merge from vendor branch SENDMAIL:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / unix / dir.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: dir.c,v 1.18.2.3 2004/03/09 06:12:09 marka Exp $ */
19
20 /* Principal Authors: DCL */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <unistd.h>
30
31 #include <isc/dir.h>
32 #include <isc/magic.h>
33 #include <isc/string.h>
34 #include <isc/util.h>
35
36 #include "errno2result.h"
37
38 #define ISC_DIR_MAGIC           ISC_MAGIC('D', 'I', 'R', '*')
39 #define VALID_DIR(dir)          ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
40
41 void
42 isc_dir_init(isc_dir_t *dir) {
43         REQUIRE(dir != NULL);
44
45         dir->entry.name[0] = '\0';
46         dir->entry.length = 0;
47
48         dir->handle = NULL;
49
50         dir->magic = ISC_DIR_MAGIC;
51 }
52
53 /*
54  * Allocate workspace and open directory stream. If either one fails,
55  * NULL will be returned.
56  */
57 isc_result_t
58 isc_dir_open(isc_dir_t *dir, const char *dirname) {
59         isc_result_t result = ISC_R_SUCCESS;
60
61         REQUIRE(VALID_DIR(dir));
62         REQUIRE(dirname != NULL);
63
64         /*
65          * Open stream.
66          */
67         dir->handle = opendir(dirname);
68
69         if (dir->handle == NULL)
70                 return isc__errno2result(errno);
71
72         return (result);
73 }
74
75 /*
76  * Return previously retrieved file or get next one.  Unix's dirent has
77  * separate open and read functions, but the Win32 and DOS interfaces open
78  * the dir stream and reads the first file in one operation.
79  */
80 isc_result_t
81 isc_dir_read(isc_dir_t *dir) {
82         struct dirent *entry;
83
84         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
85
86         /*
87          * Fetch next file in directory.
88          */
89         entry = readdir(dir->handle);
90
91         if (entry == NULL)
92                 return (ISC_R_NOMORE);
93
94         /*
95          * Make sure that the space for the name is long enough.
96          */
97         if (sizeof(dir->entry.name) <= strlen(entry->d_name))
98             return (ISC_R_UNEXPECTED);
99
100         strcpy(dir->entry.name, entry->d_name);
101
102         /*
103          * Some dirents have d_namlen, but it is not portable.
104          */
105         dir->entry.length = strlen(entry->d_name);
106
107         return (ISC_R_SUCCESS);
108 }
109
110 /*
111  * Close directory stream.
112  */
113 void
114 isc_dir_close(isc_dir_t *dir) {
115        REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
116
117        (void)closedir(dir->handle);
118        dir->handle = NULL;
119 }
120
121 /*
122  * Reposition directory stream at start.
123  */
124 isc_result_t
125 isc_dir_reset(isc_dir_t *dir) {
126         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
127
128         rewinddir(dir->handle);
129
130         return (ISC_R_SUCCESS);
131 }
132
133 isc_result_t
134 isc_dir_chdir(const char *dirname) {
135         /*
136          * Change the current directory to 'dirname'.
137          */
138
139         REQUIRE(dirname != NULL);
140
141         if (chdir(dirname) < 0)
142                 return (isc__errno2result(errno));
143
144         return (ISC_R_SUCCESS);
145 }
146
147 isc_result_t
148 isc_dir_chroot(const char *dirname) {
149
150         REQUIRE(dirname != NULL);
151
152         if (chroot(dirname) < 0)
153                 return (isc__errno2result(errno));
154
155         return (ISC_R_SUCCESS);
156 }
157
158 isc_result_t
159 isc_dir_current(char *dirname, size_t length, isc_boolean_t end_sep) {
160         char *cwd;
161         isc_result_t result = ISC_R_SUCCESS;
162
163         /*
164          * XXXDCL Could automatically allocate memory if dirname == NULL.
165          */
166         REQUIRE(dirname != NULL);
167         REQUIRE(length > 0U);
168
169         cwd = getcwd(dirname, length);
170
171         if (cwd == NULL) {
172                 if (errno == ERANGE)
173                         result = ISC_R_NOSPACE;
174                 else
175                         result = isc__errno2result(errno);
176         } else if (end_sep) {
177                 if (strlen(dirname) + 1 == length)
178                         result = ISC_R_NOSPACE;
179                 else if (dirname[1] != '\0')
180                         strcat(dirname, "/");
181         }
182
183         return (result);
184 }
185
186 isc_result_t
187 isc_dir_createunique(char *templet) {
188         isc_result_t result;
189         char *x;
190         char *p;
191         int i;
192         int pid;
193
194         REQUIRE(templet != NULL);
195
196         /*
197          * mkdtemp is not portable, so this emulates it.
198          */
199
200         pid = getpid();
201
202         /*
203          * Replace trailing Xs with the process-id, zero-filled.
204          */
205         for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
206              x--, pid /= 10)
207                 *x = pid % 10 + '0';
208
209         x++;                    /* Set x to start of ex-Xs. */
210
211         do {
212                 i = mkdir(templet, 0700);
213                 if (i == 0 || errno != EEXIST)
214                         break;
215
216                 /*
217                  * The BSD algorithm.
218                  */
219                 p = x;
220                 while (*p != '\0') {
221                         if (isdigit(*p & 0xff))
222                                 *p = 'a';
223                         else if (*p != 'z')
224                                 ++*p;
225                         else {
226                                 /*
227                                  * Reset character and move to next.
228                                  */
229                                 *p++ = 'a';
230                                 continue;
231                         }
232
233                         break;
234                 }
235
236                 if (*p == '\0') {
237                         /*
238                          * Tried all combinations.  errno should already
239                          * be EEXIST, but ensure it is anyway for
240                          * isc__errno2result().
241                          */
242                         errno = EEXIST;
243                         break;
244                 }
245         } while (1);
246
247         if (i == -1)
248                 result = isc__errno2result(errno);
249         else
250                 result = ISC_R_SUCCESS;
251
252         return (result);
253 }