Giter Site home page Giter Site logo

tinydir's Introduction

TinyDir

CMake Release

Lightweight, portable and easy to integrate C directory and file reader. TinyDir wraps dirent for POSIX and FindFirstFile for Windows.

Windows unicode is supported by defining UNICODE and _UNICODE before including tinydir.h.

Example

There are two methods. Error checking omitted:

tinydir_dir dir;
tinydir_open(&dir, "/path/to/dir");

while (dir.has_next)
{
	tinydir_file file;
	tinydir_readfile(&dir, &file);

	printf("%s", file.name);
	if (file.is_dir)
	{
		printf("/");
	}
	printf("\n");

	tinydir_next(&dir);
}

tinydir_close(&dir);
tinydir_dir dir;
int i;
tinydir_open_sorted(&dir, "/path/to/dir");

for (i = 0; i < dir.n_files; i++)
{
	tinydir_file file;
	tinydir_readfile_n(&dir, &file, i);

	printf("%s", file.name);
	if (file.is_dir)
	{
		printf("/");
	}
	printf("\n");
}

tinydir_close(&dir);

See the /samples folder for more examples, including an interactive command-line directory navigator.

Language

ANSI C, or C90.

Platforms

POSIX and Windows supported. Open to the possibility of supporting other platforms.

License

Simplified BSD; if you use tinydir you can comply by including tinydir.h or COPYING somewhere in your package.

Known Limitations

tinydir's People

Contributors

amdg2 avatar andargor avatar besser82 avatar bitdeli-chef avatar cg505 avatar cosmy1 avatar cxong avatar czipperz avatar dariomanesku avatar gabeni avatar gfdevelop avatar isty001 avatar lautis0503 avatar praihan avatar sergw avatar wjwwood avatar wojdyr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tinydir's Issues

Compilation error with MSVC 14 x86

UNICODE is defined in my project. But I get the following errors.

E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(263): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(263): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(268): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(268): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(286): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(286): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(288): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(288): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(295): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(295): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(296): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(296): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(497): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(497): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(504): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const WCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(504): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(517): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const WCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(517): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(523): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(523): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(524): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(524): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(531): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(531): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(532): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(532): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(609): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(609): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(635): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(635): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(640): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(640): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(654): error C2664: 'errno_t _splitpath_s(const char *,char *,std::size_t,char *,std::size_t,char *,std::size_t,char *,std::size_t)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(654): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(681): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(681): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(684): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [3]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(684): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(687): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(687): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(710): error C2664: 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(710): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(732): error C2665: 'strrchr': none of the 2 overloads could convert all the argument types
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(732): note: while trying to match the argument list '(TCHAR [256], char)'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(735): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(735): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(752): error C2664: 'int strncmp(const char *,const char *,std::size_t)': cannot convert argument 1 from 'const TCHAR [256]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(752): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\string.h(529): note: could be 'char *strrchr(char *const ,const int)'
D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vcruntime_string.h(73): note: or       'const char *strrchr(const char *,int)'

tinydir_file_open crashes with debug msvcr

Hi,

The following code crashes when compiled on MSVS2013 with debug version of C runtime lib:

#define UNICODE
#define _UNICODE
#include "tinydir.h"

int main() {
    tinydir_file fi;
    tinydir_file_open(&fi, L".");
    (void) fi;
    return 0;
}

Compilation command:

>cl test.c /MDd
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.40629 for x86

I can see that something is wrong with this call -

_tsplitpath_s(
but it is not clear what is wrong.

MSVC does not appreciate mixing widechar and char

Samples did not work till I modified tinydir_open() and tinydir_readfile() for msvc120 (2012?).
Got always the same error "could not open file/directory"n tried with different path, etc.
The string passed originaly have to be converted to widechar.
And for tinydir_readfile() some widechar string had to be converted back to char, just to resolve some warning.

Thanks for this awesome lil' lib.

see modifs here: http://pastie.org/9488270 (sorry I'm not that familiar with github, so I don't know if they are any easier way to get and compare directly both files.)

file.exension not working?

my code gets the file name fine but fails to get the extension

Heres my code:
void get_files(std::string fpath, std::string extension, stringVec &vec)
{

tinydir_dir dir;

if (fpath.back() != '/')
    fpath += "/";

tinydir_open_sorted(&dir, fpath.c_str());

for (unsigned i = 0; i < dir.n_files; i++)
{
    tinydir_file file;
    tinydir_readfile_n(&dir, &file, i);

    if (file.is_dir)
    {
        if (strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0)
        {
            get_files(fpath + file.name + "/", extension, vec);
        }
    }
    else
    {
        std::cout << "File:" << std::endl;
        std::cout << file.name << std::endl;
        std::cout << "Extension:" << std::endl;
        std::cout << file.extension << std::endl;
        if (strcmp(file.extension, extension.c_str()) == 1)
        { 
            std::string resource = fpath + file.name;
            vec.push_back(resource);
        }
    }
}

tinydir_close(&dir);

}

Heres the output:

File:
obj_cheeseboy.hpp
Extension:

File:
room_1.xml
Extension:

File:
sprite_1.xml
Extension:
eboy.hpp

somehow it misses all the extensions and gives me garbage for last one?

Add Github Actions CI

Set up a build matrix so we can support multiple platforms/compilers, including: linux, macOS, gcc, clang

tinydir_readfile returns -1 when file is a broken link

I.e. a file, which you could create with

ln -s /broken/link .

In that case iterate_sample.c shows directory listing only partially. And random_access_sample.c fails to open a directory at line 8.

tinydir_readfile finishes at line 559 when processing a broken link

My OS is Linux.

Add Windows/Appveyor job

We have Travis CI already, building the samples and tests and running the latter. Do the same using Appveyor, which does so under Windows.

Open file from path

Would be nice to have a function that returns a tinydir_file from a path.

int tinydir_file_open(tinydir_file *file, const char *path);

Compilation fails with -std=c99

/home/cxu/test/tinydir/samples/../tinydir.h: In function ‘tinydir_open’:
/home/cxu/test/tinydir/samples/../tinydir.h:212:10: error: implicit declaration of function ‘readdir_r’ [-Werror=implicit-function-declaration]
  error = readdir_r(dir->_d, dir->_ep, &dir->_e);
          ^
/home/cxu/test/tinydir/samples/../tinydir.h: In function ‘_tinydir_dirent_buf_size’:
/home/cxu/test/tinydir/samples/../tinydir.h:654:13: error: #error "buffer size for readdir_r cannot be determined"
 #           error "buffer size for readdir_r cannot be determined"
             ^
cc1: all warnings being treated as errors
make[2]: *** [CMakeFiles/file_open_sample.dir/file_open_sample.c.o] Error 1
make[1]: *** [CMakeFiles/file_open_sample.dir/all] Error 2
make: *** [all] Error 2

In particular, the _tinydir_dirent_buf_size function needs fixing as it is relying on preprocessor flags that haven't been defined like HAVE_FPATHCONF.

Question: How to recursively store all files into a vector?

I'm trying to recursively search a folder for files and then store them into a vector for later use but my vector is coming up empty. if I cout resources inside the else it does list files though. Any ideas what I'm doing wrong?

include "tinydir.h"

include

include

include

typedef std::vectorstd::string fpathVec;
fpathVec sprites, rooms;

void get_files(std::string fpath, fpathVec vec)
{

tinydir_dir dir;
int i;

if (fpath.back() != '/')
    fpath += "/";

tinydir_open_sorted(&dir, fpath.c_str());

for (i = 0; i < dir.n_files; i++)
{
    tinydir_file file;
    tinydir_readfile_n(&dir, &file, i);

    if (file.is_dir)
    {
        if (strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0)
        {
            get_files(fpath + file.name + "/", vec);
        }
    }
    else
    {
        std::string resource = fpath + file.name;
        vec.push_back(resource);
    }
}

tinydir_close(&dir);

}

int main()
{
get_files("../../gaym", sprites);
std::cout << sprites.size() << std::endl;
}

Fix licensing

The COPYING file contains a reference to FreeBSD
The entire BSD license can be added to tinydir.h

Use rewinddir to count number of files

In tinydir_open_sorted, a first pass is used to count the number of files, then a second pass actually reads the file info. In between the two passes, tinydir closes and reopens the dir.

This is unnecessary with dirent; there's the rewinddir function which allows going back to the first file. This would also eliminate a race condition where the directory may have changed between the two passes.

Not sure if there's an equivalent for Windows.

_MSC_VER is the wrong macro to check for Windows support

The _MSC_VER macro is used to detect Windows mode. This is the wrong macro to use. That macro detects which compiler is in use, not which platform is being targeted. Both GCC and Clang can create Windows application, as well as a number of other lesser known compilers. The correct macro to check for is _WIN32 which is enabled for any application which has the Win32 API available to it (even in 64-bit mode).

The current macro usage means that Windows support is only enabled specifically when using Microsoft's compiler.

Some compilers may make both Windows and POSIX APIs available, e.g. CygWin compiles. It may be useful to have a feature macro (like a TINYDIR_USE_WIN32 and TINYDIR_USE_POSIX or something) that explicitly forces tinydir to use one API or the other and to default to something sensible. Good defaults may well be to use the Win32 API if _WIN32 is defined and POSIX otherwise.

Compile error with newer glibc version: readdir_r is deprecated

Compiling with newer glibc versions will not work as readdir_r is deprecated.
It will lead into this errors:

tinydir/tinydir.h:241:2: error: 'readdir_r' is deprecated [-Werror=deprecated-declarations]
error = readdir_r(dir->_d, dir->_ep, &dir->_e);
^~~~~
tinydir/tinydir.h:377:2: error: 'readdir_r' is deprecated [-Werror=deprecated-declarations]
if (readdir_r(dir->_d, dir->_ep, &dir->_e) != 0)
^~

It is recommended to use readdir instead of readdir_r and use external locking (e.g. implement a simple mutex) until a new POSIX version is release which requires readdir to be thread-safe.
See also the "Portability Note" of readdir_r in [2].
The OLA project encountered the same issue [0], for reasons why to use readdir instead of readdir_r see also [1].

Some references:
[0] OpenLightingProject/ola#1055
[1] https://womble.decadent.org.uk/readdir_r-advisory.html
[2] https://www.gnu.org/software/libc/manual/html_node/Reading_002fClosing-Directory.html

Force use of ANSI on Windows

As I'm currently working on a project which needs to run under Windows and Linux simultaneously and I need to use ANSI coding, under MSVC 14 on Windows, I had problems to compile it, as everything was apparently targetted towards Unicode.

I changed line at 71 to
#if (defined _MSC_VER || defined __MINGW32__) && defined UNICODE

and added this after line 89

#if (defined _MSC_VER || defined __MINGW32__) && !defined UNICODE
#undef FindFirstFile
#define FindFirstFile FindFirstFileA
#undef FindNextFile
#define FindNextFile FindNextFileA
#define WIN32_FIND_DATA WIN32_FIND_DATAA
#define _tsplitpath_s _splitpath_s
#define _tsplitpath _splitpath
#endif

Don't know if there is a better workaround or if this is a good approach, just sharing :)

tinydir_open_sorted is really slow under windows

I have a folder of 8000 images in it. If I call tinydir_open_sorted on it's path, it will take as long as 2 minutes to retrieve the full list of file. I am under MSVC 2010 under windows 7 x64. Profiling shows 99.9% of the time spent in an unknown method. If I use tinydir_open instead and I sort the file myself using a vector and std::sort, the result will take much less than a second to return.

I think the problem is with the use of realloc, that must constantly allocate new block of memory compare to a std::vector that might handle the reallocation much better.

Here's the C++ version that run much faster (to compare with tinydir_open_sorted)
std::vectorstd::string dirList
tinydir_open(&dir, dirPath.c_str());
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if(!file.is_dir)
dirList.push_back(std::string(file.name));
tinydir_next(&dir);
}

tinydir_close(&dir);
std::sort(dirList.begin(), dirList.end());
return dirList;

Cannot open drive root on Windows

Hello,

I'm struggling to open the top directory on Windows (C:). Other directories are fine.
For instance, when using file_open_sample.c I get:

C:\msys32\home\IEUser\tinydir>a.exe C:\
Error opening file: No error

C:\msys32\home\IEUser\tinydir>a.exe C:
Error opening file: No error

C:\msys32\home\IEUser\tinydir>a.exe "C:\Program Files"
Path: C:/Program Files
Name: Program Files
Extension:
Is dir? yes
Is regular file? no

i used MinGW here, but I think it's the same with MSVC.
Am I missing something?

clang-tidy warning (tinydir.h:366) Call to 'malloc' has an allocation size of 0

It's about a clang-tidy warning. It can be reproduced on tinydir samples, see below. I didn't analyze the flow so I'm not sure if it's a false positive. If it is a false positive, would it be ok to add a NOLINT comment to silence it?

$ /local/clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin/clang-tidy samples/random_access_sample.c -- -I.
9 warnings generated.
./tinydir.h:366:32: warning: Call to 'malloc' has an allocation size of 0 bytes [clang-analyzer-unix.API]
        dir->_files = (tinydir_file *)_TINYDIR_MALLOC(sizeof *dir->_files * n_files);
                                      ^
./tinydir.h:175:33: note: expanded from macro '_TINYDIR_MALLOC'
        #define _TINYDIR_MALLOC(_size) malloc(_size)
                                       ^
/home/wojdyr/fresh/tinydir/samples/random_access_sample.c:8:32: note: Assuming 'argc' is < 2
        if (tinydir_open_sorted(&dir, argc >= 2 ? argv[1] : ".") == -1)
                                      ^
/home/wojdyr/fresh/tinydir/samples/random_access_sample.c:8:32: note: '?' condition is false
/home/wojdyr/fresh/tinydir/samples/random_access_sample.c:8:6: note: Calling 'tinydir_open_sorted'
        if (tinydir_open_sorted(&dir, argc >= 2 ? argv[1] : ".") == -1)
            ^
./tinydir.h:346:2: note: Taking false branch
        if (tinydir_open(dir, path) == -1)
        ^
./tinydir.h:350:2: note: Loop condition is false. Execution continues on line 358
        while (dir->has_next)
        ^
./tinydir.h:360:2: note: Taking false branch
        if (tinydir_open(dir, path) == -1)
        ^
./tinydir.h:366:32: note: Call to 'malloc' has an allocation size of 0 bytes
        dir->_files = (tinydir_file *)_TINYDIR_MALLOC(sizeof *dir->_files * n_files);
                                      ^
./tinydir.h:175:33: note: expanded from macro '_TINYDIR_MALLOC'
        #define _TINYDIR_MALLOC(_size) malloc(_size)

MSVC and tinydir_file_open

There is an issue in tinydir_file_open when compiled with MSVC.

At the beginning of the function, there is following declaration:

_tinydir_char_t drive_buf[_TINYDIR_DRIVE_MAX];

_TINYDIR_DRIVE_MAX is defined as 3

Later on, there is following line:

_tinydir_strcat(drive_buf, dir_name_buf);

which always overflows the buffer.

Quick solution is to define drive_buf as

_tinydir_char_t drive_buf[_TINYDIR_PATH_MAX];

Reporting security vulnerabilities

Hi,

What's the preferred way to report potential security vulnerabilities in tinydir? Should I open an issue here on GitHub or is there another channel?

Thanks!

GCC11 warnings

In our repository idaholab/moose, we are getting the following warnings when compiling with gcc 11

In file included from /usr/include/string.h:519,
                 from /usr/include/c++/11/cstring:42,
                 from /home/lindad/projects/moose/libmesh/installed/include/Eigen/Core:286,
                 from /home/lindad/projects/moose/libmesh/installed/include/libmesh/type_vector.h:35,
                 from /home/lindad/projects/moose/libmesh/installed/include/libmesh/point.h:24,
                 from /home/lindad/projects/moose/libmesh/installed/include/libmesh/bounding_box.h:25,
                 from /home/lindad/projects/moose/framework/build/header_symlinks/MooseUtils.h:22,
                 from /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:11:
In function ‘char* strncat(char*, const char*, size_t)’,
    inlined from ‘int tinydir_readfile(const tinydir_dir*, tinydir_file*)’ at /home/lindad/projects/moose/framework/contrib/tinydir/include/tinydir.h:334:10,
    inlined from ‘std::__cxx11::list<std::__cxx11::basic_string<char> > MooseUtils::listDir(std::string, bool)’ at /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:701:21:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:135:34: warning: ‘char* __builtin___strncat_chk(char*, const char*, long unsigned int, long unsigned int)’ accessing between 3 and 1 bytes at offsets 0 and 4096 overlaps 1 byte at offset 4096 [-Wrestrict]
  135 |   return __builtin___strncat_chk (__dest, __src, __len,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
  136 |                                   __glibc_objsize (__dest));
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:24:
In function ‘void tinydir_close(tinydir_dir*)’,
    inlined from ‘void tinydir_close(tinydir_dir*)’ at /home/lindad/projects/moose/framework/contrib/tinydir/include/tinydir.h:215:6,
    inlined from ‘std::__cxx11::list<std::__cxx11::basic_string<char> > MooseUtils::listDir(std::string, bool)’ at /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:709:16:
/home/lindad/projects/moose/framework/contrib/tinydir/include/tinydir.h:225:18: warning: ‘dir.tinydir_dir::_files’ may be used uninitialized [-Wmaybe-uninitialized]
  225 |         if (dir->_files != NULL)
      |             ~~~~~^~~~~~
/home/lindad/projects/moose/framework/src/utils/MooseUtils.C: In function ‘std::__cxx11::list<std::__cxx11::basic_string<char> > MooseUtils::listDir(std::string, bool)’:
/home/lindad/projects/moose/framework/src/utils/MooseUtils.C:693:15: note: ‘dir’ declared here
  693 |   tinydir_dir dir;

Is this user error or is this something that can be fixed on your end? This is our user code

std::list<std::string>
listDir(const std::string path, bool files_only)
{
  std::list<std::string> files;

  tinydir_dir dir;
  dir.has_next = 0; // Avoid a garbage value in has_next (clang StaticAnalysis)                                       
  tinydir_open(&dir, path.c_str());

  while (dir.has_next)
  {
    tinydir_file file;
    file.is_dir = 0; // Avoid a garbage value in is_dir (clang StaticAnalysis)                                        
    tinydir_readfile(&dir, &file);

    if (!files_only || !file.is_dir)
      files.push_back(path + "/" + file.name);

    tinydir_next(&dir);
  }

  tinydir_close(&dir);

  return files;
}

Fix implementation of _tinydir_dirent_buf_size

The implementation of _tinydir_dirent_buf_size is lifted from an advisory on readdir_r, and uses lots of ifdefs to make it work across a large variety of platforms. The problem is that some of those ifdefs are using unimplemented preprocessor definitions like HAVE_FPATHCONF. These can work under an autoconf project but for tinydir, a different method must be used.

It may be possible to replace these with various feature test macros.

API Reference

A document that describes the public functions, parameters, return values etc.

Doesn't work with MinGW-w64

It looks like readdir_r is not supported by MinGW-w64 distributions. When I try to build the samples on my MinGW Builds 5.3.0 compiler, I get the following errors.

In file included from tinydir\samples\file_open_sample.c:2:0:
tinydir/tinydir.h: In function 'tinydir_open':
tinydir/tinydir.h:194:10: error: implicit declaration of function 'readdir_r' [-Werror=implicit-function-declaration]
  error = readdir_r(dir->_d, dir->_ep, &dir->_e);
          ^
tinydir/tinydir.h: In function '_tinydir_dirent_buf_size':
tinydir/tinydir.h:629:13: error: #error "buffer size for readdir_r cannot be determined"
 #           error "buffer size for readdir_r cannot be determined"
             ^

Is there an alternative?

About how to use it in vs2010 under win7

Hello, the lib is fantastic.
I am a newer in C language and I wonder how to implement it. Should I just include the header file tinydir.h ? But when I do this I cannot run the samples because there are many errors about strict type transforms. Should I do something more? Thank you.

PATH_MAX is not defined on linux when building with `-std=c99`

Actually documentation says that it can be undefined if no real limit is applicable.

Would this be OK?:

diff --git a/tinydir.h b/tinydir.h
index 324f9f6..7863564 100644
--- a/tinydir.h
+++ b/tinydir.h
@@ -89,7 +89,9 @@ extern "C" {
 # define _TINYDIR_PATH_MAX MAX_PATH
 #elif defined  __linux__
 # include <limits.h>
-# define _TINYDIR_PATH_MAX PATH_MAX
+# ifdef PATH_MAX
+#  define _TINYDIR_PATH_MAX PATH_MAX
+# endif
 #elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
 # include <sys/param.h>
 # if defined(BSD)

Fix small bug

Tinydir disable pragma warning in all file that include this header, I have fixed the bug, update the repo:
before the line 41 add this:

# pragma warning(push)

before the last line ( last #endif ) add this;

#ifdef _MSC_VER
# pragma warning(pop)
#endif

Then pragma warning working in all files except tinydir.

Include msys2

Sorry, I was wrong repo.
(Good job, this is a fantastic work)

Not all Linux systems store PATH_MAX in linux/limits.h

I found this out while trying to compile on Alpine Linux, and I'm aware of a number of other distros like it. Systems that conform to POSIX (can't seem to find a clear reference to which standard) will keep it in the standard C limits.h - see comments here. The GNU docs also have some helpful notes on portability.

It's also worth noting that some implementations may have PATH_MAX include null-termination, and others may not. Not sure how you'd want to resolve that.

A more portable way to resolve this issue on POSIX 2001+ systems is to use pathconf as noted here.

Fails on UNICODE builds on Windows

Compiling with the UNICODE macro (which is required by most other well-behaving Windows libraries) currently fails.

If you want to be ASCII-only then you must explicitly call the *A versions of functions, e.g. FindFirstFileA instead of FindFirstFile. The unadorned versions are macros that expand to either FindFirstFileW or FindFirstFileA depending on whether _UNICODE is in use or not.

A better bet would be to just be compatible with _UNICODE as there are some places where you need Windows' native UTF16 paths and can't use the ASCII variants. To do this you need to use TCHAR instead of char for your strings and buffers and use the the TEXT macro to wrap all your literals.

Yeah, Windows is lame for not just being UTF8 everywhere, but it is what it is. Recommended practice for Win32 developers is to use UTF8 internally for all strings, enable _UNICODE to catch mistakes like this one with Win32 calls, to always explicitly call the *W variants of functions (in case a user turns off _UNICODE), and then do any translation to and from UTF16/UTF8 at the boundaries where Win32 OS calls are made.

For a library like this one, I think the best option would be to just support both modes using the macros described above.

Errors received with latest tinydir when using _UNICODE:

1>C:\Users\Sean\Documents\Projects\tinydir/tinydir.h(142): error C2664: 'HANDLE FindFirstFileW(LPCWSTR,LPWIN32_FIND_DATAW)' : cannot convert argument 1 from 'char [4096]' to 'LPCWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\Users\Sean\Documents\Projects\tinydir/tinydir.h(327): error C2664: 'size_t strlen(const char *)' : cannot convert argument 1 from 'const WCHAR [260]' to 'const char *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\Users\Sean\Documents\Projects\tinydir/tinydir.h(340): error C2664: 'size_t strlen(const char *)' : cannot convert argument 1 from 'const WCHAR [260]' to 'const char *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\Users\Sean\Documents\Projects\tinydir/tinydir.h(354): error C2664: 'char *strcpy(char *,const char *)' : cannot convert argument 2 from 'const WCHAR [260]' to 'const char *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.