Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
9 #define CACHE ".dmenu_cache"
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);
16 static char **items
= NULL
;
17 static const char *home
, *path
;
21 if(!(home
= getenv("HOME")))
23 if(!(path
= getenv("PATH")))
28 execlp("cat", "cat", CACHE
, NULL
);
37 fprintf(stderr
, "dmenu_path: %s\n", s
);
42 qstrcmp(const void *a
, const void *b
) {
43 return strcmp(*(const char **)a
, *(const char **)b
);
56 if(!(p
= strdup(path
)))
58 for(dir
= strtok(p
, ":"); 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(!(p
= strdup(path
)))
96 for(dir
= strtok(p
, ":"); dir
; dir
= strtok(NULL
, ":"))
97 if(!stat(dir
, &st
) && st
.st_mtime
> mtime
)