find.c 39 KB

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