-
Notifications
You must be signed in to change notification settings - Fork 5
/
dirent.c
198 lines (163 loc) · 4.86 KB
/
dirent.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/****************************************************************************
*
* Copyright (c) 2010 Dave Hylands <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*
****************************************************************************/
/**
*
* @file dirent.c
*
* @brief This file contains an implementation on readdir and friends
* which works under Win32.
*
*****************************************************************************/
/* ---- Include Files ----------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "dirent2.h"
#include <windows.h>
/* ---- Public Variables -------------------------------------------------- */
/* ---- Private Constants and Types --------------------------------------- */
/* ---- Private Variables ------------------------------------------------- */
/* ---- Private Function Prototypes --------------------------------------- */
/* ---- Functions --------------------------------------------------------- */
/**
* @addtogroup XXX
* @{
*/
//***************************************************************************
/**
* The opendir() function opens a directory stream corresponding to
* the directory name, and returns a pointer to the directory stream.
* The stream is positioned at the first entry in the directory.
*
* On error, NULL is returned and errno is set appropriately.
*/
DIR2 *opendir2( const char *dirName )
{
DIR2 *dir;
DWORD fileAttr;
// Validate parameters
if ( dirName == NULL )
{
errno = EFAULT;
return NULL;
}
if ( dirName[0] == '\0' )
{
errno = ENOENT;
return NULL;
}
// Make sure that dirName is really a directory
if (( fileAttr = GetFileAttributes( dirName )) == INVALID_FILE_ATTRIBUTES )
{
errno = ENOTDIR;
return NULL;
}
if (( dir = calloc( 1, sizeof( *dir ))) == NULL )
{
errno = ENOMEM;
return NULL;
}
if (( dir->dd_finddata = malloc( sizeof( WIN32_FIND_DATA ))) == NULL )
{
errno = ENOMEM;
free( dir );
return NULL;
}
// We need to pass in a pattern to FindFirstFile, so we allocate
// memory to contain the directory passed in followed by /*
if (( dir->dd_dirpattern = malloc( strlen( dirName ) + 3 )) == NULL )
{
errno = ENOMEM;
free( dir->dd_finddata );
free( dir );
return NULL;
}
strcpy( dir->dd_dirpattern, dirName );
strcat( dir->dd_dirpattern, "/*" );
dir->dd_handle = 0;
return dir;
}
//***************************************************************************
/**
* The readdir() function returns a pointer to a dirent structure
* representing the next directory entry in the directory stream
* pointed to by dir. It returns NULL on reaching the end-of-file
* or if an error occurred.
*/
struct dirent2 *readdir2( DIR2 *dir )
{
WIN32_FIND_DATA *fd;
if ( dir->dd_handle == INVALID_HANDLE_VALUE )
{
errno = EBADF;
return NULL;
}
if ( dir->dd_handle == 0 )
{
dir->dd_handle = FindFirstFile( dir->dd_dirpattern, dir->dd_finddata );
if ( dir->dd_handle == INVALID_HANDLE_VALUE )
{
errno = 0;
return NULL;
}
}
else
{
if ( !FindNextFile( dir->dd_handle, dir->dd_finddata ))
{
if ( GetLastError() == ERROR_NO_MORE_FILES )
{
errno = 0;
return NULL;
}
}
}
fd = dir->dd_finddata;
strncpy( dir->dd_dirent.d_name, fd->cFileName, sizeof( dir->dd_dirent.d_name ));
dir->dd_dirent.d_name[ sizeof( dir->dd_dirent.d_name ) - 1 ] = '\0';
dir->dd_dirent.d_type = 0;
if ( fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
dir->dd_dirent.d_type |= DT_DIR;
}
else
{
dir->dd_dirent.d_type |= DT_REG;
}
return &dir->dd_dirent;
}
//***************************************************************************
/**
* The closedir() function closes the directory stream associated with dir.
* The directory stream descriptor dir is not available after this call.
*/
int closedir2( DIR2 *dir )
{
int rc = 0;
if (( dir->dd_handle == 0 ) || ( dir->dd_handle != INVALID_HANDLE_VALUE ))
{
if ( !FindClose( dir->dd_handle ))
{
errno = EBADF;
rc = -1;
}
dir->dd_handle = 0;
}
free( dir->dd_dirpattern );
free( dir->dd_finddata );
free( dir );
return rc;
}
/** @} */