3
0

find.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. * Licensed under the GPL version 2, see the file LICENSE in this tarball.
  11. */
  12. /* findutils-4.1.20:
  13. *
  14. * # find file.txt -exec 'echo {}' '{} {}' ';'
  15. * find: echo file.txt: No such file or directory
  16. * # find file.txt -exec 'echo' '{} {}' '; '
  17. * find: missing argument to `-exec'
  18. * # find file.txt -exec 'echo {}' '{} {}' ';' junk
  19. * find: paths must precede expression
  20. * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';'
  21. * find: paths must precede expression
  22. * # find file.txt -exec 'echo' '{} {}' ';'
  23. * file.txt file.txt
  24. * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ]))
  25. * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
  26. * file.txt file.txt
  27. * file.txt
  28. * /tmp
  29. * # find -name '*.c' -o -name '*.h'
  30. * [shows files, *.c and *.h intermixed]
  31. * # find file.txt -name '*f*' -o -name '*t*'
  32. * file.txt
  33. * # find file.txt -name '*z*' -o -name '*t*'
  34. * file.txt
  35. * # find file.txt -name '*f*' -o -name '*z*'
  36. * file.txt
  37. *
  38. * # find t z -name '*t*' -print -o -name '*z*'
  39. * t
  40. * # find t z t z -name '*t*' -o -name '*z*' -print
  41. * z
  42. * z
  43. * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print
  44. * (no output)
  45. */
  46. /* Testing script
  47. * ./busybox find "$@" | tee /tmp/bb_find
  48. * echo ==================
  49. * /path/to/gnu/find "$@" | tee /tmp/std_find
  50. * echo ==================
  51. * diff -u /tmp/std_find /tmp/bb_find && echo Identical
  52. */
  53. #include <fnmatch.h>
  54. #include "libbb.h"
  55. #if ENABLE_FEATURE_FIND_REGEX
  56. #include "xregex.h"
  57. #endif
  58. /* This is a NOEXEC applet. Be very careful! */
  59. typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
  60. typedef struct {
  61. action_fp f;
  62. #if ENABLE_FEATURE_FIND_NOT
  63. bool invert;
  64. #endif
  65. } action;
  66. #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
  67. #define ACTF(name) \
  68. static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
  69. const struct stat *statbuf UNUSED_PARAM, \
  70. action_##name* ap UNUSED_PARAM)
  71. ACTS(print)
  72. ACTS(name, const char *pattern; bool iname;)
  73. IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
  74. IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
  75. IF_FEATURE_FIND_PRINT0( ACTS(print0))
  76. IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
  77. IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
  78. IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
  79. IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
  80. IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
  81. IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
  82. IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
  83. IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
  84. IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
  85. IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
  86. IF_FEATURE_FIND_PRUNE( ACTS(prune))
  87. IF_FEATURE_FIND_DELETE( ACTS(delete))
  88. IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
  89. IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
  90. IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
  91. struct globals {
  92. IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
  93. IF_FEATURE_FIND_XDEV(int xdev_count;)
  94. action ***actions;
  95. bool need_print;
  96. recurse_flags_t recurse_flags;
  97. };
  98. #define G (*(struct globals*)&bb_common_bufsiz1)
  99. #define INIT_G() do { \
  100. struct G_sizecheck { \
  101. char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
  102. }; \
  103. /* we have to zero it out because of NOEXEC */ \
  104. memset(&G, 0, offsetof(struct globals, need_print)); \
  105. G.need_print = 1; \
  106. G.recurse_flags = ACTION_RECURSE; \
  107. } while (0)
  108. #if ENABLE_FEATURE_FIND_EXEC
  109. static unsigned count_subst(const char *str)
  110. {
  111. unsigned count = 0;
  112. while ((str = strstr(str, "{}")) != NULL) {
  113. count++;
  114. str++;
  115. }
  116. return count;
  117. }
  118. static char* subst(const char *src, unsigned count, const char* filename)
  119. {
  120. char *buf, *dst, *end;
  121. size_t flen = strlen(filename);
  122. /* we replace each '{}' with filename: growth by strlen-2 */
  123. buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
  124. while ((end = strstr(src, "{}"))) {
  125. memcpy(dst, src, end - src);
  126. dst += end - src;
  127. src = end + 2;
  128. memcpy(dst, filename, flen);
  129. dst += flen;
  130. }
  131. strcpy(dst, src);
  132. return buf;
  133. }
  134. #endif
  135. /* Return values of ACTFs ('action functions') are a bit mask:
  136. * bit 1=1: prune (use SKIP constant for setting it)
  137. * bit 0=1: matched successfully (TRUE)
  138. */
  139. static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
  140. {
  141. int cur_group;
  142. int cur_action;
  143. int rc = 0;
  144. action **app, *ap;
  145. /* "action group" is a set of actions ANDed together.
  146. * groups are ORed together.
  147. * We simply evaluate each group until we find one in which all actions
  148. * succeed. */
  149. /* -prune is special: if it is encountered, then we won't
  150. * descend into current directory. It doesn't matter whether
  151. * action group (in which -prune sits) will succeed or not:
  152. * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
  153. * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
  154. * not starting with 'f' */
  155. /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
  156. * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
  157. * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
  158. * On return, bit is restored. */
  159. cur_group = -1;
  160. while ((app = appp[++cur_group]) != NULL) {
  161. rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
  162. cur_action = -1;
  163. while (1) {
  164. ap = app[++cur_action];
  165. if (!ap) /* all actions in group were successful */
  166. return rc ^ TRUE; /* restore TRUE bit */
  167. rc |= TRUE ^ ap->f(fileName, statbuf, ap);
  168. #if ENABLE_FEATURE_FIND_NOT
  169. if (ap->invert) rc ^= TRUE;
  170. #endif
  171. if (rc & TRUE) /* current group failed, try next */
  172. break;
  173. }
  174. }
  175. return rc ^ TRUE; /* restore TRUE bit */
  176. }
  177. ACTF(name)
  178. {
  179. const char *tmp = bb_basename(fileName);
  180. if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
  181. tmp--;
  182. while (tmp != fileName && *--tmp != '/')
  183. continue;
  184. if (*tmp == '/')
  185. tmp++;
  186. }
  187. return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
  188. }
  189. #if ENABLE_FEATURE_FIND_PATH
  190. ACTF(path)
  191. {
  192. return fnmatch(ap->pattern, fileName, 0) == 0;
  193. }
  194. #endif
  195. #if ENABLE_FEATURE_FIND_REGEX
  196. ACTF(regex)
  197. {
  198. regmatch_t match;
  199. if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
  200. return 0; /* no match */
  201. if (match.rm_so)
  202. return 0; /* match doesn't start at pos 0 */
  203. if (fileName[match.rm_eo])
  204. return 0; /* match doesn't end exactly at end of pathname */
  205. return 1;
  206. }
  207. #endif
  208. #if ENABLE_FEATURE_FIND_TYPE
  209. ACTF(type)
  210. {
  211. return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
  212. }
  213. #endif
  214. #if ENABLE_FEATURE_FIND_PERM
  215. ACTF(perm)
  216. {
  217. /* -perm +mode: at least one of perm_mask bits are set */
  218. if (ap->perm_char == '+')
  219. return (statbuf->st_mode & ap->perm_mask) != 0;
  220. /* -perm -mode: all of perm_mask are set */
  221. if (ap->perm_char == '-')
  222. return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
  223. /* -perm mode: file mode must match perm_mask */
  224. return (statbuf->st_mode & 07777) == ap->perm_mask;
  225. }
  226. #endif
  227. #if ENABLE_FEATURE_FIND_MTIME
  228. ACTF(mtime)
  229. {
  230. time_t file_age = time(NULL) - statbuf->st_mtime;
  231. time_t mtime_secs = ap->mtime_days * 24*60*60;
  232. if (ap->mtime_char == '+')
  233. return file_age >= mtime_secs + 24*60*60;
  234. if (ap->mtime_char == '-')
  235. return file_age < mtime_secs;
  236. /* just numeric mtime */
  237. return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
  238. }
  239. #endif
  240. #if ENABLE_FEATURE_FIND_MMIN
  241. ACTF(mmin)
  242. {
  243. time_t file_age = time(NULL) - statbuf->st_mtime;
  244. time_t mmin_secs = ap->mmin_mins * 60;
  245. if (ap->mmin_char == '+')
  246. return file_age >= mmin_secs + 60;
  247. if (ap->mmin_char == '-')
  248. return file_age < mmin_secs;
  249. /* just numeric mmin */
  250. return file_age >= mmin_secs && file_age < (mmin_secs + 60);
  251. }
  252. #endif
  253. #if ENABLE_FEATURE_FIND_NEWER
  254. ACTF(newer)
  255. {
  256. return (ap->newer_mtime < statbuf->st_mtime);
  257. }
  258. #endif
  259. #if ENABLE_FEATURE_FIND_INUM
  260. ACTF(inum)
  261. {
  262. return (statbuf->st_ino == ap->inode_num);
  263. }
  264. #endif
  265. #if ENABLE_FEATURE_FIND_EXEC
  266. ACTF(exec)
  267. {
  268. int i, rc;
  269. #if ENABLE_USE_PORTABLE_CODE
  270. char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1));
  271. #else /* gcc 4.3.1 generates smaller code: */
  272. char *argv[ap->exec_argc + 1];
  273. #endif
  274. for (i = 0; i < ap->exec_argc; i++)
  275. argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
  276. argv[i] = NULL; /* terminate the list */
  277. rc = spawn_and_wait(argv);
  278. if (rc < 0)
  279. bb_simple_perror_msg(argv[0]);
  280. i = 0;
  281. while (argv[i])
  282. free(argv[i++]);
  283. return rc == 0; /* return 1 if exitcode 0 */
  284. }
  285. #endif
  286. #if ENABLE_FEATURE_FIND_USER
  287. ACTF(user)
  288. {
  289. return (statbuf->st_uid == ap->uid);
  290. }
  291. #endif
  292. #if ENABLE_FEATURE_FIND_GROUP
  293. ACTF(group)
  294. {
  295. return (statbuf->st_gid == ap->gid);
  296. }
  297. #endif
  298. #if ENABLE_FEATURE_FIND_PRINT0
  299. ACTF(print0)
  300. {
  301. printf("%s%c", fileName, '\0');
  302. return TRUE;
  303. }
  304. #endif
  305. ACTF(print)
  306. {
  307. puts(fileName);
  308. return TRUE;
  309. }
  310. #if ENABLE_FEATURE_FIND_PAREN
  311. ACTF(paren)
  312. {
  313. return exec_actions(ap->subexpr, fileName, statbuf);
  314. }
  315. #endif
  316. #if ENABLE_FEATURE_FIND_SIZE
  317. ACTF(size)
  318. {
  319. if (ap->size_char == '+')
  320. return statbuf->st_size > ap->size;
  321. if (ap->size_char == '-')
  322. return statbuf->st_size < ap->size;
  323. return statbuf->st_size == ap->size;
  324. }
  325. #endif
  326. #if ENABLE_FEATURE_FIND_PRUNE
  327. /*
  328. * -prune: if -depth is not given, return true and do not descend
  329. * current dir; if -depth is given, return false with no effect.
  330. * Example:
  331. * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
  332. */
  333. ACTF(prune)
  334. {
  335. return SKIP + TRUE;
  336. }
  337. #endif
  338. #if ENABLE_FEATURE_FIND_DELETE
  339. ACTF(delete)
  340. {
  341. int rc;
  342. if (S_ISDIR(statbuf->st_mode)) {
  343. rc = rmdir(fileName);
  344. } else {
  345. rc = unlink(fileName);
  346. }
  347. if (rc < 0)
  348. bb_simple_perror_msg(fileName);
  349. return TRUE;
  350. }
  351. #endif
  352. #if ENABLE_FEATURE_FIND_CONTEXT
  353. ACTF(context)
  354. {
  355. security_context_t con;
  356. int rc;
  357. if (G.recurse_flags & ACTION_FOLLOWLINKS) {
  358. rc = getfilecon(fileName, &con);
  359. } else {
  360. rc = lgetfilecon(fileName, &con);
  361. }
  362. if (rc < 0)
  363. return FALSE;
  364. rc = strcmp(ap->context, con);
  365. freecon(con);
  366. return rc == 0;
  367. }
  368. #endif
  369. #if ENABLE_FEATURE_FIND_LINKS
  370. ACTF(links)
  371. {
  372. switch(ap->links_char) {
  373. case '-' : return (statbuf->st_nlink < ap->links_count);
  374. case '+' : return (statbuf->st_nlink > ap->links_count);
  375. default: return (statbuf->st_nlink == ap->links_count);
  376. }
  377. }
  378. #endif
  379. static int FAST_FUNC fileAction(const char *fileName,
  380. struct stat *statbuf,
  381. void *userData IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
  382. int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
  383. {
  384. int r;
  385. #if ENABLE_FEATURE_FIND_MAXDEPTH
  386. #define minmaxdepth ((int*)userData)
  387. if (depth < minmaxdepth[0])
  388. return TRUE; /* skip this, continue recursing */
  389. if (depth > minmaxdepth[1])
  390. return SKIP; /* stop recursing */
  391. #endif
  392. r = exec_actions(G.actions, fileName, statbuf);
  393. /* Had no explicit -print[0] or -exec? then print */
  394. if ((r & TRUE) && G.need_print)
  395. puts(fileName);
  396. #if ENABLE_FEATURE_FIND_MAXDEPTH
  397. if (S_ISDIR(statbuf->st_mode)) {
  398. if (depth == minmaxdepth[1])
  399. return SKIP;
  400. }
  401. #endif
  402. #if ENABLE_FEATURE_FIND_XDEV
  403. /* -xdev stops on mountpoints, but AFTER mountpoit itself
  404. * is processed as usual */
  405. if (S_ISDIR(statbuf->st_mode)) {
  406. if (G.xdev_count) {
  407. int i;
  408. for (i = 0; i < G.xdev_count; i++) {
  409. if (G.xdev_dev[i] == statbuf->st_dev)
  410. goto found;
  411. }
  412. return SKIP;
  413. found: ;
  414. }
  415. }
  416. #endif
  417. /* Cannot return 0: our caller, recursive_action(),
  418. * will perror() and skip dirs (if called on dir) */
  419. return (r & SKIP) ? SKIP : TRUE;
  420. #undef minmaxdepth
  421. }
  422. #if ENABLE_FEATURE_FIND_TYPE
  423. static int find_type(const char *type)
  424. {
  425. int mask = 0;
  426. if (*type == 'b')
  427. mask = S_IFBLK;
  428. else if (*type == 'c')
  429. mask = S_IFCHR;
  430. else if (*type == 'd')
  431. mask = S_IFDIR;
  432. else if (*type == 'p')
  433. mask = S_IFIFO;
  434. else if (*type == 'f')
  435. mask = S_IFREG;
  436. else if (*type == 'l')
  437. mask = S_IFLNK;
  438. else if (*type == 's')
  439. mask = S_IFSOCK;
  440. if (mask == 0 || type[1] != '\0')
  441. bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
  442. return mask;
  443. }
  444. #endif
  445. #if ENABLE_FEATURE_FIND_PERM \
  446. || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
  447. || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
  448. static const char* plus_minus_num(const char* str)
  449. {
  450. if (*str == '-' || *str == '+')
  451. str++;
  452. return str;
  453. }
  454. #endif
  455. static action*** parse_params(char **argv)
  456. {
  457. enum {
  458. PARM_a ,
  459. PARM_o ,
  460. IF_FEATURE_FIND_NOT( PARM_char_not ,)
  461. #if ENABLE_DESKTOP
  462. PARM_and ,
  463. PARM_or ,
  464. IF_FEATURE_FIND_NOT( PARM_not ,)
  465. #endif
  466. PARM_print ,
  467. IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
  468. IF_FEATURE_FIND_DEPTH( PARM_depth ,)
  469. IF_FEATURE_FIND_PRUNE( PARM_prune ,)
  470. IF_FEATURE_FIND_DELETE( PARM_delete ,)
  471. IF_FEATURE_FIND_EXEC( PARM_exec ,)
  472. IF_FEATURE_FIND_PAREN( PARM_char_brace,)
  473. /* All options starting from here require argument */
  474. PARM_name ,
  475. PARM_iname ,
  476. IF_FEATURE_FIND_PATH( PARM_path ,)
  477. IF_FEATURE_FIND_REGEX( PARM_regex ,)
  478. IF_FEATURE_FIND_TYPE( PARM_type ,)
  479. IF_FEATURE_FIND_PERM( PARM_perm ,)
  480. IF_FEATURE_FIND_MTIME( PARM_mtime ,)
  481. IF_FEATURE_FIND_MMIN( PARM_mmin ,)
  482. IF_FEATURE_FIND_NEWER( PARM_newer ,)
  483. IF_FEATURE_FIND_INUM( PARM_inum ,)
  484. IF_FEATURE_FIND_USER( PARM_user ,)
  485. IF_FEATURE_FIND_GROUP( PARM_group ,)
  486. IF_FEATURE_FIND_SIZE( PARM_size ,)
  487. IF_FEATURE_FIND_CONTEXT(PARM_context ,)
  488. IF_FEATURE_FIND_LINKS( PARM_links ,)
  489. };
  490. static const char params[] ALIGN1 =
  491. "-a\0"
  492. "-o\0"
  493. IF_FEATURE_FIND_NOT( "!\0" )
  494. #if ENABLE_DESKTOP
  495. "-and\0"
  496. "-or\0"
  497. IF_FEATURE_FIND_NOT( "-not\0" )
  498. #endif
  499. "-print\0"
  500. IF_FEATURE_FIND_PRINT0( "-print0\0" )
  501. IF_FEATURE_FIND_DEPTH( "-depth\0" )
  502. IF_FEATURE_FIND_PRUNE( "-prune\0" )
  503. IF_FEATURE_FIND_DELETE( "-delete\0" )
  504. IF_FEATURE_FIND_EXEC( "-exec\0" )
  505. IF_FEATURE_FIND_PAREN( "(\0" )
  506. /* All options starting from here require argument */
  507. "-name\0"
  508. "-iname\0"
  509. IF_FEATURE_FIND_PATH( "-path\0" )
  510. IF_FEATURE_FIND_REGEX( "-regex\0" )
  511. IF_FEATURE_FIND_TYPE( "-type\0" )
  512. IF_FEATURE_FIND_PERM( "-perm\0" )
  513. IF_FEATURE_FIND_MTIME( "-mtime\0" )
  514. IF_FEATURE_FIND_MMIN( "-mmin\0" )
  515. IF_FEATURE_FIND_NEWER( "-newer\0" )
  516. IF_FEATURE_FIND_INUM( "-inum\0" )
  517. IF_FEATURE_FIND_USER( "-user\0" )
  518. IF_FEATURE_FIND_GROUP( "-group\0" )
  519. IF_FEATURE_FIND_SIZE( "-size\0" )
  520. IF_FEATURE_FIND_CONTEXT("-context\0")
  521. IF_FEATURE_FIND_LINKS( "-links\0" )
  522. ;
  523. action*** appp;
  524. unsigned cur_group = 0;
  525. unsigned cur_action = 0;
  526. IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
  527. /* This is the only place in busybox where we use nested function.
  528. * So far more standard alternatives were bigger. */
  529. /* Suppress a warning "func without a prototype" */
  530. auto action* alloc_action(int sizeof_struct, action_fp f);
  531. action* alloc_action(int sizeof_struct, action_fp f)
  532. {
  533. action *ap;
  534. appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
  535. appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
  536. appp[cur_group][cur_action] = NULL;
  537. ap->f = f;
  538. IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
  539. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  540. return ap;
  541. }
  542. #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
  543. appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
  544. /* Actions have side effects and return a true or false value
  545. * We implement: -print, -print0, -exec
  546. *
  547. * The rest are tests.
  548. *
  549. * Tests and actions are grouped by operators
  550. * ( expr ) Force precedence
  551. * ! expr True if expr is false
  552. * -not expr Same as ! expr
  553. * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
  554. * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
  555. * expr1 , expr2 List; both expr1 and expr2 are always evaluated
  556. * We implement: (), -a, -o
  557. */
  558. while (*argv) {
  559. const char *arg = argv[0];
  560. int parm = index_in_strings(params, arg);
  561. const char *arg1 = argv[1];
  562. if (parm >= PARM_name) {
  563. /* All options starting from -name require argument */
  564. if (!arg1)
  565. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  566. argv++;
  567. }
  568. /* We can use big switch() here, but on i386
  569. * it doesn't give smaller code. Other arches? */
  570. /* --- Operators --- */
  571. if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
  572. /* no further special handling required */
  573. }
  574. else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
  575. /* start new OR group */
  576. cur_group++;
  577. appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
  578. /*appp[cur_group] = NULL; - already NULL */
  579. appp[cur_group+1] = NULL;
  580. cur_action = 0;
  581. }
  582. #if ENABLE_FEATURE_FIND_NOT
  583. else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
  584. /* also handles "find ! ! -name 'foo*'" */
  585. invert_flag ^= 1;
  586. }
  587. #endif
  588. /* --- Tests and actions --- */
  589. else if (parm == PARM_print) {
  590. G.need_print = 0;
  591. /* GNU find ignores '!' here: "find ! -print" */
  592. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  593. (void) ALLOC_ACTION(print);
  594. }
  595. #if ENABLE_FEATURE_FIND_PRINT0
  596. else if (parm == PARM_print0) {
  597. G.need_print = 0;
  598. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  599. (void) ALLOC_ACTION(print0);
  600. }
  601. #endif
  602. #if ENABLE_FEATURE_FIND_DEPTH
  603. else if (parm == PARM_depth) {
  604. G.recurse_flags |= ACTION_DEPTHFIRST;
  605. }
  606. #endif
  607. #if ENABLE_FEATURE_FIND_PRUNE
  608. else if (parm == PARM_prune) {
  609. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  610. (void) ALLOC_ACTION(prune);
  611. }
  612. #endif
  613. #if ENABLE_FEATURE_FIND_DELETE
  614. else if (parm == PARM_delete) {
  615. G.need_print = 0;
  616. G.recurse_flags |= ACTION_DEPTHFIRST;
  617. (void) ALLOC_ACTION(delete);
  618. }
  619. #endif
  620. #if ENABLE_FEATURE_FIND_EXEC
  621. else if (parm == PARM_exec) {
  622. int i;
  623. action_exec *ap;
  624. G.need_print = 0;
  625. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  626. ap = ALLOC_ACTION(exec);
  627. ap->exec_argv = ++argv; /* first arg after -exec */
  628. ap->exec_argc = 0;
  629. while (1) {
  630. if (!*argv) /* did not see ';' until end */
  631. bb_error_msg_and_die("-exec CMD must end by ';'");
  632. if (LONE_CHAR(argv[0], ';'))
  633. break;
  634. argv++;
  635. ap->exec_argc++;
  636. }
  637. if (ap->exec_argc == 0)
  638. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  639. ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
  640. i = ap->exec_argc;
  641. while (i--)
  642. ap->subst_count[i] = count_subst(ap->exec_argv[i]);
  643. }
  644. #endif
  645. #if ENABLE_FEATURE_FIND_PAREN
  646. else if (parm == PARM_char_brace) {
  647. action_paren *ap;
  648. char **endarg;
  649. unsigned nested = 1;
  650. endarg = argv;
  651. while (1) {
  652. if (!*++endarg)
  653. bb_error_msg_and_die("unpaired '('");
  654. if (LONE_CHAR(*endarg, '('))
  655. nested++;
  656. else if (LONE_CHAR(*endarg, ')') && !--nested) {
  657. *endarg = NULL;
  658. break;
  659. }
  660. }
  661. ap = ALLOC_ACTION(paren);
  662. ap->subexpr = parse_params(argv + 1);
  663. *endarg = (char*) ")"; /* restore NULLed parameter */
  664. argv = endarg;
  665. }
  666. #endif
  667. else if (parm == PARM_name || parm == PARM_iname) {
  668. action_name *ap;
  669. ap = ALLOC_ACTION(name);
  670. ap->pattern = arg1;
  671. ap->iname = (parm == PARM_iname);
  672. }
  673. #if ENABLE_FEATURE_FIND_PATH
  674. else if (parm == PARM_path) {
  675. action_path *ap;
  676. ap = ALLOC_ACTION(path);
  677. ap->pattern = arg1;
  678. }
  679. #endif
  680. #if ENABLE_FEATURE_FIND_REGEX
  681. else if (parm == PARM_regex) {
  682. action_regex *ap;
  683. ap = ALLOC_ACTION(regex);
  684. xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
  685. }
  686. #endif
  687. #if ENABLE_FEATURE_FIND_TYPE
  688. else if (parm == PARM_type) {
  689. action_type *ap;
  690. ap = ALLOC_ACTION(type);
  691. ap->type_mask = find_type(arg1);
  692. }
  693. #endif
  694. #if ENABLE_FEATURE_FIND_PERM
  695. /* -perm mode File's permission bits are exactly mode (octal or symbolic).
  696. * Symbolic modes use mode 0 as a point of departure.
  697. * -perm -mode All of the permission bits mode are set for the file.
  698. * -perm +mode Any of the permission bits mode are set for the file.
  699. */
  700. else if (parm == PARM_perm) {
  701. action_perm *ap;
  702. ap = ALLOC_ACTION(perm);
  703. ap->perm_char = arg1[0];
  704. arg1 = plus_minus_num(arg1);
  705. ap->perm_mask = 0;
  706. if (!bb_parse_mode(arg1, &ap->perm_mask))
  707. bb_error_msg_and_die("invalid mode: %s", arg1);
  708. }
  709. #endif
  710. #if ENABLE_FEATURE_FIND_MTIME
  711. else if (parm == PARM_mtime) {
  712. action_mtime *ap;
  713. ap = ALLOC_ACTION(mtime);
  714. ap->mtime_char = arg1[0];
  715. ap->mtime_days = xatoul(plus_minus_num(arg1));
  716. }
  717. #endif
  718. #if ENABLE_FEATURE_FIND_MMIN
  719. else if (parm == PARM_mmin) {
  720. action_mmin *ap;
  721. ap = ALLOC_ACTION(mmin);
  722. ap->mmin_char = arg1[0];
  723. ap->mmin_mins = xatoul(plus_minus_num(arg1));
  724. }
  725. #endif
  726. #if ENABLE_FEATURE_FIND_NEWER
  727. else if (parm == PARM_newer) {
  728. struct stat stat_newer;
  729. action_newer *ap;
  730. ap = ALLOC_ACTION(newer);
  731. xstat(arg1, &stat_newer);
  732. ap->newer_mtime = stat_newer.st_mtime;
  733. }
  734. #endif
  735. #if ENABLE_FEATURE_FIND_INUM
  736. else if (parm == PARM_inum) {
  737. action_inum *ap;
  738. ap = ALLOC_ACTION(inum);
  739. ap->inode_num = xatoul(arg1);
  740. }
  741. #endif
  742. #if ENABLE_FEATURE_FIND_USER
  743. else if (parm == PARM_user) {
  744. action_user *ap;
  745. ap = ALLOC_ACTION(user);
  746. ap->uid = bb_strtou(arg1, NULL, 10);
  747. if (errno)
  748. ap->uid = xuname2uid(arg1);
  749. }
  750. #endif
  751. #if ENABLE_FEATURE_FIND_GROUP
  752. else if (parm == PARM_group) {
  753. action_group *ap;
  754. ap = ALLOC_ACTION(group);
  755. ap->gid = bb_strtou(arg1, NULL, 10);
  756. if (errno)
  757. ap->gid = xgroup2gid(arg1);
  758. }
  759. #endif
  760. #if ENABLE_FEATURE_FIND_SIZE
  761. else if (parm == PARM_size) {
  762. /* -size n[bckw]: file uses n units of space
  763. * b (default): units are 512-byte blocks
  764. * c: 1 byte
  765. * k: kilobytes
  766. * w: 2-byte words
  767. */
  768. #if ENABLE_LFS
  769. #define XATOU_SFX xatoull_sfx
  770. #else
  771. #define XATOU_SFX xatoul_sfx
  772. #endif
  773. static const struct suffix_mult find_suffixes[] = {
  774. { "c", 1 },
  775. { "w", 2 },
  776. { "", 512 },
  777. { "b", 512 },
  778. { "k", 1024 },
  779. { "", 0 }
  780. };
  781. action_size *ap;
  782. ap = ALLOC_ACTION(size);
  783. ap->size_char = arg1[0];
  784. ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
  785. }
  786. #endif
  787. #if ENABLE_FEATURE_FIND_CONTEXT
  788. else if (parm == PARM_context) {
  789. action_context *ap;
  790. ap = ALLOC_ACTION(context);
  791. ap->context = NULL;
  792. /* SELinux headers erroneously declare non-const parameter */
  793. if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
  794. bb_simple_perror_msg(arg1);
  795. }
  796. #endif
  797. #if ENABLE_FEATURE_FIND_LINKS
  798. else if (parm == PARM_links) {
  799. action_links *ap;
  800. ap = ALLOC_ACTION(links);
  801. ap->links_char = arg1[0];
  802. ap->links_count = xatoul(plus_minus_num(arg1));
  803. }
  804. #endif
  805. else {
  806. bb_error_msg("unrecognized: %s", arg);
  807. bb_show_usage();
  808. }
  809. argv++;
  810. }
  811. return appp;
  812. #undef ALLOC_ACTION
  813. }
  814. int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  815. int find_main(int argc UNUSED_PARAM, char **argv)
  816. {
  817. static const char options[] ALIGN1 =
  818. "-follow\0"
  819. IF_FEATURE_FIND_XDEV( "-xdev\0" )
  820. IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
  821. ;
  822. enum {
  823. OPT_FOLLOW,
  824. IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
  825. IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,)
  826. };
  827. char *arg;
  828. char **argp;
  829. int i, firstopt, status = EXIT_SUCCESS;
  830. #if ENABLE_FEATURE_FIND_MAXDEPTH
  831. int minmaxdepth[2] = { 0, INT_MAX };
  832. #else
  833. #define minmaxdepth NULL
  834. #endif
  835. INIT_G();
  836. for (firstopt = 1; argv[firstopt]; firstopt++) {
  837. if (argv[firstopt][0] == '-')
  838. break;
  839. if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
  840. break;
  841. #if ENABLE_FEATURE_FIND_PAREN
  842. if (LONE_CHAR(argv[firstopt], '('))
  843. break;
  844. #endif
  845. }
  846. if (firstopt == 1) {
  847. argv[0] = (char*)".";
  848. argv--;
  849. firstopt++;
  850. }
  851. /* All options always return true. They always take effect
  852. * rather than being processed only when their place in the
  853. * expression is reached.
  854. * We implement: -follow, -xdev, -maxdepth
  855. */
  856. /* Process options, and replace then with -a */
  857. /* (-a will be ignored by recursive parser later) */
  858. argp = &argv[firstopt];
  859. while ((arg = argp[0])) {
  860. int opt = index_in_strings(options, arg);
  861. if (opt == OPT_FOLLOW) {
  862. G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
  863. argp[0] = (char*)"-a";
  864. }
  865. #if ENABLE_FEATURE_FIND_XDEV
  866. if (opt == OPT_XDEV) {
  867. struct stat stbuf;
  868. if (!G.xdev_count) {
  869. G.xdev_count = firstopt - 1;
  870. G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
  871. for (i = 1; i < firstopt; i++) {
  872. /* not xstat(): shouldn't bomb out on
  873. * "find not_exist exist -xdev" */
  874. if (stat(argv[i], &stbuf) == 0)
  875. G.xdev_dev[i-1] = stbuf.st_dev;
  876. /* else G.xdev_dev[i-1] stays 0 and
  877. * won't match any real device dev_t */
  878. }
  879. }
  880. argp[0] = (char*)"-a";
  881. }
  882. #endif
  883. #if ENABLE_FEATURE_FIND_MAXDEPTH
  884. if (opt == OPT_MINDEPTH || opt == OPT_MINDEPTH + 1) {
  885. if (!argp[1])
  886. bb_show_usage();
  887. minmaxdepth[opt - OPT_MINDEPTH] = xatoi_u(argp[1]);
  888. argp[0] = (char*)"-a";
  889. argp[1] = (char*)"-a";
  890. argp++;
  891. }
  892. #endif
  893. argp++;
  894. }
  895. G.actions = parse_params(&argv[firstopt]);
  896. for (i = 1; i < firstopt; i++) {
  897. if (!recursive_action(argv[i],
  898. G.recurse_flags,/* flags */
  899. fileAction, /* file action */
  900. fileAction, /* dir action */
  901. #if ENABLE_FEATURE_FIND_MAXDEPTH
  902. minmaxdepth, /* user data */
  903. #else
  904. NULL, /* user data */
  905. #endif
  906. 0)) /* depth */
  907. status = EXIT_FAILURE;
  908. }
  909. return status;
  910. }