My dmenu build
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

dmenu_path.c 2.1 KiB

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