Xinqi Bao's Git

Unify header includes
[slstatus.git] / components / num_files.c
1 /* See LICENSE file for copyright and license details. */
2 #include <dirent.h>
3 #include <errno.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))) {
17 fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno));
18 return NULL;
19 }
20
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 }