Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / unix / dir.c
blob7405ca037806bfd004373858154f6019287286de
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
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.
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.
18 /* $Id: dir.c,v 1.18.2.3 2004/03/09 06:12:09 marka Exp $ */
20 /* Principal Authors: DCL */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <unistd.h>
31 #include <isc/dir.h>
32 #include <isc/magic.h>
33 #include <isc/string.h>
34 #include <isc/util.h>
36 #include "errno2result.h"
38 #define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
39 #define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
41 void
42 isc_dir_init(isc_dir_t *dir) {
43 REQUIRE(dir != NULL);
45 dir->entry.name[0] = '\0';
46 dir->entry.length = 0;
48 dir->handle = NULL;
50 dir->magic = ISC_DIR_MAGIC;
54 * Allocate workspace and open directory stream. If either one fails,
55 * NULL will be returned.
57 isc_result_t
58 isc_dir_open(isc_dir_t *dir, const char *dirname) {
59 isc_result_t result = ISC_R_SUCCESS;
61 REQUIRE(VALID_DIR(dir));
62 REQUIRE(dirname != NULL);
65 * Open stream.
67 dir->handle = opendir(dirname);
69 if (dir->handle == NULL)
70 return isc__errno2result(errno);
72 return (result);
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.
80 isc_result_t
81 isc_dir_read(isc_dir_t *dir) {
82 struct dirent *entry;
84 REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
87 * Fetch next file in directory.
89 entry = readdir(dir->handle);
91 if (entry == NULL)
92 return (ISC_R_NOMORE);
95 * Make sure that the space for the name is long enough.
97 if (sizeof(dir->entry.name) <= strlen(entry->d_name))
98 return (ISC_R_UNEXPECTED);
100 strcpy(dir->entry.name, entry->d_name);
103 * Some dirents have d_namlen, but it is not portable.
105 dir->entry.length = strlen(entry->d_name);
107 return (ISC_R_SUCCESS);
111 * Close directory stream.
113 void
114 isc_dir_close(isc_dir_t *dir) {
115 REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
117 (void)closedir(dir->handle);
118 dir->handle = NULL;
122 * Reposition directory stream at start.
124 isc_result_t
125 isc_dir_reset(isc_dir_t *dir) {
126 REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
128 rewinddir(dir->handle);
130 return (ISC_R_SUCCESS);
133 isc_result_t
134 isc_dir_chdir(const char *dirname) {
136 * Change the current directory to 'dirname'.
139 REQUIRE(dirname != NULL);
141 if (chdir(dirname) < 0)
142 return (isc__errno2result(errno));
144 return (ISC_R_SUCCESS);
147 isc_result_t
148 isc_dir_chroot(const char *dirname) {
150 REQUIRE(dirname != NULL);
152 if (chroot(dirname) < 0)
153 return (isc__errno2result(errno));
155 return (ISC_R_SUCCESS);
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;
164 * XXXDCL Could automatically allocate memory if dirname == NULL.
166 REQUIRE(dirname != NULL);
167 REQUIRE(length > 0U);
169 cwd = getcwd(dirname, length);
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, "/");
183 return (result);
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;
194 REQUIRE(templet != NULL);
197 * mkdtemp is not portable, so this emulates it.
200 pid = getpid();
203 * Replace trailing Xs with the process-id, zero-filled.
205 for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
206 x--, pid /= 10)
207 *x = pid % 10 + '0';
209 x++; /* Set x to start of ex-Xs. */
211 do {
212 i = mkdir(templet, 0700);
213 if (i == 0 || errno != EEXIST)
214 break;
217 * The BSD algorithm.
219 p = x;
220 while (*p != '\0') {
221 if (isdigit(*p & 0xff))
222 *p = 'a';
223 else if (*p != 'z')
224 ++*p;
225 else {
227 * Reset character and move to next.
229 *p++ = 'a';
230 continue;
233 break;
236 if (*p == '\0') {
238 * Tried all combinations. errno should already
239 * be EEXIST, but ensure it is anyway for
240 * isc__errno2result().
242 errno = EEXIST;
243 break;
245 } while (1);
247 if (i == -1)
248 result = isc__errno2result(errno);
249 else
250 result = ISC_R_SUCCESS;
252 return (result);