acpi.4: Add some missing references.
[dragonfly.git] / contrib / bind-9.3 / lib / isc / unix / dir.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: dir.c,v 1.18.2.1.2.3 2004/03/08 09:04:55 marka Exp $ */
19
20 /* Principal Authors: DCL */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <unistd.h>
30
31 #include <isc/dir.h>
32 #include <isc/magic.h>
33 #include <isc/string.h>
34 #include <isc/util.h>
35
36 #include "errno2result.h"
37
38 #define ISC_DIR_MAGIC           ISC_MAGIC('D', 'I', 'R', '*')
39 #define VALID_DIR(dir)          ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
40
41 void
42 isc_dir_init(isc_dir_t *dir) {
43         REQUIRE(dir != NULL);
44
45         dir->entry.name[0] = '\0';
46         dir->entry.length = 0;
47
48         dir->handle = NULL;
49
50         dir->magic = ISC_DIR_MAGIC;
51 }
52
53 /*
54  * Allocate workspace and open directory stream. If either one fails,
55  * NULL will be returned.
56  */
57 isc_result_t
58 isc_dir_open(isc_dir_t *dir, const char *dirname) {
59         isc_result_t result = ISC_R_SUCCESS;
60
61         REQUIRE(VALID_DIR(dir));
62         REQUIRE(dirname != NULL);
63
64         /*
65          * Open stream.
66          */
67         dir->handle = opendir(dirname);
68
69         if (dir->handle == NULL)
70                 return isc__errno2result(errno);
71
72         return (result);
73 }
74
75 /*
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.
79  */
80 isc_result_t
81 isc_dir_read(isc_dir_t *dir) {
82         struct dirent *entry;
83
84         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
85
86         /*
87          * Fetch next file in directory.
88          */
89         entry = readdir(dir->handle);
90
91         if (entry == NULL)
92                 return (ISC_R_NOMORE);
93
94         /*
95          * Make sure that the space for the name is long enough.
96          */
97         if (sizeof(dir->entry.name) <= strlen(entry->d_name))
98             return (ISC_R_UNEXPECTED);
99
100         strcpy(dir->entry.name, entry->d_name);
101
102         /*
103          * Some dirents have d_namlen, but it is not portable.
104          */
105         dir->entry.length = strlen(entry->d_name);
106
107         return (ISC_R_SUCCESS);
108 }
109
110 /*
111  * Close directory stream.
112  */
113 void
114 isc_dir_close(isc_dir_t *dir) {
115        REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
116
117        (void)closedir(dir->handle);
118        dir->handle = NULL;
119 }
120
121 /*
122  * Reposition directory stream at start.
123  */
124 isc_result_t
125 isc_dir_reset(isc_dir_t *dir) {
126         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
127
128         rewinddir(dir->handle);
129
130         return (ISC_R_SUCCESS);
131 }
132
133 isc_result_t
134 isc_dir_chdir(const char *dirname) {
135         /*
136          * Change the current directory to 'dirname'.
137          */
138
139         REQUIRE(dirname != NULL);
140
141         if (chdir(dirname) < 0)
142                 return (isc__errno2result(errno));
143
144         return (ISC_R_SUCCESS);
145 }
146
147 isc_result_t
148 isc_dir_chroot(const char *dirname) {
149
150         REQUIRE(dirname != NULL);
151
152         if (chroot(dirname) < 0)
153                 return (isc__errno2result(errno));
154
155         return (ISC_R_SUCCESS);
156 }
157
158 isc_result_t
159 isc_dir_createunique(char *templet) {
160         isc_result_t result;
161         char *x;
162         char *p;
163         int i;
164         int pid;
165
166         REQUIRE(templet != NULL);
167
168         /*
169          * mkdtemp is not portable, so this emulates it.
170          */
171
172         pid = getpid();
173
174         /*
175          * Replace trailing Xs with the process-id, zero-filled.
176          */
177         for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
178              x--, pid /= 10)
179                 *x = pid % 10 + '0';
180
181         x++;                    /* Set x to start of ex-Xs. */
182
183         do {
184                 i = mkdir(templet, 0700);
185                 if (i == 0 || errno != EEXIST)
186                         break;
187
188                 /*
189                  * The BSD algorithm.
190                  */
191                 p = x;
192                 while (*p != '\0') {
193                         if (isdigit(*p & 0xff))
194                                 *p = 'a';
195                         else if (*p != 'z')
196                                 ++*p;
197                         else {
198                                 /*
199                                  * Reset character and move to next.
200                                  */
201                                 *p++ = 'a';
202                                 continue;
203                         }
204
205                         break;
206                 }
207
208                 if (*p == '\0') {
209                         /*
210                          * Tried all combinations.  errno should already
211                          * be EEXIST, but ensure it is anyway for
212                          * isc__errno2result().
213                          */
214                         errno = EEXIST;
215                         break;
216                 }
217         } while (1);
218
219         if (i == -1)
220                 result = isc__errno2result(errno);
221         else
222                 result = ISC_R_SUCCESS;
223
224         return (result);
225 }