Xinqi Bao's Git

comment xdg fallback behaviour
[dmenu.git] / lsx.c
1 /* See LICENSE file for copyright and license details. */
2 #include <dirent.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9
10 static void lsx(const char *dir);
11
12 static int status = EXIT_SUCCESS;
13
14 int
15 main(int argc, char *argv[]) {
16 int i;
17
18 if(argc < 2)
19 lsx(".");
20 else for(i = 1; i < argc; i++)
21 lsx(argv[i]);
22 return status;
23 }
24
25 void
26 lsx(const char *dir) {
27 char buf[PATH_MAX];
28 struct dirent *d;
29 struct stat st;
30 DIR *dp;
31
32 for(dp = opendir(dir); dp && (d = readdir(dp)); errno = 0)
33 if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf
34 && access(buf, X_OK) == 0 && stat(buf, &st) == 0 && S_ISREG(st.st_mode))
35 puts(d->d_name);
36
37 if(errno != 0) {
38 status = EXIT_FAILURE;
39 perror(dir);
40 }
41 if(dp)
42 closedir(dp);
43 }