Xinqi Bao's Git

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