14.2.7 Simple Program to List a Directory, Mark II
Here is a revised version of the directory lister found above
(see Simple Directory Lister). Using the scandir function we
can avoid the functions which work directly with the directory contents.
After the call the returned entries are available for direct use.
one (struct dirent *unused)
struct dirent **eps;
n = scandir ("./", &eps, one, alphasort);
if (n >= 0)
for (cnt = 0; cnt < n; ++cnt)
perror ("Couldn't open the directory");
Note the simple selector function in this example. Since we want to see
all directory entries we always return 1.
Published under the terms of the GNU General Public License