Xinqi Bao's Git

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