diff --git a/.gitignore b/.gitignore index e69de29..6646021 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,23 @@ +*.vcproj +CMakeCache.txt +CMakeFiles +CTestTestfile.cmake +DartConfiguration.tcl +cmake_install.cmake +Testing +Debug +Release +dirent.sln +dirent.ncb +dirent.suo +*.dir +locate.db +Makefile +t-compile +t-dirent +find +ls +locate +updatedb +*.user + diff --git a/CMakeLists.txt b/CMakeLists.txt index 316f83d..fe8fb38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,19 +1,33 @@ -cmake_minimum_required (VERSION 2.8) +cmake_minimum_required (VERSION 2.8.11) project (dirent) -enable_language(C) +enable_language (C) -install(FILES include/dirent.h DESTINATION include) +install (FILES include/dirent.h DESTINATION include) +# Add include directory to path if current system omits dirent.h file. This +# step is imporant for compiling the test programs under Linux. +include (CheckIncludeFiles) +check_include_files (dirent.h HAVE_DIRENT_H) +if (NOT HAVE_DIRENT_H) + include_directories (${CMAKE_SOURCE_DIR}/include) +endif (NOT HAVE_DIRENT_H) -include(CTest) -add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C ${CMAKE_CFG_INTDIR}) -function(add_test_executable TEST_NAME) +# Build example programs +add_executable (find examples/find.c) +add_executable (ls examples/ls.c) +add_executable (locate examples/locate.c) +add_executable (updatedb examples/updatedb.c) + +# Build test programs +include (CTest) +add_custom_target (check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C ${CMAKE_CFG_INTDIR}) +function (add_test_executable TEST_NAME) add_executable (${TEST_NAME} EXCLUDE_FROM_ALL ${ARGN}) - add_test(NAME ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND $) - add_dependencies(check ${TEST_NAME}) - target_include_directories(${TEST_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/include) -endfunction(add_test_executable) + add_test (NAME ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND $) + add_dependencies (check ${TEST_NAME}) +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-compile tests/t-compile.c) -add_test_executable(t-dirent tests/t-dirent.c) diff --git a/ChangeLog b/ChangeLog index f1af4a6..a1e4bb7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,8 @@ -2015-09-25 Toni Rönkkö +2016-09-11 Toni Rönkkö + + * Version 1.22: added support for CMake. Thanks to Paul Fultz II. + +2014-09-25 Toni Rönkkö * Version 1.21: compiles correctly under Open Watcom. Thanks to Virgil Banowetz for a patch! diff --git a/Makefile.linux b/Makefile.linux index b4b5dc2..287eb69 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -1,14 +1,14 @@ -# This Linux Makefile will pack dirent into a distributable zip +# This Makefile will pack dirent into a distributable zip # package. To build the package, invoke this Makefile as # -# make dist +# make -f Makefile.linux dist # # Current cersion number -VERSION=1.21 +VERSION=1.22 # List of files to include in zip package -DIST_FILES = include/*.h examples/*.c tests/*.c tests/1/file tests/1/dir/* tests/2/* vs2008/vs2008.sln vs2008/ls/ls.vcproj vs2008/find/find.vcproj vs2008/updatedb/updatedb.vcproj vs2008/locate/locate.vcproj vs2008/t-dirent/t-dirent.vcproj ChangeLog +DIST_FILES = include/*.h examples/*.c tests/*.c tests/1/file tests/1/dir/* tests/2/* CMakeLists.txt ChangeLog README.md # Default target all: ls find @@ -34,19 +34,22 @@ help: @echo " dist Build distributable package" @echo " check Run regression tests" -# Wipe out temporary files left behind by MS-Visual Studio +# Wipe out temporary files left behind by MS-Visual Studio and CMake clean: - rm -f ls find t-dirent - rm -f vs2008/*.suo vs2008/*.ncb - rm -f vs2008/*/*.user - rm -fr vs2008/Debug vs2008/*/Debug - rm -fr vs2008/Release vs2008/*/Release + rm -f ls find t-dirent t-compile locate updatedb + rm -fr Debug Release Testing + rm -fr *.dir + rm -f *.vcproj *.user + rm -fr CMakeFiles + rm -f CMakeCache.txt CTestTestfile.cmake cmake_install.cmake + rm -f dirent.ncb dirent.sln dirent.suo + rm -f DartConfiguration.tcl locate.db Makefile rm -fr dirent-$(VERSION) rm -f dirent-*.zip # Build installation package dist: dirent-$(VERSION).zip -dirent-$(VERSION).zip: $(DIST_FILES) Makefile +dirent-$(VERSION).zip: $(DIST_FILES) rm -f dirent-$(VERSION).zip rm -fr dirent-$(VERSION) mkdir dirent-$(VERSION) diff --git a/README.md b/README.md index f35013b..8ba3703 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,7 @@ # Dirent -Dirent is a C/C++ programming interface that allows programs to retrieve -information on files and directories residing on a hard disk. Dirent is -commonly found in Linux and other UNIX operating systems. - -This project provides Linux-compatible Dirent interface for Microsoft Visual -Studio and Open Watcom to be used on Microsoft Windows. +Dirent is a C/C++ programming interface that allows programmers to retrieve +information about files and directories under Linux/UNIX. This project +provides Linux compatible Dirent interface for Microsoft Windows. # Installation @@ -13,46 +10,55 @@ Download the latest Dirent installation package from [softagalleria.net](http://softagalleria.net/download/dirent/?C=M;O=D) Unpack the installation file with 7-zip, for example. The installation -package contains dirent.h file as well as a few example programs and a -Visual Studio 2008 project for compiling them. You will definitely need the -``include/dirent.h`` file. +package contains dirent.h file as well as a few example programs. ## Install Dirent for All Programs -To make dirent.h available for all programs, copy the ``include/dirent.h`` -file from the installation package to system include directory. System -include directory contains header files such as assert.h and windows.h. In -Visual Studio 2008, for example, the system include directory may be found at +To make dirent.h available for all C/C++ programs, simply copy the +``include/dirent.h`` file to the system include directory. System include +directory contains header files such as assert.h and windows.h. In Visual +Studio 2008, for example, the system include may be found at ``C:\Program Files\Microsoft Visual Studio 9.0\VC\include``. -Everything you need is included in the single dirent.h file. Thus, you can +Everything you need is included in the single dirent.h file, and you can start using Dirent immediately -- there is no need to add files to your Visual Studio project. -## Embed Dirent into Your Own Program +## Embed Dirent into Your Own Project + +If you wish to distribute dirent.h alongside with your own source code, then +copy ``include/dirent.h`` file to a new sub-directory within your project and +add that directory to include path on Windows while omitting the directory +under Linux/UNIX. This allows your project to be compiled against native +dirent.h on Linux/UNIX while substituting the functionality on Microsoft +Windows. + + +## Building Example Programs -As an alternative to the above installation method, you can also embed -dirent.h into your own program -- just copy the ``include/dirent.h`` file to -a suitable include directory in your project. However, if you do so, please -make sure that you include dirent.h by +The installation package contains some example programs and tests under +the directories examples and tests. To run these programs, install +[CMake](https://cmake.org/) + +Open command prompt, navigate to dirent directory with cd and generate +build files as ``` -#include /*good*/ +cmake . ``` -and not +Load the generated dirent.sln file into Visual Studio and build the +solution. Run the example programs from command prompt as ``` -#include "dirent.h" /*bad*/ +Debug\updatedb c:\ +Debug\locate cmd.exe +Debug\ls . +Debug\find . ``` -The former code instructs the compiler to look for dirent.h first from the -system include directory and only then from your project specific include -directory. This arrangement allows your program to compile cleanly under -Linux against the dirent.h file provided by the operating system. - # Copying @@ -68,3 +74,4 @@ both [Cygwin](http://www.cygwin.com) and [MingW](http://www.mingw.org) allow you to compile a great number of UNIX programs in Microsoft Windows. They both provide a full dirent API as well as many other UNIX APIs. MingW can even be used for commercial applications! + diff --git a/examples/locate.c b/examples/locate.c index ff2c043..b05b46d 100644 --- a/examples/locate.c +++ b/examples/locate.c @@ -25,8 +25,10 @@ #include #include #include -#include -#include +#ifdef WIN32 +# include +# include +#endif #include /* File name and location of database file */ @@ -49,6 +51,7 @@ int main( int argc, char *argv[]) { +#ifdef WIN32 int i; /* Prepare for unicode output */ @@ -82,6 +85,9 @@ main( wprintf (L"Usage: locate pattern\n"); exit (EXIT_FAILURE); } +#else + printf ("locate only works on Microsoft Windows\n"); +#endif return EXIT_SUCCESS; } @@ -91,9 +97,11 @@ static int db_locate( const wchar_t *pattern) { - wchar_t buffer[PATH_MAX + 1]; int count = 0; +#ifdef WIN32 + wchar_t buffer[PATH_MAX + 1]; + /* Open locate.db for read */ db_open (); @@ -109,6 +117,8 @@ db_locate( } db_close (); +#endif + return count; } @@ -117,12 +127,14 @@ static int db_match( const wchar_t *fn, const wchar_t *pattern) { + int found = 0; + +#ifdef WIN32 wchar_t *p; wchar_t base[PATH_MAX + 1]; wchar_t patt[PATH_MAX + 1]; - int done = 0; - int found; int i; + int done = 0; /* Locate zero-terminator from fn */ p = wcschr (fn, '\0'); @@ -165,6 +177,8 @@ db_match( } else { found = 0; } +#endif + return found; } @@ -176,10 +190,12 @@ static int db_read( wchar_t *buffer, size_t max) { + int ok = 0; + +#ifdef WIN32 size_t i = 0; wchar_t c; int done = 0; - int ok = 0; do { /* Read wide-character from stream */ @@ -228,6 +244,7 @@ db_read( /* Zero-terminate buffer */ buffer[i] = '\0'; +#endif return ok; } @@ -237,6 +254,7 @@ static void db_open( void) { +#ifdef WIN32 if (db == NULL) { errno_t error; @@ -247,6 +265,7 @@ db_open( exit (EXIT_FAILURE); } } +#endif } /* Close database file */ diff --git a/examples/updatedb.c b/examples/updatedb.c index fc3a179..58147f9 100644 --- a/examples/updatedb.c +++ b/examples/updatedb.c @@ -29,8 +29,10 @@ #include #include #include -#include -#include +#ifdef WIN32 +# include +# include +#endif #include /* File name and location of database file */ @@ -52,6 +54,7 @@ int main( int argc, char *argv[]) { +#ifdef WIN32 int i; int ok; @@ -90,6 +93,9 @@ main( } db_close (); +#else + printf ("updatedb only works on Microsoft Windows\n"); +#endif return EXIT_SUCCESS; } @@ -99,12 +105,14 @@ static int update_directory( const wchar_t *dirname) { + int ok = 0; + +#ifdef WIN32 _WDIR *dir; wchar_t buffer[PATH_MAX + 2]; wchar_t *p = buffer; const wchar_t *src; wchar_t *end = &buffer[PATH_MAX]; - int ok; /* Copy directory name to buffer */ src = dirname; @@ -173,6 +181,8 @@ update_directory( ok = 0; } +#endif + return ok; } @@ -181,6 +191,7 @@ static void db_store( const wchar_t *dirname) { +#ifdef WIN32 if (db) { /* Output line to file */ fwprintf (db, L"%s\n", dirname); @@ -188,6 +199,7 @@ db_store( wprintf (L"Database not open\n"); exit (EXIT_FAILURE); } +#endif } /* Open database file locate.db */ @@ -195,6 +207,7 @@ static void db_open( void) { +#ifdef WIN32 if (db == NULL) { errno_t error; @@ -205,6 +218,7 @@ db_open( exit (EXIT_FAILURE); } } +#endif } /* Close database file */ diff --git a/tests/t-compile.c b/tests/t-compile.c index aa3ad4d..81b47ec 100644 --- a/tests/t-compile.c +++ b/tests/t-compile.c @@ -10,8 +10,10 @@ * https://github.com/tronkko/dirent */ #include -#include -#include +#ifdef WIN32 +# include +# include +#endif #include #include #include diff --git a/vs2008/.gitignore b/vs2008/.gitignore deleted file mode 100644 index a173652..0000000 --- a/vs2008/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -Debug -vs2008.ncb -vs2008.suo -*.user diff --git a/vs2008/find/find.vcproj b/vs2008/find/find.vcproj deleted file mode 100755 index 53e1b12..0000000 --- a/vs2008/find/find.vcproj +++ /dev/null @@ -1,177 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/vs2008/locate/locate.vcproj b/vs2008/locate/locate.vcproj deleted file mode 100644 index 8bfc57f..0000000 --- a/vs2008/locate/locate.vcproj +++ /dev/null @@ -1,177 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/vs2008/ls/ls.vcproj b/vs2008/ls/ls.vcproj deleted file mode 100755 index f24a385..0000000 --- a/vs2008/ls/ls.vcproj +++ /dev/null @@ -1,177 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/vs2008/t-compile/t-compile.vcproj b/vs2008/t-compile/t-compile.vcproj deleted file mode 100644 index 1f249ff..0000000 --- a/vs2008/t-compile/t-compile.vcproj +++ /dev/null @@ -1,176 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/vs2008/t-dirent/t-dirent.vcproj b/vs2008/t-dirent/t-dirent.vcproj deleted file mode 100755 index 23940ca..0000000 --- a/vs2008/t-dirent/t-dirent.vcproj +++ /dev/null @@ -1,177 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/vs2008/updatedb/updatedb.vcproj b/vs2008/updatedb/updatedb.vcproj deleted file mode 100644 index ed91601..0000000 --- a/vs2008/updatedb/updatedb.vcproj +++ /dev/null @@ -1,177 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/vs2008/vs2008.sln b/vs2008/vs2008.sln deleted file mode 100755 index 13bbfd7..0000000 --- a/vs2008/vs2008.sln +++ /dev/null @@ -1,50 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "find", "find\find.vcproj", "{23D2045E-A3D0-4457-B6B2-6206BD32EF8D}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ls", "ls\ls.vcproj", "{1E147E3C-64E3-4DF2-A86E-22A6F10D2DA4}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "updatedb", "updatedb\updatedb.vcproj", "{39364AE2-57CD-44A3-BE08-5BDDD7EE074A}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "locate", "locate\locate.vcproj", "{0045C33B-59DB-46D5-A55A-B5DD1C09F6E9}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "t-dirent", "t-dirent\t-dirent.vcproj", "{B01AFDF2-F1D1-4377-B9A4-A8AC26CAFF0C}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "t-compile", "t-compile\t-compile.vcproj", "{AB07FE4C-6587-473F-A91D-0DBF00FC846D}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Release|Win32 = Release|Win32 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {23D2045E-A3D0-4457-B6B2-6206BD32EF8D}.Debug|Win32.ActiveCfg = Debug|Win32 - {23D2045E-A3D0-4457-B6B2-6206BD32EF8D}.Debug|Win32.Build.0 = Debug|Win32 - {23D2045E-A3D0-4457-B6B2-6206BD32EF8D}.Release|Win32.ActiveCfg = Release|Win32 - {23D2045E-A3D0-4457-B6B2-6206BD32EF8D}.Release|Win32.Build.0 = Release|Win32 - {1E147E3C-64E3-4DF2-A86E-22A6F10D2DA4}.Debug|Win32.ActiveCfg = Debug|Win32 - {1E147E3C-64E3-4DF2-A86E-22A6F10D2DA4}.Debug|Win32.Build.0 = Debug|Win32 - {1E147E3C-64E3-4DF2-A86E-22A6F10D2DA4}.Release|Win32.ActiveCfg = Release|Win32 - {1E147E3C-64E3-4DF2-A86E-22A6F10D2DA4}.Release|Win32.Build.0 = Release|Win32 - {39364AE2-57CD-44A3-BE08-5BDDD7EE074A}.Debug|Win32.ActiveCfg = Debug|Win32 - {39364AE2-57CD-44A3-BE08-5BDDD7EE074A}.Debug|Win32.Build.0 = Debug|Win32 - {39364AE2-57CD-44A3-BE08-5BDDD7EE074A}.Release|Win32.ActiveCfg = Release|Win32 - {39364AE2-57CD-44A3-BE08-5BDDD7EE074A}.Release|Win32.Build.0 = Release|Win32 - {0045C33B-59DB-46D5-A55A-B5DD1C09F6E9}.Debug|Win32.ActiveCfg = Debug|Win32 - {0045C33B-59DB-46D5-A55A-B5DD1C09F6E9}.Debug|Win32.Build.0 = Debug|Win32 - {0045C33B-59DB-46D5-A55A-B5DD1C09F6E9}.Release|Win32.ActiveCfg = Release|Win32 - {0045C33B-59DB-46D5-A55A-B5DD1C09F6E9}.Release|Win32.Build.0 = Release|Win32 - {B01AFDF2-F1D1-4377-B9A4-A8AC26CAFF0C}.Debug|Win32.ActiveCfg = Debug|Win32 - {B01AFDF2-F1D1-4377-B9A4-A8AC26CAFF0C}.Debug|Win32.Build.0 = Debug|Win32 - {B01AFDF2-F1D1-4377-B9A4-A8AC26CAFF0C}.Release|Win32.ActiveCfg = Release|Win32 - {B01AFDF2-F1D1-4377-B9A4-A8AC26CAFF0C}.Release|Win32.Build.0 = Release|Win32 - {AB07FE4C-6587-473F-A91D-0DBF00FC846D}.Debug|Win32.ActiveCfg = Debug|Win32 - {AB07FE4C-6587-473F-A91D-0DBF00FC846D}.Debug|Win32.Build.0 = Debug|Win32 - {AB07FE4C-6587-473F-A91D-0DBF00FC846D}.Release|Win32.ActiveCfg = Release|Win32 - {AB07FE4C-6587-473F-A91D-0DBF00FC846D}.Release|Win32.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal