Last active
August 29, 2015 14:21
-
-
Save koooge/4b3e6b60add15eeea278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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