CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
direntwin.cpp
Go to the documentation of this file.
1
5/*
6
7 Implementation of POSIX directory browsing functions and types for Win32.
8
9 Kevlin Henney (mailto:kevlin@acm.org), March 1997.
10
11 Copyright Kevlin Henney, 1997. All rights reserved.
12
13 Permission to use, copy, modify, and distribute this software and its
14 documentation for any purpose is hereby granted without fee, provided
15 that this copyright and permissions notice appear in all copies and
16 derivatives, and that no charge may be made for the software and its
17 documentation except to cover cost of distribution.
18
19 This software is supplied "as is" without express or implied warranty.
20
21 But that said, if there are any problems please get in touch.
22
23*/
24
25#include "direntwin.h"
26
27namespace cmlabs {
28
29
30#if defined(POCKETPC)
31// #define CONSTCHAR unsigned short
32 #define CONSTCHAR WCHAR
33#else
34 #define CONSTCHAR char
35#endif
36
37#if defined(POCKETPC)
38 int errno;
39#endif
40
41#ifdef CYGWIN
42 int errno;
43#endif
44
45struct DIR
46{
47 HANDLE handle; /* -1 for failed rewind */
48 WIN32_FIND_DATA info;
49 struct dirent result; /* d_name null iff first time */
51};
52
53DIR *opendir(const char *name)
54{
55 DIR *dir = 0;
56
57 if(name && name[0])
58 {
59 size_t base_length = strlen(name);
60 const char *all = /* the root directory is a special case... */
61 strchr("/\\", name[base_length - 1]) ? "*" : "/*";
62
63 if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
64 (dir->name = (CONSTCHAR*) malloc(base_length + strlen(all) + 1)) != 0)
65 {
66 utils::strcpyavail((char*)dir->name, name, (uint32)base_length + (uint32)strlen(all) + 1, true);
67 strcat_s((char*)dir->name, base_length + (uint32)strlen(all) + 1, all);
68
69 if((dir->handle = FindFirstFile(dir->name, &dir->info)) != INVALID_HANDLE_VALUE)
70 {
71 dir->result.d_name = 0;
72 }
73 else /* rollback */
74 {
75 free((void*)dir->name);
76 free(dir);
77 dir = 0;
78 }
79 }
80 else /* rollback */
81 {
82 free(dir);
83 dir = 0;
84 errno = 12; // ENOMEM;
85 }
86 }
87 else
88 {
89 errno = 22; // EINVAL;
90 }
91
92 return dir;
93}
94
95int closedir(DIR *dir)
96{
97 int result = -1;
98
99 if(dir)
100 {
101 if(dir->handle != INVALID_HANDLE_VALUE)
102 {
103 result = FindClose(dir->handle);
104 }
105
106 free((void*)dir->name);
107 free(dir);
108 }
109
110 if(result == -1) /* map all errors to EBADF */
111 {
112 errno = 9; // EBADF;
113 }
114
115 return result;
116}
117
118struct dirent *readdir(DIR *dir)
119{
120 struct dirent *result = 0;
121
122 if(dir && dir->handle != INVALID_HANDLE_VALUE)
123 {
124 if((!dir->result.d_name) || (FindNextFile(dir->handle, &dir->info)))
125 {
126 result = &dir->result;
127 result->d_name = (char*) dir->info.cFileName;
128 }
129 }
130 else
131 {
132 errno = 9; // EBADF;
133 }
134
135 return result;
136}
137
138void rewinddir(DIR *dir)
139{
140 if(dir && dir->handle != INVALID_HANDLE_VALUE)
141 {
142 FindClose(dir->handle);
143 dir->handle = FindFirstFile(dir->name, &dir->info);
144 dir->result.d_name = 0;
145 }
146 else
147 {
148 errno = 9; // EBADF;
149 }
150}
151
152
153} // namespace cmlabs
#define CONSTCHAR
Definition direntwin.cpp:34
Third-party (vendored): POSIX dirent (directory browsing) emulation for Win32 by Kevlin Henney (1997)...
uint32 strcpyavail(char *dst, const char *src, uint32 maxlen, bool copyAvailable)
Bounded strcpy that always NUL-terminates.
Definition Utils.cpp:6056
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
Definition direntwin.cpp:53
int closedir(DIR *)
Definition direntwin.cpp:95
void rewinddir(DIR *)
HANDLE handle
Definition direntwin.cpp:47
CONSTCHAR * name
Definition direntwin.cpp:50
WIN32_FIND_DATA info
Definition direntwin.cpp:48
struct dirent result
Definition direntwin.cpp:49
char * d_name
Definition direntwin.h:35