Searching a Directory

Opening and closing directories might be fun, but it doesn't accomplish too much without any additional functions. The function readdir(3) allows an open directory to be searched for one directory member at a time. The function synopsis for readdir(3) is as follows:

#include <sys/types.h>
#include <dirent.h>

struct dirent *readdir(DIR *dirp);

struct dirent {
    /* etc. */           /* Other members are implementation specific */
    char    d_name[256]; /* Max POSIX name is 255 bytes */
};

The input to readdir(3) is simply a pointer to an open DIR structure provided by opendir(3). The value returned is a pointer to the structure dirent, or a null pointer if it fails or reaches the end of the directory. FreeBSD does not document any error ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.