2 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.25.128.3 2009/02/16 23:46:44 tbox Exp $ */
21 * \author Principal Authors: DCL */
25 #include <sys/types.h>
33 #include <isc/magic.h>
34 #include <isc/string.h>
37 #include "errno2result.h"
39 #define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
40 #define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
43 isc_dir_init(isc_dir_t
*dir
) {
46 dir
->entry
.name
[0] = '\0';
47 dir
->entry
.length
= 0;
51 dir
->magic
= ISC_DIR_MAGIC
;
55 * \brief Allocate workspace and open directory stream. If either one fails,
56 * NULL will be returned.
59 isc_dir_open(isc_dir_t
*dir
, const char *dirname
) {
61 isc_result_t result
= ISC_R_SUCCESS
;
63 REQUIRE(VALID_DIR(dir
));
64 REQUIRE(dirname
!= NULL
);
67 * Copy directory name. Need to have enough space for the name,
68 * a possible path separator, the wildcard, and the final NUL.
70 if (strlen(dirname
) + 3 > sizeof(dir
->dirname
))
72 return (ISC_R_NOSPACE
);
73 strcpy(dir
->dirname
, dirname
);
76 * Append path separator, if needed, and "*".
78 p
= dir
->dirname
+ strlen(dir
->dirname
);
79 if (dir
->dirname
< p
&& *(p
- 1) != '/')
87 dir
->handle
= opendir(dirname
);
89 if (dir
->handle
== NULL
)
90 return isc__errno2result(errno
);
96 * \brief Return previously retrieved file or get next one.
99 * separate open and read functions, but the Win32 and DOS interfaces open
100 * the dir stream and reads the first file in one operation.
103 isc_dir_read(isc_dir_t
*dir
) {
104 struct dirent
*entry
;
106 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
109 * Fetch next file in directory.
111 entry
= readdir(dir
->handle
);
114 return (ISC_R_NOMORE
);
117 * Make sure that the space for the name is long enough.
119 if (sizeof(dir
->entry
.name
) <= strlen(entry
->d_name
))
120 return (ISC_R_UNEXPECTED
);
122 strcpy(dir
->entry
.name
, entry
->d_name
);
125 * Some dirents have d_namlen, but it is not portable.
127 dir
->entry
.length
= strlen(entry
->d_name
);
129 return (ISC_R_SUCCESS
);
133 * \brief Close directory stream.
136 isc_dir_close(isc_dir_t
*dir
) {
137 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
139 (void)closedir(dir
->handle
);
144 * \brief Reposition directory stream at start.
147 isc_dir_reset(isc_dir_t
*dir
) {
148 REQUIRE(VALID_DIR(dir
) && dir
->handle
!= NULL
);
150 rewinddir(dir
->handle
);
152 return (ISC_R_SUCCESS
);
156 isc_dir_chdir(const char *dirname
) {
158 * \brief Change the current directory to 'dirname'.
161 REQUIRE(dirname
!= NULL
);
163 if (chdir(dirname
) < 0)
164 return (isc__errno2result(errno
));
166 return (ISC_R_SUCCESS
);
170 isc_dir_chroot(const char *dirname
) {
172 REQUIRE(dirname
!= NULL
);
175 if (chroot(dirname
) < 0 || chdir("/") < 0)
176 return (isc__errno2result(errno
));
178 return (ISC_R_SUCCESS
);
180 return (ISC_R_NOTIMPLEMENTED
);
185 isc_dir_createunique(char *templet
) {
192 REQUIRE(templet
!= NULL
);
195 * \brief mkdtemp is not portable, so this emulates it.
201 * Replace trailing Xs with the process-id, zero-filled.
203 for (x
= templet
+ strlen(templet
) - 1; *x
== 'X' && x
>= templet
;
207 x
++; /* Set x to start of ex-Xs. */
210 i
= mkdir(templet
, 0700);
211 if (i
== 0 || errno
!= EEXIST
)
219 if (isdigit(*p
& 0xff))
225 * Reset character and move to next.
236 * Tried all combinations. errno should already
237 * be EEXIST, but ensure it is anyway for
238 * isc__errno2result().
246 result
= isc__errno2result(errno
);
248 result
= ISC_R_SUCCESS
;