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