Xinqi Bao's Git

325c50855a6e626ddc99f01f385606f405e00ae6
[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 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) == 0 && S_ISREG(st.st_mode) && access(buf, X_OK) == 0)
38 puts(d->d_name);
39 }
40 closedir(dp);
41 }