find.c 42 KB

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