2 * Copyright (C) 2004, 2005, 2007-2009, 2011, 2012 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.
21 * \author Principal Authors: DCL */
25 #include <sys/types.h>
33 #include <isc/magic.h>
34 #include <isc/string.h>
37 #include "errno2result.h"
38 #include "ntp_stdlib.h" /* NTP change for strlcpy, strlcat */
40 #define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
41 #define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
44 isc_dir_init(isc_dir_t *dir) {
47 dir->entry.name[0] = '\0';
48 dir->entry.length = 0;
52 dir->magic = ISC_DIR_MAGIC;
56 * \brief Allocate workspace and open directory stream. If either one fails,
57 * NULL will be returned.
60 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 octets = strlen(dirname) + 1;
73 if (octets + 2 > sizeof(dir->dirname))
75 return (ISC_R_NOSPACE);
76 strlcpy(dir->dirname, dirname, octets);
79 * Append path separator, if needed, and "*".
81 p = dir->dirname + strlen(dir->dirname);
82 if (dir->dirname < p && *(p - 1) != '/')
90 dir->handle = opendir(dirname);
92 if (dir->handle == NULL)
93 return isc__errno2result(errno);
99 * \brief Return previously retrieved file or get next one.
102 * separate open and read functions, but the Win32 and DOS interfaces open
103 * the dir stream and reads the first file in one operation.
106 isc_dir_read(isc_dir_t *dir) {
107 struct dirent *entry;
110 REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
113 * Fetch next file in directory.
115 entry = readdir(dir->handle);
118 return (ISC_R_NOMORE);
121 * Make sure that the space for the name is long enough.
123 octets = strlen(entry->d_name) + 1;
124 if (sizeof(dir->entry.name) < octets)
125 return (ISC_R_UNEXPECTED);
127 strlcpy(dir->entry.name, entry->d_name, octets);
130 * Some dirents have d_namlen, but it is not portable.
132 dir->entry.length = strlen(entry->d_name);
134 return (ISC_R_SUCCESS);
138 * \brief Close directory stream.
141 isc_dir_close(isc_dir_t *dir) {
142 REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
144 (void)closedir(dir->handle);
149 * \brief Reposition directory stream at start.
152 isc_dir_reset(isc_dir_t *dir) {
153 REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
155 rewinddir(dir->handle);
157 return (ISC_R_SUCCESS);
161 isc_dir_chdir(const char *dirname) {
163 * \brief Change the current directory to 'dirname'.
166 REQUIRE(dirname != NULL);
168 if (chdir(dirname) < 0)
169 return (isc__errno2result(errno));
171 return (ISC_R_SUCCESS);
175 isc_dir_chroot(const char *dirname) {
177 REQUIRE(dirname != NULL);
180 if (chroot(dirname) < 0 || chdir("/") < 0)
181 return (isc__errno2result(errno));
183 return (ISC_R_SUCCESS);
185 return (ISC_R_NOTIMPLEMENTED);
190 isc_dir_createunique(char *templet) {
197 REQUIRE(templet != NULL);
200 * \brief mkdtemp is not portable, so this emulates it.
206 * Replace trailing Xs with the process-id, zero-filled.
208 for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
212 x++; /* Set x to start of ex-Xs. */
215 i = mkdir(templet, 0700);
216 if (i == 0 || errno != EEXIST)
224 if (isdigit(*p & 0xff))
230 * Reset character and move to next.
241 * Tried all combinations. errno should already
242 * be EEXIST, but ensure it is anyway for
243 * isc__errno2result().
251 result = isc__errno2result(errno);
253 result = ISC_R_SUCCESS;