O'Reilly logo

Advanced UNIX Programming by Warren W. Gay

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Scanning a Directory

While the family of routines (see directory(3)) starting with opendir(3) performs the functions that a programmer might need, they are somewhat tedious to code if you need them frequently enough. The scandir(3) and alphasort(3) routines assist in reducing the programmer effort required:

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

int scandir(
    const char *dirname,
    struct dirent ***namelist,
    int (*select)(struct dirent *),
    int (*compar)(const void *, const void *));

int alphasort(const void *d1, const void *d2);

Function scandir(3) might look somewhat intimidating. However, once you spend a moment examining it, you will see that it is easy to use. The argument dirname is given the pathname of the directory that you want to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required