find.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini find implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Reworked by David Douthitt <n9ubh@callsign.net> and
  8. * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <dirent.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <fnmatch.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include "busybox.h"
  34. //XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz
  35. const char msg_req_arg[] = "option `%s' requires an argument";
  36. const char msg_invalid_arg[] = "invalid argument `%s' to `%s'";
  37. static char *pattern;
  38. #ifdef CONFIG_FEATURE_FIND_TYPE
  39. static int type_mask = 0;
  40. #endif
  41. #ifdef CONFIG_FEATURE_FIND_PERM
  42. static char perm_char = 0;
  43. static int perm_mask = 0;
  44. #endif
  45. #ifdef CONFIG_FEATURE_FIND_MTIME
  46. static char mtime_char;
  47. static int mtime_days;
  48. #endif
  49. #ifdef CONFIG_FEATURE_FIND_XDEV
  50. static dev_t *xdev_dev;
  51. static int xdev_count = 0;
  52. #endif
  53. #ifdef CONFIG_FEATURE_FIND_NEWER
  54. static time_t newer_mtime;
  55. #endif
  56. #ifdef CONFIG_FEATURE_FIND_INUM
  57. static ino_t inode_num;
  58. #endif
  59. static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
  60. {
  61. if (pattern != NULL) {
  62. const char *tmp = strrchr(fileName, '/');
  63. if (tmp == NULL)
  64. tmp = fileName;
  65. else
  66. tmp++;
  67. if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
  68. goto no_match;
  69. }
  70. #ifdef CONFIG_FEATURE_FIND_TYPE
  71. if (type_mask != 0) {
  72. if (!((statbuf->st_mode & S_IFMT) == type_mask))
  73. goto no_match;
  74. }
  75. #endif
  76. #ifdef CONFIG_FEATURE_FIND_PERM
  77. if (perm_mask != 0) {
  78. if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
  79. (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
  80. (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
  81. goto no_match;
  82. }
  83. #endif
  84. #ifdef CONFIG_FEATURE_FIND_MTIME
  85. if (mtime_char != 0) {
  86. time_t file_age = time(NULL) - statbuf->st_mtime;
  87. time_t mtime_secs = mtime_days * 24 * 60 * 60;
  88. if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
  89. file_age < mtime_secs + 24 * 60 * 60) ||
  90. (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
  91. (mtime_char == '-' && file_age < mtime_secs)))
  92. goto no_match;
  93. }
  94. #endif
  95. #ifdef CONFIG_FEATURE_FIND_XDEV
  96. if (xdev_count) {
  97. int i;
  98. for (i=0; i<xdev_count; i++) {
  99. if (xdev_dev[i] == statbuf-> st_dev)
  100. break;
  101. }
  102. if (i == xdev_count) {
  103. if(S_ISDIR(statbuf->st_mode))
  104. return SKIP;
  105. else
  106. goto no_match;
  107. }
  108. }
  109. #endif
  110. #ifdef CONFIG_FEATURE_FIND_NEWER
  111. if (newer_mtime != 0) {
  112. time_t file_age = newer_mtime - statbuf->st_mtime;
  113. if (file_age >= 0)
  114. goto no_match;
  115. }
  116. #endif
  117. #ifdef CONFIG_FEATURE_FIND_INUM
  118. if (inode_num != 0) {
  119. if (!(statbuf->st_ino == inode_num))
  120. goto no_match;
  121. }
  122. #endif
  123. puts(fileName);
  124. no_match:
  125. return (TRUE);
  126. }
  127. #ifdef CONFIG_FEATURE_FIND_TYPE
  128. static int find_type(char *type)
  129. {
  130. int mask = 0;
  131. switch (type[0]) {
  132. case 'b':
  133. mask = S_IFBLK;
  134. break;
  135. case 'c':
  136. mask = S_IFCHR;
  137. break;
  138. case 'd':
  139. mask = S_IFDIR;
  140. break;
  141. case 'p':
  142. mask = S_IFIFO;
  143. break;
  144. case 'f':
  145. mask = S_IFREG;
  146. break;
  147. case 'l':
  148. mask = S_IFLNK;
  149. break;
  150. case 's':
  151. mask = S_IFSOCK;
  152. break;
  153. }
  154. if (mask == 0 || type[1] != '\0')
  155. bb_error_msg_and_die(msg_invalid_arg, type, "-type");
  156. return mask;
  157. }
  158. #endif
  159. int find_main(int argc, char **argv)
  160. {
  161. int dereference = FALSE;
  162. int i, firstopt, status = EXIT_SUCCESS;
  163. for (firstopt = 1; firstopt < argc; firstopt++) {
  164. if (argv[firstopt][0] == '-')
  165. break;
  166. }
  167. /* Parse any options */
  168. for (i = firstopt; i < argc; i++) {
  169. if (strcmp(argv[i], "-follow") == 0)
  170. dereference = TRUE;
  171. else if (strcmp(argv[i], "-print") == 0) {
  172. ;
  173. }
  174. else if (strcmp(argv[i], "-name") == 0) {
  175. if (++i == argc)
  176. bb_error_msg_and_die(msg_req_arg, "-name");
  177. pattern = argv[i];
  178. #ifdef CONFIG_FEATURE_FIND_TYPE
  179. } else if (strcmp(argv[i], "-type") == 0) {
  180. if (++i == argc)
  181. bb_error_msg_and_die(msg_req_arg, "-type");
  182. type_mask = find_type(argv[i]);
  183. #endif
  184. #ifdef CONFIG_FEATURE_FIND_PERM
  185. } else if (strcmp(argv[i], "-perm") == 0) {
  186. char *end;
  187. if (++i == argc)
  188. bb_error_msg_and_die(msg_req_arg, "-perm");
  189. perm_mask = strtol(argv[i], &end, 8);
  190. if ((end[0] != '\0') || (perm_mask > 07777))
  191. bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
  192. if ((perm_char = argv[i][0]) == '-')
  193. perm_mask = -perm_mask;
  194. #endif
  195. #ifdef CONFIG_FEATURE_FIND_MTIME
  196. } else if (strcmp(argv[i], "-mtime") == 0) {
  197. char *end;
  198. if (++i == argc)
  199. bb_error_msg_and_die(msg_req_arg, "-mtime");
  200. mtime_days = strtol(argv[i], &end, 10);
  201. if (end[0] != '\0')
  202. bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
  203. if ((mtime_char = argv[i][0]) == '-')
  204. mtime_days = -mtime_days;
  205. #endif
  206. #ifdef CONFIG_FEATURE_FIND_XDEV
  207. } else if (strcmp(argv[i], "-xdev") == 0) {
  208. struct stat stbuf;
  209. xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
  210. xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
  211. if ( firstopt == 1 ) {
  212. if ( stat ( ".", &stbuf ) < 0 )
  213. bb_error_msg_and_die("could not stat '.'" );
  214. xdev_dev [0] = stbuf. st_dev;
  215. }
  216. else {
  217. for (i = 1; i < firstopt; i++) {
  218. if ( stat ( argv [i], &stbuf ) < 0 )
  219. bb_error_msg_and_die("could not stat '%s'", argv [i] );
  220. xdev_dev [i-1] = stbuf. st_dev;
  221. }
  222. }
  223. #endif
  224. #ifdef CONFIG_FEATURE_FIND_NEWER
  225. } else if (strcmp(argv[i], "-newer") == 0) {
  226. struct stat stat_newer;
  227. if (++i == argc)
  228. bb_error_msg_and_die(msg_req_arg, "-newer");
  229. if (stat (argv[i], &stat_newer) != 0)
  230. bb_error_msg_and_die("file %s not found", argv[i]);
  231. newer_mtime = stat_newer.st_mtime;
  232. #endif
  233. #ifdef CONFIG_FEATURE_FIND_INUM
  234. } else if (strcmp(argv[i], "-inum") == 0) {
  235. char *end;
  236. if (++i == argc)
  237. bb_error_msg_and_die(msg_req_arg, "-inum");
  238. inode_num = strtol(argv[i], &end, 10);
  239. if (end[0] != '\0')
  240. bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
  241. #endif
  242. } else
  243. bb_show_usage();
  244. }
  245. if (firstopt == 1) {
  246. if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
  247. fileAction, NULL))
  248. status = EXIT_FAILURE;
  249. } else {
  250. for (i = 1; i < firstopt; i++) {
  251. if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
  252. fileAction, NULL))
  253. status = EXIT_FAILURE;
  254. }
  255. }
  256. return status;
  257. }