num_files.c (510B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <dirent.h> 3 #include <stdio.h> 4 #include <string.h> 5 6 #include "../util.h" 7 8 const char * 9 num_files(const char *path) 10 { 11 struct dirent *dp; 12 DIR *fd; 13 int num; 14 15 if (!(fd = opendir(path))) { 16 warn("opendir '%s':", path); 17 return NULL; 18 } 19 20 num = 0; 21 while ((dp = readdir(fd))) { 22 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) { 23 continue; /* skip self and parent */ 24 } 25 num++; 26 } 27 28 closedir(fd); 29 30 return bprintf("%d", num); 31 }