etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / roken / dirent.c
blob9362cac6ec5d6594f1741bd09bcf919fbf5453c1
1 /* $NetBSD: dirent.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
3 /***********************************************************************
4 * Copyright (c) 2009, Secure Endpoints Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 **********************************************************************/
34 #include<config.h>
36 #include <stdlib.h>
37 #include <io.h>
38 #include <string.h>
39 #include <errno.h>
40 #include "dirent.h"
42 #ifndef _WIN32
43 #error Only implemented for Win32
44 #endif
46 struct _dirent_dirinfo {
47 int magic;
48 long n_entries;
49 long nc_entries;
50 long cursor;
51 struct dirent **entries;
53 #define DIRINFO_MAGIC 0xf8c0639d
54 #define IS_DP(p) ((p) && ((DIR *)(p))->magic == DIRINFO_MAGIC)
56 #define INITIAL_ENTRIES 16
58 /**
59 * Create a filespec for use with _findfirst() using a path spec
61 * If the last component of the path spec contains wildcards, we let
62 * it be. If the last component doesn't end with a slash, we add one.
64 static const char *
65 filespec_from_dir_path(const char * path, char * buffer, size_t cch_buffer)
67 char *comp, *t;
68 size_t pos;
69 int found_sep = 0;
71 if (strcpy_s(buffer, cch_buffer, path) != 0)
72 return NULL;
74 comp = strrchr(buffer, '\\');
75 if (comp == NULL)
76 comp = buffer;
77 else
78 found_sep = 1;
80 t = strrchr(comp, '/');
81 if (t != NULL) {
82 comp = t;
83 found_sep = 1;
86 if (found_sep)
87 comp++;
89 pos = strcspn(comp, "*?");
90 if (comp[pos] != '\0')
91 return buffer;
93 /* We don't append a slash if pos == 0 because that changes the
94 * meaning:
96 * "*.*" is all files in the current directory.
97 * "\*.*" is all files in the root directory of the current drive.
99 if (pos > 0 && comp[pos - 1] != '\\' &&
100 comp[pos - 1] != '/') {
101 strcat_s(comp, cch_buffer - (comp - buffer), "\\");
104 strcat_s(comp, cch_buffer - (comp - buffer), "*.*");
106 return buffer;
109 ROKEN_LIB_FUNCTION DIR * ROKEN_LIB_CALL
110 opendir(const char * path)
112 DIR * dp;
113 struct _finddata_t fd;
114 intptr_t fd_handle;
115 const char *filespec;
116 char path_buffer[1024];
118 memset(&fd, 0, sizeof(fd));
120 filespec = filespec_from_dir_path(path, path_buffer, sizeof(path_buffer)/sizeof(char));
121 if (filespec == NULL)
122 return NULL;
124 fd_handle = _findfirst(filespec, &fd);
126 if (fd_handle == -1)
127 return NULL;
129 dp = malloc(sizeof(*dp));
130 if (dp == NULL)
131 goto done;
133 memset(dp, 0, sizeof(*dp));
134 dp->magic = DIRINFO_MAGIC;
135 dp->cursor = 0;
136 dp->n_entries = 0;
137 dp->nc_entries = INITIAL_ENTRIES;
138 dp->entries = calloc(dp->nc_entries, sizeof(dp->entries[0]));
140 if (dp->entries == NULL) {
141 closedir(dp);
142 dp = NULL;
143 goto done;
146 do {
147 size_t len = strlen(fd.name);
148 struct dirent * e;
150 if (dp->n_entries == dp->nc_entries) {
151 struct dirent ** ne;
153 dp->nc_entries *= 2;
154 ne = realloc(dp->entries, sizeof(dp->entries[0]) * dp->nc_entries);
156 if (ne == NULL) {
157 closedir(dp);
158 dp = NULL;
159 goto done;
162 dp->entries = ne;
165 e = malloc(sizeof(*e) + len * sizeof(char));
166 if (e == NULL) {
167 closedir(dp);
168 dp = NULL;
169 goto done;
172 e->d_ino = 0; /* no inodes :( */
173 strcpy_s(e->d_name, len + 1, fd.name);
175 dp->entries[dp->n_entries++] = e;
177 } while (_findnext(fd_handle, &fd) == 0);
179 done:
180 if (fd_handle != -1)
181 _findclose(fd_handle);
183 return dp;
186 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
187 closedir(DIR * dp)
189 if (!IS_DP(dp))
190 return EINVAL;
192 if (dp->entries) {
193 long i;
195 for (i=0; i < dp->n_entries; i++) {
196 free(dp->entries[i]);
199 free(dp->entries);
202 free(dp);
204 return 0;
207 ROKEN_LIB_FUNCTION struct dirent * ROKEN_LIB_CALL
208 readdir(DIR * dp)
210 if (!IS_DP(dp) ||
211 dp->cursor < 0 ||
212 dp->cursor >= dp->n_entries)
214 return NULL;
216 return dp->entries[dp->cursor++];
219 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
220 rewinddir(DIR * dp)
222 if (IS_DP(dp))
223 dp->cursor = 0;
226 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
227 seekdir(DIR * dp, long offset)
229 if (IS_DP(dp) && offset >= 0 && offset < dp->n_entries)
230 dp->cursor = offset;
233 ROKEN_LIB_FUNCTION long ROKEN_LIB_CALL
234 telldir(DIR * dp)
236 return dp->cursor;