find.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. static const char msg_req_arg[] = "option `%s' requires an argument";
  36. static 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_MMIN
  50. static char mmin_char;
  51. static int mmin_mins;
  52. #endif
  53. #ifdef CONFIG_FEATURE_FIND_XDEV
  54. static dev_t *xdev_dev;
  55. static int xdev_count = 0;
  56. #endif
  57. #ifdef CONFIG_FEATURE_FIND_NEWER
  58. static time_t newer_mtime;
  59. #endif
  60. #ifdef CONFIG_FEATURE_FIND_INUM
  61. static ino_t inode_num;
  62. #endif
  63. #ifdef CONFIG_FEATURE_FIND_EXEC
  64. static char **exec_str;
  65. static int num_matches;
  66. static int exec_opt;
  67. #endif
  68. static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
  69. {
  70. #ifdef CONFIG_FEATURE_FIND_XDEV
  71. if (S_ISDIR(statbuf->st_mode) && xdev_count) {
  72. int i;
  73. for (i=0; i<xdev_count; i++) {
  74. if (xdev_dev[i] != statbuf->st_dev)
  75. return SKIP;
  76. }
  77. }
  78. #endif
  79. if (pattern != NULL) {
  80. const char *tmp = strrchr(fileName, '/');
  81. if (tmp == NULL)
  82. tmp = fileName;
  83. else
  84. tmp++;
  85. if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
  86. goto no_match;
  87. }
  88. #ifdef CONFIG_FEATURE_FIND_TYPE
  89. if (type_mask != 0) {
  90. if (!((statbuf->st_mode & S_IFMT) == type_mask))
  91. goto no_match;
  92. }
  93. #endif
  94. #ifdef CONFIG_FEATURE_FIND_PERM
  95. if (perm_mask != 0) {
  96. if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
  97. (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
  98. (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
  99. goto no_match;
  100. }
  101. #endif
  102. #ifdef CONFIG_FEATURE_FIND_MTIME
  103. if (mtime_char != 0) {
  104. time_t file_age = time(NULL) - statbuf->st_mtime;
  105. time_t mtime_secs = mtime_days * 24 * 60 * 60;
  106. if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
  107. file_age < mtime_secs + 24 * 60 * 60) ||
  108. (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
  109. (mtime_char == '-' && file_age < mtime_secs)))
  110. goto no_match;
  111. }
  112. #endif
  113. #ifdef CONFIG_FEATURE_FIND_MMIN
  114. if (mmin_char != 0) {
  115. time_t file_age = time(NULL) - statbuf->st_mtime;
  116. time_t mmin_secs = mmin_mins * 60;
  117. if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
  118. file_age < mmin_secs + 60) ||
  119. (mmin_char == '+' && file_age >= mmin_secs + 60) ||
  120. (mmin_char == '-' && file_age < mmin_secs)))
  121. goto no_match;
  122. }
  123. #endif
  124. #ifdef CONFIG_FEATURE_FIND_NEWER
  125. if (newer_mtime != 0) {
  126. time_t file_age = newer_mtime - statbuf->st_mtime;
  127. if (file_age >= 0)
  128. goto no_match;
  129. }
  130. #endif
  131. #ifdef CONFIG_FEATURE_FIND_INUM
  132. if (inode_num != 0) {
  133. if (!(statbuf->st_ino == inode_num))
  134. goto no_match;
  135. }
  136. #endif
  137. #ifdef CONFIG_FEATURE_FIND_EXEC
  138. if (exec_opt) {
  139. int i;
  140. char *cmd_string = "";
  141. for (i = 0; i < num_matches; i++)
  142. cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
  143. cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
  144. system(cmd_string);
  145. goto no_match;
  146. }
  147. #endif
  148. puts(fileName);
  149. no_match:
  150. return (TRUE);
  151. }
  152. #ifdef CONFIG_FEATURE_FIND_TYPE
  153. static int find_type(char *type)
  154. {
  155. int mask = 0;
  156. switch (type[0]) {
  157. case 'b':
  158. mask = S_IFBLK;
  159. break;
  160. case 'c':
  161. mask = S_IFCHR;
  162. break;
  163. case 'd':
  164. mask = S_IFDIR;
  165. break;
  166. case 'p':
  167. mask = S_IFIFO;
  168. break;
  169. case 'f':
  170. mask = S_IFREG;
  171. break;
  172. case 'l':
  173. mask = S_IFLNK;
  174. break;
  175. case 's':
  176. mask = S_IFSOCK;
  177. break;
  178. }
  179. if (mask == 0 || type[1] != '\0')
  180. bb_error_msg_and_die(msg_invalid_arg, type, "-type");
  181. return mask;
  182. }
  183. #endif
  184. int find_main(int argc, char **argv)
  185. {
  186. int dereference = FALSE;
  187. int i, firstopt, status = EXIT_SUCCESS;
  188. for (firstopt = 1; firstopt < argc; firstopt++) {
  189. if (argv[firstopt][0] == '-')
  190. break;
  191. }
  192. /* Parse any options */
  193. for (i = firstopt; i < argc; i++) {
  194. if (strcmp(argv[i], "-follow") == 0)
  195. dereference = TRUE;
  196. else if (strcmp(argv[i], "-print") == 0) {
  197. ;
  198. }
  199. else if (strcmp(argv[i], "-name") == 0) {
  200. if (++i == argc)
  201. bb_error_msg_and_die(msg_req_arg, "-name");
  202. pattern = argv[i];
  203. #ifdef CONFIG_FEATURE_FIND_TYPE
  204. } else if (strcmp(argv[i], "-type") == 0) {
  205. if (++i == argc)
  206. bb_error_msg_and_die(msg_req_arg, "-type");
  207. type_mask = find_type(argv[i]);
  208. #endif
  209. #ifdef CONFIG_FEATURE_FIND_PERM
  210. } else if (strcmp(argv[i], "-perm") == 0) {
  211. char *end;
  212. if (++i == argc)
  213. bb_error_msg_and_die(msg_req_arg, "-perm");
  214. perm_mask = strtol(argv[i], &end, 8);
  215. if ((end[0] != '\0') || (perm_mask > 07777))
  216. bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
  217. if ((perm_char = argv[i][0]) == '-')
  218. perm_mask = -perm_mask;
  219. #endif
  220. #ifdef CONFIG_FEATURE_FIND_MTIME
  221. } else if (strcmp(argv[i], "-mtime") == 0) {
  222. char *end;
  223. if (++i == argc)
  224. bb_error_msg_and_die(msg_req_arg, "-mtime");
  225. mtime_days = strtol(argv[i], &end, 10);
  226. if (end[0] != '\0')
  227. bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
  228. if ((mtime_char = argv[i][0]) == '-')
  229. mtime_days = -mtime_days;
  230. #endif
  231. #ifdef CONFIG_FEATURE_FIND_MMIN
  232. } else if (strcmp(argv[i], "-mmin") == 0) {
  233. char *end;
  234. if (++i == argc)
  235. bb_error_msg_and_die(msg_req_arg, "-mmin");
  236. mmin_mins = strtol(argv[i], &end, 10);
  237. if (end[0] != '\0')
  238. bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mmin");
  239. if ((mmin_char = argv[i][0]) == '-')
  240. mmin_mins = -mmin_mins;
  241. #endif
  242. #ifdef CONFIG_FEATURE_FIND_XDEV
  243. } else if (strcmp(argv[i], "-xdev") == 0) {
  244. struct stat stbuf;
  245. xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
  246. xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
  247. if ( firstopt == 1 ) {
  248. xstat ( ".", &stbuf );
  249. xdev_dev [0] = stbuf. st_dev;
  250. }
  251. else {
  252. for (i = 1; i < firstopt; i++) {
  253. xstat ( argv [i], &stbuf );
  254. xdev_dev [i-1] = stbuf. st_dev;
  255. }
  256. }
  257. #endif
  258. #ifdef CONFIG_FEATURE_FIND_NEWER
  259. } else if (strcmp(argv[i], "-newer") == 0) {
  260. struct stat stat_newer;
  261. if (++i == argc)
  262. bb_error_msg_and_die(msg_req_arg, "-newer");
  263. xstat (argv[i], &stat_newer);
  264. newer_mtime = stat_newer.st_mtime;
  265. #endif
  266. #ifdef CONFIG_FEATURE_FIND_INUM
  267. } else if (strcmp(argv[i], "-inum") == 0) {
  268. char *end;
  269. if (++i == argc)
  270. bb_error_msg_and_die(msg_req_arg, "-inum");
  271. inode_num = strtol(argv[i], &end, 10);
  272. if (end[0] != '\0')
  273. bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
  274. #endif
  275. #ifdef CONFIG_FEATURE_FIND_EXEC
  276. } else if (strcmp(argv[i], "-exec") == 0) {
  277. int b_pos;
  278. char *cmd_string = "";
  279. while (i++) {
  280. if (i == argc)
  281. bb_error_msg_and_die(msg_req_arg, "-exec");
  282. if (*argv[i] == ';')
  283. break;
  284. cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
  285. }
  286. if (*cmd_string == 0)
  287. bb_error_msg_and_die(msg_req_arg, "-exec");
  288. cmd_string++;
  289. exec_str = xmalloc(sizeof(char *));
  290. while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
  291. num_matches++;
  292. exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
  293. exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
  294. cmd_string += b_pos + 2;
  295. }
  296. exec_str[num_matches] = bb_xstrdup(cmd_string);
  297. exec_opt = 1;
  298. #endif
  299. } else
  300. bb_show_usage();
  301. }
  302. if (firstopt == 1) {
  303. if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
  304. fileAction, NULL))
  305. status = EXIT_FAILURE;
  306. } else {
  307. for (i = 1; i < firstopt; i++) {
  308. if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
  309. fileAction, NULL))
  310. status = EXIT_FAILURE;
  311. }
  312. }
  313. return status;
  314. }