Xinqi Bao's Git

505e24ab1cc9747106935302a7a6c16faea1d001
[slstatus.git] / num_files.c
1 #include <dirent.h>
2 #include <err.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "util.h"
7
8 const char *
9 num_files(const char *dir)
10 {
11 struct dirent *dp;
12 DIR *fd;
13 int num = 0;
14
15 if ((fd = opendir(dir)) == NULL) {
16 warn("Failed to get number of files in directory %s", dir);
17 return NULL;
18 }
19
20 while ((dp = readdir(fd)) != NULL) {
21 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
22 continue; /* skip self and parent */
23 num++;
24 }
25
26 closedir(fd);
27
28 return bprintf("%d", num);
29 }