-
Notifications
You must be signed in to change notification settings - Fork 333
dirent cleanup #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dirent cleanup #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,33 @@ | ||
// win32_dirent.c | ||
// directory entry functions in Windows platform like Ubix/Linux | ||
// opendir(), readdir(), closedir(). | ||
/* | ||
* | ||
* Copyright (c) 2016 Microsoft Corp. | ||
* All rights reserved | ||
* | ||
* Implementation of following POSIX APIs | ||
* opendir(), readdir(), closedir(). | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
|
||
#include <windows.h> | ||
#include <stdlib.h> | ||
|
@@ -20,10 +47,12 @@ struct DIR_ { | |
int first; | ||
}; | ||
|
||
/* Open a directory stream on NAME. | ||
Return a DIR stream on the directory, or NULL if it could not be opened. */ | ||
DIR * opendir(const char *name) | ||
{ | ||
/* | ||
* Open a directory stream on NAME. | ||
* Return a DIR stream on the directory, or NULL if it could not be opened. | ||
*/ | ||
DIR* | ||
opendir(const char *name) { | ||
struct _wfinddata_t c_file; | ||
intptr_t hFile; | ||
DIR *pdir; | ||
|
@@ -36,65 +65,68 @@ DIR * opendir(const char *name) | |
return NULL; | ||
} | ||
|
||
// add *.* for Windows _findfirst() search pattern | ||
swprintf_s(searchstr, MAX_PATH, L"%s\\*.*", wname); | ||
/* add *.* for Windows _findfirst() search pattern */ | ||
if (swprintf(searchstr, MAX_PATH, L"%s\\*.*", wname) == -1) { | ||
/* breached MAX_PATH */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. swprintfreturns the number of wide characters stored in buffer, not counting the terminating null wide character. so this comment "breached MAX_PATH" is misleading? If we are unable to write to searchstr then we will be inside if loop. |
||
errno = ENOTSUP; | ||
return NULL; | ||
} | ||
free(wname); | ||
|
||
if ((hFile = _wfindfirst(searchstr, &c_file)) == -1L) | ||
if ((hFile = _wfindfirst(searchstr, &c_file)) == -1) | ||
return NULL; /* errno is set by _wfindfirst */ | ||
else { | ||
if ((pdir = malloc(sizeof(DIR))) == NULL) { | ||
_findclose(hFile); | ||
errno = ENOMEM; | ||
return NULL; | ||
} | ||
|
||
memset(pdir, 0, sizeof(DIR)); | ||
pdir->hFile = hFile; | ||
memcpy(&pdir->c_file, &c_file, sizeof(c_file)); | ||
pdir->first = 1; | ||
|
||
return pdir ; | ||
if ((pdir = malloc(sizeof(DIR))) == NULL) { | ||
_findclose(hFile); | ||
errno = ENOMEM; | ||
return NULL; | ||
} | ||
|
||
memset(pdir, 0, sizeof(DIR)); | ||
pdir->hFile = hFile; | ||
memcpy(&pdir->c_file, &c_file, sizeof(c_file)); | ||
pdir->first = 1; | ||
|
||
return pdir ; | ||
} | ||
|
||
/* Close the directory stream DIRP. | ||
Return 0 if successful, -1 if not. */ | ||
int closedir(DIR *dirp) | ||
{ | ||
if ( dirp && (dirp->hFile) ) { | ||
_findclose( dirp->hFile ); | ||
/* | ||
* Close the directory stream DIRP. | ||
* Return 0 if successful, -1 if not. | ||
*/ | ||
int | ||
closedir(DIR *dirp) { | ||
if ( dirp && dirp->hFile) { | ||
_findclose(dirp->hFile); | ||
dirp->hFile = 0; | ||
free (dirp); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have to free(dirp) irrespective of dirp->hFile.. so plz move this out of if loop.. |
||
} | ||
|
||
return 0; | ||
} | ||
|
||
/* Read a directory entry from DIRP. | ||
Return a pointer to a `struct dirent' describing the entry, | ||
or NULL for EOF or error. The storage returned may be overwritten | ||
by a later readdir call on the same DIR stream. */ | ||
struct dirent *readdir(void *avp) | ||
{ | ||
struct dirent *pdirentry; | ||
/* Read a directory entry from DIRP. */ | ||
struct dirent* | ||
readdir(void *avp) { | ||
struct dirent *pdirentry = NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plz update the code as I have modified this to static to avoid the memory leaks.. |
||
struct _wfinddata_t c_file; | ||
DIR *dirp = (DIR *)avp; | ||
char *tmp = NULL; | ||
|
||
for (;;) { | ||
if (dirp->first) { | ||
if (dirp->first) | ||
memcpy(&c_file, &dirp->c_file, sizeof(c_file)); | ||
dirp->first = 0; | ||
} | ||
else if (_wfindnext(dirp->hFile, &c_file) != 0) | ||
return NULL; | ||
|
||
dirp->first = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will execute everytime even though it is 0 so better to move it inside if loop at line 116. |
||
|
||
/* Skip . and .. */ | ||
if (wcscmp(c_file.name, L".") == 0 || wcscmp(c_file.name, L"..") == 0 ) | ||
continue; | ||
|
||
if ((pdirentry = malloc(sizeof(struct dirent))) == NULL || | ||
(tmp = utf16_to_utf8(c_file.name)) == NULL) { | ||
if (pdirentry) | ||
free(pdirentry); | ||
errno = ENOMEM; | ||
return NULL; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for line 134: strncpy(pdirentry->d_name, tmp, strlen(tmp) + 1); |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the NAME is capitalized?