4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code to implement most of the opendir() family of
13 ** POSIX functions on Win32 using the MSVCRT.
16 #if defined(_WIN32) && defined(_MSC_VER)
17 #include "test_windirent.h"
20 ** Implementation of the POSIX getenv() function using the Win32 API.
21 ** This function is not thread-safe.
23 const char *windirent_getenv(
26 static char value
[32768]; /* Maximum length, per MSDN */
27 DWORD dwSize
= sizeof(value
) / sizeof(char); /* Size in chars */
28 DWORD dwRet
; /* Value returned by GetEnvironmentVariableA() */
30 memset(value
, 0, sizeof(value
));
31 dwRet
= GetEnvironmentVariableA(name
, value
, dwSize
);
32 if( dwRet
==0 || dwRet
>dwSize
){
34 ** The function call to GetEnvironmentVariableA() failed -OR-
35 ** the buffer is not large enough. Either way, return NULL.
40 ** The function call to GetEnvironmentVariableA() succeeded
41 ** -AND- the buffer contains the entire value.
48 ** Implementation of the POSIX opendir() function using the MSVCRT.
53 struct _finddata_t data
;
54 LPDIR dirp
= (LPDIR
)sqlite3_malloc(sizeof(DIR));
55 SIZE_T namesize
= sizeof(data
.name
) / sizeof(data
.name
[0]);
57 if( dirp
==NULL
) return NULL
;
58 memset(dirp
, 0, sizeof(DIR));
60 /* TODO: Remove this if Unix-style root paths are not used. */
61 if( sqlite3_stricmp(dirname
, "/")==0 ){
62 dirname
= windirent_getenv("SystemDrive");
65 memset(&data
, 0, sizeof(struct _finddata_t
));
66 _snprintf(data
.name
, namesize
, "%s\\*", dirname
);
67 dirp
->d_handle
= _findfirst(data
.name
, &data
);
69 if( dirp
->d_handle
==BAD_INTPTR_T
){
74 /* TODO: Remove this block to allow hidden and/or system files. */
75 if( is_filtered(data
) ){
78 memset(&data
, 0, sizeof(struct _finddata_t
));
79 if( _findnext(dirp
->d_handle
, &data
)==-1 ){
84 /* TODO: Remove this block to allow hidden and/or system files. */
85 if( is_filtered(data
) ) goto next
;
88 dirp
->d_first
.d_attributes
= data
.attrib
;
89 strncpy(dirp
->d_first
.d_name
, data
.name
, NAME_MAX
);
90 dirp
->d_first
.d_name
[NAME_MAX
] = '\0';
96 ** Implementation of the POSIX readdir() function using the MSVCRT.
101 struct _finddata_t data
;
103 if( dirp
==NULL
) return NULL
;
105 if( dirp
->d_first
.d_ino
==0 ){
106 dirp
->d_first
.d_ino
++;
107 dirp
->d_next
.d_ino
++;
109 return &dirp
->d_first
;
114 memset(&data
, 0, sizeof(struct _finddata_t
));
115 if( _findnext(dirp
->d_handle
, &data
)==-1 ) return NULL
;
117 /* TODO: Remove this block to allow hidden and/or system files. */
118 if( is_filtered(data
) ) goto next
;
120 dirp
->d_next
.d_ino
++;
121 dirp
->d_next
.d_attributes
= data
.attrib
;
122 strncpy(dirp
->d_next
.d_name
, data
.name
, NAME_MAX
);
123 dirp
->d_next
.d_name
[NAME_MAX
] = '\0';
125 return &dirp
->d_next
;
129 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
136 struct _finddata_t data
;
138 if( dirp
==NULL
) return EBADF
;
140 if( dirp
->d_first
.d_ino
==0 ){
141 dirp
->d_first
.d_ino
++;
142 dirp
->d_next
.d_ino
++;
144 entry
->d_ino
= dirp
->d_first
.d_ino
;
145 entry
->d_attributes
= dirp
->d_first
.d_attributes
;
146 strncpy(entry
->d_name
, dirp
->d_first
.d_name
, NAME_MAX
);
147 entry
->d_name
[NAME_MAX
] = '\0';
155 memset(&data
, 0, sizeof(struct _finddata_t
));
156 if( _findnext(dirp
->d_handle
, &data
)==-1 ){
161 /* TODO: Remove this block to allow hidden and/or system files. */
162 if( is_filtered(data
) ) goto next
;
164 entry
->d_ino
= (ino_t
)-1; /* not available */
165 entry
->d_attributes
= data
.attrib
;
166 strncpy(entry
->d_name
, data
.name
, NAME_MAX
);
167 entry
->d_name
[NAME_MAX
] = '\0';
174 ** Implementation of the POSIX closedir() function using the MSVCRT.
181 if( dirp
==NULL
) return EINVAL
;
183 if( dirp
->d_handle
!=NULL_INTPTR_T
&& dirp
->d_handle
!=BAD_INTPTR_T
){
184 result
= _findclose(dirp
->d_handle
);
191 #endif /* defined(WIN32) && defined(_MSC_VER) */