Xinqi Bao's Git

swap.c: Refactor getstats() and its usage
[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;
15
16 if (!(fd = opendir(dir))) {
17 warn("opendir '%s':", dir);
18 return NULL;
19 }
20
21 num = 0;
22 while ((dp = readdir(fd))) {
23 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
24 continue; /* skip self and parent */
25 }
26 num++;
27 }
28
29 closedir(fd);
30
31 return bprintf("%d", num);
32 }