find.c 48 KB

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