patched suckless dmenu
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

stest.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/stat.h>
  3. #include <dirent.h>
  4. #include <limits.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include "arg.h"
  10. char *argv0;
  11. #define FLAG(x) (flag[(x)-'a'])
  12. static void test(const char *, const char *);
  13. static void usage(void);
  14. static int match = 0;
  15. static int flag[26];
  16. static struct stat old, new;
  17. static void
  18. test(const char *path, const char *name)
  19. {
  20. struct stat st, ln;
  21. if ((!stat(path, &st) && (FLAG('a') || name[0] != '.') /* hidden files */
  22. && (!FLAG('b') || S_ISBLK(st.st_mode)) /* block special */
  23. && (!FLAG('c') || S_ISCHR(st.st_mode)) /* character special */
  24. && (!FLAG('d') || S_ISDIR(st.st_mode)) /* directory */
  25. && (!FLAG('e') || access(path, F_OK) == 0) /* exists */
  26. && (!FLAG('f') || S_ISREG(st.st_mode)) /* regular file */
  27. && (!FLAG('g') || st.st_mode & S_ISGID) /* set-group-id flag */
  28. && (!FLAG('h') || (!lstat(path, &ln) && S_ISLNK(ln.st_mode))) /* symbolic link */
  29. && (!FLAG('n') || st.st_mtime > new.st_mtime) /* newer than file */
  30. && (!FLAG('o') || st.st_mtime < old.st_mtime) /* older than file */
  31. && (!FLAG('p') || S_ISFIFO(st.st_mode)) /* named pipe */
  32. && (!FLAG('r') || access(path, R_OK) == 0) /* readable */
  33. && (!FLAG('s') || st.st_size > 0) /* not empty */
  34. && (!FLAG('u') || st.st_mode & S_ISUID) /* set-user-id flag */
  35. && (!FLAG('w') || access(path, W_OK) == 0) /* writable */
  36. && (!FLAG('x') || access(path, X_OK) == 0)) != FLAG('v')) { /* executable */
  37. if (FLAG('q'))
  38. exit(0);
  39. match = 1;
  40. puts(name);
  41. }
  42. }
  43. static void
  44. usage(void)
  45. {
  46. fprintf(stderr, "usage: %s [-abcdefghlpqrsuvwx] "
  47. "[-n file] [-o file] [file...]\n", argv0);
  48. exit(2); /* like test(1) return > 1 on error */
  49. }
  50. int
  51. main(int argc, char *argv[])
  52. {
  53. struct dirent *d;
  54. char path[PATH_MAX], *line = NULL, *file;
  55. size_t linesiz = 0;
  56. ssize_t n;
  57. DIR *dir;
  58. int r;
  59. ARGBEGIN {
  60. case 'n': /* newer than file */
  61. case 'o': /* older than file */
  62. file = EARGF(usage());
  63. if (!(FLAG(ARGC()) = !stat(file, (ARGC() == 'n' ? &new : &old))))
  64. perror(file);
  65. break;
  66. default:
  67. /* miscellaneous operators */
  68. if (strchr("abcdefghlpqrsuvwx", ARGC()))
  69. FLAG(ARGC()) = 1;
  70. else
  71. usage(); /* unknown flag */
  72. } ARGEND;
  73. if (!argc) {
  74. /* read list from stdin */
  75. while ((n = getline(&line, &linesiz, stdin)) > 0) {
  76. if (n && line[n - 1] == '\n')
  77. line[n - 1] = '\0';
  78. test(line, line);
  79. }
  80. free(line);
  81. } else {
  82. for (; argc; argc--, argv++) {
  83. if (FLAG('l') && (dir = opendir(*argv))) {
  84. /* test directory contents */
  85. while ((d = readdir(dir))) {
  86. r = snprintf(path, sizeof path, "%s/%s",
  87. *argv, d->d_name);
  88. if (r >= 0 && (size_t)r < sizeof path)
  89. test(path, d->d_name);
  90. }
  91. closedir(dir);
  92. } else {
  93. test(*argv, *argv);
  94. }
  95. }
  96. }
  97. return match ? 0 : 1;
  98. }