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.
 
 
 
 
 
 

86 lines
2.8 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #define OPER(x) (oper[(x)-'a'])
  9. static bool test(const char *);
  10. static bool quiet = false;
  11. static bool oper[26];
  12. static struct stat old, new;
  13. int
  14. main(int argc, char *argv[]) {
  15. char buf[BUFSIZ], *p;
  16. bool match = false;
  17. int opt;
  18. while((opt = getopt(argc, argv, "C:bcdefghn:o:pqrsuwx")) != -1)
  19. switch(opt) {
  20. case 'C': /* tests relative to directory */
  21. if(chdir(optarg) == -1) {
  22. perror(optarg);
  23. exit(2);
  24. }
  25. break;
  26. case 'n': /* newer than file */
  27. case 'o': /* older than file */
  28. if(!(OPER(opt) = stat(optarg, (opt == 'n' ? &new : &old)) == 0))
  29. perror(optarg);
  30. break;
  31. case 'q': /* quiet (no output, just status) */
  32. quiet = true;
  33. break;
  34. default: /* miscellaneous operators */
  35. OPER(opt) = true;
  36. break;
  37. case '?': /* error: unknown flag */
  38. fprintf(stderr, "usage: %s [-bcdefghpqrsuwx] [-C dir] [-n file] [-o file] [file...]\n", argv[0]);
  39. exit(2);
  40. }
  41. if(optind == argc)
  42. while(fgets(buf, sizeof buf, stdin)) {
  43. if(*(p = &buf[strlen(buf)-1]) == '\n')
  44. *p = '\0';
  45. match |= test(buf);
  46. }
  47. else
  48. while(optind < argc)
  49. match |= test(argv[optind++]);
  50. return match ? 0 : 1;
  51. }
  52. bool
  53. test(const char *path) {
  54. struct stat st;
  55. if((!OPER('b') || (stat(path, &st) == 0 && S_ISBLK(st.st_mode))) /* block special */
  56. && (!OPER('c') || (stat(path, &st) == 0 && S_ISCHR(st.st_mode))) /* character special */
  57. && (!OPER('d') || (stat(path, &st) == 0 && S_ISDIR(st.st_mode))) /* directory */
  58. && (!OPER('e') || (access(path, F_OK) == 0)) /* exists */
  59. && (!OPER('f') || (stat(path, &st) == 0 && S_ISREG(st.st_mode))) /* regular file */
  60. && (!OPER('g') || (stat(path, &st) == 0 && (st.st_mode & S_ISGID))) /* set-group-id flag */
  61. && (!OPER('h') || (lstat(path, &st) == 0 && S_ISLNK(st.st_mode))) /* symbolic link */
  62. && (!OPER('n') || (stat(path, &st) == 0 && st.st_mtime > new.st_mtime)) /* newer than file */
  63. && (!OPER('o') || (stat(path, &st) == 0 && st.st_mtime < old.st_mtime)) /* older than file */
  64. && (!OPER('p') || (stat(path, &st) == 0 && S_ISFIFO(st.st_mode))) /* named pipe */
  65. && (!OPER('r') || (access(path, R_OK) == 0)) /* readable */
  66. && (!OPER('s') || (stat(path, &st) == 0 && st.st_size > 0)) /* not empty */
  67. && (!OPER('u') || (stat(path, &st) == 0 && (st.st_mode & S_ISUID))) /* set-user-id flag */
  68. && (!OPER('w') || (access(path, W_OK) == 0)) /* writable */
  69. && (!OPER('x') || (access(path, X_OK) == 0))) { /* executable */
  70. if(quiet)
  71. exit(0);
  72. puts(path);
  73. return true;
  74. }
  75. else
  76. return false;
  77. }