Skip to content

Instantly share code, notes, and snippets.

@koooge
Last active August 29, 2015 14:21
Show Gist options
  • Save koooge/4b3e6b60add15eeea278 to your computer and use it in GitHub Desktop.
Save koooge/4b3e6b60add15eeea278 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#define DIR_NAME "dir"
int main(void)
{
int i, ret;
struct stat st;
char *dir_path;
struct dirent **file_list;
dir_path = (char *)calloc(128, sizeof(char));
if (dir_path == NULL) {
return -1;
}
strncat(dir_path, DIR_NAME, 4);
ret = stat(dir_path, &st);
if (ret == -1) {
printf("%s doesn't exist\n", dir_path);
free(dir_path);
return -1;
}
ret = scandir(dir_path, &file_list, NULL, NULL);
if(ret == -1) {
printf("scandir failed\n");
free(dir_path);
return -1;
}
for(i=0; i<ret; ++i) {
if (strcmp(file_list[i]->d_name, ".") == 0 ||
strcmp(file_list[i]->d_name, "..") == 0) {
continue;
}
printf("%s\n", file_list[i]->d_name);
free(file_list[i]);
}
free(file_list);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment