find.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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 GPLv2, see file LICENSE in this source tree.
  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. //config:config FIND
  54. //config: bool "find"
  55. //config: default y
  56. //config: help
  57. //config: find is used to search your system to find specified files.
  58. //config:
  59. //config:config FEATURE_FIND_PRINT0
  60. //config: bool "Enable -print0: NUL-terminated output"
  61. //config: default y
  62. //config: depends on FIND
  63. //config: help
  64. //config: Causes output names to be separated by a NUL character
  65. //config: rather than a newline. This allows names that contain
  66. //config: newlines and other whitespace to be more easily
  67. //config: interpreted by other programs.
  68. //config:
  69. //config:config FEATURE_FIND_MTIME
  70. //config: bool "Enable -mtime: modified time matching"
  71. //config: default y
  72. //config: depends on FIND
  73. //config: help
  74. //config: Allow searching based on the modification time of
  75. //config: files, in days.
  76. //config:
  77. //config:config FEATURE_FIND_MMIN
  78. //config: bool "Enable -mmin: modified time matching by minutes"
  79. //config: default y
  80. //config: depends on FIND
  81. //config: help
  82. //config: Allow searching based on the modification time of
  83. //config: files, in minutes.
  84. //config:
  85. //config:config FEATURE_FIND_PERM
  86. //config: bool "Enable -perm: permissions matching"
  87. //config: default y
  88. //config: depends on FIND
  89. //config: help
  90. //config: Enable searching based on file permissions.
  91. //config:
  92. //config:config FEATURE_FIND_TYPE
  93. //config: bool "Enable -type: file type matching (file/dir/link/...)"
  94. //config: default y
  95. //config: depends on FIND
  96. //config: help
  97. //config: Enable searching based on file type (file,
  98. //config: directory, socket, device, etc.).
  99. //config:
  100. //config:config FEATURE_FIND_XDEV
  101. //config: bool "Enable -xdev: 'stay in filesystem'"
  102. //config: default y
  103. //config: depends on FIND
  104. //config: help
  105. //config: This option allows find to restrict searches to a single filesystem.
  106. //config:
  107. //config:config FEATURE_FIND_MAXDEPTH
  108. //config: bool "Enable -mindepth N and -maxdepth N"
  109. //config: default y
  110. //config: depends on FIND
  111. //config: help
  112. //config: This option enables -mindepth N and -maxdepth N option.
  113. //config:
  114. //config:config FEATURE_FIND_NEWER
  115. //config: bool "Enable -newer: compare file modification times"
  116. //config: default y
  117. //config: depends on FIND
  118. //config: help
  119. //config: Support the 'find -newer' option for finding any files which have
  120. //config: modification time that is more recent than the specified FILE.
  121. //config:
  122. //config:config FEATURE_FIND_INUM
  123. //config: bool "Enable -inum: inode number matching"
  124. //config: default y
  125. //config: depends on FIND
  126. //config: help
  127. //config: Support the 'find -inum' option for searching by inode number.
  128. //config:
  129. //config:config FEATURE_FIND_EXEC
  130. //config: bool "Enable -exec: execute commands"
  131. //config: default y
  132. //config: depends on FIND
  133. //config: help
  134. //config: Support the 'find -exec' option for executing commands based upon
  135. //config: the files matched.
  136. //config:
  137. //config:config FEATURE_FIND_USER
  138. //config: bool "Enable -user: username/uid matching"
  139. //config: default y
  140. //config: depends on FIND
  141. //config: help
  142. //config: Support the 'find -user' option for searching by username or uid.
  143. //config:
  144. //config:config FEATURE_FIND_GROUP
  145. //config: bool "Enable -group: group/gid matching"
  146. //config: default y
  147. //config: depends on FIND
  148. //config: help
  149. //config: Support the 'find -group' option for searching by group name or gid.
  150. //config:
  151. //config:config FEATURE_FIND_NOT
  152. //config: bool "Enable the 'not' (!) operator"
  153. //config: default y
  154. //config: depends on FIND
  155. //config: help
  156. //config: Support the '!' operator to invert the test results.
  157. //config: If 'Enable full-blown desktop' is enabled, then will also support
  158. //config: the non-POSIX notation '-not'.
  159. //config:
  160. //config:config FEATURE_FIND_DEPTH
  161. //config: bool "Enable -depth"
  162. //config: default y
  163. //config: depends on FIND
  164. //config: help
  165. //config: Process each directory's contents before the directory itself.
  166. //config:
  167. //config:config FEATURE_FIND_PAREN
  168. //config: bool "Enable parens in options"
  169. //config: default y
  170. //config: depends on FIND
  171. //config: help
  172. //config: Enable usage of parens '(' to specify logical order of arguments.
  173. //config:
  174. //config:config FEATURE_FIND_SIZE
  175. //config: bool "Enable -size: file size matching"
  176. //config: default y
  177. //config: depends on FIND
  178. //config: help
  179. //config: Support the 'find -size' option for searching by file size.
  180. //config:
  181. //config:config FEATURE_FIND_PRUNE
  182. //config: bool "Enable -prune: exclude subdirectories"
  183. //config: default y
  184. //config: depends on FIND
  185. //config: help
  186. //config: If the file is a directory, dont descend into it. Useful for
  187. //config: exclusion .svn and CVS directories.
  188. //config:
  189. //config:config FEATURE_FIND_DELETE
  190. //config: bool "Enable -delete: delete files/dirs"
  191. //config: default y
  192. //config: depends on FIND && FEATURE_FIND_DEPTH
  193. //config: help
  194. //config: Support the 'find -delete' option for deleting files and directories.
  195. //config: WARNING: This option can do much harm if used wrong. Busybox will not
  196. //config: try to protect the user from doing stupid things. Use with care.
  197. //config:
  198. //config:config FEATURE_FIND_PATH
  199. //config: bool "Enable -path: match pathname with shell pattern"
  200. //config: default y
  201. //config: depends on FIND
  202. //config: help
  203. //config: The -path option matches whole pathname instead of just filename.
  204. //config:
  205. //config:config FEATURE_FIND_REGEX
  206. //config: bool "Enable -regex: match pathname with regex"
  207. //config: default y
  208. //config: depends on FIND
  209. //config: help
  210. //config: The -regex option matches whole pathname against regular expression.
  211. //config:
  212. //config:config FEATURE_FIND_CONTEXT
  213. //config: bool "Enable -context: security context matching"
  214. //config: default n
  215. //config: depends on FIND && SELINUX
  216. //config: help
  217. //config: Support the 'find -context' option for matching security context.
  218. //config:
  219. //config:config FEATURE_FIND_LINKS
  220. //config: bool "Enable -links: link count matching"
  221. //config: default y
  222. //config: depends on FIND
  223. //config: help
  224. //config: Support the 'find -links' option for matching number of links.
  225. //applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
  226. //kbuild:lib-$(CONFIG_FIND) += find.o
  227. //usage:#define find_trivial_usage
  228. //usage: "[PATH]... [OPTIONS] [ACTIONS]"
  229. //usage:#define find_full_usage "\n\n"
  230. //usage: "Search for files and perform actions on them.\n"
  231. //usage: "First failed action stops processing of current file.\n"
  232. //usage: "Defaults: PATH is current directory, action is '-print'\n"
  233. //usage: "\n -follow Follow symlinks"
  234. //usage: IF_FEATURE_FIND_XDEV(
  235. //usage: "\n -xdev Don't descend directories on other filesystems"
  236. //usage: )
  237. //usage: IF_FEATURE_FIND_MAXDEPTH(
  238. //usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
  239. //usage: "\n actions to command line arguments only"
  240. //usage: "\n -mindepth N Don't act on first N levels"
  241. //usage: )
  242. //usage: IF_FEATURE_FIND_DEPTH(
  243. //usage: "\n -depth Act on directory *after* traversing it"
  244. //usage: )
  245. //usage: "\n"
  246. //usage: "\nActions:"
  247. //usage: IF_FEATURE_FIND_PAREN(
  248. //usage: "\n ( ACTIONS ) Group actions for -o / -a"
  249. //usage: )
  250. //usage: IF_FEATURE_FIND_NOT(
  251. //usage: "\n ! ACT Invert ACT's success/failure"
  252. //usage: )
  253. //usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
  254. //usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
  255. //usage: "\n Note: -a has higher priority than -o"
  256. //usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
  257. //usage: "\n -iname PATTERN Case insensitive -name"
  258. //usage: IF_FEATURE_FIND_PATH(
  259. //usage: "\n -path PATTERN Match path to PATTERN"
  260. //usage: "\n -ipath PATTERN Case insensitive -path"
  261. //usage: )
  262. //usage: IF_FEATURE_FIND_REGEX(
  263. //usage: "\n -regex PATTERN Match path to regex PATTERN"
  264. //usage: )
  265. //usage: IF_FEATURE_FIND_TYPE(
  266. //usage: "\n -type X File type is X (one of: f,d,l,b,c,...)"
  267. //usage: )
  268. //usage: IF_FEATURE_FIND_PERM(
  269. //usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
  270. //usage: "\n or exactly MASK bits are set in file's mode"
  271. //usage: )
  272. //usage: IF_FEATURE_FIND_MTIME(
  273. //usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
  274. //usage: "\n or exactly N days in the past"
  275. //usage: )
  276. //usage: IF_FEATURE_FIND_MMIN(
  277. //usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
  278. //usage: "\n or exactly N minutes in the past"
  279. //usage: )
  280. //usage: IF_FEATURE_FIND_NEWER(
  281. //usage: "\n -newer FILE mtime is more recent than FILE's"
  282. //usage: )
  283. //usage: IF_FEATURE_FIND_INUM(
  284. //usage: "\n -inum N File has inode number N"
  285. //usage: )
  286. //usage: IF_FEATURE_FIND_USER(
  287. //usage: "\n -user NAME/ID File is owned by given user"
  288. //usage: )
  289. //usage: IF_FEATURE_FIND_GROUP(
  290. //usage: "\n -group NAME/ID File is owned by given group"
  291. //usage: )
  292. //usage: IF_FEATURE_FIND_SIZE(
  293. //usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
  294. //usage: "\n +/-N: file size is bigger/smaller than N"
  295. //usage: )
  296. //usage: IF_FEATURE_FIND_LINKS(
  297. //usage: "\n -links N Number of links is greater than (+N), less than (-N),"
  298. //usage: "\n or exactly N"
  299. //usage: )
  300. //usage: IF_FEATURE_FIND_CONTEXT(
  301. //usage: "\n -context CTX File has specified security context"
  302. //usage: )
  303. //usage: IF_FEATURE_FIND_PRUNE(
  304. //usage: "\n -prune If current file is directory, don't descend into it"
  305. //usage: )
  306. //usage: "\nIf none of the following actions is specified, -print is assumed"
  307. //usage: "\n -print Print file name"
  308. //usage: IF_FEATURE_FIND_PRINT0(
  309. //usage: "\n -print0 Print file name, NUL terminated"
  310. //usage: )
  311. //usage: IF_FEATURE_FIND_EXEC(
  312. //usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
  313. //usage: "\n file name. Fails if CMD exits with nonzero"
  314. //usage: )
  315. //usage: IF_FEATURE_FIND_DELETE(
  316. //usage: "\n -delete Delete current file/directory. Turns on -depth option"
  317. //usage: )
  318. //usage:
  319. //usage:#define find_example_usage
  320. //usage: "$ find / -name passwd\n"
  321. //usage: "/etc/passwd\n"
  322. #include <fnmatch.h>
  323. #include "libbb.h"
  324. #if ENABLE_FEATURE_FIND_REGEX
  325. # include "xregex.h"
  326. #endif
  327. /* GNUism: */
  328. #ifndef FNM_CASEFOLD
  329. # define FNM_CASEFOLD 0
  330. #endif
  331. #define dbg(...) ((void)0)
  332. /* #define dbg(...) bb_error_msg(__VA_ARGS__) */
  333. /* This is a NOEXEC applet. Be very careful! */
  334. typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
  335. typedef struct {
  336. action_fp f;
  337. #if ENABLE_FEATURE_FIND_NOT
  338. bool invert;
  339. #endif
  340. } action;
  341. #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
  342. #define ACTF(name) \
  343. static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
  344. const struct stat *statbuf UNUSED_PARAM, \
  345. action_##name* ap UNUSED_PARAM)
  346. ACTS(print)
  347. ACTS(name, const char *pattern; bool iname;)
  348. IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
  349. IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
  350. IF_FEATURE_FIND_PRINT0( ACTS(print0))
  351. IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
  352. IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
  353. IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
  354. IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
  355. IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
  356. IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
  357. IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
  358. IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
  359. IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
  360. IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
  361. IF_FEATURE_FIND_PRUNE( ACTS(prune))
  362. IF_FEATURE_FIND_DELETE( ACTS(delete))
  363. IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
  364. IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
  365. IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
  366. struct globals {
  367. IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
  368. IF_FEATURE_FIND_XDEV(int xdev_count;)
  369. #if ENABLE_FEATURE_FIND_MAXDEPTH
  370. int minmaxdepth[2];
  371. #endif
  372. action ***actions;
  373. smallint need_print;
  374. smallint xdev_on;
  375. recurse_flags_t recurse_flags;
  376. } FIX_ALIASING;
  377. #define G (*(struct globals*)&bb_common_bufsiz1)
  378. #define INIT_G() do { \
  379. struct G_sizecheck { \
  380. char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
  381. }; \
  382. /* we have to zero it out because of NOEXEC */ \
  383. memset(&G, 0, sizeof(G)); \
  384. IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
  385. G.need_print = 1; \
  386. G.recurse_flags = ACTION_RECURSE; \
  387. } while (0)
  388. #if ENABLE_FEATURE_FIND_EXEC
  389. static unsigned count_subst(const char *str)
  390. {
  391. unsigned count = 0;
  392. while ((str = strstr(str, "{}")) != NULL) {
  393. count++;
  394. str++;
  395. }
  396. return count;
  397. }
  398. static char* subst(const char *src, unsigned count, const char* filename)
  399. {
  400. char *buf, *dst, *end;
  401. size_t flen = strlen(filename);
  402. /* we replace each '{}' with filename: growth by strlen-2 */
  403. buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
  404. while ((end = strstr(src, "{}"))) {
  405. memcpy(dst, src, end - src);
  406. dst += end - src;
  407. src = end + 2;
  408. memcpy(dst, filename, flen);
  409. dst += flen;
  410. }
  411. strcpy(dst, src);
  412. return buf;
  413. }
  414. #endif
  415. /* Return values of ACTFs ('action functions') are a bit mask:
  416. * bit 1=1: prune (use SKIP constant for setting it)
  417. * bit 0=1: matched successfully (TRUE)
  418. */
  419. static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
  420. {
  421. int cur_group;
  422. int cur_action;
  423. int rc = 0;
  424. action **app, *ap;
  425. /* "action group" is a set of actions ANDed together.
  426. * groups are ORed together.
  427. * We simply evaluate each group until we find one in which all actions
  428. * succeed. */
  429. /* -prune is special: if it is encountered, then we won't
  430. * descend into current directory. It doesn't matter whether
  431. * action group (in which -prune sits) will succeed or not:
  432. * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
  433. * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
  434. * not starting with 'f' */
  435. /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
  436. * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
  437. * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
  438. * On return, bit is restored. */
  439. cur_group = -1;
  440. while ((app = appp[++cur_group]) != NULL) {
  441. rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
  442. cur_action = -1;
  443. while (1) {
  444. ap = app[++cur_action];
  445. if (!ap) /* all actions in group were successful */
  446. return rc ^ TRUE; /* restore TRUE bit */
  447. rc |= TRUE ^ ap->f(fileName, statbuf, ap);
  448. #if ENABLE_FEATURE_FIND_NOT
  449. if (ap->invert) rc ^= TRUE;
  450. #endif
  451. dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
  452. if (rc & TRUE) /* current group failed, try next */
  453. break;
  454. }
  455. }
  456. dbg("returning:0x%x", rc ^ TRUE);
  457. return rc ^ TRUE; /* restore TRUE bit */
  458. }
  459. #if !FNM_CASEFOLD
  460. static char *strcpy_upcase(char *dst, const char *src)
  461. {
  462. char *d = dst;
  463. while (1) {
  464. unsigned char ch = *src++;
  465. if (ch >= 'a' && ch <= 'z')
  466. ch -= ('a' - 'A');
  467. *d++ = ch;
  468. if (ch == '\0')
  469. break;
  470. }
  471. return dst;
  472. }
  473. #endif
  474. ACTF(name)
  475. {
  476. const char *tmp = bb_basename(fileName);
  477. if (tmp != fileName && *tmp == '\0') {
  478. /* "foo/bar/". Oh no... go back to 'b' */
  479. tmp--;
  480. while (tmp != fileName && *--tmp != '/')
  481. continue;
  482. if (*tmp == '/')
  483. tmp++;
  484. }
  485. /* Was using FNM_PERIOD flag too,
  486. * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
  487. * find -name '*foo' should match .foo too:
  488. */
  489. #if FNM_CASEFOLD
  490. return fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)) == 0;
  491. #else
  492. if (ap->iname)
  493. tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
  494. return fnmatch(ap->pattern, tmp, 0) == 0;
  495. #endif
  496. }
  497. #if ENABLE_FEATURE_FIND_PATH
  498. ACTF(path)
  499. {
  500. # if FNM_CASEFOLD
  501. return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
  502. # else
  503. if (ap->ipath)
  504. fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
  505. return fnmatch(ap->pattern, fileName, 0) == 0;
  506. # endif
  507. }
  508. #endif
  509. #if ENABLE_FEATURE_FIND_REGEX
  510. ACTF(regex)
  511. {
  512. regmatch_t match;
  513. if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
  514. return 0; /* no match */
  515. if (match.rm_so)
  516. return 0; /* match doesn't start at pos 0 */
  517. if (fileName[match.rm_eo])
  518. return 0; /* match doesn't end exactly at end of pathname */
  519. return 1;
  520. }
  521. #endif
  522. #if ENABLE_FEATURE_FIND_TYPE
  523. ACTF(type)
  524. {
  525. return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
  526. }
  527. #endif
  528. #if ENABLE_FEATURE_FIND_PERM
  529. ACTF(perm)
  530. {
  531. /* -perm +mode: at least one of perm_mask bits are set */
  532. if (ap->perm_char == '+')
  533. return (statbuf->st_mode & ap->perm_mask) != 0;
  534. /* -perm -mode: all of perm_mask are set */
  535. if (ap->perm_char == '-')
  536. return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
  537. /* -perm mode: file mode must match perm_mask */
  538. return (statbuf->st_mode & 07777) == ap->perm_mask;
  539. }
  540. #endif
  541. #if ENABLE_FEATURE_FIND_MTIME
  542. ACTF(mtime)
  543. {
  544. time_t file_age = time(NULL) - statbuf->st_mtime;
  545. time_t mtime_secs = ap->mtime_days * 24*60*60;
  546. if (ap->mtime_char == '+')
  547. return file_age >= mtime_secs + 24*60*60;
  548. if (ap->mtime_char == '-')
  549. return file_age < mtime_secs;
  550. /* just numeric mtime */
  551. return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
  552. }
  553. #endif
  554. #if ENABLE_FEATURE_FIND_MMIN
  555. ACTF(mmin)
  556. {
  557. time_t file_age = time(NULL) - statbuf->st_mtime;
  558. time_t mmin_secs = ap->mmin_mins * 60;
  559. if (ap->mmin_char == '+')
  560. return file_age >= mmin_secs + 60;
  561. if (ap->mmin_char == '-')
  562. return file_age < mmin_secs;
  563. /* just numeric mmin */
  564. return file_age >= mmin_secs && file_age < (mmin_secs + 60);
  565. }
  566. #endif
  567. #if ENABLE_FEATURE_FIND_NEWER
  568. ACTF(newer)
  569. {
  570. return (ap->newer_mtime < statbuf->st_mtime);
  571. }
  572. #endif
  573. #if ENABLE_FEATURE_FIND_INUM
  574. ACTF(inum)
  575. {
  576. return (statbuf->st_ino == ap->inode_num);
  577. }
  578. #endif
  579. #if ENABLE_FEATURE_FIND_EXEC
  580. ACTF(exec)
  581. {
  582. int i, rc;
  583. #if ENABLE_USE_PORTABLE_CODE
  584. char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1));
  585. #else /* gcc 4.3.1 generates smaller code: */
  586. char *argv[ap->exec_argc + 1];
  587. #endif
  588. for (i = 0; i < ap->exec_argc; i++)
  589. argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
  590. argv[i] = NULL; /* terminate the list */
  591. rc = spawn_and_wait(argv);
  592. if (rc < 0)
  593. bb_simple_perror_msg(argv[0]);
  594. i = 0;
  595. while (argv[i])
  596. free(argv[i++]);
  597. return rc == 0; /* return 1 if exitcode 0 */
  598. }
  599. #endif
  600. #if ENABLE_FEATURE_FIND_USER
  601. ACTF(user)
  602. {
  603. return (statbuf->st_uid == ap->uid);
  604. }
  605. #endif
  606. #if ENABLE_FEATURE_FIND_GROUP
  607. ACTF(group)
  608. {
  609. return (statbuf->st_gid == ap->gid);
  610. }
  611. #endif
  612. #if ENABLE_FEATURE_FIND_PRINT0
  613. ACTF(print0)
  614. {
  615. printf("%s%c", fileName, '\0');
  616. return TRUE;
  617. }
  618. #endif
  619. ACTF(print)
  620. {
  621. puts(fileName);
  622. return TRUE;
  623. }
  624. #if ENABLE_FEATURE_FIND_PAREN
  625. ACTF(paren)
  626. {
  627. return exec_actions(ap->subexpr, fileName, statbuf);
  628. }
  629. #endif
  630. #if ENABLE_FEATURE_FIND_SIZE
  631. ACTF(size)
  632. {
  633. if (ap->size_char == '+')
  634. return statbuf->st_size > ap->size;
  635. if (ap->size_char == '-')
  636. return statbuf->st_size < ap->size;
  637. return statbuf->st_size == ap->size;
  638. }
  639. #endif
  640. #if ENABLE_FEATURE_FIND_PRUNE
  641. /*
  642. * -prune: if -depth is not given, return true and do not descend
  643. * current dir; if -depth is given, return false with no effect.
  644. * Example:
  645. * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
  646. */
  647. ACTF(prune)
  648. {
  649. return SKIP + TRUE;
  650. }
  651. #endif
  652. #if ENABLE_FEATURE_FIND_DELETE
  653. ACTF(delete)
  654. {
  655. int rc;
  656. if (S_ISDIR(statbuf->st_mode)) {
  657. rc = rmdir(fileName);
  658. } else {
  659. rc = unlink(fileName);
  660. }
  661. if (rc < 0)
  662. bb_simple_perror_msg(fileName);
  663. return TRUE;
  664. }
  665. #endif
  666. #if ENABLE_FEATURE_FIND_CONTEXT
  667. ACTF(context)
  668. {
  669. security_context_t con;
  670. int rc;
  671. if (G.recurse_flags & ACTION_FOLLOWLINKS) {
  672. rc = getfilecon(fileName, &con);
  673. } else {
  674. rc = lgetfilecon(fileName, &con);
  675. }
  676. if (rc < 0)
  677. return FALSE;
  678. rc = strcmp(ap->context, con);
  679. freecon(con);
  680. return rc == 0;
  681. }
  682. #endif
  683. #if ENABLE_FEATURE_FIND_LINKS
  684. ACTF(links)
  685. {
  686. switch(ap->links_char) {
  687. case '-' : return (statbuf->st_nlink < ap->links_count);
  688. case '+' : return (statbuf->st_nlink > ap->links_count);
  689. default: return (statbuf->st_nlink == ap->links_count);
  690. }
  691. }
  692. #endif
  693. static int FAST_FUNC fileAction(const char *fileName,
  694. struct stat *statbuf,
  695. void *userData UNUSED_PARAM,
  696. int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
  697. {
  698. int r;
  699. int same_fs = 1;
  700. #if ENABLE_FEATURE_FIND_XDEV
  701. if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
  702. int i;
  703. for (i = 0; i < G.xdev_count; i++) {
  704. if (G.xdev_dev[i] == statbuf->st_dev)
  705. goto found;
  706. }
  707. //bb_error_msg("'%s': not same fs", fileName);
  708. same_fs = 0;
  709. found: ;
  710. }
  711. #endif
  712. #if ENABLE_FEATURE_FIND_MAXDEPTH
  713. if (depth < G.minmaxdepth[0]) {
  714. if (same_fs)
  715. return TRUE; /* skip this, continue recursing */
  716. return SKIP; /* stop recursing */
  717. }
  718. if (depth > G.minmaxdepth[1])
  719. return SKIP; /* stop recursing */
  720. #endif
  721. r = exec_actions(G.actions, fileName, statbuf);
  722. /* Had no explicit -print[0] or -exec? then print */
  723. if ((r & TRUE) && G.need_print)
  724. puts(fileName);
  725. #if ENABLE_FEATURE_FIND_MAXDEPTH
  726. if (S_ISDIR(statbuf->st_mode)) {
  727. if (depth == G.minmaxdepth[1])
  728. return SKIP;
  729. }
  730. #endif
  731. /* -xdev stops on mountpoints, but AFTER mountpoit itself
  732. * is processed as usual */
  733. if (!same_fs) {
  734. return SKIP;
  735. }
  736. /* Cannot return 0: our caller, recursive_action(),
  737. * will perror() and skip dirs (if called on dir) */
  738. return (r & SKIP) ? SKIP : TRUE;
  739. }
  740. #if ENABLE_FEATURE_FIND_TYPE
  741. static int find_type(const char *type)
  742. {
  743. int mask = 0;
  744. if (*type == 'b')
  745. mask = S_IFBLK;
  746. else if (*type == 'c')
  747. mask = S_IFCHR;
  748. else if (*type == 'd')
  749. mask = S_IFDIR;
  750. else if (*type == 'p')
  751. mask = S_IFIFO;
  752. else if (*type == 'f')
  753. mask = S_IFREG;
  754. else if (*type == 'l')
  755. mask = S_IFLNK;
  756. else if (*type == 's')
  757. mask = S_IFSOCK;
  758. if (mask == 0 || type[1] != '\0')
  759. bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
  760. return mask;
  761. }
  762. #endif
  763. #if ENABLE_FEATURE_FIND_PERM \
  764. || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
  765. || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
  766. static const char* plus_minus_num(const char* str)
  767. {
  768. if (*str == '-' || *str == '+')
  769. str++;
  770. return str;
  771. }
  772. #endif
  773. static action*** parse_params(char **argv)
  774. {
  775. enum {
  776. OPT_FOLLOW ,
  777. IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
  778. IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
  779. PARM_a ,
  780. PARM_o ,
  781. IF_FEATURE_FIND_NOT( PARM_char_not ,)
  782. #if ENABLE_DESKTOP
  783. PARM_and ,
  784. PARM_or ,
  785. IF_FEATURE_FIND_NOT( PARM_not ,)
  786. #endif
  787. PARM_print ,
  788. IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
  789. IF_FEATURE_FIND_PRUNE( PARM_prune ,)
  790. IF_FEATURE_FIND_DELETE( PARM_delete ,)
  791. IF_FEATURE_FIND_EXEC( PARM_exec ,)
  792. IF_FEATURE_FIND_PAREN( PARM_char_brace,)
  793. /* All options/actions starting from here require argument */
  794. PARM_name ,
  795. PARM_iname ,
  796. IF_FEATURE_FIND_PATH( PARM_path ,)
  797. #if ENABLE_DESKTOP
  798. /* -wholename is a synonym for -path */
  799. /* We support it because Linux kernel's "make tags" uses it */
  800. IF_FEATURE_FIND_PATH( PARM_wholename ,)
  801. #endif
  802. IF_FEATURE_FIND_PATH( PARM_ipath ,)
  803. IF_FEATURE_FIND_REGEX( PARM_regex ,)
  804. IF_FEATURE_FIND_TYPE( PARM_type ,)
  805. IF_FEATURE_FIND_PERM( PARM_perm ,)
  806. IF_FEATURE_FIND_MTIME( PARM_mtime ,)
  807. IF_FEATURE_FIND_MMIN( PARM_mmin ,)
  808. IF_FEATURE_FIND_NEWER( PARM_newer ,)
  809. IF_FEATURE_FIND_INUM( PARM_inum ,)
  810. IF_FEATURE_FIND_USER( PARM_user ,)
  811. IF_FEATURE_FIND_GROUP( PARM_group ,)
  812. IF_FEATURE_FIND_SIZE( PARM_size ,)
  813. IF_FEATURE_FIND_CONTEXT(PARM_context ,)
  814. IF_FEATURE_FIND_LINKS( PARM_links ,)
  815. IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
  816. };
  817. static const char params[] ALIGN1 =
  818. "-follow\0"
  819. IF_FEATURE_FIND_XDEV( "-xdev\0" )
  820. IF_FEATURE_FIND_DEPTH( "-depth\0" )
  821. "-a\0"
  822. "-o\0"
  823. IF_FEATURE_FIND_NOT( "!\0" )
  824. #if ENABLE_DESKTOP
  825. "-and\0"
  826. "-or\0"
  827. IF_FEATURE_FIND_NOT( "-not\0" )
  828. #endif
  829. "-print\0"
  830. IF_FEATURE_FIND_PRINT0( "-print0\0" )
  831. IF_FEATURE_FIND_PRUNE( "-prune\0" )
  832. IF_FEATURE_FIND_DELETE( "-delete\0" )
  833. IF_FEATURE_FIND_EXEC( "-exec\0" )
  834. IF_FEATURE_FIND_PAREN( "(\0" )
  835. /* All options/actions starting from here require argument */
  836. "-name\0"
  837. "-iname\0"
  838. IF_FEATURE_FIND_PATH( "-path\0" )
  839. #if ENABLE_DESKTOP
  840. IF_FEATURE_FIND_PATH( "-wholename\0")
  841. #endif
  842. IF_FEATURE_FIND_PATH( "-ipath\0" )
  843. IF_FEATURE_FIND_REGEX( "-regex\0" )
  844. IF_FEATURE_FIND_TYPE( "-type\0" )
  845. IF_FEATURE_FIND_PERM( "-perm\0" )
  846. IF_FEATURE_FIND_MTIME( "-mtime\0" )
  847. IF_FEATURE_FIND_MMIN( "-mmin\0" )
  848. IF_FEATURE_FIND_NEWER( "-newer\0" )
  849. IF_FEATURE_FIND_INUM( "-inum\0" )
  850. IF_FEATURE_FIND_USER( "-user\0" )
  851. IF_FEATURE_FIND_GROUP( "-group\0" )
  852. IF_FEATURE_FIND_SIZE( "-size\0" )
  853. IF_FEATURE_FIND_CONTEXT("-context\0")
  854. IF_FEATURE_FIND_LINKS( "-links\0" )
  855. IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
  856. ;
  857. action*** appp;
  858. unsigned cur_group = 0;
  859. unsigned cur_action = 0;
  860. IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
  861. /* This is the only place in busybox where we use nested function.
  862. * So far more standard alternatives were bigger. */
  863. /* Auto decl suppresses "func without a prototype" warning: */
  864. auto action* alloc_action(int sizeof_struct, action_fp f);
  865. action* alloc_action(int sizeof_struct, action_fp f)
  866. {
  867. action *ap;
  868. appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
  869. appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
  870. appp[cur_group][cur_action] = NULL;
  871. ap->f = f;
  872. IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
  873. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  874. return ap;
  875. }
  876. #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
  877. appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
  878. while (*argv) {
  879. const char *arg = argv[0];
  880. int parm = index_in_strings(params, arg);
  881. const char *arg1 = argv[1];
  882. dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
  883. if (parm >= PARM_name) {
  884. /* All options/actions starting from -name require argument */
  885. if (!arg1)
  886. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  887. argv++;
  888. }
  889. /* We can use big switch() here, but on i386
  890. * it doesn't give smaller code. Other arches? */
  891. /* Options always return true. They always take effect
  892. * rather than being processed only when their place in the
  893. * expression is reached.
  894. */
  895. /* Options */
  896. if (parm == OPT_FOLLOW) {
  897. dbg("follow enabled: %d", __LINE__);
  898. G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
  899. }
  900. #if ENABLE_FEATURE_FIND_XDEV
  901. else if (parm == OPT_XDEV) {
  902. dbg("%d", __LINE__);
  903. G.xdev_on = 1;
  904. }
  905. #endif
  906. #if ENABLE_FEATURE_FIND_MAXDEPTH
  907. else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
  908. dbg("%d", __LINE__);
  909. G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
  910. }
  911. #endif
  912. #if ENABLE_FEATURE_FIND_DEPTH
  913. else if (parm == OPT_DEPTH) {
  914. dbg("%d", __LINE__);
  915. G.recurse_flags |= ACTION_DEPTHFIRST;
  916. }
  917. #endif
  918. /* Actions are grouped by operators
  919. * ( expr ) Force precedence
  920. * ! expr True if expr is false
  921. * -not expr Same as ! expr
  922. * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
  923. * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
  924. * expr1 , expr2 List; both expr1 and expr2 are always evaluated
  925. * We implement: (), -a, -o
  926. */
  927. /* Operators */
  928. else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
  929. dbg("%d", __LINE__);
  930. /* no further special handling required */
  931. }
  932. else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
  933. dbg("%d", __LINE__);
  934. /* start new OR group */
  935. cur_group++;
  936. appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
  937. /*appp[cur_group] = NULL; - already NULL */
  938. appp[cur_group+1] = NULL;
  939. cur_action = 0;
  940. }
  941. #if ENABLE_FEATURE_FIND_NOT
  942. else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
  943. /* also handles "find ! ! -name 'foo*'" */
  944. invert_flag ^= 1;
  945. dbg("invert_flag:%d", invert_flag);
  946. }
  947. #endif
  948. /* Actions */
  949. else if (parm == PARM_print) {
  950. dbg("%d", __LINE__);
  951. G.need_print = 0;
  952. (void) ALLOC_ACTION(print);
  953. }
  954. #if ENABLE_FEATURE_FIND_PRINT0
  955. else if (parm == PARM_print0) {
  956. dbg("%d", __LINE__);
  957. G.need_print = 0;
  958. (void) ALLOC_ACTION(print0);
  959. }
  960. #endif
  961. #if ENABLE_FEATURE_FIND_PRUNE
  962. else if (parm == PARM_prune) {
  963. dbg("%d", __LINE__);
  964. (void) ALLOC_ACTION(prune);
  965. }
  966. #endif
  967. #if ENABLE_FEATURE_FIND_DELETE
  968. else if (parm == PARM_delete) {
  969. dbg("%d", __LINE__);
  970. G.need_print = 0;
  971. G.recurse_flags |= ACTION_DEPTHFIRST;
  972. (void) ALLOC_ACTION(delete);
  973. }
  974. #endif
  975. #if ENABLE_FEATURE_FIND_EXEC
  976. else if (parm == PARM_exec) {
  977. int i;
  978. action_exec *ap;
  979. dbg("%d", __LINE__);
  980. G.need_print = 0;
  981. ap = ALLOC_ACTION(exec);
  982. ap->exec_argv = ++argv; /* first arg after -exec */
  983. /*ap->exec_argc = 0; - ALLOC_ACTION did it */
  984. while (1) {
  985. if (!*argv) /* did not see ';' or '+' until end */
  986. bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
  987. // find -exec echo Foo ">{}<" ";"
  988. // executes "echo Foo >FILENAME<",
  989. // find -exec echo Foo ">{}<" "+"
  990. // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
  991. // TODO (so far we treat "+" just like ";")
  992. if ((argv[0][0] == ';' || argv[0][0] == '+')
  993. && argv[0][1] == '\0'
  994. ) {
  995. break;
  996. }
  997. argv++;
  998. ap->exec_argc++;
  999. }
  1000. if (ap->exec_argc == 0)
  1001. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1002. ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
  1003. i = ap->exec_argc;
  1004. while (i--)
  1005. ap->subst_count[i] = count_subst(ap->exec_argv[i]);
  1006. }
  1007. #endif
  1008. #if ENABLE_FEATURE_FIND_PAREN
  1009. else if (parm == PARM_char_brace) {
  1010. action_paren *ap;
  1011. char **endarg;
  1012. unsigned nested = 1;
  1013. dbg("%d", __LINE__);
  1014. endarg = argv;
  1015. while (1) {
  1016. if (!*++endarg)
  1017. bb_error_msg_and_die("unpaired '('");
  1018. if (LONE_CHAR(*endarg, '('))
  1019. nested++;
  1020. else if (LONE_CHAR(*endarg, ')') && !--nested) {
  1021. *endarg = NULL;
  1022. break;
  1023. }
  1024. }
  1025. ap = ALLOC_ACTION(paren);
  1026. ap->subexpr = parse_params(argv + 1);
  1027. *endarg = (char*) ")"; /* restore NULLed parameter */
  1028. argv = endarg;
  1029. }
  1030. #endif
  1031. else if (parm == PARM_name || parm == PARM_iname) {
  1032. action_name *ap;
  1033. dbg("%d", __LINE__);
  1034. ap = ALLOC_ACTION(name);
  1035. ap->pattern = arg1;
  1036. ap->iname = (parm == PARM_iname);
  1037. }
  1038. #if ENABLE_FEATURE_FIND_PATH
  1039. else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
  1040. action_path *ap;
  1041. dbg("%d", __LINE__);
  1042. ap = ALLOC_ACTION(path);
  1043. ap->pattern = arg1;
  1044. ap->ipath = (parm == PARM_ipath);
  1045. }
  1046. #endif
  1047. #if ENABLE_FEATURE_FIND_REGEX
  1048. else if (parm == PARM_regex) {
  1049. action_regex *ap;
  1050. dbg("%d", __LINE__);
  1051. ap = ALLOC_ACTION(regex);
  1052. xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
  1053. }
  1054. #endif
  1055. #if ENABLE_FEATURE_FIND_TYPE
  1056. else if (parm == PARM_type) {
  1057. action_type *ap;
  1058. ap = ALLOC_ACTION(type);
  1059. ap->type_mask = find_type(arg1);
  1060. dbg("created:type mask:%x", ap->type_mask);
  1061. }
  1062. #endif
  1063. #if ENABLE_FEATURE_FIND_PERM
  1064. /* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
  1065. * Symbolic modes use mode 0 as a point of departure.
  1066. * -perm -BITS All of the BITS are set in file's mode.
  1067. * -perm +BITS At least one of the BITS is set in file's mode.
  1068. */
  1069. else if (parm == PARM_perm) {
  1070. action_perm *ap;
  1071. dbg("%d", __LINE__);
  1072. ap = ALLOC_ACTION(perm);
  1073. ap->perm_char = arg1[0];
  1074. arg1 = plus_minus_num(arg1);
  1075. /*ap->perm_mask = 0; - ALLOC_ACTION did it */
  1076. if (!bb_parse_mode(arg1, &ap->perm_mask))
  1077. bb_error_msg_and_die("invalid mode '%s'", arg1);
  1078. }
  1079. #endif
  1080. #if ENABLE_FEATURE_FIND_MTIME
  1081. else if (parm == PARM_mtime) {
  1082. action_mtime *ap;
  1083. dbg("%d", __LINE__);
  1084. ap = ALLOC_ACTION(mtime);
  1085. ap->mtime_char = arg1[0];
  1086. ap->mtime_days = xatoul(plus_minus_num(arg1));
  1087. }
  1088. #endif
  1089. #if ENABLE_FEATURE_FIND_MMIN
  1090. else if (parm == PARM_mmin) {
  1091. action_mmin *ap;
  1092. dbg("%d", __LINE__);
  1093. ap = ALLOC_ACTION(mmin);
  1094. ap->mmin_char = arg1[0];
  1095. ap->mmin_mins = xatoul(plus_minus_num(arg1));
  1096. }
  1097. #endif
  1098. #if ENABLE_FEATURE_FIND_NEWER
  1099. else if (parm == PARM_newer) {
  1100. struct stat stat_newer;
  1101. action_newer *ap;
  1102. dbg("%d", __LINE__);
  1103. ap = ALLOC_ACTION(newer);
  1104. xstat(arg1, &stat_newer);
  1105. ap->newer_mtime = stat_newer.st_mtime;
  1106. }
  1107. #endif
  1108. #if ENABLE_FEATURE_FIND_INUM
  1109. else if (parm == PARM_inum) {
  1110. action_inum *ap;
  1111. dbg("%d", __LINE__);
  1112. ap = ALLOC_ACTION(inum);
  1113. ap->inode_num = xatoul(arg1);
  1114. }
  1115. #endif
  1116. #if ENABLE_FEATURE_FIND_USER
  1117. else if (parm == PARM_user) {
  1118. action_user *ap;
  1119. dbg("%d", __LINE__);
  1120. ap = ALLOC_ACTION(user);
  1121. ap->uid = bb_strtou(arg1, NULL, 10);
  1122. if (errno)
  1123. ap->uid = xuname2uid(arg1);
  1124. }
  1125. #endif
  1126. #if ENABLE_FEATURE_FIND_GROUP
  1127. else if (parm == PARM_group) {
  1128. action_group *ap;
  1129. dbg("%d", __LINE__);
  1130. ap = ALLOC_ACTION(group);
  1131. ap->gid = bb_strtou(arg1, NULL, 10);
  1132. if (errno)
  1133. ap->gid = xgroup2gid(arg1);
  1134. }
  1135. #endif
  1136. #if ENABLE_FEATURE_FIND_SIZE
  1137. else if (parm == PARM_size) {
  1138. /* -size n[bckw]: file uses n units of space
  1139. * b (default): units are 512-byte blocks
  1140. * c: 1 byte
  1141. * k: kilobytes
  1142. * w: 2-byte words
  1143. */
  1144. #if ENABLE_LFS
  1145. #define XATOU_SFX xatoull_sfx
  1146. #else
  1147. #define XATOU_SFX xatoul_sfx
  1148. #endif
  1149. static const struct suffix_mult find_suffixes[] = {
  1150. { "c", 1 },
  1151. { "w", 2 },
  1152. { "", 512 },
  1153. { "b", 512 },
  1154. { "k", 1024 },
  1155. { "", 0 }
  1156. };
  1157. action_size *ap;
  1158. dbg("%d", __LINE__);
  1159. ap = ALLOC_ACTION(size);
  1160. ap->size_char = arg1[0];
  1161. ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
  1162. }
  1163. #endif
  1164. #if ENABLE_FEATURE_FIND_CONTEXT
  1165. else if (parm == PARM_context) {
  1166. action_context *ap;
  1167. dbg("%d", __LINE__);
  1168. ap = ALLOC_ACTION(context);
  1169. /*ap->context = NULL; - ALLOC_ACTION did it */
  1170. /* SELinux headers erroneously declare non-const parameter */
  1171. if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
  1172. bb_simple_perror_msg(arg1);
  1173. }
  1174. #endif
  1175. #if ENABLE_FEATURE_FIND_LINKS
  1176. else if (parm == PARM_links) {
  1177. action_links *ap;
  1178. dbg("%d", __LINE__);
  1179. ap = ALLOC_ACTION(links);
  1180. ap->links_char = arg1[0];
  1181. ap->links_count = xatoul(plus_minus_num(arg1));
  1182. }
  1183. #endif
  1184. else {
  1185. bb_error_msg("unrecognized: %s", arg);
  1186. bb_show_usage();
  1187. }
  1188. argv++;
  1189. }
  1190. dbg("exiting %s", __func__);
  1191. return appp;
  1192. #undef ALLOC_ACTION
  1193. }
  1194. int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1195. int find_main(int argc UNUSED_PARAM, char **argv)
  1196. {
  1197. int i, firstopt, status = EXIT_SUCCESS;
  1198. INIT_G();
  1199. argv++;
  1200. for (firstopt = 0; argv[firstopt]; firstopt++) {
  1201. if (argv[firstopt][0] == '-')
  1202. break;
  1203. if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
  1204. break;
  1205. if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
  1206. break;
  1207. }
  1208. if (firstopt == 0) {
  1209. *--argv = (char*)".";
  1210. firstopt++;
  1211. }
  1212. G.actions = parse_params(&argv[firstopt]);
  1213. argv[firstopt] = NULL;
  1214. #if ENABLE_FEATURE_FIND_XDEV
  1215. if (G.xdev_on) {
  1216. struct stat stbuf;
  1217. G.xdev_count = firstopt;
  1218. G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
  1219. for (i = 0; argv[i]; i++) {
  1220. /* not xstat(): shouldn't bomb out on
  1221. * "find not_exist exist -xdev" */
  1222. if (stat(argv[i], &stbuf) == 0)
  1223. G.xdev_dev[i] = stbuf.st_dev;
  1224. /* else G.xdev_dev[i] stays 0 and
  1225. * won't match any real device dev_t
  1226. */
  1227. }
  1228. }
  1229. #endif
  1230. for (i = 0; argv[i]; i++) {
  1231. if (!recursive_action(argv[i],
  1232. G.recurse_flags,/* flags */
  1233. fileAction, /* file action */
  1234. fileAction, /* dir action */
  1235. NULL, /* user data */
  1236. 0) /* depth */
  1237. ) {
  1238. status = EXIT_FAILURE;
  1239. }
  1240. }
  1241. return status;
  1242. }