fixed xinerama corner case
[dmenu.git] / dmenu_path.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 <string.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9
10 static void die(const char *s);
11 static int qstrcmp(const void *a, const void *b);
12 static void scan(void);
13 static int uptodate(void);
14
15 static char **items = NULL;
16 static const char *home, *path;
17
18 int
19 main(void) {
20         if(!(home = getenv("HOME")))
21                 die("no $HOME");
22         if(!(path = getenv("PATH")))
23                 die("no $PATH");
24         if(chdir(home) < 0)
25                 die("chdir failed");
26         if(uptodate()) {
27                 execl("/bin/cat", "cat", CACHE, NULL);
28                 die("exec failed");
29         }
30         scan();
31         return EXIT_SUCCESS;
32 }
33
34 void
35 die(const char *s) {
36         fprintf(stderr, "dmenu_path: %s\n", s);
37         exit(EXIT_FAILURE);
38 }
39
40 int
41 qstrcmp(const void *a, const void *b) {
42         return strcmp(*(const char **)a, *(const char **)b);
43 }
44
45 void
46 scan(void) {
47         char buf[PATH_MAX];
48         char *dir, *p;
49         size_t i, count;
50         struct dirent *ent;
51         DIR *dp;
52         FILE *cache;
53
54         count = 0;
55         if(!(p = strdup(path)))
56                 die("strdup failed");
57         for(dir = strtok(p, ":"); dir; dir = strtok(NULL, ":")) {
58                 if(!(dp = opendir(dir)))
59                         continue;
60                 while((ent = readdir(dp))) {
61                         snprintf(buf, sizeof buf, "%s/%s", dir, ent->d_name);
62                         if(ent->d_name[0] == '.' || access(buf, X_OK) < 0)
63                                 continue;
64                         if(!(items = realloc(items, ++count * sizeof *items)))
65                                 die("malloc failed");
66                         if(!(items[count-1] = strdup(ent->d_name)))
67                                 die("strdup failed");
68                 }
69                 closedir(dp);
70         }
71         qsort(items, count, sizeof *items, qstrcmp);
72         if(!(cache = fopen(CACHE, "w")))
73                 die("open failed");
74         for(i = 0; i < count; i++) {
75                 if(i > 0 && !strcmp(items[i], items[i-1]))
76                         continue;
77                 fprintf(cache,  "%s\n", items[i]);
78                 fprintf(stdout, "%s\n", items[i]);
79         }
80         fclose(cache);
81         free(p);
82 }
83
84 int
85 uptodate(void) {
86         char *dir, *p;
87         time_t mtime;
88         struct stat st;
89
90         if(stat(CACHE, &st) < 0)
91                 return 0;
92         mtime = st.st_mtime;
93         if(!(p = strdup(path)))
94                 die("strdup failed");
95         for(dir = strtok(p, ":"); dir; dir = strtok(NULL, ":"))
96                 if(!stat(dir, &st) && st.st_mtime > mtime)
97                         return 0;
98         free(p);
99         return 1;
100 }