1 | /*
|
---|
2 |
|
---|
3 | Implementation of POSIX directory browsing functions and types for Win32.
|
---|
4 |
|
---|
5 | Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
|
---|
6 | History: Created March 1997. Updated June 2003 and July 2012.
|
---|
7 | Rights: See end of file.
|
---|
8 |
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include <errno.h>
|
---|
12 | #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
|
---|
13 | #include <stdlib.h>
|
---|
14 | #include <string.h>
|
---|
15 |
|
---|
16 | #ifdef __cplusplus
|
---|
17 | extern "C"
|
---|
18 | {
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */
|
---|
22 |
|
---|
23 | struct dirent
|
---|
24 | {
|
---|
25 | char *d_name;
|
---|
26 | };
|
---|
27 |
|
---|
28 | struct DIR
|
---|
29 | {
|
---|
30 | handle_type handle; /* -1 for failed rewind */
|
---|
31 | struct _finddata_t info;
|
---|
32 | struct dirent result; /* d_name null iff first time */
|
---|
33 | char *name; /* null-terminated char string */
|
---|
34 | };
|
---|
35 |
|
---|
36 | typedef struct DIR DIR;
|
---|
37 |
|
---|
38 | DIR *opendir(const char *name)
|
---|
39 | {
|
---|
40 | DIR *dir = 0;
|
---|
41 |
|
---|
42 | if(name && name[0])
|
---|
43 | {
|
---|
44 | size_t base_length = strlen(name);
|
---|
45 | const char *all = /* search pattern must end with suitable wildcard */
|
---|
46 | strchr("/\\", name[base_length - 1]) ? "*" : "/*";
|
---|
47 |
|
---|
48 | if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
|
---|
49 | (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
|
---|
50 | {
|
---|
51 | strcat(strcpy(dir->name, name), all);
|
---|
52 |
|
---|
53 | if((dir->handle =
|
---|
54 | (handle_type) _findfirst(dir->name, &dir->info)) != -1)
|
---|
55 | {
|
---|
56 | dir->result.d_name = 0;
|
---|
57 | }
|
---|
58 | else /* rollback */
|
---|
59 | {
|
---|
60 | free(dir->name);
|
---|
61 | free(dir);
|
---|
62 | dir = 0;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | else /* rollback */
|
---|
66 | {
|
---|
67 | free(dir);
|
---|
68 | dir = 0;
|
---|
69 | errno = ENOMEM;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | else
|
---|
73 | {
|
---|
74 | errno = EINVAL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return dir;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int closedir(DIR *dir)
|
---|
81 | {
|
---|
82 | int result = -1;
|
---|
83 |
|
---|
84 | if(dir)
|
---|
85 | {
|
---|
86 | if(dir->handle != -1)
|
---|
87 | {
|
---|
88 | result = _findclose(dir->handle);
|
---|
89 | }
|
---|
90 |
|
---|
91 | free(dir->name);
|
---|
92 | free(dir);
|
---|
93 | }
|
---|
94 |
|
---|
95 | if(result == -1) /* map all errors to EBADF */
|
---|
96 | {
|
---|
97 | errno = EBADF;
|
---|
98 | }
|
---|
99 |
|
---|
100 | return result;
|
---|
101 | }
|
---|
102 |
|
---|
103 | struct dirent *readdir(DIR *dir)
|
---|
104 | {
|
---|
105 | struct dirent *result = 0;
|
---|
106 |
|
---|
107 | if(dir && dir->handle != -1)
|
---|
108 | {
|
---|
109 | if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
|
---|
110 | {
|
---|
111 | result = &dir->result;
|
---|
112 | result->d_name = dir->info.name;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | errno = EBADF;
|
---|
118 | }
|
---|
119 |
|
---|
120 | return result;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void rewinddir(DIR *dir)
|
---|
124 | {
|
---|
125 | if(dir && dir->handle != -1)
|
---|
126 | {
|
---|
127 | _findclose(dir->handle);
|
---|
128 | dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
|
---|
129 | dir->result.d_name = 0;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | errno = EBADF;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | #ifdef __cplusplus
|
---|
138 | }
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | /*
|
---|
142 |
|
---|
143 | Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
|
---|
144 |
|
---|
145 | Permission to use, copy, modify, and distribute this software and its
|
---|
146 | documentation for any purpose is hereby granted without fee, provided
|
---|
147 | that this copyright and permissions notice appear in all copies and
|
---|
148 | derivatives.
|
---|
149 |
|
---|
150 | This software is supplied "as is" without express or implied warranty.
|
---|
151 |
|
---|
152 | But that said, if there are any problems please get in touch.
|
---|
153 |
|
---|
154 | */
|
---|