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)
18 #include "test_windirent.h"
21 ** Implementation of the POSIX getenv() function using the Win32 API.
22 ** This function is not thread-safe.
24 const char *windirent_getenv(
27 static char value
[32768]; /* Maximum length, per MSDN */
28 DWORD dwSize
= sizeof(value
) / sizeof(char); /* Size in chars */
29 DWORD dwRet
; /* Value returned by GetEnvironmentVariableA() */
31 memset(value
, 0, sizeof(value
));
32 dwRet
= GetEnvironmentVariableA(name
, value
, dwSize
);
33 if( dwRet
==0 || dwRet
>dwSize
){
35 ** The function call to GetEnvironmentVariableA() failed -OR-
36 ** the buffer is not large enough. Either way, return NULL.
41 ** The function call to GetEnvironmentVariableA() succeeded
42 ** -AND- the buffer contains the entire value.
49 ** Implementation of the POSIX opendir() function using the MSVCRT.
54 struct _finddata_t data
;
55 LPDIR dirp
= (LPDIR
)sqlite3_malloc(sizeof(DIR));
56 SIZE_T namesize
= sizeof(data
.name
) / sizeof(data
.name
[0]);
58 if( dirp
==NULL
) return NULL
;
59 memset(dirp
, 0, sizeof(DIR));
61 /* TODO: Remove this if Unix-style root paths are not used. */
62 if( sqlite3_stricmp(dirname
, "/")==0 ){
63 dirname
= windirent_getenv("SystemDrive");
66 memset(&data
, 0, sizeof(struct _finddata_t
));
67 _snprintf(data
.name
, namesize
, "%s\\*", dirname
);
68 dirp
->d_handle
= _findfirst(data
.name
, &data
);
70 if( dirp
->d_handle
==BAD_INTPTR_T
){
75 /* TODO: Remove this block to allow hidden and/or system files. */
76 if( is_filtered(data
) ){
79 memset(&data
, 0, sizeof(struct _finddata_t
));
80 if( _findnext(dirp
->d_handle
, &data
)==-1 ){
85 /* TODO: Remove this block to allow hidden and/or system files. */
86 if( is_filtered(data
) ) goto next
;
89 dirp
->d_first
.d_attributes
= data
.attrib
;
90 strncpy(dirp
->d_first
.d_name
, data
.name
, NAME_MAX
);
91 dirp
->d_first
.d_name
[NAME_MAX
] = '\0';
97 ** Implementation of the POSIX readdir() function using the MSVCRT.
102 struct _finddata_t data
;
104 if( dirp
==NULL
) return NULL
;
106 if( dirp
->d_first
.d_ino
==0 ){
107 dirp
->d_first
.d_ino
++;
108 dirp
->d_next
.d_ino
++;
110 return &dirp
->d_first
;
115 memset(&data
, 0, sizeof(struct _finddata_t
));
116 if( _findnext(dirp
->d_handle
, &data
)==-1 ) return NULL
;
118 /* TODO: Remove this block to allow hidden and/or system files. */
119 if( is_filtered(data
) ) goto next
;
121 dirp
->d_next
.d_ino
++;
122 dirp
->d_next
.d_attributes
= data
.attrib
;
123 strncpy(dirp
->d_next
.d_name
, data
.name
, NAME_MAX
);
124 dirp
->d_next
.d_name
[NAME_MAX
] = '\0';
126 return &dirp
->d_next
;
130 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
137 struct _finddata_t data
;
139 if( dirp
==NULL
) return EBADF
;
141 if( dirp
->d_first
.d_ino
==0 ){
142 dirp
->d_first
.d_ino
++;
143 dirp
->d_next
.d_ino
++;
145 entry
->d_ino
= dirp
->d_first
.d_ino
;
146 entry
->d_attributes
= dirp
->d_first
.d_attributes
;
147 strncpy(entry
->d_name
, dirp
->d_first
.d_name
, NAME_MAX
);
148 entry
->d_name
[NAME_MAX
] = '\0';
156 memset(&data
, 0, sizeof(struct _finddata_t
));
157 if( _findnext(dirp
->d_handle
, &data
)==-1 ){
162 /* TODO: Remove this block to allow hidden and/or system files. */
163 if( is_filtered(data
) ) goto next
;
165 entry
->d_ino
= (ino_t
)-1; /* not available */
166 entry
->d_attributes
= data
.attrib
;
167 strncpy(entry
->d_name
, data
.name
, NAME_MAX
);
168 entry
->d_name
[NAME_MAX
] = '\0';
175 ** Implementation of the POSIX closedir() function using the MSVCRT.
182 if( dirp
==NULL
) return EINVAL
;
184 if( dirp
->d_handle
!=NULL_INTPTR_T
&& dirp
->d_handle
!=BAD_INTPTR_T
){
185 result
= _findclose(dirp
->d_handle
);
192 #endif /* defined(WIN32) && defined(_MSC_VER) */