Skip to content
Snippets Groups Projects
Commit d37c9b62 authored by Bjoern Esswein's avatar Bjoern Esswein
Browse files

added filter to scandir

parent bda14f81
No related branches found
No related tags found
No related merge requests found
...@@ -2,18 +2,36 @@ ...@@ -2,18 +2,36 @@
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#define BASEDIR "."
int filter(const struct dirent *dir) {
if(dir->d_name[0] == '.') {
return 0;//filter out .files
}
struct stat st;
char name[strlen(BASEDIR) + strlen(dir->d_name)];
sprintf(name, "%s/%s", BASEDIR, dir->d_name);
if(stat(name, &st) == -1) die("stat");//lstat don't folow symlinks
if(S_ISREG(st.st_mode)) {
return 1;
}
return 0;
}
/** list files /** list files
*/ */
static void listDir(){ static void listDir(){
const char *dirp = ".";
struct dirent **namelist; //array of dirents struct dirent **namelist; //array of dirents
int n = scandir(dirp, &namelist, NULL, alphasort); int n = scandir(BASEDIR, &namelist, filter, alphasort);//filter oder NULL
if (n == -1) { if (n == -1) {
die("scandir"); die("scandir");
} }
while (n--) { while (n--) {
printf("%s\n", namelist[n]->d_name); printf("%s/%s\n", BASEDIR, namelist[n]->d_name);
free(namelist[n]); free(namelist[n]);
} }
free(namelist); free(namelist);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment