3
0

find.c 42 KB

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