Fix for Savannah bug #20273, find -ok with seekable stdin.
[findutils.git] / find / util.c
blob8cd7ef90bb334cf2ed321fe3770fd2ac0f6f5408
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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>.
18 #include <config.h>
19 #include "defs.h"
21 #include <fcntl.h>
22 #ifdef HAVE_SYS_UTSNAME_H
23 #include <sys/utsname.h>
24 #endif
25 #include <sys/time.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <errno.h>
30 #include <assert.h>
32 #include "xalloc.h"
33 #include "quotearg.h"
34 #include "timespec.h"
35 #include "error.h"
36 #include "verify.h"
37 #include "openat.h"
39 #if ENABLE_NLS
40 # include <libintl.h>
41 # define _(Text) gettext (Text)
42 #else
43 # define _(Text) Text
44 #endif
45 #ifdef gettext_noop
46 # define N_(String) gettext_noop (String)
47 #else
48 /* See locate.c for explanation as to why not use (String) */
49 # define N_(String) String
50 #endif
53 struct debug_option_assoc
55 char *name;
56 int val;
57 char *docstring;
59 static struct debug_option_assoc debugassoc[] =
61 { "help", DebugHelp, "Explain the various -D options" },
62 { "tree", DebugExpressionTree, "Display the expression tree" },
63 { "search",DebugSearch, "Navigate the directory tree verbosely" },
64 { "stat", DebugStat, "Trace calls to stat(2) and lstat(2)" },
65 { "rates", DebugSuccessRates, "Indicate how often each predicate succeeded" },
66 { "opt", DebugExpressionTree|DebugTreeOpt, "Show diagnostic information relating to optimisation" },
67 { "exec", DebugExec, "Show diagnostic information relating to -exec, -execdir, -ok and -okdir" }
69 #define N_DEBUGASSOC (sizeof(debugassoc)/sizeof(debugassoc[0]))
74 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
76 Return a pointer to the predicate node just inserted.
78 Fills in the following cells of the new predicate node:
80 pred_func PRED_FUNC
81 args(.str) NULL
82 p_type PRIMARY_TYPE
83 p_prec NO_PREC
85 Other cells that need to be filled in are defaulted by
86 get_new_pred_chk_op, which is used to insure that the prior node is
87 either not there at all (we are the very first node) or is an
88 operator. */
90 struct predicate *
91 insert_primary_withpred (const struct parser_table *entry, PRED_FUNC pred_func)
93 struct predicate *new_pred;
95 new_pred = get_new_pred_chk_op (entry);
96 new_pred->pred_func = pred_func;
97 new_pred->p_name = entry->parser_name;
98 new_pred->args.str = NULL;
99 new_pred->p_type = PRIMARY_TYPE;
100 new_pred->p_prec = NO_PREC;
101 return new_pred;
104 /* Add a primary described by ENTRY to the predicate input list.
106 Return a pointer to the predicate node just inserted.
108 Fills in the following cells of the new predicate node:
110 pred_func PRED_FUNC
111 args(.str) NULL
112 p_type PRIMARY_TYPE
113 p_prec NO_PREC
115 Other cells that need to be filled in are defaulted by
116 get_new_pred_chk_op, which is used to insure that the prior node is
117 either not there at all (we are the very first node) or is an
118 operator. */
119 struct predicate *
120 insert_primary (const struct parser_table *entry)
122 assert (entry->pred_func != NULL);
123 return insert_primary_withpred(entry, entry->pred_func);
128 static void
129 show_valid_debug_options(FILE *fp, int full)
131 int i;
132 if (full)
134 fprintf(fp, "Valid arguments for -D:\n");
135 for (i=0; i<N_DEBUGASSOC; ++i)
137 fprintf(fp, "%-10s %s\n",
138 debugassoc[i].name,
139 debugassoc[i].docstring);
142 else
144 for (i=0; i<N_DEBUGASSOC; ++i)
146 fprintf(fp, "%s%s", (i>0 ? "|" : ""), debugassoc[i].name);
151 void
152 usage (FILE *fp, int status, char *msg)
154 if (msg)
155 fprintf (fp, "%s: %s\n", program_name, msg);
157 fprintf (fp, _("Usage: %s [-H] [-L] [-P] [-Olevel] [-D "), program_name);
158 show_valid_debug_options(fp, 0);
159 fprintf (fp, _("] [path...] [expression]\n"));
160 if (0 != status)
161 exit (status);
164 void
165 set_stat_placeholders(struct stat *p)
167 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
168 p->st_birthtime = 0;
169 #endif
170 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
171 p->st_birthtimensec = 0;
172 #endif
173 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
174 p->st_birthtimespec.tv_nsec = -1;
175 #endif
176 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_SEC
177 p->st_birthtimespec.tv_sec = 0;
178 #endif
182 /* Get the stat information for a file, if it is
183 * not already known.
186 get_statinfo (const char *pathname, const char *name, struct stat *p)
188 /* Set markers in fields so we have a good idea if the implementation
189 * didn't bother to set them (e.g., NetBSD st_birthtimespec for MS-DOS
190 * files)
192 if (!state.have_stat)
194 set_stat_placeholders(p);
195 if (0 == (*options.xstat) (name, p))
197 if (00000 == p->st_mode)
199 /* Savannah bug #16378. */
200 error(0, 0, _("Warning: file %s appears to have mode 0000"),
201 quotearg_n_style(0, options.err_quoting_style, name));
204 else
206 if (!options.ignore_readdir_race || (errno != ENOENT) )
208 error (0, errno, "%s",
209 safely_quote_err_filename(0, pathname));
210 state.exit_status = 1;
212 return -1;
215 state.have_stat = true;
216 state.have_type = true;
217 state.type = p->st_mode;
219 return 0;
223 /* Get the stat/type information for a file, if it is
224 * not already known.
227 get_info (const char *pathname,
228 struct stat *p,
229 struct predicate *pred_ptr)
231 boolean todo = false;
233 /* If we need the full stat info, or we need the type info but don't
234 * already have it, stat the file now.
236 if (pred_ptr->need_stat)
237 todo = true;
238 else if ((pred_ptr->need_type && (0 == state.have_type)))
239 todo = true;
241 if (todo)
242 return get_statinfo(pathname, state.rel_pathname, p);
243 else
244 return 0;
247 /* Determine if we can use O_NOFOLLOW.
249 #if defined O_NOFOLLOW
250 boolean
251 check_nofollow(void)
253 struct utsname uts;
254 float release;
256 if (0 == O_NOFOLLOW)
258 return false;
261 if (0 == uname(&uts))
263 /* POSIX requires that atof() ignore "unrecognised suffixes". */
264 release = atof(uts.release);
266 if (0 == strcmp("Linux", uts.sysname))
268 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
269 return release >= 2.2; /* close enough */
271 else if (0 == strcmp("FreeBSD", uts.sysname))
273 /* FreeBSD 3.0-CURRENT and later support it */
274 return release >= 3.1;
278 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
279 return true;
281 #endif
285 /* Examine the predicate list for instances of -execdir or -okdir
286 * which have been terminated with '+' (build argument list) rather
287 * than ';' (singles only). If there are any, run them (this will
288 * have no effect if there are no arguments waiting).
290 static void
291 do_complete_pending_execdirs(struct predicate *p, int dirfd)
293 if (NULL == p)
294 return;
296 assert (state.execdirs_outstanding);
298 do_complete_pending_execdirs(p->pred_left, dirfd);
300 if (pred_is(p, pred_execdir) || pred_is(p, pred_okdir))
302 /* It's an exec-family predicate. p->args.exec_val is valid. */
303 if (p->args.exec_vec.multiple)
305 struct exec_val *execp = &p->args.exec_vec;
307 /* This one was terminated by '+' and so might have some
308 * left... Run it if necessary.
310 if (execp->state.todo)
312 /* There are not-yet-executed arguments. */
313 launch (&execp->ctl, &execp->state);
318 do_complete_pending_execdirs(p->pred_right, dirfd);
321 void
322 complete_pending_execdirs(int dirfd)
324 if (state.execdirs_outstanding)
326 do_complete_pending_execdirs(get_eval_tree(), dirfd);
327 state.execdirs_outstanding = false;
333 /* Examine the predicate list for instances of -exec which have been
334 * terminated with '+' (build argument list) rather than ';' (singles
335 * only). If there are any, run them (this will have no effect if
336 * there are no arguments waiting).
338 void
339 complete_pending_execs(struct predicate *p)
341 if (NULL == p)
342 return;
344 complete_pending_execs(p->pred_left);
346 /* It's an exec-family predicate then p->args.exec_val is valid
347 * and we can check it.
349 /* XXX: what about pred_ok() ? */
350 if (pred_is(p, pred_exec) && p->args.exec_vec.multiple)
352 struct exec_val *execp = &p->args.exec_vec;
354 /* This one was terminated by '+' and so might have some
355 * left... Run it if necessary. Set state.exit_status if
356 * there are any problems.
358 if (execp->state.todo)
360 /* There are not-yet-executed arguments. */
361 launch (&execp->ctl, &execp->state);
365 complete_pending_execs(p->pred_right);
368 static void
369 traverse_tree(struct predicate *tree,
370 void (*callback)(struct predicate*))
372 if (tree->pred_left)
373 traverse_tree(tree->pred_left, callback);
375 callback(tree);
377 if (tree->pred_right)
378 traverse_tree(tree->pred_right, callback);
381 static void
382 flush_and_close_output_files(struct predicate *p)
384 if (pred_is(p, pred_fprint)
385 || pred_is(p, pred_fprintf)
386 || pred_is(p, pred_fls)
387 || pred_is(p, pred_fprint0))
389 FILE *f = p->args.printf_vec.stream;
390 bool failed;
392 if (f == stdout || f == stderr)
393 failed = fflush(p->args.printf_vec.stream) == EOF;
394 else
395 failed = fclose(p->args.printf_vec.stream) == EOF;
397 if (failed)
398 nonfatal_file_error(p->args.printf_vec.filename);
400 else if (pred_is(p, pred_print))
402 if (fflush(p->args.printf_vec.stream) == EOF)
404 nonfatal_file_error(p->args.printf_vec.filename);
407 else if (pred_is(p, pred_ls) || pred_is(p, pred_print0))
409 if (fflush(stdout) == EOF)
411 /* XXX: migrate to printf_vec. */
412 nonfatal_file_error("standard output");
417 /* Complete any outstanding commands.
419 void
420 cleanup(void)
422 struct predicate *eval_tree = get_eval_tree();
423 if (eval_tree)
425 traverse_tree(eval_tree, complete_pending_execs);
426 complete_pending_execdirs(get_current_dirfd());
427 traverse_tree(eval_tree, flush_and_close_output_files);
431 /* Savannah bug #16378 manifests as an assertion failure in pred_type()
432 * when an NFS server returns st_mode with value 0 (of course the stat(2)
433 * system call is itself returning 0 in this case).
435 #undef DEBUG_SV_BUG_16378
436 #if defined DEBUG_SV_BUG_16378
437 static int hook_fstatat(int fd, const char *name, struct stat *p, int flags)
439 static int warned = 0;
441 if (!warned)
443 /* No use of _() here; no point asking translators to translate a debug msg */
444 error(0, 0,
445 "Warning: some debug code is enabled for Savannah bug #16378; "
446 "this should not occur in released versions of findutils!");
447 warned = 1;
450 if (0 == strcmp(name, "./mode0file")
451 || 0 == strcmp(name, "mode0file"))
453 time_t now = time(NULL);
454 long day = 86400;
456 p->st_rdev = 0;
457 p->st_dev = 0x300;
458 p->st_ino = 0;
459 p->st_mode = 0; /* SV bug #16378 */
460 p->st_nlink = 1;
461 p->st_uid = geteuid();
462 p->st_gid = 0;
463 p->st_size = 42;
464 p->st_blksize = 32768;
465 p->st_atime = now-1*day;
466 p->st_mtime = now-2*day;
467 p->st_ctime = now-3*day;
469 return 0;
471 return fstatat(fd, name, p, flags);
474 # undef fstatat
475 # define fstatat(fd,name,p,flags) hook_fstatat((fd),(name),(p),(flags))
476 #endif
479 static int
480 fallback_stat(const char *name, struct stat *p, int prev_rv)
482 /* Our original stat() call failed. Perhaps we can't follow a
483 * symbolic link. If that might be the problem, lstat() the link.
484 * Otherwise, admit defeat.
486 switch (errno)
488 case ENOENT:
489 case ENOTDIR:
490 if (options.debug_options & DebugStat)
491 fprintf(stderr, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name);
492 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
494 case EACCES:
495 case EIO:
496 case ELOOP:
497 case ENAMETOOLONG:
498 #ifdef EOVERFLOW
499 case EOVERFLOW: /* EOVERFLOW is not #defined on UNICOS. */
500 #endif
501 default:
502 return prev_rv;
507 /* optionh_stat() implements the stat operation when the -H option is
508 * in effect.
510 * If the item to be examined is a command-line argument, we follow
511 * symbolic links. If the stat() call fails on the command-line item,
512 * we fall back on the properties of the symbolic link.
514 * If the item to be examined is not a command-line argument, we
515 * examine the link itself.
517 int
518 optionh_stat(const char *name, struct stat *p)
520 if (AT_FDCWD != state.cwd_dir_fd)
521 assert (state.cwd_dir_fd >= 0);
522 set_stat_placeholders(p);
523 if (0 == state.curdepth)
525 /* This file is from the command line; deference the link (if it
526 * is a link).
528 int rv;
529 rv = fstatat(state.cwd_dir_fd, name, p, 0);
530 if (0 == rv)
531 return 0; /* success */
532 else
533 return fallback_stat(name, p, rv);
535 else
537 /* Not a file on the command line; do not dereference the link.
539 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
543 /* optionl_stat() implements the stat operation when the -L option is
544 * in effect. That option makes us examine the thing the symbolic
545 * link points to, not the symbolic link itself.
547 int
548 optionl_stat(const char *name, struct stat *p)
550 int rv;
551 if (AT_FDCWD != state.cwd_dir_fd)
552 assert (state.cwd_dir_fd >= 0);
554 set_stat_placeholders(p);
555 rv = fstatat(state.cwd_dir_fd, name, p, 0);
556 if (0 == rv)
557 return 0; /* normal case. */
558 else
559 return fallback_stat(name, p, rv);
562 /* optionp_stat() implements the stat operation when the -P option is
563 * in effect (this is also the default). That option makes us examine
564 * the symbolic link itself, not the thing it points to.
566 int
567 optionp_stat(const char *name, struct stat *p)
569 assert ((state.cwd_dir_fd >= 0) || (state.cwd_dir_fd==AT_FDCWD));
570 set_stat_placeholders(p);
571 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
575 static uintmax_t stat_count = 0u;
578 debug_stat (const char *file, struct stat *bufp)
580 ++stat_count;
581 fprintf (stderr, "debug_stat (%s)\n", file);
583 switch (options.symlink_handling)
585 case SYMLINK_ALWAYS_DEREF:
586 return optionl_stat(file, bufp);
587 case SYMLINK_DEREF_ARGSONLY:
588 return optionh_stat(file, bufp);
589 case SYMLINK_NEVER_DEREF:
590 return optionp_stat(file, bufp);
592 /*NOTREACHED*/
593 assert (0);
594 return -1;
599 following_links(void)
601 switch (options.symlink_handling)
603 case SYMLINK_ALWAYS_DEREF:
604 return 1;
605 case SYMLINK_DEREF_ARGSONLY:
606 return (state.curdepth == 0);
607 case SYMLINK_NEVER_DEREF:
608 default:
609 return 0;
614 /* Take a "mode" indicator and fill in the files of 'state'.
617 digest_mode(mode_t mode,
618 const char *pathname,
619 const char *name,
620 struct stat *pstat,
621 boolean leaf)
623 /* If we know the type of the directory entry, and it is not a
624 * symbolic link, we may be able to avoid a stat() or lstat() call.
626 if (mode)
628 if (S_ISLNK(mode) && following_links())
630 /* mode is wrong because we should have followed the symlink. */
631 if (get_statinfo(pathname, name, pstat) != 0)
632 return 0;
633 mode = state.type = pstat->st_mode;
634 state.have_type = true;
636 else
638 state.have_type = true;
639 pstat->st_mode = state.type = mode;
642 else
644 /* Mode is not yet known; may have to stat the file unless we
645 * can deduce that it is not a directory (which is all we need to
646 * know at this stage)
648 if (leaf)
650 state.have_stat = false;
651 state.have_type = false;;
652 state.type = 0;
654 else
656 if (get_statinfo(pathname, name, pstat) != 0)
657 return 0;
659 /* If -L is in effect and we are dealing with a symlink,
660 * st_mode is the mode of the pointed-to file, while mode is
661 * the mode of the directory entry (S_IFLNK). Hence now
662 * that we have the stat information, override "mode".
664 state.type = pstat->st_mode;
665 state.have_type = true;
669 /* success. */
670 return 1;
674 /* Return true if there are no predicates with no_default_print in
675 predicate list PRED, false if there are any.
676 Returns true if default print should be performed */
678 boolean
679 default_prints (struct predicate *pred)
681 while (pred != NULL)
683 if (pred->no_default_print)
684 return (false);
685 pred = pred->pred_next;
687 return (true);
690 boolean
691 looks_like_expression(const char *arg, boolean leading)
693 switch (arg[0])
695 case '-':
696 if (arg[1]) /* "-foo" is an expression. */
697 return true;
698 else
699 return false; /* Just "-" is a filename. */
700 break;
702 case ')':
703 case ',':
704 if (arg[1])
705 return false; /* )x and ,z are not expressions */
706 else
707 return !leading; /* A leading ) or , is not either */
709 /* ( and ! are part of an expression, but (2 and !foo are
710 * filenames.
712 case '!':
713 case '(':
714 if (arg[1])
715 return false;
716 else
717 return true;
719 default:
720 return false;
724 static void
725 process_debug_options(char *arg)
727 const char *p;
728 char *token_context = NULL;
729 const char delimiters[] = ",";
730 boolean empty = true;
731 size_t i;
733 p = strtok_r(arg, delimiters, &token_context);
734 while (p)
736 empty = false;
738 for (i=0; i<N_DEBUGASSOC; ++i)
740 if (0 == strcmp(debugassoc[i].name, p))
742 options.debug_options |= debugassoc[i].val;
743 break;
746 if (i >= N_DEBUGASSOC)
748 error(0, 0, _("Ignoring unrecognised debug flag %s"),
749 quotearg_n_style(0, options.err_quoting_style, arg));
751 p = strtok_r(NULL, delimiters, &token_context);
753 if (empty)
755 error(1, 0, _("Empty argument to the -D option."));
757 else if (options.debug_options & DebugHelp)
759 show_valid_debug_options(stdout, 1);
760 exit(0);
764 static void
765 process_optimisation_option(const char *arg)
767 if (0 == arg[0])
769 error(1, 0, _("The -O option must be immediately followed by a decimal integer"));
771 else
773 unsigned long opt_level;
774 char *end;
776 if (!isdigit( (unsigned char) arg[0] ))
778 error(1, 0, _("Please specify a decimal number immediately after -O"));
780 else
782 int prev_errno = errno;
783 errno = 0;
785 opt_level = strtoul(arg, &end, 10);
786 if ( (0==opt_level) && (end==arg) )
788 error(1, 0, _("Please specify a decimal number immediately after -O"));
790 else if (*end)
792 /* unwanted trailing characters. */
793 error(1, 0, _("Invalid optimisation level %s"), arg);
795 else if ( (ULONG_MAX==opt_level) && errno)
797 error(1, errno, _("Invalid optimisation level %s"), arg);
799 else if (opt_level > USHRT_MAX)
801 /* tricky to test, as on some platforms USHORT_MAX and ULONG_MAX
802 * can have the same value, though this is unusual.
804 error(1, 0, _("Optimisation level %lu is too high. "
805 "If you want to find files very quickly, "
806 "consider using GNU locate."),
807 opt_level);
809 else
811 options.optimisation_level = opt_level;
812 errno = prev_errno;
819 process_leading_options(int argc, char *argv[])
821 int i, end_of_leading_options;
823 for (i=1; (end_of_leading_options = i) < argc; ++i)
825 if (0 == strcmp("-H", argv[i]))
827 /* Meaning: dereference symbolic links on command line, but nowhere else. */
828 set_follow_state(SYMLINK_DEREF_ARGSONLY);
830 else if (0 == strcmp("-L", argv[i]))
832 /* Meaning: dereference all symbolic links. */
833 set_follow_state(SYMLINK_ALWAYS_DEREF);
835 else if (0 == strcmp("-P", argv[i]))
837 /* Meaning: never dereference symbolic links (default). */
838 set_follow_state(SYMLINK_NEVER_DEREF);
840 else if (0 == strcmp("--", argv[i]))
842 /* -- signifies the end of options. */
843 end_of_leading_options = i+1; /* Next time start with the next option */
844 break;
846 else if (0 == strcmp("-D", argv[i]))
848 process_debug_options(argv[i+1]);
849 ++i; /* skip the argument too. */
851 else if (0 == strncmp("-O", argv[i], 2))
853 process_optimisation_option(argv[i]+2);
855 else
857 /* Hmm, must be one of
858 * (a) A path name
859 * (b) A predicate
861 end_of_leading_options = i; /* Next time start with this option */
862 break;
865 return end_of_leading_options;
868 static struct timespec
869 now(void)
871 struct timespec retval;
872 struct timeval tv;
873 time_t t;
875 if (0 == gettimeofday(&tv, NULL))
877 retval.tv_sec = tv.tv_sec;
878 retval.tv_nsec = tv.tv_usec * 1000; /* convert unit from microseconds to nanoseconds */
879 return retval;
881 t = time(NULL);
882 assert (t != (time_t)-1);
883 retval.tv_sec = t;
884 retval.tv_nsec = 0;
885 return retval;
888 void
889 set_option_defaults(struct options *p)
891 /* We call check_nofollow() before setlocale() because the numbers
892 * for which we check (in the results of uname) definitiely have "."
893 * as the decimal point indicator even under locales for which that
894 * is not normally true. Hence atof() would do the wrong thing
895 * if we call it after setlocale().
897 #ifdef O_NOFOLLOW
898 p->open_nofollow_available = check_nofollow();
899 #else
900 p->open_nofollow_available = false;
901 #endif
903 p->regex_options = RE_SYNTAX_EMACS;
905 if (isatty(0))
907 p->warnings = true;
908 p->literal_control_chars = false;
910 else
912 p->warnings = false;
913 p->literal_control_chars = false; /* may change */
917 p->do_dir_first = true;
918 p->maxdepth = p->mindepth = -1;
919 p->start_time = now();
920 p->cur_day_start = p->start_time.tv_sec - DAYSECS;
921 p->full_days = false;
922 p->stay_on_filesystem = false;
923 p->ignore_readdir_race = false;
925 if (getenv("POSIXLY_CORRECT"))
926 p->output_block_size = 512;
927 else
928 p->output_block_size = 1024;
930 p->debug_options = 0uL;
931 p->optimisation_level = 0;
933 if (getenv("FIND_BLOCK_SIZE"))
935 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"));
938 #if LEAF_OPTIMISATION
939 /* The leaf optimisation is enabled. */
940 p->no_leaf_check = false;
941 #else
942 /* The leaf optimisation is disabled. */
943 p->no_leaf_check = true;
944 #endif
946 set_follow_state(SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */
948 p->err_quoting_style = locale_quoting_style;
952 /* get_start_dirfd
954 * Returns the fd for the directory we started in.
956 int get_start_dirfd(void)
958 return starting_desc;
961 /* apply_predicate
964 boolean
965 apply_predicate(const char *pathname, struct stat *stat_buf, struct predicate *p)
967 ++p->perf.visits;
969 if (p->need_stat || p->need_type)
971 /* We may need a stat here. */
972 if (get_info(pathname, stat_buf, p) != 0)
973 return false;
975 if ((p->pred_func)(pathname, stat_buf, p))
977 ++(p->perf.successes);
978 return true;
980 else
982 return false;
987 /* safely_quote_err_filename
990 const char *
991 safely_quote_err_filename (int n, char const *arg)
993 return quotearg_n_style (n, options.err_quoting_style, arg);
996 /* report_file_err
998 static void
999 report_file_err(int exitval, int errno_value, const char *name)
1001 /* It is important that the errno value is passed in as a function
1002 * argument before we call safely_quote_err_filename(), because otherwise
1003 * we might find that safely_quote_err_filename() changes errno.
1005 if (state.exit_status < 1)
1006 state.exit_status = 1;
1008 error (exitval, errno_value, "%s", safely_quote_err_filename(0, name));
1011 /* fatal_file_error
1014 void
1015 fatal_file_error(const char *name)
1017 report_file_err(1, errno, name);
1018 /*NOTREACHED*/
1019 abort();
1022 void
1023 nonfatal_file_error(const char *name)
1025 report_file_err(0, errno, name);