find.c 49 KB

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