1 /* $NetBSD: dir.c,v 1.1.1.3 2014/12/10 03:34:31 christos Exp $ */
4 * Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
27 #include "dlz_minimal.h"
31 dir_init(dir_t
*dir
) {
32 dir
->entry
.name
[0] = '\0';
33 dir
->entry
.length
= 0;
39 dir_open(dir_t
*dir
, const char *dirname
) {
41 isc_result_t result
= ISC_R_SUCCESS
;
43 if (strlen(dirname
) + 3 > sizeof(dir
->dirname
))
44 return (ISC_R_NOSPACE
);
45 strcpy(dir
->dirname
, dirname
);
47 p
= dir
->dirname
+ strlen(dir
->dirname
);
48 if (dir
->dirname
< p
&& *(p
- 1) != '/')
53 dir
->handle
= opendir(dirname
);
54 if (dir
->handle
== NULL
) {
61 result
= ISC_R_INVALIDFILE
;
63 result
= ISC_R_FILENOTFOUND
;
66 result
= ISC_R_NOPERM
;
68 result
= ISC_R_NOMEMORY
;
70 result
= ISC_R_UNEXPECTED
;
78 * \brief Return previously retrieved file or get next one.
81 * separate open and read functions, but the Win32 and DOS interfaces open
82 * the dir stream and reads the first file in one operation.
85 dir_read(dir_t
*dir
) {
88 entry
= readdir(dir
->handle
);
90 return (ISC_R_NOMORE
);
92 if (sizeof(dir
->entry
.name
) <= strlen(entry
->d_name
))
93 return (ISC_R_UNEXPECTED
);
95 strcpy(dir
->entry
.name
, entry
->d_name
);
97 dir
->entry
.length
= strlen(entry
->d_name
);
98 return (ISC_R_SUCCESS
);
102 * \brief Close directory stream.
105 dir_close(dir_t
*dir
) {
106 (void)closedir(dir
->handle
);
111 * \brief Reposition directory stream at start.
114 dir_reset(dir_t
*dir
) {
115 rewinddir(dir
->handle
);
117 return (ISC_R_SUCCESS
);