My dmenu build
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
2.0 KiB

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