Skip to content

Commit 16ad140

Browse files
author
Toni Rönkkö
committed
Fixes a compilation error with C++
This should fix issues #13 and #14
1 parent 0406864 commit 16ad140

File tree

3 files changed

+133
-8
lines changed

3 files changed

+133
-8
lines changed

CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cmake_minimum_required (VERSION 2.8.11)
2-
project (dirent LANGUAGES C)
2+
project (dirent LANGUAGES C CXX)
33

4-
# Initialize C compiler only (don't require C++ compiler)
5-
enable_language (C)
4+
# Initialize C and C++ compilers
5+
enable_language (C CXX)
66

77
# Compile in debug mode by default
88
if (NOT CMAKE_BUILD_TYPE)
@@ -13,14 +13,18 @@ if (NOT CMAKE_BUILD_TYPE)
1313
)
1414
endif (NOT CMAKE_BUILD_TYPE)
1515

16-
# Only use the dirent file on windows systems
16+
# Use the include directory only on Windows
1717
if (WIN32)
1818
include_directories (${CMAKE_SOURCE_DIR}/include)
19+
endif (WIN32)
20+
21+
# Install dirent.h
22+
if (WIN32)
1923
install (FILES include/dirent.h DESTINATION include)
20-
else()
24+
else (WIN32)
2125
cmake_policy(SET CMP0037 OLD) # Supress warnings about fake install
2226
add_custom_target(install) # Fake install target
23-
endif()
27+
endif (WIN32)
2428

2529
# Add distclean target
2630
add_custom_target (distclean
@@ -47,4 +51,5 @@ endfunction (add_test_executable)
4751
add_test_executable (t-compile tests/t-compile.c)
4852
add_test_executable (t-dirent tests/t-dirent.c)
4953
add_test_executable (t-scandir tests/t-scandir.c)
54+
add_test_executable (t-cplusplus tests/t-cplusplus.cpp)
5055

include/dirent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ scandir(
914914
p = realloc (files, sizeof (void*) * num_entries);
915915
if (p != NULL) {
916916
/* Got the memory */
917-
files = p;
917+
files = (dirent**) p;
918918
allocated = num_entries;
919919
} else {
920920
/* Out of memory */
@@ -965,7 +965,7 @@ scandir(
965965
* End of directory stream reached => sort entries and
966966
* exit.
967967
*/
968-
qsort (files, size, sizeof (void*), (void*) compare);
968+
qsort (files, size, sizeof (void*), compare);
969969
break;
970970

971971
}

tests/t-cplusplus.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Test program to make sure that dirent compiles cleanly with C++
3+
*
4+
* Copyright (C) 2006-2012 Toni Ronkko
5+
* This file is part of dirent. Dirent may be freely distributed
6+
* under the MIT license. For all details and documentation, see
7+
* https://github.com/tronkko/dirent
8+
*/
9+
#include <iostream>
10+
#include <string.h>
11+
#include <dirent.h>
12+
#include <assert.h>
13+
using namespace std;
14+
15+
int
16+
main(
17+
int argc, char *argv[])
18+
{
19+
(void) argc;
20+
(void) argv;
21+
22+
/* Basic directory retrieval */
23+
{
24+
DIR *dir;
25+
struct dirent *ent;
26+
int found = 0;
27+
28+
/* Open directory */
29+
dir = opendir ("tests/1");
30+
if (dir == NULL) {
31+
cerr << "Directory tests/1 not found" << endl;
32+
abort ();
33+
}
34+
35+
/* Read entries */
36+
while ((ent = readdir (dir)) != NULL) {
37+
38+
/* Check each file */
39+
if (strcmp (ent->d_name, ".") == 0) {
40+
/* Directory itself */
41+
#ifdef _DIRENT_HAVE_D_TYPE
42+
assert (ent->d_type == DT_DIR);
43+
#endif
44+
#ifdef _DIRENT_HAVE_D_NAMLEN
45+
assert (ent->d_namlen == 1);
46+
#endif
47+
#ifdef _D_EXACT_NAMLEN
48+
assert (_D_EXACT_NAMLEN(ent) == 1);
49+
#endif
50+
#ifdef _D_ALLOC_NAMLEN
51+
assert (_D_ALLOC_NAMLEN(ent) > 1);
52+
#endif
53+
found += 1;
54+
55+
} else if (strcmp (ent->d_name, "..") == 0) {
56+
/* Parent directory */
57+
#ifdef _DIRENT_HAVE_D_TYPE
58+
assert (ent->d_type == DT_DIR);
59+
#endif
60+
#ifdef _DIRENT_HAVE_D_NAMLEN
61+
assert (ent->d_namlen == 2);
62+
#endif
63+
#ifdef _D_EXACT_NAMLEN
64+
assert (_D_EXACT_NAMLEN(ent) == 2);
65+
#endif
66+
#ifdef _D_ALLOC_NAMLEN
67+
assert (_D_ALLOC_NAMLEN(ent) > 2);
68+
#endif
69+
found += 2;
70+
71+
} else if (strcmp (ent->d_name, "file") == 0) {
72+
/* Regular file */
73+
#ifdef _DIRENT_HAVE_D_TYPE
74+
assert (ent->d_type == DT_REG);
75+
#endif
76+
#ifdef _DIRENT_HAVE_D_NAMLEN
77+
assert (ent->d_namlen == 4);
78+
#endif
79+
#ifdef _D_EXACT_NAMLEN
80+
assert (_D_EXACT_NAMLEN(ent) == 4);
81+
#endif
82+
#ifdef _D_ALLOC_NAMLEN
83+
assert (_D_ALLOC_NAMLEN(ent) > 4);
84+
#endif
85+
found += 4;
86+
87+
} else if (strcmp (ent->d_name, "dir") == 0) {
88+
/* Just a directory */
89+
#ifdef _DIRENT_HAVE_D_TYPE
90+
assert (ent->d_type == DT_DIR);
91+
#endif
92+
#ifdef _DIRENT_HAVE_D_NAMLEN
93+
assert (ent->d_namlen == 3);
94+
#endif
95+
#ifdef _D_EXACT_NAMLEN
96+
assert (_D_EXACT_NAMLEN(ent) == 3);
97+
#endif
98+
#ifdef _D_ALLOC_NAMLEN
99+
assert (_D_ALLOC_NAMLEN(ent) > 3);
100+
#endif
101+
found += 8;
102+
103+
} else {
104+
/* Other file */
105+
cerr << "Unexpected file " << ent->d_name << endl;
106+
abort ();
107+
}
108+
109+
}
110+
111+
/* Make sure that all files were found */
112+
assert (found == 0xf);
113+
114+
closedir (dir);
115+
}
116+
117+
cout << "OK" << endl;
118+
return EXIT_SUCCESS;
119+
}
120+

0 commit comments

Comments
 (0)