Skip to content

Commit

Permalink
Supports scandir()
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni Rönkkö committed Aug 26, 2017
1 parent 9064fdf commit d2faf06
Show file tree
Hide file tree
Showing 19 changed files with 506 additions and 32 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
cmake_minimum_required (VERSION 2.8.11)
project (dirent LANGUAGES C)

# Initialize C compiler only (don't require C++ compiler)
enable_language (C)

# Compile in debug mode by default
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Debug
CACHE STRING
"Type of build: None Debug Release RelWithDebInfo MinSizeRel."
FORCE
)
endif (NOT CMAKE_BUILD_TYPE)

# Only use the dirent file on windows systems
if (WIN32)
include_directories (${CMAKE_SOURCE_DIR}/include)
Expand All @@ -23,6 +33,7 @@ add_executable (find examples/find.c)
add_executable (ls examples/ls.c)
add_executable (locate examples/locate.c)
add_executable (updatedb examples/updatedb.c)
add_executable (scandir examples/scandir.c)

# Build test programs
include (CTest)
Expand All @@ -35,4 +46,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)

2 changes: 1 addition & 1 deletion examples/locate.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ db_locate(

/* Open locate.db for read */
db_open ();

/* Read one directory and file name at a time from database file */
while (db_read (buffer, PATH_MAX)) {
/* See if file name in buffer matches the search pattern */
Expand Down
4 changes: 2 additions & 2 deletions examples/ls.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* An example demonstrating basic directory listing.
*
* Compile this file with Visual Studio and run the produced command in
Expand Down Expand Up @@ -62,7 +62,7 @@ list_directory(
{
DIR *dir;
struct dirent *ent;

/* Open directory stream */
dir = opendir (dirname);
if (dir != NULL) {
Expand Down
105 changes: 105 additions & 0 deletions examples/scandir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Example program demonstrating the use of scandir function.
*
* Compile this file with Visual Studio and run the produced command in
* console with a directory name argument. For example, command
*
* scandir "c:\Program Files"
*
* might output something like
*
* ./
* ../
* 7-Zip/
* Internet Explorer/
* Microsoft Visual Studio 9.0/
* Microsoft.NET/
* Mozilla Firefox/
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

static void list_directory (const char *dirname);


int
main(
int argc, char *argv[])
{
int i;

/* For each directory in command line */
i = 1;
while (i < argc) {
list_directory (argv[i]);
i++;
}

/* List current working directory if no arguments on command line */
if (argc == 1) {
list_directory (".");
}
return EXIT_SUCCESS;
}

/*
* List files and directories within a directory.
*/
static void
list_directory(
const char *dirname)
{
struct dirent **files;
int i;
int n;

/* Scan files in directory */
n = scandir (dirname, &files, NULL, alphasort);
if (n >= 0) {

/* Loop through file names */
for (i = 0; i < n; i++) {
struct dirent *ent;

/* Get pointer to file entry */
ent = files[i];

/* Output file name */
switch (ent->d_type) {
case DT_REG:
printf ("%s\n", ent->d_name);
break;

case DT_DIR:
printf ("%s/\n", ent->d_name);
break;

case DT_LNK:
printf ("%s@\n", ent->d_name);
break;

default:
printf ("%s*\n", ent->d_name);
}

}

/* Release file names */
for (i = 0; i < n; i++) {
free (files[i]);
}
free (files);

} else {
printf ("Cannot open directory %s\n", dirname);
}
}


Loading

0 comments on commit d2faf06

Please sign in to comment.