1 /* See LICENSE file for copyright and license details. */
9 #define CACHE ".dmenu_cache"
11 static int qstrcmp(const void *a, const void *b);
12 static void die(const char *s);
13 static void scan(void);
14 static int uptodate(void);
16 static char **items = NULL;
17 static const char *Home, *Path;
18 static size_t count = 0;
22 if(!(Home = getenv("HOME")))
24 if(!(Path = getenv("PATH")))
29 execlp("cat", "cat", CACHE, NULL);
38 fprintf(stderr, "dmenu_path: %s\n", s);
43 qstrcmp(const void *a, const void *b) {
44 return strcmp(*(const char **)a, *(const char **)b);
56 if(!(path = strdup(Path)))
58 for(dir = strtok(path, ":"); dir; dir = strtok(NULL, ":")) {
59 if(!(dp = opendir(dir)))
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)
65 if(!(items = realloc(items, ++count * sizeof *items)))
67 if(!(items[count-1] = strdup(ent->d_name)))
72 qsort(items, count, sizeof *items, qstrcmp);
73 if(!(cache = fopen(CACHE, "w")))
75 for(i = 0; i < count; i++) {
76 if(i > 0 && !strcmp(items[i], items[i-1]))
78 fprintf(cache, "%s\n", items[i]);
79 fprintf(stdout, "%s\n", items[i]);
91 if(stat(CACHE, &st) < 0)
94 if(!(path = strdup(Path)))
96 for(dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
97 if(!stat(dir, &st) && st.st_mtime > mtime)