Skip to content

Instantly share code, notes, and snippets.

@markrwilliams
Created March 22, 2015 23:05
Show Gist options
  • Save markrwilliams/85cc7ebd0cc5e47ed2c2 to your computer and use it in GitHub Desktop.
Save markrwilliams/85cc7ebd0cc5e47ed2c2 to your computer and use it in GitHub Desktop.
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
static void lookup()
{
DIR *dirp;
struct dirent *dp;
int fd;
if ((fd = open(".", O_RDONLY | O_DIRECTORY)) < 0) {
perror("open");
return;
}
if ((dirp = fdopendir(fd)) == NULL) {
perror("couldn't open '.'");
return;
}
rewinddir(dirp);
do {
errno = 0;
if ((dp = readdir(dirp)) != NULL) {
printf("%s\n", dp->d_name);
}
} while (dp != NULL);
if (errno != 0)
perror("error reading directory");
(void) closedir(dirp);
return;
}
int main()
{
lookup();
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment