dirs_links/file_type_stats.c

This is dirs_links/file_type_stats.c, an example to accompany the book, The Linux Programming Interface.

This file is not printed in the book; it is the solution to Exercise 18-7 (page 373).

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.

 

Download dirs_links/file_type_stats.c

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* file_type_stats.c

   An example of the use of nftw(): traverse the directory tree named in the
   command line, and print out statistics about the types of file in the tree.
*/
#if defined(__sun)
#define _XOPEN_SOURCE 500   /* Solaris 8 needs it this way */
#else
#if ! defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600
#define _XOPEN_SOURCE 600   /* Get nftw() and S_IFSOCK declarations */
#endif
#endif
#include <ftw.h>
#include "tlpi_hdr.h"

static int numReg = 0, numDir = 0, numSymLk = 0, numSocket = 0,
           numFifo = 0, numChar = 0, numBlock = 0,
           numNonstatable = 0;
static int
countFile(const char *path, const struct stat *sb, int flag, struct FTW *ftwb)

{
    if (flag == FTW_NS) {
        numNonstatable++;
        return 0;
    }

    switch (sb->st_mode & S_IFMT) {
    case S_IFREG:  numReg++;    break;
    case S_IFDIR:  numDir++;    break;
    case S_IFCHR:  numChar++;   break;
    case S_IFBLK:  numBlock++;  break;
    case S_IFLNK:  numSymLk++;  break;
    case S_IFIFO:  numFifo++;   break;
    case S_IFSOCK: numSocket++; break;
    }
    return 0;           /* Always tell nftw() to continue */
}
static void
printStats(const char *msg, int num, int numFiles)
{
    printf("%-15s   %6d %6.1f%%\n", msg, num, num * 100.0 / numFiles);
}
int
main(int argc, char *argv[])
{
    if (argc != 2 || strcmp(argv[1], "--help") == 0)
        usageErr("%s dir-path\n", argv[0]);

    /* Traverse directory tree counting files; don't follow symbolic links */

    if (nftw(argv[1], &countFile, 20, FTW_PHYS) == -1)
        errExit("nftw");

    int numFiles = numReg + numDir + numSymLk + numSocket +
                numFifo + numChar + numBlock + numNonstatable;

    if (numFiles == 0) {
        printf("No files found\n");
    } else {
        printf("Total files:      %6d\n", numFiles);
        printStats("Regular:", numReg, numFiles);
        printStats("Directory:", numDir, numFiles);
        printStats("Char device:", numChar, numFiles);
        printStats("Block device:", numBlock, numFiles);
        printStats("Symbolic link:", numSymLk, numFiles);
        printStats("FIFO:", numFifo, numFiles);
        printStats("Socket:", numSocket, numFiles);
        printStats("Non-statable:", numNonstatable, numFiles);
    }
    exit(EXIT_SUCCESS);
}

 

Download dirs_links/file_type_stats.c

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.

Valid XHTML 1.1