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