Skip to content

Instantly share code, notes, and snippets.

@Fedjmike
Last active August 26, 2015 04:58
Show Gist options
  • Save Fedjmike/093de3ff96afc9be7fd2 to your computer and use it in GitHub Desktop.
Save Fedjmike/093de3ff96afc9be7fd2 to your computer and use it in GitHub Desktop.
A macro for iterating over the contents of a directory.
#include <dirent.h>
#define for_dir(entry, dirname, continuation) \
do { \
DIR* for_dir_dir__ = opendir(dirname); \
for (struct dirent* (entry); \
((entry) = readdir(for_dir_dir__));) { \
continuation \
} \
closedir(for_dir_dir__); \
} while (0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment