find.c 24 KB

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