Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Created October 7, 2013 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshavardhana/6862240 to your computer and use it in GitHub Desktop.
Save harshavardhana/6862240 to your computer and use it in GitHub Desktop.
Closedir dirstream corruption check during directory crawling
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#define _FILE_OFFSET_BITS 64
int main()
{
DIR *dir = NULL;
struct dirent *ptr;
struct dirent *result;
int fd = 0;
dir = opendir(".");
ptr = malloc(sizeof(*ptr) + (PATH_MAX + 1));
if (ptr == NULL) {
fprintf (stderr, "Malloc error");
closedir(dir);
return -1;
}
while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) {
if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
continue;
fprintf (stdout, "I am here -> %s\n", ptr->d_name);
}
closedir(dir);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment