find.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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_FEATURE_FIND_PATH( PARM_ipath ,)
  792. IF_FEATURE_FIND_REGEX( PARM_regex ,)
  793. IF_FEATURE_FIND_TYPE( PARM_type ,)
  794. IF_FEATURE_FIND_PERM( PARM_perm ,)
  795. IF_FEATURE_FIND_MTIME( PARM_mtime ,)
  796. IF_FEATURE_FIND_MMIN( PARM_mmin ,)
  797. IF_FEATURE_FIND_NEWER( PARM_newer ,)
  798. IF_FEATURE_FIND_INUM( PARM_inum ,)
  799. IF_FEATURE_FIND_USER( PARM_user ,)
  800. IF_FEATURE_FIND_GROUP( PARM_group ,)
  801. IF_FEATURE_FIND_SIZE( PARM_size ,)
  802. IF_FEATURE_FIND_CONTEXT(PARM_context ,)
  803. IF_FEATURE_FIND_LINKS( PARM_links ,)
  804. IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
  805. };
  806. static const char params[] ALIGN1 =
  807. "-follow\0"
  808. IF_FEATURE_FIND_XDEV( "-xdev\0" )
  809. IF_FEATURE_FIND_DEPTH( "-depth\0" )
  810. "-a\0"
  811. "-o\0"
  812. IF_FEATURE_FIND_NOT( "!\0" )
  813. #if ENABLE_DESKTOP
  814. "-and\0"
  815. "-or\0"
  816. IF_FEATURE_FIND_NOT( "-not\0" )
  817. #endif
  818. "-print\0"
  819. IF_FEATURE_FIND_PRINT0( "-print0\0" )
  820. IF_FEATURE_FIND_PRUNE( "-prune\0" )
  821. IF_FEATURE_FIND_DELETE( "-delete\0" )
  822. IF_FEATURE_FIND_EXEC( "-exec\0" )
  823. IF_FEATURE_FIND_PAREN( "(\0" )
  824. /* All options/actions starting from here require argument */
  825. "-name\0"
  826. "-iname\0"
  827. IF_FEATURE_FIND_PATH( "-path\0" )
  828. IF_FEATURE_FIND_PATH( "-ipath\0" )
  829. IF_FEATURE_FIND_REGEX( "-regex\0" )
  830. IF_FEATURE_FIND_TYPE( "-type\0" )
  831. IF_FEATURE_FIND_PERM( "-perm\0" )
  832. IF_FEATURE_FIND_MTIME( "-mtime\0" )
  833. IF_FEATURE_FIND_MMIN( "-mmin\0" )
  834. IF_FEATURE_FIND_NEWER( "-newer\0" )
  835. IF_FEATURE_FIND_INUM( "-inum\0" )
  836. IF_FEATURE_FIND_USER( "-user\0" )
  837. IF_FEATURE_FIND_GROUP( "-group\0" )
  838. IF_FEATURE_FIND_SIZE( "-size\0" )
  839. IF_FEATURE_FIND_CONTEXT("-context\0")
  840. IF_FEATURE_FIND_LINKS( "-links\0" )
  841. IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
  842. ;
  843. action*** appp;
  844. unsigned cur_group = 0;
  845. unsigned cur_action = 0;
  846. IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
  847. /* This is the only place in busybox where we use nested function.
  848. * So far more standard alternatives were bigger. */
  849. /* Auto decl suppresses "func without a prototype" warning: */
  850. auto action* alloc_action(int sizeof_struct, action_fp f);
  851. action* alloc_action(int sizeof_struct, action_fp f)
  852. {
  853. action *ap;
  854. appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
  855. appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
  856. appp[cur_group][cur_action] = NULL;
  857. ap->f = f;
  858. IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
  859. IF_FEATURE_FIND_NOT( invert_flag = 0; )
  860. return ap;
  861. }
  862. #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
  863. appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
  864. while (*argv) {
  865. const char *arg = argv[0];
  866. int parm = index_in_strings(params, arg);
  867. const char *arg1 = argv[1];
  868. dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
  869. if (parm >= PARM_name) {
  870. /* All options/actions starting from -name require argument */
  871. if (!arg1)
  872. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  873. argv++;
  874. }
  875. /* We can use big switch() here, but on i386
  876. * it doesn't give smaller code. Other arches? */
  877. /* Options always return true. They always take effect
  878. * rather than being processed only when their place in the
  879. * expression is reached.
  880. */
  881. /* Options */
  882. if (parm == OPT_FOLLOW) {
  883. dbg("follow enabled: %d", __LINE__);
  884. G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
  885. }
  886. #if ENABLE_FEATURE_FIND_XDEV
  887. else if (parm == OPT_XDEV) {
  888. dbg("%d", __LINE__);
  889. G.xdev_on = 1;
  890. }
  891. #endif
  892. #if ENABLE_FEATURE_FIND_MAXDEPTH
  893. else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
  894. dbg("%d", __LINE__);
  895. G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
  896. }
  897. #endif
  898. #if ENABLE_FEATURE_FIND_DEPTH
  899. else if (parm == OPT_DEPTH) {
  900. dbg("%d", __LINE__);
  901. G.recurse_flags |= ACTION_DEPTHFIRST;
  902. }
  903. #endif
  904. /* Actions are grouped by operators
  905. * ( expr ) Force precedence
  906. * ! expr True if expr is false
  907. * -not expr Same as ! expr
  908. * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
  909. * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
  910. * expr1 , expr2 List; both expr1 and expr2 are always evaluated
  911. * We implement: (), -a, -o
  912. */
  913. /* Operators */
  914. else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
  915. dbg("%d", __LINE__);
  916. /* no further special handling required */
  917. }
  918. else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
  919. dbg("%d", __LINE__);
  920. /* start new OR group */
  921. cur_group++;
  922. appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
  923. /*appp[cur_group] = NULL; - already NULL */
  924. appp[cur_group+1] = NULL;
  925. cur_action = 0;
  926. }
  927. #if ENABLE_FEATURE_FIND_NOT
  928. else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
  929. /* also handles "find ! ! -name 'foo*'" */
  930. invert_flag ^= 1;
  931. dbg("invert_flag:%d", invert_flag);
  932. }
  933. #endif
  934. /* Actions */
  935. else if (parm == PARM_print) {
  936. dbg("%d", __LINE__);
  937. G.need_print = 0;
  938. (void) ALLOC_ACTION(print);
  939. }
  940. #if ENABLE_FEATURE_FIND_PRINT0
  941. else if (parm == PARM_print0) {
  942. dbg("%d", __LINE__);
  943. G.need_print = 0;
  944. (void) ALLOC_ACTION(print0);
  945. }
  946. #endif
  947. #if ENABLE_FEATURE_FIND_PRUNE
  948. else if (parm == PARM_prune) {
  949. dbg("%d", __LINE__);
  950. (void) ALLOC_ACTION(prune);
  951. }
  952. #endif
  953. #if ENABLE_FEATURE_FIND_DELETE
  954. else if (parm == PARM_delete) {
  955. dbg("%d", __LINE__);
  956. G.need_print = 0;
  957. G.recurse_flags |= ACTION_DEPTHFIRST;
  958. (void) ALLOC_ACTION(delete);
  959. }
  960. #endif
  961. #if ENABLE_FEATURE_FIND_EXEC
  962. else if (parm == PARM_exec) {
  963. int i;
  964. action_exec *ap;
  965. dbg("%d", __LINE__);
  966. G.need_print = 0;
  967. ap = ALLOC_ACTION(exec);
  968. ap->exec_argv = ++argv; /* first arg after -exec */
  969. /*ap->exec_argc = 0; - ALLOC_ACTION did it */
  970. while (1) {
  971. if (!*argv) /* did not see ';' or '+' until end */
  972. bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
  973. // find -exec echo Foo ">{}<" ";"
  974. // executes "echo Foo >FILENAME<",
  975. // find -exec echo Foo ">{}<" "+"
  976. // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
  977. // TODO (so far we treat "+" just like ";")
  978. if ((argv[0][0] == ';' || argv[0][0] == '+')
  979. && argv[0][1] == '\0'
  980. ) {
  981. break;
  982. }
  983. argv++;
  984. ap->exec_argc++;
  985. }
  986. if (ap->exec_argc == 0)
  987. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  988. ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
  989. i = ap->exec_argc;
  990. while (i--)
  991. ap->subst_count[i] = count_subst(ap->exec_argv[i]);
  992. }
  993. #endif
  994. #if ENABLE_FEATURE_FIND_PAREN
  995. else if (parm == PARM_char_brace) {
  996. action_paren *ap;
  997. char **endarg;
  998. unsigned nested = 1;
  999. dbg("%d", __LINE__);
  1000. endarg = argv;
  1001. while (1) {
  1002. if (!*++endarg)
  1003. bb_error_msg_and_die("unpaired '('");
  1004. if (LONE_CHAR(*endarg, '('))
  1005. nested++;
  1006. else if (LONE_CHAR(*endarg, ')') && !--nested) {
  1007. *endarg = NULL;
  1008. break;
  1009. }
  1010. }
  1011. ap = ALLOC_ACTION(paren);
  1012. ap->subexpr = parse_params(argv + 1);
  1013. *endarg = (char*) ")"; /* restore NULLed parameter */
  1014. argv = endarg;
  1015. }
  1016. #endif
  1017. else if (parm == PARM_name || parm == PARM_iname) {
  1018. action_name *ap;
  1019. dbg("%d", __LINE__);
  1020. ap = ALLOC_ACTION(name);
  1021. ap->pattern = arg1;
  1022. ap->iname = (parm == PARM_iname);
  1023. }
  1024. #if ENABLE_FEATURE_FIND_PATH
  1025. else if (parm == PARM_path || parm == PARM_ipath) {
  1026. action_path *ap;
  1027. dbg("%d", __LINE__);
  1028. ap = ALLOC_ACTION(path);
  1029. ap->pattern = arg1;
  1030. ap->ipath = (parm == PARM_ipath);
  1031. }
  1032. #endif
  1033. #if ENABLE_FEATURE_FIND_REGEX
  1034. else if (parm == PARM_regex) {
  1035. action_regex *ap;
  1036. dbg("%d", __LINE__);
  1037. ap = ALLOC_ACTION(regex);
  1038. xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
  1039. }
  1040. #endif
  1041. #if ENABLE_FEATURE_FIND_TYPE
  1042. else if (parm == PARM_type) {
  1043. action_type *ap;
  1044. ap = ALLOC_ACTION(type);
  1045. ap->type_mask = find_type(arg1);
  1046. dbg("created:type mask:%x", ap->type_mask);
  1047. }
  1048. #endif
  1049. #if ENABLE_FEATURE_FIND_PERM
  1050. /* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
  1051. * Symbolic modes use mode 0 as a point of departure.
  1052. * -perm -BITS All of the BITS are set in file's mode.
  1053. * -perm +BITS At least one of the BITS is set in file's mode.
  1054. */
  1055. else if (parm == PARM_perm) {
  1056. action_perm *ap;
  1057. dbg("%d", __LINE__);
  1058. ap = ALLOC_ACTION(perm);
  1059. ap->perm_char = arg1[0];
  1060. arg1 = plus_minus_num(arg1);
  1061. /*ap->perm_mask = 0; - ALLOC_ACTION did it */
  1062. if (!bb_parse_mode(arg1, &ap->perm_mask))
  1063. bb_error_msg_and_die("invalid mode '%s'", arg1);
  1064. }
  1065. #endif
  1066. #if ENABLE_FEATURE_FIND_MTIME
  1067. else if (parm == PARM_mtime) {
  1068. action_mtime *ap;
  1069. dbg("%d", __LINE__);
  1070. ap = ALLOC_ACTION(mtime);
  1071. ap->mtime_char = arg1[0];
  1072. ap->mtime_days = xatoul(plus_minus_num(arg1));
  1073. }
  1074. #endif
  1075. #if ENABLE_FEATURE_FIND_MMIN
  1076. else if (parm == PARM_mmin) {
  1077. action_mmin *ap;
  1078. dbg("%d", __LINE__);
  1079. ap = ALLOC_ACTION(mmin);
  1080. ap->mmin_char = arg1[0];
  1081. ap->mmin_mins = xatoul(plus_minus_num(arg1));
  1082. }
  1083. #endif
  1084. #if ENABLE_FEATURE_FIND_NEWER
  1085. else if (parm == PARM_newer) {
  1086. struct stat stat_newer;
  1087. action_newer *ap;
  1088. dbg("%d", __LINE__);
  1089. ap = ALLOC_ACTION(newer);
  1090. xstat(arg1, &stat_newer);
  1091. ap->newer_mtime = stat_newer.st_mtime;
  1092. }
  1093. #endif
  1094. #if ENABLE_FEATURE_FIND_INUM
  1095. else if (parm == PARM_inum) {
  1096. action_inum *ap;
  1097. dbg("%d", __LINE__);
  1098. ap = ALLOC_ACTION(inum);
  1099. ap->inode_num = xatoul(arg1);
  1100. }
  1101. #endif
  1102. #if ENABLE_FEATURE_FIND_USER
  1103. else if (parm == PARM_user) {
  1104. action_user *ap;
  1105. dbg("%d", __LINE__);
  1106. ap = ALLOC_ACTION(user);
  1107. ap->uid = bb_strtou(arg1, NULL, 10);
  1108. if (errno)
  1109. ap->uid = xuname2uid(arg1);
  1110. }
  1111. #endif
  1112. #if ENABLE_FEATURE_FIND_GROUP
  1113. else if (parm == PARM_group) {
  1114. action_group *ap;
  1115. dbg("%d", __LINE__);
  1116. ap = ALLOC_ACTION(group);
  1117. ap->gid = bb_strtou(arg1, NULL, 10);
  1118. if (errno)
  1119. ap->gid = xgroup2gid(arg1);
  1120. }
  1121. #endif
  1122. #if ENABLE_FEATURE_FIND_SIZE
  1123. else if (parm == PARM_size) {
  1124. /* -size n[bckw]: file uses n units of space
  1125. * b (default): units are 512-byte blocks
  1126. * c: 1 byte
  1127. * k: kilobytes
  1128. * w: 2-byte words
  1129. */
  1130. #if ENABLE_LFS
  1131. #define XATOU_SFX xatoull_sfx
  1132. #else
  1133. #define XATOU_SFX xatoul_sfx
  1134. #endif
  1135. static const struct suffix_mult find_suffixes[] = {
  1136. { "c", 1 },
  1137. { "w", 2 },
  1138. { "", 512 },
  1139. { "b", 512 },
  1140. { "k", 1024 },
  1141. { "", 0 }
  1142. };
  1143. action_size *ap;
  1144. dbg("%d", __LINE__);
  1145. ap = ALLOC_ACTION(size);
  1146. ap->size_char = arg1[0];
  1147. ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
  1148. }
  1149. #endif
  1150. #if ENABLE_FEATURE_FIND_CONTEXT
  1151. else if (parm == PARM_context) {
  1152. action_context *ap;
  1153. dbg("%d", __LINE__);
  1154. ap = ALLOC_ACTION(context);
  1155. /*ap->context = NULL; - ALLOC_ACTION did it */
  1156. /* SELinux headers erroneously declare non-const parameter */
  1157. if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
  1158. bb_simple_perror_msg(arg1);
  1159. }
  1160. #endif
  1161. #if ENABLE_FEATURE_FIND_LINKS
  1162. else if (parm == PARM_links) {
  1163. action_links *ap;
  1164. dbg("%d", __LINE__);
  1165. ap = ALLOC_ACTION(links);
  1166. ap->links_char = arg1[0];
  1167. ap->links_count = xatoul(plus_minus_num(arg1));
  1168. }
  1169. #endif
  1170. else {
  1171. bb_error_msg("unrecognized: %s", arg);
  1172. bb_show_usage();
  1173. }
  1174. argv++;
  1175. }
  1176. dbg("exiting %s", __func__);
  1177. return appp;
  1178. #undef ALLOC_ACTION
  1179. }
  1180. int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1181. int find_main(int argc UNUSED_PARAM, char **argv)
  1182. {
  1183. int i, firstopt, status = EXIT_SUCCESS;
  1184. INIT_G();
  1185. argv++;
  1186. for (firstopt = 0; argv[firstopt]; firstopt++) {
  1187. if (argv[firstopt][0] == '-')
  1188. break;
  1189. if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
  1190. break;
  1191. if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
  1192. break;
  1193. }
  1194. if (firstopt == 0) {
  1195. *--argv = (char*)".";
  1196. firstopt++;
  1197. }
  1198. G.actions = parse_params(&argv[firstopt]);
  1199. argv[firstopt] = NULL;
  1200. #if ENABLE_FEATURE_FIND_XDEV
  1201. if (G.xdev_on) {
  1202. struct stat stbuf;
  1203. G.xdev_count = firstopt;
  1204. G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
  1205. for (i = 0; argv[i]; i++) {
  1206. /* not xstat(): shouldn't bomb out on
  1207. * "find not_exist exist -xdev" */
  1208. if (stat(argv[i], &stbuf) == 0)
  1209. G.xdev_dev[i] = stbuf.st_dev;
  1210. /* else G.xdev_dev[i] stays 0 and
  1211. * won't match any real device dev_t
  1212. */
  1213. }
  1214. }
  1215. #endif
  1216. for (i = 0; argv[i]; i++) {
  1217. if (!recursive_action(argv[i],
  1218. G.recurse_flags,/* flags */
  1219. fileAction, /* file action */
  1220. fileAction, /* dir action */
  1221. NULL, /* user data */
  1222. 0) /* depth */
  1223. ) {
  1224. status = EXIT_FAILURE;
  1225. }
  1226. }
  1227. return status;
  1228. }