7b84acc9a7a86e49825ab05be688dd924531446d
[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 *s);
10
11 int
12 main(int argc, char *argv[]) {
13         int i;
14
15         if(argc < 2)
16                 lsx(".");
17         else if(!strcmp(argv[1], "-v"))
18                 puts("lsx-0.2, © 2006-2011 dmenu engineers, see LICENSE for details");
19         else for(i = 1; i < argc; i++)
20                 lsx(argv[i]);
21         return EXIT_SUCCESS;
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                 perror(dir);
33                 return;
34         }
35         while((d = readdir(dp))) {
36                 snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name);
37                 if(stat(buf, &st) == -1)
38                         perror(buf);
39                 else if(S_ISREG(st.st_mode) && access(buf, X_OK) == 0)
40                         puts(d->d_name);
41         }
42         closedir(dp);
43 }