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 */
24 #include <sys/types.h>
32 #include <isc/magic.h>
33 #include <isc/string.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)
42 isc_dir_init(isc_dir_t
*dir
) {
45 dir
->entry
.name
[0] = '\0';
46 dir
->entry
.length
= 0;
50 dir
->magic
= ISC_DIR_MAGIC
;
54 * Allocate workspace and open directory stream. If either one fails,
55 * NULL will be returned.
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
);
67 dir
->handle
= opendir(dirname
);
69 if (dir
->handle
== NULL
)
70 return isc__errno2result(errno
);
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.
81 isc_dir_read(isc_dir_t
*dir
) {
84 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
87 * Fetch next file in directory.
89 entry
= readdir(dir
->handle
);
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.
114 isc_dir_close(isc_dir_t
*dir
) {
115 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
117 (void)closedir(dir
->handle
);
122 * Reposition directory stream at start.
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
);
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
);
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
);
159 isc_dir_current(char *dirname
, size_t length
, isc_boolean_t end_sep
) {
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
);
173 result
= ISC_R_NOSPACE
;
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
, "/");
187 isc_dir_createunique(char *templet
) {
194 REQUIRE(templet
!= NULL
);
197 * mkdtemp is not portable, so this emulates it.
203 * Replace trailing Xs with the process-id, zero-filled.
205 for (x
= templet
+ strlen(templet
) - 1; *x
== 'X' && x
>= templet
;
209 x
++; /* Set x to start of ex-Xs. */
212 i
= mkdir(templet
, 0700);
213 if (i
== 0 || errno
!= EEXIST
)
221 if (isdigit(*p
& 0xff))
227 * Reset character and move to next.
238 * Tried all combinations. errno should already
239 * be EEXIST, but ensure it is anyway for
240 * isc__errno2result().
248 result
= isc__errno2result(errno
);
250 result
= ISC_R_SUCCESS
;