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