Skip to content

Commit

Permalink
Compile examples using CMake on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni Rönkkö committed Sep 11, 2016
1 parent 6e2c0b8 commit bb9dae1
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 1,177 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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

38 changes: 26 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<TARGET_FILE:${TEST_NAME}>)
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 $<TARGET_FILE:${TEST_NAME}>)
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)
6 changes: 5 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -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!
Expand Down
25 changes: 14 additions & 11 deletions Makefile.linux
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
61 changes: 34 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <dirent.h> /*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

Expand All @@ -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!

31 changes: 25 additions & 6 deletions examples/locate.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <io.h>
#include <fcntl.h>
#ifdef WIN32
# include <io.h>
# include <fcntl.h>
#endif
#include <dirent.h>

/* File name and location of database file */
Expand All @@ -49,6 +51,7 @@ int
main(
int argc, char *argv[])
{
#ifdef WIN32
int i;

/* Prepare for unicode output */
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 ();

Expand All @@ -109,6 +117,8 @@ db_locate(
}

db_close ();
#endif

return count;
}

Expand All @@ -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');
Expand Down Expand Up @@ -165,6 +177,8 @@ db_match(
} else {
found = 0;
}
#endif

return found;
}

Expand All @@ -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 */
Expand Down Expand Up @@ -228,6 +244,7 @@ db_read(

/* Zero-terminate buffer */
buffer[i] = '\0';
#endif

return ok;
}
Expand All @@ -237,6 +254,7 @@ static void
db_open(
void)
{
#ifdef WIN32
if (db == NULL) {
errno_t error;

Expand All @@ -247,6 +265,7 @@ db_open(
exit (EXIT_FAILURE);
}
}
#endif
}

/* Close database file */
Expand Down
Loading

0 comments on commit bb9dae1

Please sign in to comment.