quite down clang analyzer warnings for the generate asn1 code
[heimdal.git] / lib / roken / dirent.c
blobc76724c72dee9b0777ded7abfda8dd5250e80273
1 /***********************************************************************
2 * Copyright (c) 2009, Secure Endpoints Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 **********************************************************************/
32 #include<config.h>
34 #include <stdlib.h>
35 #include <io.h>
36 #include <string.h>
37 #include <errno.h>
38 #include "dirent.h"
40 #ifndef _WIN32
41 #error Only implemented for Win32
42 #endif
44 struct _dirent_dirinfo {
45 int magic;
46 long n_entries;
47 long nc_entries;
48 long cursor;
49 struct dirent **entries;
51 #define DIRINFO_MAGIC 0xf8c0639d
52 #define IS_DP(p) ((p) && ((DIR *)(p))->magic == DIRINFO_MAGIC)
54 #define INITIAL_ENTRIES 16
56 /**
57 * Create a filespec for use with _findfirst() using a path spec
59 * If the last component of the path spec contains wildcards, we let
60 * it be. If the last component doesn't end with a slash, we add one.
62 static const char *
63 filespec_from_dir_path(const char * path, char * buffer, size_t cch_buffer)
65 char *comp, *t;
66 size_t pos;
68 if (strcpy_s(buffer, cch_buffer, path) != 0)
69 return NULL;
71 comp = strrchr(buffer, '\\');
72 if (comp == NULL)
73 comp = buffer;
75 t = strrchr(comp, '/');
76 if (t != NULL)
77 comp = t;
79 comp++;
81 pos = strcspn(comp, "*?");
82 if (comp[pos] != '\0')
83 return buffer;
85 /* We don't append a slash if pos == 0 because that changes the
86 * meaning:
88 * "*.*" is all files in the current directory.
89 * "\*.*" is all files in the root directory of the current drive.
91 if (pos > 0 && comp[pos - 1] != '\\' &&
92 comp[pos - 1] != '/') {
93 strcat_s(comp, cch_buffer - (comp - buffer), "\\");
96 strcat_s(comp, cch_buffer - (comp - buffer), "*.*");
98 return buffer;
101 ROKEN_LIB_FUNCTION DIR * ROKEN_LIB_CALL
102 opendir(const char * path)
104 DIR * dp;
105 struct _finddata_t fd;
106 intptr_t fd_handle;
107 const char *filespec;
108 char path_buffer[1024];
110 memset(&fd, 0, sizeof(fd));
112 filespec = filespec_from_dir_path(path, path_buffer, sizeof(path_buffer)/sizeof(char));
113 if (filespec == NULL)
114 return NULL;
116 fd_handle = _findfirst(filespec, &fd);
118 if (fd_handle == -1)
119 return NULL;
121 dp = malloc(sizeof(*dp));
122 if (dp == NULL)
123 goto done;
125 memset(dp, 0, sizeof(*dp));
126 dp->magic = DIRINFO_MAGIC;
127 dp->cursor = 0;
128 dp->n_entries = 0;
129 dp->nc_entries = INITIAL_ENTRIES;
130 dp->entries = calloc(dp->nc_entries, sizeof(dp->entries[0]));
132 if (dp->entries == NULL) {
133 closedir(dp);
134 dp = NULL;
135 goto done;
138 do {
139 size_t len = strlen(fd.name);
140 struct dirent * e;
142 if (dp->n_entries == dp->nc_entries) {
143 struct dirent ** ne;
145 dp->nc_entries *= 2;
146 ne = realloc(dp->entries, sizeof(dp->entries[0]) * dp->nc_entries);
148 if (ne == NULL) {
149 closedir(dp);
150 dp = NULL;
151 goto done;
154 dp->entries = ne;
157 e = malloc(sizeof(*e) + len * sizeof(char));
158 if (e == NULL) {
159 closedir(dp);
160 dp = NULL;
161 goto done;
164 e->d_ino = 0; /* no inodes :( */
165 strcpy_s(e->d_name, len + 1, fd.name);
167 dp->entries[dp->n_entries++] = e;
169 } while (_findnext(fd_handle, &fd) == 0);
171 done:
172 if (fd_handle != -1)
173 _findclose(fd_handle);
175 return dp;
178 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
179 closedir(DIR * dp)
181 if (!IS_DP(dp))
182 return EINVAL;
184 if (dp->entries) {
185 long i;
187 for (i=0; i < dp->n_entries; i++) {
188 free(dp->entries[i]);
191 free(dp->entries);
194 free(dp);
196 return 0;
199 ROKEN_LIB_FUNCTION struct dirent * ROKEN_LIB_CALL
200 readdir(DIR * dp)
202 if (!IS_DP(dp) ||
203 dp->cursor < 0 ||
204 dp->cursor >= dp->n_entries)
206 return NULL;
208 return dp->entries[dp->cursor++];
211 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
212 rewinddir(DIR * dp)
214 if (IS_DP(dp))
215 dp->cursor = 0;
218 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
219 seekdir(DIR * dp, long offset)
221 if (IS_DP(dp) && offset >= 0 && offset < dp->n_entries)
222 dp->cursor = offset;
225 ROKEN_LIB_FUNCTION long ROKEN_LIB_CALL
226 telldir(DIR * dp)
228 return dp->cursor;