3
0

find.c 41 KB

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