Skip to content

Commit

Permalink
Fix tinydir_file_open with filename only failure in Windows (fixes #35)
Browse files Browse the repository at this point in the history
Implement file_open_test for Windows
  • Loading branch information
cxong committed Jul 17, 2016
1 parent fe19995 commit e33a1da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ add_subdirectory(cbehave)
if(MSVC)
add_definitions(-W4 -WX -wd"4127" -wd"4102" -wd"4996")
else()
add_definitions(-fsigned-char -Wall -W -Wshadow -Wpointer-arith -Wcast-qual -Winline -Werror -Wno-unused-label -Wno-unused-parameter)
add_definitions(-fsigned-char -Wall -W -Wshadow -Wpointer-arith -Wcast-qual -Winline -Werror -Wno-unused-label)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
Expand Down
2 changes: 1 addition & 1 deletion tests/cbehave/cbehave.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ if (!(cond)) {\
} while(0)

#define CBEHAVE_RUN(_description, ...)\
int main(int argc, char *argv[]) {\
int main() {\
cbehave_feature _cfeatures[] = {__VA_ARGS__};\
return cbehave_runner(_description, _cfeatures);\
}
Expand Down
28 changes: 23 additions & 5 deletions tests/file_open_test.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <tinydir.h>
#include "cbehave.h"

static void make_temp_file(const char *prefix, char *out)
{
#ifdef _MSC_VER
if (GetTempFileName(".", prefix, 0, out) != 0)
{
// Strip the ".\\" prefix
if (strncmp(out, ".\\", 2) == 0)
{
memmove(out, out + 2, strlen(out));
}
// Create file
fclose(fopen(out, "w"));
}
#else
#include <stdlib.h>
#include <unistd.h>
sprintf(out, "%sXXXXXX", prefix);
close(mkstemp(out));
#endif
}

FEATURE(file_open, "File open")
SCENARIO("Open file in current directory")
GIVEN("a file in the current directory")
char name[] = "fileXXXXXX";
int fd = mkstemp(name);
close(fd);
char name[4096];
make_temp_file("temp_file_", name);
WHEN("we open it")
tinydir_file file;
int r = tinydir_file_open(&file, name);
Expand Down
6 changes: 6 additions & 0 deletions tinydir.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,12 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path)
errno = EINVAL;
return -1;
}
/* Emulate the behavior of dirname by returning "." for dir name if it's
empty */
if (drive_buf[0] == '\0' && dir_name_buf[0] == '\0')
{
strcpy(dir_name_buf, ".");
}
/* Concatenate the drive letter and dir name to form full dir name */
_tinydir_strcat(drive_buf, dir_name_buf);
dir_name = drive_buf;
Expand Down

0 comments on commit e33a1da

Please sign in to comment.