count.c (619B)
1 #include <dirent.h> 2 #include <err.h> 3 #include <stdio.h> 4 #include <string.h> 5 6 int 7 countread(void *arg, char *buf, size_t len) 8 { 9 char *path = arg; 10 unsigned long count = 0; 11 struct dirent *e; 12 DIR *d; 13 14 if ((d = opendir(path)) == NULL) { 15 warn("open %s", path); 16 return -1; 17 } 18 19 while ((e = readdir(d)) != NULL) { 20 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) 21 continue; 22 ++count; 23 } 24 closedir(d); 25 26 snprintf(buf, len, "%lu", count); 27 28 return 0; 29 }