find.c 23 KB

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