From 16ad140604d0716e50f193c5b7b4a77c35bd4215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20R=C3=B6nkk=C3=B6?= Date: Mon, 18 Sep 2017 19:47:17 +0300 Subject: [PATCH] Fixes a compilation error with C++ This should fix issues #13 and #14 --- CMakeLists.txt | 17 +++--- include/dirent.h | 4 +- tests/t-cplusplus.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 tests/t-cplusplus.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 73446a8..c291105 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,8 @@ cmake_minimum_required (VERSION 2.8.11) -project (dirent LANGUAGES C) +project (dirent LANGUAGES C CXX) -# Initialize C compiler only (don't require C++ compiler) -enable_language (C) +# Initialize C and C++ compilers +enable_language (C CXX) # Compile in debug mode by default if (NOT CMAKE_BUILD_TYPE) @@ -13,14 +13,18 @@ if (NOT CMAKE_BUILD_TYPE) ) endif (NOT CMAKE_BUILD_TYPE) -# Only use the dirent file on windows systems +# Use the include directory only on Windows if (WIN32) include_directories (${CMAKE_SOURCE_DIR}/include) +endif (WIN32) + +# Install dirent.h +if (WIN32) install (FILES include/dirent.h DESTINATION include) -else() +else (WIN32) cmake_policy(SET CMP0037 OLD) # Supress warnings about fake install add_custom_target(install) # Fake install target -endif() +endif (WIN32) # Add distclean target add_custom_target (distclean @@ -47,4 +51,5 @@ endfunction (add_test_executable) add_test_executable (t-compile tests/t-compile.c) add_test_executable (t-dirent tests/t-dirent.c) add_test_executable (t-scandir tests/t-scandir.c) +add_test_executable (t-cplusplus tests/t-cplusplus.cpp) diff --git a/include/dirent.h b/include/dirent.h index 42b86d7..08916d4 100644 --- a/include/dirent.h +++ b/include/dirent.h @@ -914,7 +914,7 @@ scandir( p = realloc (files, sizeof (void*) * num_entries); if (p != NULL) { /* Got the memory */ - files = p; + files = (dirent**) p; allocated = num_entries; } else { /* Out of memory */ @@ -965,7 +965,7 @@ scandir( * End of directory stream reached => sort entries and * exit. */ - qsort (files, size, sizeof (void*), (void*) compare); + qsort (files, size, sizeof (void*), compare); break; } diff --git a/tests/t-cplusplus.cpp b/tests/t-cplusplus.cpp new file mode 100644 index 0000000..3b272ea --- /dev/null +++ b/tests/t-cplusplus.cpp @@ -0,0 +1,120 @@ +/* + * Test program to make sure that dirent compiles cleanly with C++ + * + * Copyright (C) 2006-2012 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#include +#include +#include +#include +using namespace std; + +int +main( + int argc, char *argv[]) +{ + (void) argc; + (void) argv; + + /* Basic directory retrieval */ + { + DIR *dir; + struct dirent *ent; + int found = 0; + + /* Open directory */ + dir = opendir ("tests/1"); + if (dir == NULL) { + cerr << "Directory tests/1 not found" << endl; + abort (); + } + + /* Read entries */ + while ((ent = readdir (dir)) != NULL) { + + /* Check each file */ + if (strcmp (ent->d_name, ".") == 0) { + /* Directory itself */ +#ifdef _DIRENT_HAVE_D_TYPE + assert (ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert (ent->d_namlen == 1); +#endif +#ifdef _D_EXACT_NAMLEN + assert (_D_EXACT_NAMLEN(ent) == 1); +#endif +#ifdef _D_ALLOC_NAMLEN + assert (_D_ALLOC_NAMLEN(ent) > 1); +#endif + found += 1; + + } else if (strcmp (ent->d_name, "..") == 0) { + /* Parent directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert (ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert (ent->d_namlen == 2); +#endif +#ifdef _D_EXACT_NAMLEN + assert (_D_EXACT_NAMLEN(ent) == 2); +#endif +#ifdef _D_ALLOC_NAMLEN + assert (_D_ALLOC_NAMLEN(ent) > 2); +#endif + found += 2; + + } else if (strcmp (ent->d_name, "file") == 0) { + /* Regular file */ +#ifdef _DIRENT_HAVE_D_TYPE + assert (ent->d_type == DT_REG); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert (ent->d_namlen == 4); +#endif +#ifdef _D_EXACT_NAMLEN + assert (_D_EXACT_NAMLEN(ent) == 4); +#endif +#ifdef _D_ALLOC_NAMLEN + assert (_D_ALLOC_NAMLEN(ent) > 4); +#endif + found += 4; + + } else if (strcmp (ent->d_name, "dir") == 0) { + /* Just a directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert (ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert (ent->d_namlen == 3); +#endif +#ifdef _D_EXACT_NAMLEN + assert (_D_EXACT_NAMLEN(ent) == 3); +#endif +#ifdef _D_ALLOC_NAMLEN + assert (_D_ALLOC_NAMLEN(ent) > 3); +#endif + found += 8; + + } else { + /* Other file */ + cerr << "Unexpected file " << ent->d_name << endl; + abort (); + } + + } + + /* Make sure that all files were found */ + assert (found == 0xf); + + closedir (dir); + } + + cout << "OK" << endl; + return EXIT_SUCCESS; +} +