Fixed Savannah bug #19970, taking into account the limitations of the gnulib _Bool...
[findutils.git] / find / util.c
blob2e81de3549217da73db2124db9c9edc0893c22d8
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 "defs.h"
22 #ifdef HAVE_FCNTL_H
23 #include <fcntl.h>
24 #else
25 #include <sys/file.h>
26 #endif
27 #ifdef HAVE_SYS_UTSNAME_H
28 #include <sys/utsname.h>
29 #endif
30 #include <sys/time.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <errno.h>
35 #include <assert.h>
37 #include "xalloc.h"
38 #include "quotearg.h"
39 #include "timespec.h"
40 #include "error.h"
41 #include "verify.h"
42 #include "openat.h"
44 #if ENABLE_NLS
45 # include <libintl.h>
46 # define _(Text) gettext (Text)
47 #else
48 # define _(Text) Text
49 #endif
50 #ifdef gettext_noop
51 # define N_(String) gettext_noop (String)
52 #else
53 /* See locate.c for explanation as to why not use (String) */
54 # define N_(String) String
55 #endif
58 struct debug_option_assoc
60 char *name;
61 int val;
62 char *docstring;
64 static struct debug_option_assoc debugassoc[] =
66 { "help", DebugHelp, "Explain the various -D options" },
67 { "tree", DebugExpressionTree, "Display the expression tree" },
68 { "search",DebugSearch, "Navigate the directory tree verbosely" },
69 { "stat", DebugStat, "Trace calls to stat(2) and lstat(2)" },
70 { "rates", DebugSuccessRates, "Indicate how often each predicate succeeded" },
71 { "opt", DebugExpressionTree|DebugTreeOpt, "Show diagnostic information relating to optimisation" },
72 { "exec", DebugExec, "Show diagnostic information relating to -exec, -execdir, -ok and -okdir" }
74 #define N_DEBUGASSOC (sizeof(debugassoc)/sizeof(debugassoc[0]))
79 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
81 Return a pointer to the predicate node just inserted.
83 Fills in the following cells of the new predicate node:
85 pred_func PRED_FUNC
86 args(.str) NULL
87 p_type PRIMARY_TYPE
88 p_prec NO_PREC
90 Other cells that need to be filled in are defaulted by
91 get_new_pred_chk_op, which is used to insure that the prior node is
92 either not there at all (we are the very first node) or is an
93 operator. */
95 struct predicate *
96 insert_primary_withpred (const struct parser_table *entry, PRED_FUNC pred_func)
98 struct predicate *new_pred;
100 new_pred = get_new_pred_chk_op (entry);
101 new_pred->pred_func = pred_func;
102 new_pred->p_name = entry->parser_name;
103 new_pred->args.str = NULL;
104 new_pred->p_type = PRIMARY_TYPE;
105 new_pred->p_prec = NO_PREC;
106 return new_pred;
109 /* Add a primary described by ENTRY to the predicate input list.
111 Return a pointer to the predicate node just inserted.
113 Fills in the following cells of the new predicate node:
115 pred_func PRED_FUNC
116 args(.str) NULL
117 p_type PRIMARY_TYPE
118 p_prec NO_PREC
120 Other cells that need to be filled in are defaulted by
121 get_new_pred_chk_op, which is used to insure that the prior node is
122 either not there at all (we are the very first node) or is an
123 operator. */
124 struct predicate *
125 insert_primary (const struct parser_table *entry)
127 assert(entry->pred_func != NULL);
128 return insert_primary_withpred(entry, entry->pred_func);
133 static void
134 show_valid_debug_options(FILE *fp, int full)
136 int i;
137 if (full)
139 fprintf(fp, "Valid arguments for -D:\n");
140 for (i=0; i<N_DEBUGASSOC; ++i)
142 fprintf(fp, "%-10s %s\n",
143 debugassoc[i].name,
144 debugassoc[i].docstring);
147 else
149 for (i=0; i<N_DEBUGASSOC; ++i)
151 fprintf(fp, "%s%s", (i>0 ? "|" : ""), debugassoc[i].name);
156 void
157 usage (FILE *fp, int status, char *msg)
159 if (msg)
160 fprintf (fp, "%s: %s\n", program_name, msg);
162 fprintf (fp, _("Usage: %s [-H] [-L] [-P] [-Olevel] [-D "), program_name);
163 show_valid_debug_options(fp, 0);
164 fprintf (fp, _("] [path...] [expression]\n"));
165 if (0 != status)
166 exit (status);
169 void
170 set_stat_placeholders(struct stat *p)
172 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
173 p->st_birthtime = 0;
174 #endif
175 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
176 p->st_birthtimensec = 0;
177 #endif
178 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
179 p->st_birthtimespec.tv_nsec = -1;
180 #endif
181 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_SEC
182 p->st_birthtimespec.tv_sec = 0;
183 #endif
187 /* Get the stat information for a file, if it is
188 * not already known.
191 get_statinfo (const char *pathname, const char *name, struct stat *p)
193 /* Set markers in fields so we have a good idea if the implementation
194 * didn't bother to set them (e.g., NetBSD st_birthtimespec for MS-DOS
195 * files)
197 if (!state.have_stat)
199 set_stat_placeholders(p);
200 if (0 == (*options.xstat) (name, p))
202 if (00000 == p->st_mode)
204 /* Savannah bug #16378. */
205 error(0, 0, _("Warning: file %s appears to have mode 0000"),
206 quotearg_n_style(0, options.err_quoting_style, name));
209 else
211 if (!options.ignore_readdir_race || (errno != ENOENT) )
213 error (0, errno, "%s",
214 safely_quote_err_filename(0, pathname));
215 state.exit_status = 1;
217 return -1;
220 state.have_stat = true;
221 state.have_type = true;
222 state.type = p->st_mode;
224 return 0;
228 /* Get the stat/type information for a file, if it is
229 * not already known.
232 get_info (const char *pathname,
233 struct stat *p,
234 struct predicate *pred_ptr)
236 boolean todo = false;
238 /* If we need the full stat info, or we need the type info but don't
239 * already have it, stat the file now.
241 if (pred_ptr->need_stat)
242 todo = true;
243 else if ((pred_ptr->need_type && (0 == state.have_type)))
244 todo = true;
246 if (todo)
247 return get_statinfo(pathname, state.rel_pathname, p);
248 else
249 return 0;
252 /* Determine if we can use O_NOFOLLOW.
254 #if defined(O_NOFOLLOW)
255 boolean
256 check_nofollow(void)
258 struct utsname uts;
259 float release;
261 if (0 == O_NOFOLLOW)
263 return false;
266 if (0 == uname(&uts))
268 /* POSIX requires that atof() ignore "unrecognised suffixes". */
269 release = atof(uts.release);
271 if (0 == strcmp("Linux", uts.sysname))
273 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
274 return release >= 2.2; /* close enough */
276 else if (0 == strcmp("FreeBSD", uts.sysname))
278 /* FreeBSD 3.0-CURRENT and later support it */
279 return release >= 3.1;
283 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
284 return true;
286 #endif
290 /* Examine the predicate list for instances of -execdir or -okdir
291 * which have been terminated with '+' (build argument list) rather
292 * than ';' (singles only). If there are any, run them (this will
293 * have no effect if there are no arguments waiting).
295 static void
296 do_complete_pending_execdirs(struct predicate *p, int dirfd)
298 if (NULL == p)
299 return;
301 assert(state.execdirs_outstanding);
303 do_complete_pending_execdirs(p->pred_left, dirfd);
305 if (pred_is(p, pred_execdir) || pred_is(p, pred_okdir))
307 /* It's an exec-family predicate. p->args.exec_val is valid. */
308 if (p->args.exec_vec.multiple)
310 struct exec_val *execp = &p->args.exec_vec;
312 /* This one was terminated by '+' and so might have some
313 * left... Run it if necessary.
315 if (execp->state.todo)
317 /* There are not-yet-executed arguments. */
318 launch (&execp->ctl, &execp->state);
323 do_complete_pending_execdirs(p->pred_right, dirfd);
326 void
327 complete_pending_execdirs(int dirfd)
329 if (state.execdirs_outstanding)
331 do_complete_pending_execdirs(get_eval_tree(), dirfd);
332 state.execdirs_outstanding = false;
338 /* Examine the predicate list for instances of -exec which have been
339 * terminated with '+' (build argument list) rather than ';' (singles
340 * only). If there are any, run them (this will have no effect if
341 * there are no arguments waiting).
343 void
344 complete_pending_execs(struct predicate *p)
346 if (NULL == p)
347 return;
349 complete_pending_execs(p->pred_left);
351 /* It's an exec-family predicate then p->args.exec_val is valid
352 * and we can check it.
354 /* XXX: what about pred_ok() ? */
355 if (pred_is(p, pred_exec) && p->args.exec_vec.multiple)
357 struct exec_val *execp = &p->args.exec_vec;
359 /* This one was terminated by '+' and so might have some
360 * left... Run it if necessary. Set state.exit_status if
361 * there are any problems.
363 if (execp->state.todo)
365 /* There are not-yet-executed arguments. */
366 launch (&execp->ctl, &execp->state);
370 complete_pending_execs(p->pred_right);
373 static void
374 traverse_tree(struct predicate *tree,
375 void (*callback)(struct predicate*))
377 if (tree->pred_left)
378 traverse_tree(tree->pred_left, callback);
380 callback(tree);
382 if (tree->pred_right)
383 traverse_tree(tree->pred_right, callback);
386 static void
387 flush_and_close_output_files(struct predicate *p)
389 if (pred_is(p, pred_fprint)
390 || pred_is(p, pred_fprintf)
391 || pred_is(p, pred_fls)
392 || pred_is(p, pred_fprint0))
394 FILE *f = p->args.printf_vec.stream;
395 bool failed;
397 if (f == stdout || f == stderr)
398 failed = fflush(p->args.printf_vec.stream) == EOF;
399 else
400 failed = fclose(p->args.printf_vec.stream) == EOF;
402 if (failed)
403 nonfatal_file_error(p->args.printf_vec.filename);
405 else if (pred_is(p, pred_print))
407 if (fflush(p->args.printf_vec.stream) == EOF)
409 nonfatal_file_error(p->args.printf_vec.filename);
412 else if (pred_is(p, pred_ls) || pred_is(p, pred_print0))
414 if (fflush(stdout) == EOF)
416 /* XXX: migrate to printf_vec. */
417 nonfatal_file_error("standard output");
422 /* Complete any outstanding commands.
424 void
425 cleanup(void)
427 struct predicate *eval_tree = get_eval_tree();
428 if (eval_tree)
430 traverse_tree(eval_tree, complete_pending_execs);
431 complete_pending_execdirs(get_current_dirfd());
432 traverse_tree(eval_tree, flush_and_close_output_files);
436 /* Savannah bug #16378 manifests as an assertion failure in pred_type()
437 * when an NFS server returns st_mode with value 0 (of course the stat(2)
438 * system call is itself returning 0 in this case).
440 #undef DEBUG_SV_BUG_16378
441 #if defined(DEBUG_SV_BUG_16378)
442 static int hook_fstatat(int fd, const char *name, struct stat *p, int flags)
444 static int warned = 0;
446 if (!warned)
448 /* No use of _() here; no point asking translators to translate a debug msg */
449 error(0, 0,
450 "Warning: some debug code is enabled for Savannah bug #16378; "
451 "this should not occur in released versions of findutils!");
452 warned = 1;
455 if (0 == strcmp(name, "./mode0file")
456 || 0 == strcmp(name, "mode0file"))
458 time_t now = time(NULL);
459 long day = 86400;
461 p->st_rdev = 0;
462 p->st_dev = 0x300;
463 p->st_ino = 0;
464 p->st_mode = 0; /* SV bug #16378 */
465 p->st_nlink = 1;
466 p->st_uid = geteuid();
467 p->st_gid = 0;
468 p->st_size = 42;
469 p->st_blksize = 32768;
470 p->st_atime = now-1*day;
471 p->st_mtime = now-2*day;
472 p->st_ctime = now-3*day;
474 return 0;
476 return fstatat(fd, name, p, flags);
479 # undef fstatat
480 # define fstatat(fd,name,p,flags) hook_fstatat((fd),(name),(p),(flags))
481 #endif
484 static int
485 fallback_stat(const char *name, struct stat *p, int prev_rv)
487 /* Our original stat() call failed. Perhaps we can't follow a
488 * symbolic link. If that might be the problem, lstat() the link.
489 * Otherwise, admit defeat.
491 switch (errno)
493 case ENOENT:
494 case ENOTDIR:
495 if (options.debug_options & DebugStat)
496 fprintf(stderr, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name);
497 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
499 case EACCES:
500 case EIO:
501 case ELOOP:
502 case ENAMETOOLONG:
503 #ifdef EOVERFLOW
504 case EOVERFLOW: /* EOVERFLOW is not #defined on UNICOS. */
505 #endif
506 default:
507 return prev_rv;
512 /* optionh_stat() implements the stat operation when the -H option is
513 * in effect.
515 * If the item to be examined is a command-line argument, we follow
516 * symbolic links. If the stat() call fails on the command-line item,
517 * we fall back on the properties of the symbolic link.
519 * If the item to be examined is not a command-line argument, we
520 * examine the link itself.
522 int
523 optionh_stat(const char *name, struct stat *p)
525 if (AT_FDCWD != state.cwd_dir_fd)
526 assert(state.cwd_dir_fd >= 0);
527 set_stat_placeholders(p);
528 if (0 == state.curdepth)
530 /* This file is from the command line; deference the link (if it
531 * is a link).
533 int rv;
534 rv = fstatat(state.cwd_dir_fd, name, p, 0);
535 if (0 == rv)
536 return 0; /* success */
537 else
538 return fallback_stat(name, p, rv);
540 else
542 /* Not a file on the command line; do not dereference the link.
544 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
548 /* optionl_stat() implements the stat operation when the -L option is
549 * in effect. That option makes us examine the thing the symbolic
550 * link points to, not the symbolic link itself.
552 int
553 optionl_stat(const char *name, struct stat *p)
555 int rv;
556 if (AT_FDCWD != state.cwd_dir_fd)
557 assert(state.cwd_dir_fd >= 0);
559 set_stat_placeholders(p);
560 rv = fstatat(state.cwd_dir_fd, name, p, 0);
561 if (0 == rv)
562 return 0; /* normal case. */
563 else
564 return fallback_stat(name, p, rv);
567 /* optionp_stat() implements the stat operation when the -P option is
568 * in effect (this is also the default). That option makes us examine
569 * the symbolic link itself, not the thing it points to.
571 int
572 optionp_stat(const char *name, struct stat *p)
574 assert((state.cwd_dir_fd >= 0) || (state.cwd_dir_fd==AT_FDCWD));
575 set_stat_placeholders(p);
576 return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
580 static uintmax_t stat_count = 0u;
583 debug_stat (const char *file, struct stat *bufp)
585 ++stat_count;
586 fprintf (stderr, "debug_stat (%s)\n", file);
588 switch (options.symlink_handling)
590 case SYMLINK_ALWAYS_DEREF:
591 return optionl_stat(file, bufp);
592 case SYMLINK_DEREF_ARGSONLY:
593 return optionh_stat(file, bufp);
594 case SYMLINK_NEVER_DEREF:
595 return optionp_stat(file, bufp);
597 /*NOTREACHED*/
598 assert(false);
599 return -1;
604 following_links(void)
606 switch (options.symlink_handling)
608 case SYMLINK_ALWAYS_DEREF:
609 return 1;
610 case SYMLINK_DEREF_ARGSONLY:
611 return (state.curdepth == 0);
612 case SYMLINK_NEVER_DEREF:
613 default:
614 return 0;
619 /* Take a "mode" indicator and fill in the files of 'state'.
622 digest_mode(mode_t mode,
623 const char *pathname,
624 const char *name,
625 struct stat *pstat,
626 boolean leaf)
628 /* If we know the type of the directory entry, and it is not a
629 * symbolic link, we may be able to avoid a stat() or lstat() call.
631 if (mode)
633 if (S_ISLNK(mode) && following_links())
635 /* mode is wrong because we should have followed the symlink. */
636 if (get_statinfo(pathname, name, pstat) != 0)
637 return 0;
638 mode = state.type = pstat->st_mode;
639 state.have_type = true;
641 else
643 state.have_type = true;
644 pstat->st_mode = state.type = mode;
647 else
649 /* Mode is not yet known; may have to stat the file unless we
650 * can deduce that it is not a directory (which is all we need to
651 * know at this stage)
653 if (leaf)
655 state.have_stat = false;
656 state.have_type = false;;
657 state.type = 0;
659 else
661 if (get_statinfo(pathname, name, pstat) != 0)
662 return 0;
664 /* If -L is in effect and we are dealing with a symlink,
665 * st_mode is the mode of the pointed-to file, while mode is
666 * the mode of the directory entry (S_IFLNK). Hence now
667 * that we have the stat information, override "mode".
669 state.type = pstat->st_mode;
670 state.have_type = true;
674 /* success. */
675 return 1;
679 /* Return true if there are no predicates with no_default_print in
680 predicate list PRED, false if there are any.
681 Returns true if default print should be performed */
683 boolean
684 default_prints (struct predicate *pred)
686 while (pred != NULL)
688 if (pred->no_default_print)
689 return (false);
690 pred = pred->pred_next;
692 return (true);
695 boolean
696 looks_like_expression(const char *arg, boolean leading)
698 switch (arg[0])
700 case '-':
701 if (arg[1]) /* "-foo" is an expression. */
702 return true;
703 else
704 return false; /* Just "-" is a filename. */
705 break;
707 case ')':
708 case ',':
709 if (arg[1])
710 return false; /* )x and ,z are not expressions */
711 else
712 return !leading; /* A leading ) or , is not either */
714 /* ( and ! are part of an expression, but (2 and !foo are
715 * filenames.
717 case '!':
718 case '(':
719 if (arg[1])
720 return false;
721 else
722 return true;
724 default:
725 return false;
729 static void
730 process_debug_options(char *arg)
732 const char *p;
733 char *token_context = NULL;
734 const char delimiters[] = ",";
735 boolean empty = true;
736 size_t i;
738 p = strtok_r(arg, delimiters, &token_context);
739 while (p)
741 empty = false;
743 for (i=0; i<N_DEBUGASSOC; ++i)
745 if (0 == strcmp(debugassoc[i].name, p))
747 options.debug_options |= debugassoc[i].val;
748 break;
751 if (i >= N_DEBUGASSOC)
753 error(0, 0, _("Ignoring unrecognised debug flag %s"),
754 quotearg_n_style(0, options.err_quoting_style, arg));
756 p = strtok_r(NULL, delimiters, &token_context);
758 if (empty)
760 error(1, 0, _("Empty argument to the -D option."));
762 else if (options.debug_options & DebugHelp)
764 show_valid_debug_options(stdout, 1);
765 exit(0);
769 static void
770 process_optimisation_option(const char *arg)
772 if (0 == arg[0])
774 error(1, 0, _("The -O option must be immediately followed by a decimal integer"));
776 else
778 unsigned long opt_level;
779 char *end;
781 if (!isdigit( (unsigned char) arg[0] ))
783 error(1, 0, _("Please specify a decimal number immediately after -O"));
785 else
787 int prev_errno = errno;
788 errno = 0;
790 opt_level = strtoul(arg, &end, 10);
791 if ( (0==opt_level) && (end==arg) )
793 error(1, 0, _("Please specify a decimal number immediately after -O"));
795 else if (*end)
797 /* unwanted trailing characters. */
798 error(1, 0, _("Invalid optimisation level %s"), arg);
800 else if ( (ULONG_MAX==opt_level) && errno)
802 error(1, errno, _("Invalid optimisation level %s"), arg);
804 else if (opt_level > USHRT_MAX)
806 /* tricky to test, as on some platforms USHORT_MAX and ULONG_MAX
807 * can have the same value, though this is unusual.
809 error(1, 0, _("Optimisation level %lu is too high. "
810 "If you want to find files very quickly, "
811 "consider using GNU locate."),
812 opt_level);
814 else
816 options.optimisation_level = opt_level;
817 errno = prev_errno;
824 process_leading_options(int argc, char *argv[])
826 int i, end_of_leading_options;
828 for (i=1; (end_of_leading_options = i) < argc; ++i)
830 if (0 == strcmp("-H", argv[i]))
832 /* Meaning: dereference symbolic links on command line, but nowhere else. */
833 set_follow_state(SYMLINK_DEREF_ARGSONLY);
835 else if (0 == strcmp("-L", argv[i]))
837 /* Meaning: dereference all symbolic links. */
838 set_follow_state(SYMLINK_ALWAYS_DEREF);
840 else if (0 == strcmp("-P", argv[i]))
842 /* Meaning: never dereference symbolic links (default). */
843 set_follow_state(SYMLINK_NEVER_DEREF);
845 else if (0 == strcmp("--", argv[i]))
847 /* -- signifies the end of options. */
848 end_of_leading_options = i+1; /* Next time start with the next option */
849 break;
851 else if (0 == strcmp("-D", argv[i]))
853 process_debug_options(argv[i+1]);
854 ++i; /* skip the argument too. */
856 else if (0 == strncmp("-O", argv[i], 2))
858 process_optimisation_option(argv[i]+2);
860 else
862 /* Hmm, must be one of
863 * (a) A path name
864 * (b) A predicate
866 end_of_leading_options = i; /* Next time start with this option */
867 break;
870 return end_of_leading_options;
873 static struct timespec
874 now(void)
876 struct timespec retval;
877 struct timeval tv;
878 time_t t;
880 if (0 == gettimeofday(&tv, NULL))
882 retval.tv_sec = tv.tv_sec;
883 retval.tv_nsec = tv.tv_usec * 1000; /* convert unit from microseconds to nanoseconds */
884 return retval;
886 t = time(NULL);
887 assert(t != (time_t)-1);
888 retval.tv_sec = t;
889 retval.tv_nsec = 0;
890 return retval;
893 void
894 set_option_defaults(struct options *p)
896 /* We call check_nofollow() before setlocale() because the numbers
897 * for which we check (in the results of uname) definitiely have "."
898 * as the decimal point indicator even under locales for which that
899 * is not normally true. Hence atof() would do the wrong thing
900 * if we call it after setlocale().
902 #ifdef O_NOFOLLOW
903 p->open_nofollow_available = check_nofollow();
904 #else
905 p->open_nofollow_available = false;
906 #endif
908 p->regex_options = RE_SYNTAX_EMACS;
910 if (isatty(0))
912 p->warnings = true;
913 p->literal_control_chars = false;
915 else
917 p->warnings = false;
918 p->literal_control_chars = false; /* may change */
922 p->do_dir_first = true;
923 p->maxdepth = p->mindepth = -1;
924 p->start_time = now();
925 p->cur_day_start = p->start_time.tv_sec - DAYSECS;
926 p->full_days = false;
927 p->stay_on_filesystem = false;
928 p->ignore_readdir_race = false;
930 if (getenv("POSIXLY_CORRECT"))
931 p->output_block_size = 512;
932 else
933 p->output_block_size = 1024;
935 p->debug_options = 0uL;
936 p->optimisation_level = 0;
938 if (getenv("FIND_BLOCK_SIZE"))
940 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"));
943 #if LEAF_OPTIMISATION
944 /* The leaf optimisation is enabled. */
945 p->no_leaf_check = false;
946 #else
947 /* The leaf optimisation is disabled. */
948 p->no_leaf_check = true;
949 #endif
951 set_follow_state(SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */
953 p->err_quoting_style = locale_quoting_style;
957 /* get_start_dirfd
959 * Returns the fd for the directory we started in.
961 int get_start_dirfd(void)
963 return starting_desc;
966 /* apply_predicate
969 boolean
970 apply_predicate(const char *pathname, struct stat *stat_buf, struct predicate *p)
972 ++p->perf.visits;
974 if (p->need_stat || p->need_type)
976 /* We may need a stat here. */
977 if (get_info(pathname, stat_buf, p) != 0)
978 return false;
980 if ((p->pred_func)(pathname, stat_buf, p))
982 ++(p->perf.successes);
983 return true;
985 else
987 return false;
992 /* safely_quote_err_filename
995 const char *
996 safely_quote_err_filename (int n, char const *arg)
998 return quotearg_n_style (n, options.err_quoting_style, arg);
1001 /* report_file_err
1003 static void
1004 report_file_err(int exitval, int errno_value, const char *name)
1006 /* It is important that the errno value is passed in as a function
1007 * argument before we call safely_quote_err_filename(), because otherwise
1008 * we might find that safely_quote_err_filename() changes errno.
1010 if (state.exit_status < 1)
1011 state.exit_status = 1;
1013 error (exitval, errno_value, "%s", safely_quote_err_filename(0, name));
1016 /* fatal_file_error
1019 void
1020 fatal_file_error(const char *name)
1022 report_file_err(1, errno, name);
1023 /*NOTREACHED*/
1024 abort();
1027 void
1028 nonfatal_file_error(const char *name)
1030 report_file_err(0, errno, name);