find.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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. rc = rmdir(fileName);
  741. } else {
  742. rc = unlink(fileName);
  743. }
  744. if (rc < 0)
  745. bb_simple_perror_msg(fileName);
  746. return TRUE;
  747. }
  748. #endif
  749. #if ENABLE_FEATURE_FIND_CONTEXT
  750. ACTF(context)
  751. {
  752. security_context_t con;
  753. int rc;
  754. if (G.recurse_flags & ACTION_FOLLOWLINKS) {
  755. rc = getfilecon(fileName, &con);
  756. } else {
  757. rc = lgetfilecon(fileName, &con);
  758. }
  759. if (rc < 0)
  760. return FALSE;
  761. rc = strcmp(ap->context, con);
  762. freecon(con);
  763. return rc == 0;
  764. }
  765. #endif
  766. #if ENABLE_FEATURE_FIND_LINKS
  767. ACTF(links)
  768. {
  769. switch(ap->links_char) {
  770. case '-' : return (statbuf->st_nlink < ap->links_count);
  771. case '+' : return (statbuf->st_nlink > ap->links_count);
  772. default: return (statbuf->st_nlink == ap->links_count);
  773. }
  774. }
  775. #endif
  776. static int FAST_FUNC fileAction(const char *fileName,
  777. struct stat *statbuf,
  778. void *userData UNUSED_PARAM,
  779. int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
  780. {
  781. int r;
  782. int same_fs = 1;
  783. #if ENABLE_FEATURE_FIND_XDEV
  784. if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
  785. int i;
  786. for (i = 0; i < G.xdev_count; i++) {
  787. if (G.xdev_dev[i] == statbuf->st_dev)
  788. goto found;
  789. }
  790. //bb_error_msg("'%s': not same fs", fileName);
  791. same_fs = 0;
  792. found: ;
  793. }
  794. #endif
  795. #if ENABLE_FEATURE_FIND_MAXDEPTH
  796. if (depth < G.minmaxdepth[0]) {
  797. if (same_fs)
  798. return TRUE; /* skip this, continue recursing */
  799. return SKIP; /* stop recursing */
  800. }
  801. if (depth > G.minmaxdepth[1])
  802. return SKIP; /* stop recursing */
  803. #endif
  804. r = exec_actions(G.actions, fileName, statbuf);
  805. /* Had no explicit -print[0] or -exec? then print */
  806. if ((r & TRUE) && G.need_print)
  807. puts(fileName);
  808. #if ENABLE_FEATURE_FIND_MAXDEPTH
  809. if (S_ISDIR(statbuf->st_mode)) {
  810. if (depth == G.minmaxdepth[1])
  811. return SKIP;
  812. }
  813. #endif
  814. /* -xdev stops on mountpoints, but AFTER mountpoit itself
  815. * is processed as usual */
  816. if (!same_fs) {
  817. return SKIP;
  818. }
  819. /* Cannot return 0: our caller, recursive_action(),
  820. * will perror() and skip dirs (if called on dir) */
  821. return (r & SKIP) ? SKIP : TRUE;
  822. }
  823. #if ENABLE_FEATURE_FIND_TYPE
  824. static int find_type(const char *type)
  825. {
  826. int mask = 0;
  827. if (*type == 'b')
  828. mask = S_IFBLK;
  829. else if (*type == 'c')
  830. mask = S_IFCHR;
  831. else if (*type == 'd')
  832. mask = S_IFDIR;
  833. else if (*type == 'p')
  834. mask = S_IFIFO;
  835. else if (*type == 'f')
  836. mask = S_IFREG;
  837. else if (*type == 'l')
  838. mask = S_IFLNK;
  839. else if (*type == 's')
  840. mask = S_IFSOCK;
  841. if (mask == 0 || type[1] != '\0')
  842. bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
  843. return mask;
  844. }
  845. #endif
  846. #if ENABLE_FEATURE_FIND_PERM \
  847. || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
  848. || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
  849. static const char* plus_minus_num(const char* str)
  850. {
  851. if (*str == '-' || *str == '+')
  852. str++;
  853. return str;
  854. }
  855. #endif
  856. /* Say no to GCCism */
  857. #define USE_NESTED_FUNCTION 0
  858. #if !USE_NESTED_FUNCTION
  859. struct pp_locals {
  860. action*** appp;
  861. unsigned cur_group;
  862. unsigned cur_action;
  863. IF_FEATURE_FIND_NOT( bool invert_flag; )
  864. };
  865. static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
  866. {
  867. action *ap = xzalloc(sizeof_struct);
  868. action **app;
  869. action ***group = &ppl->appp[ppl->cur_group];
  870. *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
  871. app[ppl->cur_action++] = ap;
  872. app[ppl->cur_action] = NULL;
  873. ap->f = f;
  874. IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
  875. IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
  876. return ap;
  877. }
  878. #endif
  879. static action*** parse_params(char **argv)
  880. {
  881. enum {
  882. OPT_FOLLOW ,
  883. IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
  884. IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
  885. PARM_a ,
  886. PARM_o ,
  887. IF_FEATURE_FIND_NOT( PARM_char_not ,)
  888. #if ENABLE_DESKTOP
  889. PARM_and ,
  890. PARM_or ,
  891. IF_FEATURE_FIND_NOT( PARM_not ,)
  892. #endif
  893. PARM_print ,
  894. IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
  895. IF_FEATURE_FIND_PRUNE( PARM_prune ,)
  896. IF_FEATURE_FIND_DELETE( PARM_delete ,)
  897. IF_FEATURE_FIND_EXEC( PARM_exec ,)
  898. IF_FEATURE_FIND_PAREN( PARM_char_brace,)
  899. /* All options/actions starting from here require argument */
  900. PARM_name ,
  901. PARM_iname ,
  902. IF_FEATURE_FIND_PATH( PARM_path ,)
  903. #if ENABLE_DESKTOP
  904. /* -wholename is a synonym for -path */
  905. /* We support it because Linux kernel's "make tags" uses it */
  906. IF_FEATURE_FIND_PATH( PARM_wholename ,)
  907. #endif
  908. IF_FEATURE_FIND_PATH( PARM_ipath ,)
  909. IF_FEATURE_FIND_REGEX( PARM_regex ,)
  910. IF_FEATURE_FIND_TYPE( PARM_type ,)
  911. IF_FEATURE_FIND_PERM( PARM_perm ,)
  912. IF_FEATURE_FIND_MTIME( PARM_mtime ,)
  913. IF_FEATURE_FIND_MMIN( PARM_mmin ,)
  914. IF_FEATURE_FIND_NEWER( PARM_newer ,)
  915. IF_FEATURE_FIND_INUM( PARM_inum ,)
  916. IF_FEATURE_FIND_USER( PARM_user ,)
  917. IF_FEATURE_FIND_GROUP( PARM_group ,)
  918. IF_FEATURE_FIND_SIZE( PARM_size ,)
  919. IF_FEATURE_FIND_CONTEXT(PARM_context ,)
  920. IF_FEATURE_FIND_LINKS( PARM_links ,)
  921. IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
  922. };
  923. static const char params[] ALIGN1 =
  924. "-follow\0"
  925. IF_FEATURE_FIND_XDEV( "-xdev\0" )
  926. IF_FEATURE_FIND_DEPTH( "-depth\0" )
  927. "-a\0"
  928. "-o\0"
  929. IF_FEATURE_FIND_NOT( "!\0" )
  930. #if ENABLE_DESKTOP
  931. "-and\0"
  932. "-or\0"
  933. IF_FEATURE_FIND_NOT( "-not\0" )
  934. #endif
  935. "-print\0"
  936. IF_FEATURE_FIND_PRINT0( "-print0\0" )
  937. IF_FEATURE_FIND_PRUNE( "-prune\0" )
  938. IF_FEATURE_FIND_DELETE( "-delete\0" )
  939. IF_FEATURE_FIND_EXEC( "-exec\0" )
  940. IF_FEATURE_FIND_PAREN( "(\0" )
  941. /* All options/actions starting from here require argument */
  942. "-name\0"
  943. "-iname\0"
  944. IF_FEATURE_FIND_PATH( "-path\0" )
  945. #if ENABLE_DESKTOP
  946. IF_FEATURE_FIND_PATH( "-wholename\0")
  947. #endif
  948. IF_FEATURE_FIND_PATH( "-ipath\0" )
  949. IF_FEATURE_FIND_REGEX( "-regex\0" )
  950. IF_FEATURE_FIND_TYPE( "-type\0" )
  951. IF_FEATURE_FIND_PERM( "-perm\0" )
  952. IF_FEATURE_FIND_MTIME( "-mtime\0" )
  953. IF_FEATURE_FIND_MMIN( "-mmin\0" )
  954. IF_FEATURE_FIND_NEWER( "-newer\0" )
  955. IF_FEATURE_FIND_INUM( "-inum\0" )
  956. IF_FEATURE_FIND_USER( "-user\0" )
  957. IF_FEATURE_FIND_GROUP( "-group\0" )
  958. IF_FEATURE_FIND_SIZE( "-size\0" )
  959. IF_FEATURE_FIND_CONTEXT("-context\0")
  960. IF_FEATURE_FIND_LINKS( "-links\0" )
  961. IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
  962. ;
  963. #if !USE_NESTED_FUNCTION
  964. struct pp_locals ppl;
  965. #define appp (ppl.appp )
  966. #define cur_group (ppl.cur_group )
  967. #define cur_action (ppl.cur_action )
  968. #define invert_flag (ppl.invert_flag)
  969. #define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
  970. #else
  971. action*** appp;
  972. unsigned cur_group;
  973. unsigned cur_action;
  974. IF_FEATURE_FIND_NOT( bool invert_flag; )
  975. /* This is the only place in busybox where we use nested function.
  976. * So far more standard alternatives were bigger. */
  977. /* Auto decl suppresses "func without a prototype" warning: */
  978. auto action* alloc_action(int sizeof_struct, action_fp f);
  979. action* alloc_action(int sizeof_struct, action_fp f)
  980. {
  981. action *ap;
  982. appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
  983. appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
  984. appp[cur_group][cur_action] = NULL;
  985. ap->f = f;
  986. IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
  987. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  988. return ap;
  989. }
  990. #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
  991. #endif
  992. cur_group = 0;
  993. cur_action = 0;
  994. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  995. appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
  996. while (*argv) {
  997. const char *arg = argv[0];
  998. int parm = index_in_strings(params, arg);
  999. const char *arg1 = argv[1];
  1000. dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
  1001. if (parm >= PARM_name) {
  1002. /* All options/actions starting from -name require argument */
  1003. if (!arg1)
  1004. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1005. argv++;
  1006. }
  1007. /* We can use big switch() here, but on i386
  1008. * it doesn't give smaller code. Other arches? */
  1009. /* Options always return true. They always take effect
  1010. * rather than being processed only when their place in the
  1011. * expression is reached.
  1012. */
  1013. /* Options */
  1014. if (parm == OPT_FOLLOW) {
  1015. dbg("follow enabled: %d", __LINE__);
  1016. G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
  1017. }
  1018. #if ENABLE_FEATURE_FIND_XDEV
  1019. else if (parm == OPT_XDEV) {
  1020. dbg("%d", __LINE__);
  1021. G.xdev_on = 1;
  1022. }
  1023. #endif
  1024. #if ENABLE_FEATURE_FIND_MAXDEPTH
  1025. else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
  1026. dbg("%d", __LINE__);
  1027. G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
  1028. }
  1029. #endif
  1030. #if ENABLE_FEATURE_FIND_DEPTH
  1031. else if (parm == OPT_DEPTH) {
  1032. dbg("%d", __LINE__);
  1033. G.recurse_flags |= ACTION_DEPTHFIRST;
  1034. }
  1035. #endif
  1036. /* Actions are grouped by operators
  1037. * ( expr ) Force precedence
  1038. * ! expr True if expr is false
  1039. * -not expr Same as ! expr
  1040. * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
  1041. * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
  1042. * expr1 , expr2 List; both expr1 and expr2 are always evaluated
  1043. * We implement: (), -a, -o
  1044. */
  1045. /* Operators */
  1046. else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
  1047. dbg("%d", __LINE__);
  1048. /* no further special handling required */
  1049. }
  1050. else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
  1051. dbg("%d", __LINE__);
  1052. /* start new OR group */
  1053. cur_group++;
  1054. appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
  1055. /*appp[cur_group] = NULL; - already NULL */
  1056. appp[cur_group+1] = NULL;
  1057. cur_action = 0;
  1058. }
  1059. #if ENABLE_FEATURE_FIND_NOT
  1060. else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
  1061. /* also handles "find ! ! -name 'foo*'" */
  1062. invert_flag ^= 1;
  1063. dbg("invert_flag:%d", invert_flag);
  1064. }
  1065. #endif
  1066. /* Actions */
  1067. else if (parm == PARM_print) {
  1068. dbg("%d", __LINE__);
  1069. G.need_print = 0;
  1070. (void) ALLOC_ACTION(print);
  1071. }
  1072. #if ENABLE_FEATURE_FIND_PRINT0
  1073. else if (parm == PARM_print0) {
  1074. dbg("%d", __LINE__);
  1075. G.need_print = 0;
  1076. (void) ALLOC_ACTION(print0);
  1077. }
  1078. #endif
  1079. #if ENABLE_FEATURE_FIND_PRUNE
  1080. else if (parm == PARM_prune) {
  1081. dbg("%d", __LINE__);
  1082. (void) ALLOC_ACTION(prune);
  1083. }
  1084. #endif
  1085. #if ENABLE_FEATURE_FIND_DELETE
  1086. else if (parm == PARM_delete) {
  1087. dbg("%d", __LINE__);
  1088. G.need_print = 0;
  1089. G.recurse_flags |= ACTION_DEPTHFIRST;
  1090. (void) ALLOC_ACTION(delete);
  1091. }
  1092. #endif
  1093. #if ENABLE_FEATURE_FIND_EXEC
  1094. else if (parm == PARM_exec) {
  1095. int i;
  1096. action_exec *ap;
  1097. IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
  1098. dbg("%d", __LINE__);
  1099. G.need_print = 0;
  1100. ap = ALLOC_ACTION(exec);
  1101. ap->exec_argv = ++argv; /* first arg after -exec */
  1102. /*ap->exec_argc = 0; - ALLOC_ACTION did it */
  1103. while (1) {
  1104. if (!*argv) /* did not see ';' or '+' until end */
  1105. bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
  1106. // find -exec echo Foo ">{}<" ";"
  1107. // executes "echo Foo >FILENAME<",
  1108. // find -exec echo Foo ">{}<" "+"
  1109. // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
  1110. if ((argv[0][0] == ';' || argv[0][0] == '+')
  1111. && argv[0][1] == '\0'
  1112. ) {
  1113. # if ENABLE_FEATURE_FIND_EXEC_PLUS
  1114. if (argv[0][0] == '+')
  1115. ap->filelist = xzalloc(sizeof(ap->filelist[0]));
  1116. # endif
  1117. break;
  1118. }
  1119. argv++;
  1120. ap->exec_argc++;
  1121. }
  1122. if (ap->exec_argc == 0)
  1123. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1124. ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
  1125. i = ap->exec_argc;
  1126. while (i--) {
  1127. ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
  1128. IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
  1129. }
  1130. # if ENABLE_FEATURE_FIND_EXEC_PLUS
  1131. /*
  1132. * coreutils expects {} to appear only once in "-exec +"
  1133. */
  1134. if (all_subst != 1 && ap->filelist)
  1135. bb_error_msg_and_die("only one '{}' allowed for -exec +");
  1136. # endif
  1137. }
  1138. #endif
  1139. #if ENABLE_FEATURE_FIND_PAREN
  1140. else if (parm == PARM_char_brace) {
  1141. action_paren *ap;
  1142. char **endarg;
  1143. unsigned nested = 1;
  1144. dbg("%d", __LINE__);
  1145. endarg = argv;
  1146. while (1) {
  1147. if (!*++endarg)
  1148. bb_error_msg_and_die("unpaired '('");
  1149. if (LONE_CHAR(*endarg, '('))
  1150. nested++;
  1151. else if (LONE_CHAR(*endarg, ')') && !--nested) {
  1152. *endarg = NULL;
  1153. break;
  1154. }
  1155. }
  1156. ap = ALLOC_ACTION(paren);
  1157. ap->subexpr = parse_params(argv + 1);
  1158. *endarg = (char*) ")"; /* restore NULLed parameter */
  1159. argv = endarg;
  1160. }
  1161. #endif
  1162. else if (parm == PARM_name || parm == PARM_iname) {
  1163. action_name *ap;
  1164. dbg("%d", __LINE__);
  1165. ap = ALLOC_ACTION(name);
  1166. ap->pattern = arg1;
  1167. ap->iname = (parm == PARM_iname);
  1168. }
  1169. #if ENABLE_FEATURE_FIND_PATH
  1170. else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
  1171. action_path *ap;
  1172. dbg("%d", __LINE__);
  1173. ap = ALLOC_ACTION(path);
  1174. ap->pattern = arg1;
  1175. ap->ipath = (parm == PARM_ipath);
  1176. }
  1177. #endif
  1178. #if ENABLE_FEATURE_FIND_REGEX
  1179. else if (parm == PARM_regex) {
  1180. action_regex *ap;
  1181. dbg("%d", __LINE__);
  1182. ap = ALLOC_ACTION(regex);
  1183. xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
  1184. }
  1185. #endif
  1186. #if ENABLE_FEATURE_FIND_TYPE
  1187. else if (parm == PARM_type) {
  1188. action_type *ap;
  1189. ap = ALLOC_ACTION(type);
  1190. ap->type_mask = find_type(arg1);
  1191. dbg("created:type mask:%x", ap->type_mask);
  1192. }
  1193. #endif
  1194. #if ENABLE_FEATURE_FIND_PERM
  1195. /* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
  1196. * Symbolic modes use mode 0 as a point of departure.
  1197. * -perm -BITS All of the BITS are set in file's mode.
  1198. * -perm [+/]BITS At least one of the BITS is set in file's mode.
  1199. */
  1200. else if (parm == PARM_perm) {
  1201. action_perm *ap;
  1202. dbg("%d", __LINE__);
  1203. ap = ALLOC_ACTION(perm);
  1204. ap->perm_char = arg1[0];
  1205. arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
  1206. /*ap->perm_mask = 0; - ALLOC_ACTION did it */
  1207. if (!bb_parse_mode(arg1, &ap->perm_mask))
  1208. bb_error_msg_and_die("invalid mode '%s'", arg1);
  1209. }
  1210. #endif
  1211. #if ENABLE_FEATURE_FIND_MTIME
  1212. else if (parm == PARM_mtime) {
  1213. action_mtime *ap;
  1214. dbg("%d", __LINE__);
  1215. ap = ALLOC_ACTION(mtime);
  1216. ap->mtime_char = arg1[0];
  1217. ap->mtime_days = xatoul(plus_minus_num(arg1));
  1218. }
  1219. #endif
  1220. #if ENABLE_FEATURE_FIND_MMIN
  1221. else if (parm == PARM_mmin) {
  1222. action_mmin *ap;
  1223. dbg("%d", __LINE__);
  1224. ap = ALLOC_ACTION(mmin);
  1225. ap->mmin_char = arg1[0];
  1226. ap->mmin_mins = xatoul(plus_minus_num(arg1));
  1227. }
  1228. #endif
  1229. #if ENABLE_FEATURE_FIND_NEWER
  1230. else if (parm == PARM_newer) {
  1231. struct stat stat_newer;
  1232. action_newer *ap;
  1233. dbg("%d", __LINE__);
  1234. ap = ALLOC_ACTION(newer);
  1235. xstat(arg1, &stat_newer);
  1236. ap->newer_mtime = stat_newer.st_mtime;
  1237. }
  1238. #endif
  1239. #if ENABLE_FEATURE_FIND_INUM
  1240. else if (parm == PARM_inum) {
  1241. action_inum *ap;
  1242. dbg("%d", __LINE__);
  1243. ap = ALLOC_ACTION(inum);
  1244. ap->inode_num = xatoul(arg1);
  1245. }
  1246. #endif
  1247. #if ENABLE_FEATURE_FIND_USER
  1248. else if (parm == PARM_user) {
  1249. action_user *ap;
  1250. dbg("%d", __LINE__);
  1251. ap = ALLOC_ACTION(user);
  1252. ap->uid = bb_strtou(arg1, NULL, 10);
  1253. if (errno)
  1254. ap->uid = xuname2uid(arg1);
  1255. }
  1256. #endif
  1257. #if ENABLE_FEATURE_FIND_GROUP
  1258. else if (parm == PARM_group) {
  1259. action_group *ap;
  1260. dbg("%d", __LINE__);
  1261. ap = ALLOC_ACTION(group);
  1262. ap->gid = bb_strtou(arg1, NULL, 10);
  1263. if (errno)
  1264. ap->gid = xgroup2gid(arg1);
  1265. }
  1266. #endif
  1267. #if ENABLE_FEATURE_FIND_SIZE
  1268. else if (parm == PARM_size) {
  1269. /* -size n[bckw]: file uses n units of space
  1270. * b (default): units are 512-byte blocks
  1271. * c: 1 byte
  1272. * k: kilobytes
  1273. * w: 2-byte words
  1274. */
  1275. #if ENABLE_LFS
  1276. #define XATOU_SFX xatoull_sfx
  1277. #else
  1278. #define XATOU_SFX xatoul_sfx
  1279. #endif
  1280. static const struct suffix_mult find_suffixes[] = {
  1281. { "c", 1 },
  1282. { "w", 2 },
  1283. { "", 512 },
  1284. { "b", 512 },
  1285. { "k", 1024 },
  1286. { "", 0 }
  1287. };
  1288. action_size *ap;
  1289. dbg("%d", __LINE__);
  1290. ap = ALLOC_ACTION(size);
  1291. ap->size_char = arg1[0];
  1292. ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
  1293. }
  1294. #endif
  1295. #if ENABLE_FEATURE_FIND_CONTEXT
  1296. else if (parm == PARM_context) {
  1297. action_context *ap;
  1298. dbg("%d", __LINE__);
  1299. ap = ALLOC_ACTION(context);
  1300. /*ap->context = NULL; - ALLOC_ACTION did it */
  1301. /* SELinux headers erroneously declare non-const parameter */
  1302. if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
  1303. bb_simple_perror_msg(arg1);
  1304. }
  1305. #endif
  1306. #if ENABLE_FEATURE_FIND_LINKS
  1307. else if (parm == PARM_links) {
  1308. action_links *ap;
  1309. dbg("%d", __LINE__);
  1310. ap = ALLOC_ACTION(links);
  1311. ap->links_char = arg1[0];
  1312. ap->links_count = xatoul(plus_minus_num(arg1));
  1313. }
  1314. #endif
  1315. else {
  1316. bb_error_msg("unrecognized: %s", arg);
  1317. bb_show_usage();
  1318. }
  1319. argv++;
  1320. }
  1321. dbg("exiting %s", __func__);
  1322. return appp;
  1323. #undef ALLOC_ACTION
  1324. #undef appp
  1325. #undef cur_action
  1326. #undef invert_flag
  1327. }
  1328. int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1329. int find_main(int argc UNUSED_PARAM, char **argv)
  1330. {
  1331. int i, firstopt, status = EXIT_SUCCESS;
  1332. char **past_HLP, *saved;
  1333. INIT_G();
  1334. /* "find -type f" + getopt("+HLP") => disaster.
  1335. * Need to avoid getopt running into a non-HLP option.
  1336. * Do this by temporarily storing NULL there:
  1337. */
  1338. past_HLP = argv;
  1339. for (;;) {
  1340. saved = *++past_HLP;
  1341. if (!saved)
  1342. break;
  1343. if (saved[0] != '-')
  1344. break;
  1345. if (!saved[1])
  1346. break; /* it is "-" */
  1347. if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
  1348. break;
  1349. }
  1350. *past_HLP = NULL;
  1351. /* "+": stop on first non-option */
  1352. i = getopt32(argv, "+HLP");
  1353. if (i & (1<<0))
  1354. G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
  1355. if (i & (1<<1))
  1356. G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
  1357. /* -P is default and is ignored */
  1358. argv = past_HLP; /* same result as "argv += optind;" */
  1359. *past_HLP = saved;
  1360. for (firstopt = 0; argv[firstopt]; firstopt++) {
  1361. if (argv[firstopt][0] == '-')
  1362. break;
  1363. if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
  1364. break;
  1365. if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
  1366. break;
  1367. }
  1368. if (firstopt == 0) {
  1369. *--argv = (char*)".";
  1370. firstopt++;
  1371. }
  1372. G.actions = parse_params(&argv[firstopt]);
  1373. argv[firstopt] = NULL;
  1374. #if ENABLE_FEATURE_FIND_XDEV
  1375. if (G.xdev_on) {
  1376. struct stat stbuf;
  1377. G.xdev_count = firstopt;
  1378. G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
  1379. for (i = 0; argv[i]; i++) {
  1380. /* not xstat(): shouldn't bomb out on
  1381. * "find not_exist exist -xdev" */
  1382. if (stat(argv[i], &stbuf) == 0)
  1383. G.xdev_dev[i] = stbuf.st_dev;
  1384. /* else G.xdev_dev[i] stays 0 and
  1385. * won't match any real device dev_t
  1386. */
  1387. }
  1388. }
  1389. #endif
  1390. for (i = 0; argv[i]; i++) {
  1391. if (!recursive_action(argv[i],
  1392. G.recurse_flags,/* flags */
  1393. fileAction, /* file action */
  1394. fileAction, /* dir action */
  1395. NULL, /* user data */
  1396. 0) /* depth */
  1397. ) {
  1398. status = EXIT_FAILURE;
  1399. goto out;
  1400. }
  1401. }
  1402. IF_FEATURE_FIND_EXEC_PLUS(status = flush_exec_plus();)
  1403. out:
  1404. return status;
  1405. }