Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-dir/src/Win/dirent.c

raw
1 /*
2
3 Implementation of POSIX directory browsing functions and types for Win32.
4
5 Author: Kevlin Henney ([email protected], [email protected])
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 size_t base_length = strlen(name);
44 const char *all = /* search pattern must end with suitable wildcard */
45 strchr("/\\", name[base_length - 1]) ? "*" : "/*";
46
47 if ((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
48 (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0) {
49 strcat(strcpy(dir->name, name), all);
50
51 if ((dir->handle = (handle_type) _findfirst(dir->name, &dir->info)) != -1) {
52 dir->result.d_name = 0;
53 }
54 else { /* rollback */
55 free(dir->name);
56 free(dir);
57 dir = 0;
58 }
59 }
60 else { /* rollback */
61 free(dir);
62 dir = 0;
63 errno = ENOMEM;
64 }
65 }
66 else {
67 errno = EINVAL;
68 }
69
70 return dir;
71 }
72
73 int closedir(DIR *dir)
74 {
75 int result = -1;
76
77 if (dir) {
78 if (dir->handle != -1)
79 {
80 result = _findclose(dir->handle);
81 }
82
83 free(dir->name);
84 free(dir);
85 }
86
87 if (result == -1) { /* map all errors to EBADF */
88 errno = EBADF;
89 }
90
91 return result;
92 }
93
94 struct dirent *readdir(DIR *dir)
95 {
96 struct dirent *result = 0;
97
98 if (dir && dir->handle != -1) {
99 if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) {
100 result = &dir->result;
101 result->d_name = dir->info.name;
102 }
103 }
104 else {
105 errno = EBADF;
106 }
107
108 return result;
109 }
110
111 void rewinddir(DIR *dir)
112 {
113 if (dir && dir->handle != -1) {
114 _findclose(dir->handle);
115 dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
116 dir->result.d_name = 0;
117 }
118 else {
119 errno = EBADF;
120 }
121 }
122
123 #ifdef __cplusplus
124 }
125 #endif
126
127 /*
128
129 Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
130
131 Permission to use, copy, modify, and distribute this software and its
132 documentation for any purpose is hereby granted without fee, provided
133 that this copyright and permissions notice appear in all copies and
134 derivatives.
135
136 This software is supplied "as is" without express or implied warranty.
137
138 But that said, if there are any problems please get in touch.
139
140 */