Fixed Savannah bug #20157 (bad initialisation of groups[] argument to setgroups)...
[findutils.git] / find / util.c
blob9150d2371664bd54cc7f6b4c78ebc8c535cd23e6
1 /* util.c -- functions for initializing new tree elements, and other things.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA.
20 #include <config.h>
21 #include "defs.h"
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #else
26 #include <sys/file.h>
27 #endif
28 #ifdef HAVE_SYS_UTSNAME_H
29 #include <sys/utsname.h>
30 #endif
31 #include <sys/time.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <errno.h>
36 #include <assert.h>
38 #include "xalloc.h"
39 #include "quotearg.h"
40 #include "timespec.h"
41 #include "error.h"
42 #include "verify.h"
43 #include "openat.h"
45 #if ENABLE_NLS
46 # include <libintl.h>
47 # define _(Text) gettext (Text)
48 #else
49 # define _(Text) Text
50 #endif
51 #ifdef gettext_noop
52 # define N_(String) gettext_noop (String)
53 #else
54 /* See locate.c for explanation as to why not use (String) */
55 # define N_(String) String
56 #endif
59 struct debug_option_assoc
61 char *name;
62 int val;
63 char *docstring;
65 static struct debug_option_assoc debugassoc[] =
67 { "help", DebugHelp, "Explain the various -D options" },
68 { "tree", DebugExpressionTree, "Display the expression tree" },
69 { "search",DebugSearch, "Navigate the directory tree verbosely" },
70 { "stat", DebugStat, "Trace calls to stat(2) and lstat(2)" },
71 { "rates", DebugSuccessRates, "Indicate how often each predicate succeeded" },
72 { "opt", DebugExpressionTree|DebugTreeOpt, "Show diagnostic information relating to optimisation" },
73 { "exec", DebugExec, "Show diagnostic information relating to -exec, -execdir, -ok and -okdir" }
75 #define N_DEBUGASSOC (sizeof(debugassoc)/sizeof(debugassoc[0]))
80 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
82 Return a pointer to the predicate node just inserted.
84 Fills in the following cells of the new predicate node:
86 pred_func PRED_FUNC
87 args(.str) NULL
88 p_type PRIMARY_TYPE
89 p_prec NO_PREC
91 Other cells that need to be filled in are defaulted by
92 get_new_pred_chk_op, which is used to insure that the prior node is
93 either not there at all (we are the very first node) or is an
94 operator. */
96 struct predicate *
97 insert_primary_withpred (const struct parser_table *entry, PRED_FUNC pred_func)
99 struct predicate *new_pred;
101 new_pred = get_new_pred_chk_op (entry);
102 new_pred->pred_func = pred_func;
103 new_pred->p_name = entry->parser_name;
104 new_pred->args.str = NULL;
105 new_pred->p_type = PRIMARY_TYPE;
106 new_pred->p_prec = NO_PREC;
107 return new_pred;
110 /* Add a primary described by ENTRY to the predicate input list.
112 Return a pointer to the predicate node just inserted.
114 Fills in the following cells of the new predicate node:
116 pred_func PRED_FUNC
117 args(.str) NULL
118 p_type PRIMARY_TYPE
119 p_prec NO_PREC
121 Other cells that need to be filled in are defaulted by
122 get_new_pred_chk_op, which is used to insure that the prior node is
123 either not there at all (we are the very first node) or is an
124 operator. */
125 struct predicate *
126 insert_primary (const struct parser_table *entry)
128 assert(entry->pred_func != NULL);
129 return insert_primary_withpred(entry, entry->pred_func);
134 static void
135 show_valid_debug_options(FILE *fp, int full)
137 int i;
138 if (full)
140 fprintf(fp, "Valid arguments for -D:\n");
141 for (i=0; i<N_DEBUGASSOC; ++i)
143 fprintf(fp, "%-10s %s\n",
144 debugassoc[i].name,
145 debugassoc[i].docstring);
148 else
150 for (i=0; i<N_DEBUGASSOC; ++i)
152 fprintf(fp, "%s%s", (i>0 ? "|" : ""), debugassoc[i].name);
157 void
158 usage (FILE *fp, int status, char *msg)
160 if (msg)
161 fprintf (fp, "%s: %s\n", program_name, msg);
163 fprintf (fp, _("Usage: %s [-H] [-L] [-P] [-Olevel] [-D "), program_name);
164 show_valid_debug_options(fp, 0);
165 fprintf (fp, _("] [path...] [expression]\n"));
166 if (0 != status)
167 exit (status);
170 void
171 set_stat_placeholders(struct stat *p)
173 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
174 p->st_birthtime = 0;
175 #endif
176 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
177 p->st_birthtimensec = 0;
178 #endif
179 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
180 p->st_birthtimespec.tv_nsec = -1;
181 #endif
182 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_SEC
183 p->st_birthtimespec.tv_sec = 0;
184 #endif
188 /* Get the stat information for a file, if it is
189 * not already known.
192 get_statinfo (const char *pathname, const char *name, struct stat *p)
194 /* Set markers in fields so we have a good idea if the implementation
195 * didn't bother to set them (e.g., NetBSD st_birthtimespec for MS-DOS
196 * files)
198 if (!state.have_stat)
200 set_stat_placeholders(p);
201 if (0 == (*options.xstat) (name, p))
203 if (00000 == p->st_mode)
205 /* Savannah bug #16378. */
206 error(0, 0, _("Warning: file %s appears to have mode 0000"),
207 quotearg_n_style(0, options.err_quoting_style, name));
210 else
212 if (!options.ignore_readdir_race || (errno != ENOENT) )
214 error (0, errno, "%s",
215 safely_quote_err_filename(0, pathname));
216 state.exit_status = 1;
218 return -1;
221 state.have_stat = true;
222 state.have_type = true;
223 state.type = p->st_mode;
225 return 0;
229 /* Get the stat/type information for a file, if it is
230 * not already known.
233 get_info (const char *pathname,
234 struct stat *p,
235 struct predicate *pred_ptr)
237 boolean todo = false;
239 /* If we need the full stat info, or we need the type info but don't
240 * already have it, stat the file now.
242 if (pred_ptr->need_stat)
243 todo = true;
244 else if ((pred_ptr->need_type && (0 == state.have_type)))
245 todo = true;
247 if (todo)
248 return get_statinfo(pathname, state.rel_pathname, p);
249 else
250 return 0;
253 /* Determine if we can use O_NOFOLLOW.
255 #if defined(O_NOFOLLOW)
256 boolean
257 check_nofollow(void)
259 struct utsname uts;
260 float release;
262 if (0 == O_NOFOLLOW)
264 return false;
267 if (0 == uname(&uts))
269 /* POSIX requires that atof() ignore "unrecognised suffixes". */
270 release = atof(uts.release);
272 if (0 == strcmp("Linux", uts.sysname))
274 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
275 return release >= 2.2; /* close enough */
277 else if (0 == strcmp("FreeBSD", uts.sysname))
279 /* FreeBSD 3.0-CURRENT and later support it */
280 return release >= 3.1;
284 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
285 return true;
287 #endif
291 /* Examine the predicate list for instances of -execdir or -okdir
292 * which have been terminated with '+' (build argument list) rather
293 * than ';' (singles only). If there are any, run them (this will
294 * have no effect if there are no arguments waiting).
296 static void
297 do_complete_pending_execdirs(struct predicate *p, int dirfd)
299 if (NULL == p)
300 return;
302 assert(state.execdirs_outstanding);
304 do_complete_pending_execdirs(p->pred_left, dirfd);
306 if (pred_is(p, pred_execdir) || pred_is(p, pred_okdir))
308 /* It's an exec-family predicate. p->args.exec_val is valid. */
309 if (p->args.exec_vec.multiple)
311 struct exec_val *execp = &p->args.exec_vec;
313 /* This one was terminated by '+' and so might have some
314 * left... Run it if necessary.
316 if (execp->state.todo)
318 /* There are not-yet-executed arguments. */
319 launch (&execp->ctl, &execp->state);
324 do_complete_pending_execdirs(p->pred_right, dirfd);
327 void
328 complete_pending_execdirs(int dirfd)
330 if (state.execdirs_outstanding)
332 do_complete_pending_execdirs(get_eval_tree(), dirfd);
333 state.execdirs_outstanding = false;
339 /* Examine the predicate list for instances of -exec which have been
340 * terminated with '+' (build argument list) rather than ';' (singles
341 * only). If there are any, run them (this will have no effect if
342 * there are no arguments waiting).
344 void
345 complete_pending_execs(struct predicate *p)
347 if (NULL == p)
348 return;
350 complete_pending_execs(p->pred_left);
352 /* It's an exec-family predicate then p->args.exec_val is valid
353 * and we can check it.
355 /* XXX: what about pred_ok() ? */
356 if (pred_is(p, pred_exec) && p->args.exec_vec.multiple)
358 struct exec_val *execp = &p->args.exec_vec;
360 /* This one was terminated by '+' and so might have some
361 * left... Run it if necessary. Set state.exit_status if
362 * there are any problems.
364 if (execp->state.todo)
366 /* There are not-yet-executed arguments. */
367 launch (&execp->ctl, &execp->state);
371 complete_pending_execs(p->pred_right);
374 static void
375 traverse_tree(struct predicate *tree,
376 void (*callback)(struct predicate*))
378 if (tree->pred_left)
379 traverse_tree(tree->pred_left, callback);
381 callback(tree);
383 if (tree->pred_right)
384 traverse_tree(tree->pred_right, callback);
387 static void
388 flush_and_close_output_files(struct predicate *p)
390 if (pred_is(p, pred_fprint)
391 || pred_is(p, pred_fprintf)
392 || pred_is(p, pred_fls)
393 || pred_is(p, pred_fprint0))
395 FILE *f = p->args.printf_vec.stream;
396 bool failed;
398 if (f == stdout || f == stderr)
399 failed = fflush(p->args.printf_vec.stream) == EOF;
400 else
401 failed = fclose(p->args.printf_vec.stream) == EOF;
403 if (failed)
404 nonfatal_file_error(p->args.printf_vec.filename);
406 else if (pred_is(p, pred_print))
408 if (fflush(p->args.printf_vec.stream) == EOF)
410 nonfatal_file_error(p->args.printf_vec.filename);
413 else if (pred_is(p, pred_ls) || pred_is(p, pred_print0))
415 if (fflush(stdout) == EOF)
417 /* XXX: migrate to printf_vec. */
418 nonfatal_file_error("standard output");
423 /* Complete any outstanding commands.
425 void
426 cleanup(void)
428 struct predicate *eval_tree = get_eval_tree();
429 if (eval_tree)
431 traverse_tree(eval_tree, complete_pending_execs);
432 complete_pending_execdirs(get_current_dirfd());
433 traverse_tree(eval_tree, flush_and_close_output_files);
437 /* Savannah bug #16378 manifests as an assertion failure in pred_type()
438 * when an NFS server returns st_mode with value 0 (of course the stat(2)
439 * system call is itself returning 0 in this case).
441 #undef DEBUG_SV_BUG_16378
442 #if defined(DEBUG_SV_BUG_16378)
443 static int hook_fstatat(int fd, const char *name, struct stat *p, int flags)
445 static int warned = 0;
447 if (!warned)
449 /* No use of _() here; no point asking translators to translate a debug msg */
450 error(0, 0,
451 "Warning: some debug code is enabled for Savannah bug #16378; "
452 "this should not occur in released versions of findutils!");
453 warned = 1;
456 if (0 == strcmp(name, "./mode0file")
457 || 0 == strcmp(name, "mode0file"))
459 time_t now = time(NULL);
460 long day = 86400;
462 p->st_rdev = 0;
463 p->st_dev = 0x300;
464 p->st_ino = 0;
465 p->st_mode = 0; /* SV bug #16378 */
466 p->st_nlink = 1;
467 p->st_uid = geteuid();
468 p->st_gid = 0;
469 p->st_size = 42;
470 p->st_blksize = 32768;
471 p->st_atime = now-1*day;
472 p->st_mtime = now-2*day;
473 p->st_ctime = now-3*day;
475 return 0;
477 return fstatat(fd, name, p, flags);
480 # undef fstatat
481 # define fstatat(fd,name,p,flags) hook_fstatat((fd),(name),(p),(flags))
482 #endif
485 static int
486 fallback_stat(const char *name, struct stat *p, int prev_rv)
488 /* Our original stat() call failed. Perhaps we can't follow a
489 * symbolic link. If that might be the problem, lstat() the link.
490 * Otherwise, admit defeat.
492 switch (errno)
494 case ENOENT:
495 case ENOTDIR:
496 if (options.debug_options & DebugStat)
497 fprintf(stderr, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name);
498 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
500 case EACCES:
501 case EIO:
502 case ELOOP:
503 case ENAMETOOLONG:
504 #ifdef EOVERFLOW
505 case EOVERFLOW: /* EOVERFLOW is not #defined on UNICOS. */
506 #endif
507 default:
508 return prev_rv;
513 /* optionh_stat() implements the stat operation when the -H option is
514 * in effect.
516 * If the item to be examined is a command-line argument, we follow
517 * symbolic links. If the stat() call fails on the command-line item,
518 * we fall back on the properties of the symbolic link.
520 * If the item to be examined is not a command-line argument, we
521 * examine the link itself.
523 int
524 optionh_stat(const char *name, struct stat *p)
526 if (AT_FDCWD != state.cwd_dir_fd)
527 assert(state.cwd_dir_fd >= 0);
528 set_stat_placeholders(p);
529 if (0 == state.curdepth)
531 /* This file is from the command line; deference the link (if it
532 * is a link).
534 int rv;
535 rv = fstatat(state.cwd_dir_fd, name, p, 0);
536 if (0 == rv)
537 return 0; /* success */
538 else
539 return fallback_stat(name, p, rv);
541 else
543 /* Not a file on the command line; do not dereference the link.
545 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
549 /* optionl_stat() implements the stat operation when the -L option is
550 * in effect. That option makes us examine the thing the symbolic
551 * link points to, not the symbolic link itself.
553 int
554 optionl_stat(const char *name, struct stat *p)
556 int rv;
557 if (AT_FDCWD != state.cwd_dir_fd)
558 assert(state.cwd_dir_fd >= 0);
560 set_stat_placeholders(p);
561 rv = fstatat(state.cwd_dir_fd, name, p, 0);
562 if (0 == rv)
563 return 0; /* normal case. */
564 else
565 return fallback_stat(name, p, rv);
568 /* optionp_stat() implements the stat operation when the -P option is
569 * in effect (this is also the default). That option makes us examine
570 * the symbolic link itself, not the thing it points to.
572 int
573 optionp_stat(const char *name, struct stat *p)
575 assert((state.cwd_dir_fd >= 0) || (state.cwd_dir_fd==AT_FDCWD));
576 set_stat_placeholders(p);
577 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
581 static uintmax_t stat_count = 0u;
584 debug_stat (const char *file, struct stat *bufp)
586 ++stat_count;
587 fprintf (stderr, "debug_stat (%s)\n", file);
589 switch (options.symlink_handling)
591 case SYMLINK_ALWAYS_DEREF:
592 return optionl_stat(file, bufp);
593 case SYMLINK_DEREF_ARGSONLY:
594 return optionh_stat(file, bufp);
595 case SYMLINK_NEVER_DEREF:
596 return optionp_stat(file, bufp);
598 /*NOTREACHED*/
599 assert(false);
600 return -1;
605 following_links(void)
607 switch (options.symlink_handling)
609 case SYMLINK_ALWAYS_DEREF:
610 return 1;
611 case SYMLINK_DEREF_ARGSONLY:
612 return (state.curdepth == 0);
613 case SYMLINK_NEVER_DEREF:
614 default:
615 return 0;
620 /* Take a "mode" indicator and fill in the files of 'state'.
623 digest_mode(mode_t mode,
624 const char *pathname,
625 const char *name,
626 struct stat *pstat,
627 boolean leaf)
629 /* If we know the type of the directory entry, and it is not a
630 * symbolic link, we may be able to avoid a stat() or lstat() call.
632 if (mode)
634 if (S_ISLNK(mode) && following_links())
636 /* mode is wrong because we should have followed the symlink. */
637 if (get_statinfo(pathname, name, pstat) != 0)
638 return 0;
639 mode = state.type = pstat->st_mode;
640 state.have_type = true;
642 else
644 state.have_type = true;
645 pstat->st_mode = state.type = mode;
648 else
650 /* Mode is not yet known; may have to stat the file unless we
651 * can deduce that it is not a directory (which is all we need to
652 * know at this stage)
654 if (leaf)
656 state.have_stat = false;
657 state.have_type = false;;
658 state.type = 0;
660 else
662 if (get_statinfo(pathname, name, pstat) != 0)
663 return 0;
665 /* If -L is in effect and we are dealing with a symlink,
666 * st_mode is the mode of the pointed-to file, while mode is
667 * the mode of the directory entry (S_IFLNK). Hence now
668 * that we have the stat information, override "mode".
670 state.type = pstat->st_mode;
671 state.have_type = true;
675 /* success. */
676 return 1;
680 /* Return true if there are no predicates with no_default_print in
681 predicate list PRED, false if there are any.
682 Returns true if default print should be performed */
684 boolean
685 default_prints (struct predicate *pred)
687 while (pred != NULL)
689 if (pred->no_default_print)
690 return (false);
691 pred = pred->pred_next;
693 return (true);
696 boolean
697 looks_like_expression(const char *arg, boolean leading)
699 switch (arg[0])
701 case '-':
702 if (arg[1]) /* "-foo" is an expression. */
703 return true;
704 else
705 return false; /* Just "-" is a filename. */
706 break;
708 case ')':
709 case ',':
710 if (arg[1])
711 return false; /* )x and ,z are not expressions */
712 else
713 return !leading; /* A leading ) or , is not either */
715 /* ( and ! are part of an expression, but (2 and !foo are
716 * filenames.
718 case '!':
719 case '(':
720 if (arg[1])
721 return false;
722 else
723 return true;
725 default:
726 return false;
730 static void
731 process_debug_options(char *arg)
733 const char *p;
734 char *token_context = NULL;
735 const char delimiters[] = ",";
736 boolean empty = true;
737 size_t i;
739 p = strtok_r(arg, delimiters, &token_context);
740 while (p)
742 empty = false;
744 for (i=0; i<N_DEBUGASSOC; ++i)
746 if (0 == strcmp(debugassoc[i].name, p))
748 options.debug_options |= debugassoc[i].val;
749 break;
752 if (i >= N_DEBUGASSOC)
754 error(0, 0, _("Ignoring unrecognised debug flag %s"),
755 quotearg_n_style(0, options.err_quoting_style, arg));
757 p = strtok_r(NULL, delimiters, &token_context);
759 if (empty)
761 error(1, 0, _("Empty argument to the -D option."));
763 else if (options.debug_options & DebugHelp)
765 show_valid_debug_options(stdout, 1);
766 exit(0);
770 static void
771 process_optimisation_option(const char *arg)
773 if (0 == arg[0])
775 error(1, 0, _("The -O option must be immediately followed by a decimal integer"));
777 else
779 unsigned long opt_level;
780 char *end;
782 if (!isdigit( (unsigned char) arg[0] ))
784 error(1, 0, _("Please specify a decimal number immediately after -O"));
786 else
788 int prev_errno = errno;
789 errno = 0;
791 opt_level = strtoul(arg, &end, 10);
792 if ( (0==opt_level) && (end==arg) )
794 error(1, 0, _("Please specify a decimal number immediately after -O"));
796 else if (*end)
798 /* unwanted trailing characters. */
799 error(1, 0, _("Invalid optimisation level %s"), arg);
801 else if ( (ULONG_MAX==opt_level) && errno)
803 error(1, errno, _("Invalid optimisation level %s"), arg);
805 else if (opt_level > USHRT_MAX)
807 /* tricky to test, as on some platforms USHORT_MAX and ULONG_MAX
808 * can have the same value, though this is unusual.
810 error(1, 0, _("Optimisation level %lu is too high. "
811 "If you want to find files very quickly, "
812 "consider using GNU locate."),
813 opt_level);
815 else
817 options.optimisation_level = opt_level;
818 errno = prev_errno;
825 process_leading_options(int argc, char *argv[])
827 int i, end_of_leading_options;
829 for (i=1; (end_of_leading_options = i) < argc; ++i)
831 if (0 == strcmp("-H", argv[i]))
833 /* Meaning: dereference symbolic links on command line, but nowhere else. */
834 set_follow_state(SYMLINK_DEREF_ARGSONLY);
836 else if (0 == strcmp("-L", argv[i]))
838 /* Meaning: dereference all symbolic links. */
839 set_follow_state(SYMLINK_ALWAYS_DEREF);
841 else if (0 == strcmp("-P", argv[i]))
843 /* Meaning: never dereference symbolic links (default). */
844 set_follow_state(SYMLINK_NEVER_DEREF);
846 else if (0 == strcmp("--", argv[i]))
848 /* -- signifies the end of options. */
849 end_of_leading_options = i+1; /* Next time start with the next option */
850 break;
852 else if (0 == strcmp("-D", argv[i]))
854 process_debug_options(argv[i+1]);
855 ++i; /* skip the argument too. */
857 else if (0 == strncmp("-O", argv[i], 2))
859 process_optimisation_option(argv[i]+2);
861 else
863 /* Hmm, must be one of
864 * (a) A path name
865 * (b) A predicate
867 end_of_leading_options = i; /* Next time start with this option */
868 break;
871 return end_of_leading_options;
874 static struct timespec
875 now(void)
877 struct timespec retval;
878 struct timeval tv;
879 time_t t;
881 if (0 == gettimeofday(&tv, NULL))
883 retval.tv_sec = tv.tv_sec;
884 retval.tv_nsec = tv.tv_usec * 1000; /* convert unit from microseconds to nanoseconds */
885 return retval;
887 t = time(NULL);
888 assert(t != (time_t)-1);
889 retval.tv_sec = t;
890 retval.tv_nsec = 0;
891 return retval;
894 void
895 set_option_defaults(struct options *p)
897 /* We call check_nofollow() before setlocale() because the numbers
898 * for which we check (in the results of uname) definitiely have "."
899 * as the decimal point indicator even under locales for which that
900 * is not normally true. Hence atof() would do the wrong thing
901 * if we call it after setlocale().
903 #ifdef O_NOFOLLOW
904 p->open_nofollow_available = check_nofollow();
905 #else
906 p->open_nofollow_available = false;
907 #endif
909 p->regex_options = RE_SYNTAX_EMACS;
911 if (isatty(0))
913 p->warnings = true;
914 p->literal_control_chars = false;
916 else
918 p->warnings = false;
919 p->literal_control_chars = false; /* may change */
923 p->do_dir_first = true;
924 p->maxdepth = p->mindepth = -1;
925 p->start_time = now();
926 p->cur_day_start = p->start_time.tv_sec - DAYSECS;
927 p->full_days = false;
928 p->stay_on_filesystem = false;
929 p->ignore_readdir_race = false;
931 if (getenv("POSIXLY_CORRECT"))
932 p->output_block_size = 512;
933 else
934 p->output_block_size = 1024;
936 p->debug_options = 0uL;
937 p->optimisation_level = 0;
939 if (getenv("FIND_BLOCK_SIZE"))
941 error (1, 0, _("The environment variable FIND_BLOCK_SIZE is not supported, the only thing that affects the block size is the POSIXLY_CORRECT environment variable"));
944 #if LEAF_OPTIMISATION
945 /* The leaf optimisation is enabled. */
946 p->no_leaf_check = false;
947 #else
948 /* The leaf optimisation is disabled. */
949 p->no_leaf_check = true;
950 #endif
952 set_follow_state(SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */
954 p->err_quoting_style = locale_quoting_style;
958 /* get_start_dirfd
960 * Returns the fd for the directory we started in.
962 int get_start_dirfd(void)
964 return starting_desc;
967 /* apply_predicate
970 boolean
971 apply_predicate(const char *pathname, struct stat *stat_buf, struct predicate *p)
973 ++p->perf.visits;
975 if (p->need_stat || p->need_type)
977 /* We may need a stat here. */
978 if (get_info(pathname, stat_buf, p) != 0)
979 return false;
981 if ((p->pred_func)(pathname, stat_buf, p))
983 ++(p->perf.successes);
984 return true;
986 else
988 return false;
993 /* safely_quote_err_filename
996 const char *
997 safely_quote_err_filename (int n, char const *arg)
999 return quotearg_n_style (n, options.err_quoting_style, arg);
1002 /* report_file_err
1004 static void
1005 report_file_err(int exitval, int errno_value, const char *name)
1007 /* It is important that the errno value is passed in as a function
1008 * argument before we call safely_quote_err_filename(), because otherwise
1009 * we might find that safely_quote_err_filename() changes errno.
1011 if (state.exit_status < 1)
1012 state.exit_status = 1;
1014 error (exitval, errno_value, "%s", safely_quote_err_filename(0, name));
1017 /* fatal_file_error
1020 void
1021 fatal_file_error(const char *name)
1023 report_file_err(1, errno, name);
1024 /*NOTREACHED*/
1025 abort();
1028 void
1029 nonfatal_file_error(const char *name)
1031 report_file_err(0, errno, name);