find.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini find implementation for busybox
  4. *
  5. *
  6. * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
  7. * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
  8. * Reworked by David Douthitt <n9ubh@callsign.net> and
  9. * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <dirent.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <fnmatch.h>
  32. #include <time.h>
  33. #include <ctype.h>
  34. #include "busybox.h"
  35. static char *pattern;
  36. #ifdef BB_FEATURE_FIND_TYPE
  37. static int type_mask = 0;
  38. #endif
  39. #ifdef BB_FEATURE_FIND_PERM
  40. static char perm_char = 0;
  41. static int perm_mask = 0;
  42. #endif
  43. #ifdef BB_FEATURE_FIND_MTIME
  44. static char mtime_char;
  45. static int mtime_days;
  46. #endif
  47. #ifdef BB_FEATURE_FIND_NEWER
  48. time_t newer_mtime;
  49. #endif
  50. static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
  51. {
  52. if (pattern != NULL) {
  53. const char *tmp = strrchr(fileName, '/');
  54. if (tmp == NULL)
  55. tmp = fileName;
  56. else
  57. tmp++;
  58. if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
  59. goto no_match;
  60. }
  61. #ifdef BB_FEATURE_FIND_TYPE
  62. if (type_mask != 0) {
  63. if (!((statbuf->st_mode & S_IFMT) == type_mask))
  64. goto no_match;
  65. }
  66. #endif
  67. #ifdef BB_FEATURE_FIND_PERM
  68. if (perm_mask != 0) {
  69. if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
  70. (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
  71. (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
  72. goto no_match;
  73. }
  74. #endif
  75. #ifdef BB_FEATURE_FIND_MTIME
  76. if (mtime_char != 0) {
  77. time_t file_age = time(NULL) - statbuf->st_mtime;
  78. time_t mtime_secs = mtime_days * 24 * 60 * 60;
  79. if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
  80. file_age < mtime_secs + 24 * 60 * 60) ||
  81. (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
  82. (mtime_char == '-' && file_age < mtime_secs)))
  83. goto no_match;
  84. }
  85. #endif
  86. #ifdef BB_FEATURE_FIND_NEWER
  87. if (newer_mtime != 0) {
  88. time_t file_age = newer_mtime - statbuf->st_mtime;
  89. if (file_age >= 0)
  90. goto no_match;
  91. }
  92. #endif
  93. puts(fileName);
  94. no_match:
  95. return (TRUE);
  96. }
  97. #ifdef BB_FEATURE_FIND_TYPE
  98. static int find_type(char *type)
  99. {
  100. int mask = 0;
  101. switch (type[0]) {
  102. case 'b':
  103. mask = S_IFBLK;
  104. break;
  105. case 'c':
  106. mask = S_IFCHR;
  107. break;
  108. case 'd':
  109. mask = S_IFDIR;
  110. break;
  111. case 'p':
  112. mask = S_IFIFO;
  113. break;
  114. case 'f':
  115. mask = S_IFREG;
  116. break;
  117. case 'l':
  118. mask = S_IFLNK;
  119. break;
  120. case 's':
  121. mask = S_IFSOCK;
  122. break;
  123. }
  124. if (mask == 0 || type[1] != '\0')
  125. error_msg_and_die("invalid argument `%s' to `-type'", type);
  126. return mask;
  127. }
  128. #endif
  129. int find_main(int argc, char **argv)
  130. {
  131. int dereference = FALSE;
  132. int i, firstopt, status = EXIT_SUCCESS;
  133. for (firstopt = 1; firstopt < argc; firstopt++) {
  134. if (argv[firstopt][0] == '-')
  135. break;
  136. }
  137. /* Parse any options */
  138. for (i = firstopt; i < argc; i++) {
  139. if (strcmp(argv[i], "-follow") == 0)
  140. dereference = TRUE;
  141. else if (strcmp(argv[i], "-print") == 0) {
  142. ;
  143. }
  144. else if (strcmp(argv[i], "-name") == 0) {
  145. if (++i == argc)
  146. error_msg_and_die("option `-name' requires an argument");
  147. pattern = argv[i];
  148. #ifdef BB_FEATURE_FIND_TYPE
  149. } else if (strcmp(argv[i], "-type") == 0) {
  150. if (++i == argc)
  151. error_msg_and_die("option `-type' requires an argument");
  152. type_mask = find_type(argv[i]);
  153. #endif
  154. #ifdef BB_FEATURE_FIND_PERM
  155. } else if (strcmp(argv[i], "-perm") == 0) {
  156. char *end;
  157. if (++i == argc)
  158. error_msg_and_die("option `-perm' requires an argument");
  159. perm_mask = strtol(argv[i], &end, 8);
  160. if (end[0] != '\0')
  161. error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]);
  162. if (perm_mask > 07777)
  163. error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]);
  164. if ((perm_char = argv[i][0]) == '-')
  165. perm_mask = -perm_mask;
  166. #endif
  167. #ifdef BB_FEATURE_FIND_MTIME
  168. } else if (strcmp(argv[i], "-mtime") == 0) {
  169. char *end;
  170. if (++i == argc)
  171. error_msg_and_die("option `-mtime' requires an argument");
  172. mtime_days = strtol(argv[i], &end, 10);
  173. if (end[0] != '\0')
  174. error_msg_and_die("invalid argument `%s' to `-mtime'", argv[i]);
  175. if ((mtime_char = argv[i][0]) == '-')
  176. mtime_days = -mtime_days;
  177. #endif
  178. #ifdef BB_FEATURE_FIND_NEWER
  179. } else if (strcmp(argv[i], "-newer") == 0) {
  180. struct stat stat_newer;
  181. if (++i == argc)
  182. error_msg_and_die("option `-newer' requires an argument");
  183. if (stat (argv[i], &stat_newer) != 0)
  184. error_msg_and_die("file %s not found", argv[i]);
  185. newer_mtime = stat_newer.st_mtime;
  186. #endif
  187. } else
  188. show_usage();
  189. }
  190. if (firstopt == 1) {
  191. if (recursive_action(".", TRUE, dereference, FALSE, fileAction,
  192. fileAction, NULL) == FALSE)
  193. status = EXIT_FAILURE;
  194. } else {
  195. for (i = 1; i < firstopt; i++) {
  196. if (recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
  197. fileAction, NULL) == FALSE)
  198. status = EXIT_FAILURE;
  199. }
  200. }
  201. return status;
  202. }