4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: dir.c,v 1.25.332.3 2009/02/16 23:47:15 tbox Exp */
23 * \author Principal Authors: DCL */
27 #include <sys/types.h>
35 #include <isc/magic.h>
36 #include <isc/string.h>
39 #include "errno2result.h"
41 #define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
42 #define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
45 isc_dir_init(isc_dir_t
*dir
) {
48 dir
->entry
.name
[0] = '\0';
49 dir
->entry
.length
= 0;
53 dir
->magic
= ISC_DIR_MAGIC
;
57 * \brief Allocate workspace and open directory stream. If either one fails,
58 * NULL will be returned.
61 isc_dir_open(isc_dir_t
*dir
, const char *dirname
) {
63 isc_result_t result
= ISC_R_SUCCESS
;
65 REQUIRE(VALID_DIR(dir
));
66 REQUIRE(dirname
!= NULL
);
69 * Copy directory name. Need to have enough space for the name,
70 * a possible path separator, the wildcard, and the final NUL.
72 if (strlen(dirname
) + 3 > sizeof(dir
->dirname
))
74 return (ISC_R_NOSPACE
);
75 strcpy(dir
->dirname
, dirname
);
78 * Append path separator, if needed, and "*".
80 p
= dir
->dirname
+ strlen(dir
->dirname
);
81 if (dir
->dirname
< p
&& *(p
- 1) != '/')
89 dir
->handle
= opendir(dirname
);
91 if (dir
->handle
== NULL
)
92 return isc__errno2result(errno
);
98 * \brief Return previously retrieved file or get next one.
101 * separate open and read functions, but the Win32 and DOS interfaces open
102 * the dir stream and reads the first file in one operation.
105 isc_dir_read(isc_dir_t
*dir
) {
106 struct dirent
*entry
;
108 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
111 * Fetch next file in directory.
113 entry
= readdir(dir
->handle
);
116 return (ISC_R_NOMORE
);
119 * Make sure that the space for the name is long enough.
121 if (sizeof(dir
->entry
.name
) <= strlen(entry
->d_name
))
122 return (ISC_R_UNEXPECTED
);
124 strcpy(dir
->entry
.name
, entry
->d_name
);
127 * Some dirents have d_namlen, but it is not portable.
129 dir
->entry
.length
= strlen(entry
->d_name
);
131 return (ISC_R_SUCCESS
);
135 * \brief Close directory stream.
138 isc_dir_close(isc_dir_t
*dir
) {
139 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
141 (void)closedir(dir
->handle
);
146 * \brief Reposition directory stream at start.
149 isc_dir_reset(isc_dir_t
*dir
) {
150 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
152 rewinddir(dir
->handle
);
154 return (ISC_R_SUCCESS
);
158 isc_dir_chdir(const char *dirname
) {
160 * \brief Change the current directory to 'dirname'.
163 REQUIRE(dirname
!= NULL
);
165 if (chdir(dirname
) < 0)
166 return (isc__errno2result(errno
));
168 return (ISC_R_SUCCESS
);
172 isc_dir_chroot(const char *dirname
) {
174 REQUIRE(dirname
!= NULL
);
177 if (chroot(dirname
) < 0 || chdir("/") < 0)
178 return (isc__errno2result(errno
));
180 return (ISC_R_SUCCESS
);
182 return (ISC_R_NOTIMPLEMENTED
);
187 isc_dir_createunique(char *templet
) {
194 REQUIRE(templet
!= NULL
);
197 * \brief 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
;