find.c 37 KB

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