My dmenu build
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

stest.c 2.9 KiB

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