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.
 
 
 
 
 
 

78 rivejä
2.7 KiB

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