Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / ntp / dist / lib / isc / unix / dir.c
blob33b400150b66c7eb6a09d6e2c7692cfbad53a7ed
1 /* $NetBSD$ */
3 /*
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 */
22 /*! \file
23 * \author Principal Authors: DCL */
25 #include <config.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <unistd.h>
34 #include <isc/dir.h>
35 #include <isc/magic.h>
36 #include <isc/string.h>
37 #include <isc/util.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)
44 void
45 isc_dir_init(isc_dir_t *dir) {
46 REQUIRE(dir != NULL);
48 dir->entry.name[0] = '\0';
49 dir->entry.length = 0;
51 dir->handle = NULL;
53 dir->magic = ISC_DIR_MAGIC;
56 /*!
57 * \brief Allocate workspace and open directory stream. If either one fails,
58 * NULL will be returned.
60 isc_result_t
61 isc_dir_open(isc_dir_t *dir, const char *dirname) {
62 char *p;
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))
73 /* XXXDCL ? */
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) != '/')
82 *p++ = '/';
83 *p++ = '*';
84 *p++ = '\0';
87 * Open stream.
89 dir->handle = opendir(dirname);
91 if (dir->handle == NULL)
92 return isc__errno2result(errno);
94 return (result);
97 /*!
98 * \brief Return previously retrieved file or get next one.
100 * Unix's dirent has
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.
104 isc_result_t
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);
115 if (entry == NULL)
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.
137 void
138 isc_dir_close(isc_dir_t *dir) {
139 REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
141 (void)closedir(dir->handle);
142 dir->handle = NULL;
146 * \brief Reposition directory stream at start.
148 isc_result_t
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);
157 isc_result_t
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);
171 isc_result_t
172 isc_dir_chroot(const char *dirname) {
174 REQUIRE(dirname != NULL);
176 #ifdef HAVE_CHROOT
177 if (chroot(dirname) < 0 || chdir("/") < 0)
178 return (isc__errno2result(errno));
180 return (ISC_R_SUCCESS);
181 #else
182 return (ISC_R_NOTIMPLEMENTED);
183 #endif
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 * \brief 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);