aboutsummaryrefslogtreecommitdiff
path: root/dmenu_path.c
diff options
context:
space:
mode:
Diffstat (limited to 'dmenu_path.c')
-rw-r--r--dmenu_path.c100
1 files changed, 0 insertions, 100 deletions
diff --git a/dmenu_path.c b/dmenu_path.c
deleted file mode 100644
index 407477a..0000000
--- a/dmenu_path.c
+++ /dev/null
@@ -1,100 +0,0 @@
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
10static void die(const char *s);
11static int qstrcmp(const void *a, const void *b);
12static void scan(void);
13static int uptodate(void);
14
15static char **items = NULL;
16static const char *home, *path;
17
18int
19main(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
34void
35die(const char *s) {
36 fprintf(stderr, "dmenu_path: %s\n", s);
37 exit(EXIT_FAILURE);
38}
39
40int
41qstrcmp(const void *a, const void *b) {
42 return strcmp(*(const char **)a, *(const char **)b);
43}
44
45void
46scan(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
84int
85uptodate(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}