1 /* util.c -- functions for initializing new tree elements, and other things.
2    Copyright (C) 1990-2021 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 /* config.h must always come first. */
19 #include <config.h>
20 
21 /* system headers. */
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <string.h>
28 #include <sys/stat.h> /* for fstatat() */
29 #include <sys/time.h>
30 #include <sys/utsname.h>
31 
32 /* gnulib headers. */
33 #include "error.h"
34 #include "fdleak.h"
35 #include "progname.h"
36 #include "quotearg.h"
37 #include "save-cwd.h"
38 #include "timespec.h"
39 #include "xalloc.h"
40 
41 /* find headers. */
42 #include "defs.h"
43 #include "die.h"
44 #include "dircallback.h"
45 #include "bugreports.h"
46 #include "system.h"
47 
48 
49 struct debug_option_assoc
50 {
51   const char *name;
52   int    val;
53   const char *docstring;
54 };
55 static struct debug_option_assoc debugassoc[] =
56   {
57     { "exec", DebugExec, "Show diagnostic information relating to -exec, -execdir, -ok and -okdir" },
58     { "opt",  DebugExpressionTree|DebugTreeOpt, "Show diagnostic information relating to optimisation" },
59     { "rates", DebugSuccessRates, "Indicate how often each predicate succeeded" },
60     { "search",DebugSearch, "Navigate the directory tree verbosely" },
61     { "stat", DebugStat, "Trace calls to stat(2) and lstat(2)" },
62     { "time", DebugTime, "Show diagnostic information relating to time-of-day and timestamp comparisons" },
63     { "tree", DebugExpressionTree, "Display the expression tree" },
64 
65     { "all", DebugAll, "Set all of the debug flags (but help)" },
66     { "help", DebugHelp, "Explain the various -D options" },
67   };
68 #define N_DEBUGASSOC (sizeof(debugassoc)/sizeof(debugassoc[0]))
69 
70 
71 
72 
73 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
74 
75    Return a pointer to the predicate node just inserted.
76 
77    Fills in the following cells of the new predicate node:
78 
79    pred_func	    PRED_FUNC
80    args(.str)	    NULL
81    p_type	    PRIMARY_TYPE
82    p_prec	    NO_PREC
83 
84    Other cells that need to be filled in are defaulted by
85    get_new_pred_chk_op, which is used to ensure that the prior node is
86    either not there at all (we are the very first node) or is an
87    operator. */
88 
89 struct predicate *
insert_primary_withpred(const struct parser_table * entry,PRED_FUNC pred_func,const char * arg)90 insert_primary_withpred (const struct parser_table *entry,
91 			 PRED_FUNC pred_func,
92 			 const char *arg)
93 {
94   struct predicate *new_pred;
95 
96   new_pred = get_new_pred_chk_op (entry, arg);
97   new_pred->pred_func = pred_func;
98   new_pred->p_name = entry->parser_name;
99   new_pred->args.str = NULL;
100   new_pred->p_type = PRIMARY_TYPE;
101   new_pred->p_prec = NO_PREC;
102   return new_pred;
103 }
104 
105 /* Add a primary described by ENTRY to the predicate input list.
106 
107    Return a pointer to the predicate node just inserted.
108 
109    Fills in the following cells of the new predicate node:
110 
111    pred_func	    PRED_FUNC
112    args(.str)	    NULL
113    p_type	    PRIMARY_TYPE
114    p_prec	    NO_PREC
115 
116    Other cells that need to be filled in are defaulted by
117    get_new_pred_chk_op, which is used to insure that the prior node is
118    either not there at all (we are the very first node) or is an
119    operator. */
120 struct predicate *
insert_primary(const struct parser_table * entry,const char * arg)121 insert_primary (const struct parser_table *entry, const char *arg)
122 {
123   assert (entry->pred_func != NULL);
124   return insert_primary_withpred (entry, entry->pred_func, arg);
125 }
126 
127 struct predicate *
insert_primary_noarg(const struct parser_table * entry)128 insert_primary_noarg (const struct parser_table *entry)
129 {
130   return insert_primary (entry, NULL);
131 }
132 
133 
134 
135 static void
show_valid_debug_options(int full)136 show_valid_debug_options (int full)
137 {
138   size_t i;
139   fputs (_("Valid arguments for -D:\n"), stdout);
140   if (full)
141     {
142       for (i=0; i<N_DEBUGASSOC; ++i)
143 	{
144 	  fprintf (stdout, "%-10s %s\n",
145 		   debugassoc[i].name,
146 		   debugassoc[i].docstring);
147 	}
148     }
149   else
150     {
151       for (i=0; i<N_DEBUGASSOC; ++i)
152 	{
153 	  fprintf (stdout, "%s%s", (i>0 ? ", " : ""), debugassoc[i].name);
154 	}
155     }
156 }
157 
158 void
usage(int status)159 usage (int status)
160 {
161   if (status != EXIT_SUCCESS)
162     {
163       fprintf (stderr, _("Try '%s --help' for more information.\n"), program_name);
164       exit (status);
165     }
166 
167 #define HTL(t) fputs (t, stdout);
168 
169   fprintf (stdout, _("\
170 Usage: %s [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]\n"),
171            program_name);
172 
173   HTL (_("\n\
174 default path is the current directory; default expression is -print\n\
175 expression may consist of: operators, options, tests, and actions:\n"));
176   HTL (_("\
177 operators (decreasing precedence; -and is implicit where no others are given):\n\
178       ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2\n\
179       EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2\n"));
180   HTL (_("\
181 positional options (always true): -daystart -follow -regextype\n\n\
182 normal options (always true, specified before other expressions):\n\
183       -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf\n\
184       --version -xdev -ignore_readdir_race -noignore_readdir_race\n"));
185   HTL (_("\
186 tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N\n\
187       -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME\n\
188       -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN\n\
189       -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE"));
190   HTL (_("\n\
191       -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN\n\
192       -readable -writable -executable\n\
193       -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N\n\
194       -used N -user NAME -xtype [bcdpfls]"));
195   HTL (_("\
196       -context CONTEXT\n"));
197   HTL (_("\n\
198 actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print \n\
199       -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit\n\
200       -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;\n\
201       -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;\n\
202 \n"));
203 
204   show_valid_debug_options (0);
205   HTL (_("\n\
206 Use '-D help' for a description of the options, or see find(1)\n\
207 \n"));
208 
209   explain_how_to_report_bugs (stdout, program_name);
210   exit (status);
211 }
212 
213 void
set_stat_placeholders(struct stat * p)214 set_stat_placeholders (struct stat *p)
215 {
216   (void) p; /* silence warning for systems lacking these fields. */
217 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
218   p->st_birthtime = 0;
219 #endif
220 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
221   p->st_birthtimensec = 0;
222 #endif
223 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
224   p->st_birthtimespec.tv_nsec = -1;
225 #endif
226 #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_SEC
227   p->st_birthtimespec.tv_sec = 0;
228 #else
229   /* Avoid pointless compiler warning about unused parameters if none of these
230      macros are set to nonzero values. */
231   (void) p;
232 #endif
233 }
234 
235 
236 /* Get the stat information for a file, if it is
237  * not already known.  Returns 0 on success.
238  */
239 int
get_statinfo(const char * pathname,const char * name,struct stat * p)240 get_statinfo (const char *pathname, const char *name, struct stat *p)
241 {
242   /* Set markers in fields so we have a good idea if the implementation
243    * didn't bother to set them (e.g., NetBSD st_birthtimespec for MS-DOS
244    * files)
245    */
246   if (!state.have_stat)
247     {
248       set_stat_placeholders (p);
249       if (0 == (*options.xstat) (name, p))
250 	{
251 	  if (00000 == p->st_mode)
252 	    {
253 	      /* Savannah bug #16378. */
254 	      error (0, 0, _("WARNING: file %s appears to have mode 0000"),
255 		     quotearg_n_style (0, options.err_quoting_style, name));
256 	      error_severity (1);
257 	    }
258 	}
259       else
260 	{
261 	  if (!options.ignore_readdir_race || (errno != ENOENT) )
262 	    {
263 	      nonfatal_target_file_error (errno, pathname);
264 	    }
265 	  return -1;
266 	}
267     }
268   state.have_stat = true;
269   state.have_type = true;
270   state.type = p->st_mode;
271 
272   return 0;
273 }
274 
275 /* Get the stat/type/inode information for a file, if it is not
276  * already known.   Returns 0 on success (or if we did nothing).
277  */
278 int
get_info(const char * pathname,struct stat * p,struct predicate * pred_ptr)279 get_info (const char *pathname,
280 	  struct stat *p,
281 	  struct predicate *pred_ptr)
282 {
283   bool todo = false;
284 
285   /* If we need the full stat info, or we need the type info but don't
286    * already have it, stat the file now.
287    */
288   if (pred_ptr->need_stat)
289     {
290       todo = true;		/* need full stat info */
291     }
292   else if (pred_ptr->need_type && !state.have_type)
293     {
294       todo = true;		/* need to stat to get the type */
295     }
296   else if (pred_ptr->need_inum)
297     {
298       if (!p->st_ino)
299 	{
300 	  todo = true;		/* need to stat to get the inode number */
301 	}
302       else if ((!state.have_type) || S_ISDIR(p->st_mode))
303 	{
304 	  /* For now we decide not to trust struct dirent.d_ino for
305 	   * directory entries that are subdirectories, in case this
306 	   * subdirectory is a mount point.  We also need to call a
307 	   * stat function if we don't have st_ino (i.e. it is zero).
308 	   */
309 	  todo = true;
310 	}
311     }
312   if (todo)
313     {
314       int result = get_statinfo (pathname, state.rel_pathname, p);
315       if (result != 0)
316 	{
317 	  return -1;		/* failure. */
318 	}
319       else
320 	{
321 	  /* Verify some postconditions.  We can't check st_mode for
322 	     non-zero-ness because of Savannah bug #16378 (which is
323 	     that broken NFS servers can return st_mode==0). */
324 	  if (pred_ptr->need_type)
325 	    {
326 	      assert (state.have_type);
327 	    }
328 	  if (pred_ptr->need_inum)
329 	    {
330 	      assert (p->st_ino);
331 	    }
332 	  return 0;		/* success. */
333 	}
334     }
335   else
336     {
337       return 0;			/* success; nothing to do. */
338     }
339 }
340 
341 /* Determine if we can use O_NOFOLLOW.
342  */
343 #if defined O_NOFOLLOW
344 bool
check_nofollow(void)345 check_nofollow (void)
346 {
347   struct utsname uts;
348   float  release;
349 
350   if (0 == O_NOFOLLOW)
351     {
352       return false;
353     }
354 
355   if (0 == uname (&uts))
356     {
357       /* POSIX requires that atof ignores "unrecognised suffixes"; we specifically
358        * want that behaviour. */
359       double (*conversion)(const char*) = atof;  /* avoid sc_prohibit_atoi_atof check. */
360       release = conversion (uts.release);
361 
362       if (0 == strcmp ("Linux", uts.sysname))
363 	{
364 	  /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
365 	  return release >= 2.2f; /* close enough */
366 	}
367       else if (0 == strcmp ("FreeBSD", uts.sysname))
368 	{
369 	  /* FreeBSD 3.0-CURRENT and later support it */
370 	  return release >= 3.1f;
371 	}
372     }
373 
374   /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
375   return true;
376 }
377 #endif
378 
379 
380 static int
exec_cb(void * context)381 exec_cb (void *context)
382 {
383   struct exec_val *execp = context;
384   bc_do_exec (&execp->ctl, &execp->state);
385   return 0;
386 }
387 
388 static void
do_exec(struct exec_val * execp)389 do_exec (struct exec_val *execp)
390 {
391   run_in_dir (execp->wd_for_exec, exec_cb, execp);
392   if (execp->wd_for_exec != initial_wd)
393     {
394       free_cwd (execp->wd_for_exec);
395       free (execp->wd_for_exec);
396       execp->wd_for_exec = NULL;
397     }
398 }
399 
400 
401 /* Examine the predicate list for instances of -execdir or -okdir
402  * which have been terminated with '+' (build argument list) rather
403  * than ';' (singles only).  If there are any, run them (this will
404  * have no effect if there are no arguments waiting).
405  */
406 static void
do_complete_pending_execdirs(struct predicate * p)407 do_complete_pending_execdirs (struct predicate *p)
408 {
409   if (NULL == p)
410     return;
411 
412   assert (state.execdirs_outstanding);
413 
414   do_complete_pending_execdirs (p->pred_left);
415 
416   if (pred_is (p, pred_execdir) || pred_is(p, pred_okdir))
417     {
418       /* It's an exec-family predicate.  p->args.exec_val is valid. */
419       if (p->args.exec_vec.multiple)
420 	{
421 	  struct exec_val *execp = &p->args.exec_vec;
422 
423 	  /* This one was terminated by '+' and so might have some
424 	   * left... Run it if necessary.
425 	   */
426 	  if (execp->state.todo)
427 	    {
428 	      /* There are not-yet-executed arguments. */
429 	      do_exec (execp);
430 	    }
431 	}
432     }
433 
434   do_complete_pending_execdirs (p->pred_right);
435 }
436 
437 void
complete_pending_execdirs(void)438 complete_pending_execdirs (void)
439 {
440   if (state.execdirs_outstanding)
441     {
442       do_complete_pending_execdirs (get_eval_tree());
443       state.execdirs_outstanding = false;
444     }
445 }
446 
447 
448 
449 /* Examine the predicate list for instances of -exec which have been
450  * terminated with '+' (build argument list) rather than ';' (singles
451  * only).  If there are any, run them (this will have no effect if
452  * there are no arguments waiting).
453  */
454 void
complete_pending_execs(struct predicate * p)455 complete_pending_execs (struct predicate *p)
456 {
457   if (NULL == p)
458     return;
459 
460   complete_pending_execs (p->pred_left);
461 
462   /* It's an exec-family predicate then p->args.exec_val is valid
463    * and we can check it.
464    */
465   /* XXX: what about pred_ok() ? */
466   if (pred_is (p, pred_exec) && p->args.exec_vec.multiple)
467     {
468       struct exec_val *execp = &p->args.exec_vec;
469 
470       /* This one was terminated by '+' and so might have some
471        * left... Run it if necessary.  Set state.exit_status if
472        * there are any problems.
473        */
474       if (execp->state.todo)
475 	{
476 	  /* There are not-yet-executed arguments. */
477 	  bc_do_exec (&execp->ctl, &execp->state);
478 	}
479     }
480 
481   complete_pending_execs (p->pred_right);
482 }
483 
484 void
record_initial_cwd(void)485 record_initial_cwd (void)
486 {
487   initial_wd = xmalloc (sizeof (*initial_wd));
488   if (0 != save_cwd (initial_wd))
489     {
490       die (EXIT_FAILURE, errno,
491 	   _("Failed to save initial working directory%s%s"),
492 	   (initial_wd->desc < 0 && initial_wd->name) ? ": " : "",
493 	   (initial_wd->desc < 0 && initial_wd->name) ? initial_wd->name : "");
494     }
495 }
496 
497 static void
cleanup_initial_cwd(void)498 cleanup_initial_cwd (void)
499 {
500   if (0 == restore_cwd (initial_wd))
501     {
502       free_cwd (initial_wd);
503       free (initial_wd);
504       initial_wd = NULL;
505     }
506   else
507     {
508       /* since we may already be in atexit, die with _exit(). */
509       error (0, errno,
510 	     _("Failed to restore initial working directory%s%s"),
511 	     (initial_wd->desc < 0 && initial_wd->name) ? ": " : "",
512 	     (initial_wd->desc < 0 && initial_wd->name) ? initial_wd->name : "");
513       _exit (EXIT_FAILURE);
514     }
515 }
516 
517 
518 static void
traverse_tree(struct predicate * tree,void (* callback)(struct predicate *))519 traverse_tree (struct predicate *tree,
520 			  void (*callback)(struct predicate*))
521 {
522   if (tree->pred_left)
523     traverse_tree (tree->pred_left, callback);
524 
525   callback (tree);
526 
527   if (tree->pred_right)
528     traverse_tree (tree->pred_right, callback);
529 }
530 
531 /* After sharefile_destroy is called, our output file
532  * pointers will be dangling (fclose will already have
533  * been called on them).  NULL these out.
534  */
535 static void
undangle_file_pointers(struct predicate * p)536 undangle_file_pointers (struct predicate *p)
537 {
538   if (pred_is (p, pred_fprint)
539       || pred_is (p, pred_fprintf)
540       || pred_is (p, pred_fls)
541       || pred_is (p, pred_fprint0))
542     {
543       /* The file was already fclose()d by sharefile_destroy. */
544       p->args.printf_vec.stream = NULL;
545     }
546 }
547 
548 /* Complete any outstanding commands.
549  * Flush and close any open files.
550  */
551 void
cleanup(void)552 cleanup (void)
553 {
554   struct predicate *eval_tree = get_eval_tree ();
555   if (eval_tree)
556     {
557       traverse_tree (eval_tree, complete_pending_execs);
558       complete_pending_execdirs ();
559     }
560 
561   /* Close output files and NULL out references to them. */
562   sharefile_destroy (state.shared_files);
563   if (eval_tree)
564     traverse_tree (eval_tree, undangle_file_pointers);
565 
566   cleanup_initial_cwd ();
567 
568   if (fd_leak_check_is_enabled ())
569     {
570       complain_about_leaky_fds ();
571       forget_non_cloexec_fds ();
572     }
573 
574   if (fflush (stdout) == EOF)
575     nonfatal_nontarget_file_error (errno, "standard output");
576 }
577 
578 
579 static int
fallback_stat(const char * name,struct stat * p,int prev_rv)580 fallback_stat (const char *name, struct stat *p, int prev_rv)
581 {
582   /* Our original stat() call failed.  Perhaps we can't follow a
583    * symbolic link.  If that might be the problem, lstat() the link.
584    * Otherwise, admit defeat.
585    */
586   switch (errno)
587     {
588     case ENOENT:
589     case ENOTDIR:
590       if (options.debug_options & DebugStat)
591 	fprintf(stderr, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name);
592       return fstatat(state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
593 
594     case EACCES:
595     case EIO:
596     case ELOOP:
597     case ENAMETOOLONG:
598 #ifdef EOVERFLOW
599     case EOVERFLOW:	    /* EOVERFLOW is not #defined on UNICOS. */
600 #endif
601     default:
602       return prev_rv;
603     }
604 }
605 
606 
607 /* optionh_stat() implements the stat operation when the -H option is
608  * in effect.
609  *
610  * If the item to be examined is a command-line argument, we follow
611  * symbolic links.  If the stat() call fails on the command-line item,
612  * we fall back on the properties of the symbolic link.
613  *
614  * If the item to be examined is not a command-line argument, we
615  * examine the link itself.
616  */
617 int
optionh_stat(const char * name,struct stat * p)618 optionh_stat (const char *name, struct stat *p)
619 {
620   if (AT_FDCWD != state.cwd_dir_fd)
621     assert (state.cwd_dir_fd >= 0);
622   set_stat_placeholders (p);
623   if (0 == state.curdepth)
624     {
625       /* This file is from the command line; deference the link (if it
626        * is a link).
627        */
628       int rv;
629       rv = fstatat (state.cwd_dir_fd, name, p, 0);
630       if (0 == rv)
631 	return 0;		/* success */
632       else
633 	return fallback_stat (name, p, rv);
634     }
635   else
636     {
637       /* Not a file on the command line; do not dereference the link.
638        */
639       return fstatat (state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
640     }
641 }
642 
643 /* optionl_stat() implements the stat operation when the -L option is
644  * in effect.  That option makes us examine the thing the symbolic
645  * link points to, not the symbolic link itself.
646  */
647 int
optionl_stat(const char * name,struct stat * p)648 optionl_stat(const char *name, struct stat *p)
649 {
650   int rv;
651   if (AT_FDCWD != state.cwd_dir_fd)
652     assert (state.cwd_dir_fd >= 0);
653 
654   set_stat_placeholders (p);
655   rv = fstatat (state.cwd_dir_fd, name, p, 0);
656   if (0 == rv)
657     return 0;			/* normal case. */
658   else
659     return fallback_stat (name, p, rv);
660 }
661 
662 /* optionp_stat() implements the stat operation when the -P option is
663  * in effect (this is also the default).  That option makes us examine
664  * the symbolic link itself, not the thing it points to.
665  */
666 int
optionp_stat(const char * name,struct stat * p)667 optionp_stat (const char *name, struct stat *p)
668 {
669   assert ((state.cwd_dir_fd >= 0) || (state.cwd_dir_fd==AT_FDCWD));
670   set_stat_placeholders (p);
671   return fstatat (state.cwd_dir_fd, name, p, AT_SYMLINK_NOFOLLOW);
672 }
673 
674 
675 static uintmax_t stat_count = 0u;
676 
677 int
debug_stat(const char * file,struct stat * bufp)678 debug_stat (const char *file, struct stat *bufp)
679 {
680   ++stat_count;
681   fprintf (stderr, "debug_stat (%s)\n", file);
682 
683   switch (options.symlink_handling)
684     {
685     case SYMLINK_ALWAYS_DEREF:
686       return optionl_stat (file, bufp);
687     case SYMLINK_DEREF_ARGSONLY:
688       return optionh_stat (file, bufp);
689     case SYMLINK_NEVER_DEREF:
690       return optionp_stat (file, bufp);
691     }
692   /*NOTREACHED*/
693   assert (0);
694   return -1;
695 }
696 
697 
698 bool
following_links(void)699 following_links(void)
700 {
701   switch (options.symlink_handling)
702     {
703     case SYMLINK_ALWAYS_DEREF:
704       return true;
705     case SYMLINK_DEREF_ARGSONLY:
706       return (state.curdepth == 0);
707     case SYMLINK_NEVER_DEREF:
708     default:
709       return false;
710     }
711 }
712 
713 
714 /* Take a "mode" indicator and fill in the files of 'state'.
715  */
716 bool
digest_mode(mode_t * mode,const char * pathname,const char * name,struct stat * pstat,bool leaf)717 digest_mode (mode_t *mode,
718 	     const char *pathname,
719 	     const char *name,
720 	     struct stat *pstat,
721 	     bool leaf)
722 {
723   /* If we know the type of the directory entry, and it is not a
724    * symbolic link, we may be able to avoid a stat() or lstat() call.
725    */
726   if (*mode)
727     {
728       if (S_ISLNK(*mode) && following_links())
729 	{
730 	  /* mode is wrong because we should have followed the symlink. */
731 	  if (get_statinfo (pathname, name, pstat) != 0)
732 	    return false;
733 	  *mode = state.type = pstat->st_mode;
734 	  state.have_type = true;
735 	}
736       else
737 	{
738 	  state.have_type = true;
739 	  pstat->st_mode = state.type = *mode;
740 	}
741     }
742   else
743     {
744       /* Mode is not yet known; may have to stat the file unless we
745        * can deduce that it is not a directory (which is all we need to
746        * know at this stage)
747        */
748       if (leaf)
749 	{
750 	  state.have_stat = false;
751 	  state.have_type = false;
752 	  state.type = 0;
753 	}
754       else
755 	{
756 	  if (get_statinfo (pathname, name, pstat) != 0)
757 	    return false;
758 
759 	  /* If -L is in effect and we are dealing with a symlink,
760 	   * st_mode is the mode of the pointed-to file, while mode is
761 	   * the mode of the directory entry (S_IFLNK).  Hence now
762 	   * that we have the stat information, override "mode".
763 	   */
764 	  state.type = *mode = pstat->st_mode;
765 	  state.have_type = true;
766 	}
767     }
768 
769   /* success. */
770   return true;
771 }
772 
773 
774 /* Return true if there are no predicates with no_default_print in
775    predicate list PRED, false if there are any.
776    Returns true if default print should be performed */
777 
778 bool
default_prints(struct predicate * pred)779 default_prints (struct predicate *pred)
780 {
781   while (pred != NULL)
782     {
783       if (pred->no_default_print)
784 	return (false);
785       pred = pred->pred_next;
786     }
787   return (true);
788 }
789 
790 bool
looks_like_expression(const char * arg,bool leading)791 looks_like_expression (const char *arg, bool leading)
792 {
793   switch (arg[0])
794     {
795     case '-':
796       if (arg[1])		/* "-foo" is an expression.  */
797 	return true;
798       else
799 	return false;		/* Just "-" is a filename. */
800       break;
801 
802     case ')':
803     case ',':
804       if (arg[1])
805 	return false;		/* )x and ,z are not expressions */
806       else
807 	return !leading;	/* A leading ) or , is not either */
808 
809       /* ( and ! are part of an expression, but (2 and !foo are
810        * filenames.
811        */
812     case '!':
813     case '(':
814       if (arg[1])
815 	return false;
816       else
817 	return true;
818 
819     default:
820       return false;
821     }
822 }
823 
824 static void
process_debug_options(char * arg)825 process_debug_options (char *arg)
826 {
827   const char *p;
828   char *token_context = NULL;
829   const char delimiters[] = ",";
830   bool empty = true;
831   size_t i;
832 
833   p = strtok_r (arg, delimiters, &token_context);
834   while (p)
835     {
836       empty = false;
837 
838       for (i=0; i<N_DEBUGASSOC; ++i)
839 	{
840 	  if (0 == strcmp (debugassoc[i].name, p))
841 	    {
842 	      options.debug_options |= debugassoc[i].val;
843 	      break;
844 	    }
845 	}
846       if (i >= N_DEBUGASSOC)
847 	{
848 	  error (0, 0, _("Ignoring unrecognised debug flag %s"),
849 		 quotearg_n_style (0, options.err_quoting_style, arg));
850 	}
851       p = strtok_r (NULL, delimiters, &token_context);
852     }
853   if (empty)
854     {
855       error (0, 0, _("Empty argument to the -D option."));
856       usage (EXIT_FAILURE);
857     }
858   else if (options.debug_options & DebugHelp)
859     {
860       show_valid_debug_options (1);
861       exit (EXIT_SUCCESS);
862     }
863 }
864 
865 
866 static void
process_optimisation_option(const char * arg)867 process_optimisation_option (const char *arg)
868 {
869   if (0 == arg[0])
870     {
871       die (EXIT_FAILURE, 0,
872 	   _("The -O option must be immediately followed by a decimal integer"));
873     }
874   else
875     {
876       unsigned long opt_level;
877       char *end;
878 
879       if (!isdigit ( (unsigned char) arg[0] ))
880 	{
881 	  die (EXIT_FAILURE, 0,
882 	       _("Please specify a decimal number immediately after -O"));
883 	}
884       else
885 	{
886 	  int prev_errno = errno;
887 	  errno  = 0;
888 
889 	  opt_level = strtoul (arg, &end, 10);
890 	  if ( (0==opt_level) && (end==arg) )
891 	    {
892 	      die (EXIT_FAILURE, 0,
893 		   _("Please specify a decimal number immediately after -O"));
894 	    }
895 	  else if (*end)
896 	    {
897 	      /* unwanted trailing characters. */
898 	      die (EXIT_FAILURE, 0, _("Invalid optimisation level %s"), arg);
899 	    }
900 	  else if ( (ULONG_MAX==opt_level) && errno)
901 	    {
902 	      die (EXIT_FAILURE, errno,
903 		   _("Invalid optimisation level %s"), arg);
904 	    }
905 	  else if (opt_level > USHRT_MAX)
906 	    {
907 	      /* tricky to test, as on some platforms USHORT_MAX and ULONG_MAX
908 	       * can have the same value, though this is unusual.
909 	       */
910 	      die (EXIT_FAILURE, 0,
911 		   _("Optimisation level %lu is too high.  "
912 		     "If you want to find files very quickly, "
913 		     "consider using GNU locate."),
914 		   opt_level);
915 	    }
916 	  else
917 	    {
918 	      options.optimisation_level = opt_level;
919 	      errno = prev_errno;
920 	    }
921 	}
922     }
923 }
924 
925 int
process_leading_options(int argc,char * argv[])926 process_leading_options (int argc, char *argv[])
927 {
928   int i, end_of_leading_options;
929 
930   for (i=1; (end_of_leading_options = i) < argc; ++i)
931     {
932       if (0 == strcmp ("-H", argv[i]))
933 	{
934 	  /* Meaning: dereference symbolic links on command line, but nowhere else. */
935 	  set_follow_state (SYMLINK_DEREF_ARGSONLY);
936 	}
937       else if (0 == strcmp ("-L", argv[i]))
938 	{
939 	  /* Meaning: dereference all symbolic links. */
940 	  set_follow_state (SYMLINK_ALWAYS_DEREF);
941 	}
942       else if (0 == strcmp ("-P", argv[i]))
943 	{
944 	  /* Meaning: never dereference symbolic links (default). */
945 	  set_follow_state (SYMLINK_NEVER_DEREF);
946 	}
947       else if (0 == strcmp ("--", argv[i]))
948 	{
949 	  /* -- signifies the end of options. */
950 	  end_of_leading_options = i+1;	/* Next time start with the next option */
951 	  break;
952 	}
953       else if (0 == strcmp ("-D", argv[i]))
954 	{
955 	  if (argc <= i+1)
956 	    {
957 	      error (0, 0, _("Missing argument after the -D option."));
958 	      usage (EXIT_FAILURE);
959 	    }
960 	  process_debug_options (argv[i+1]);
961 	  ++i;			/* skip the argument too. */
962 	}
963       else if (0 == strncmp ("-O", argv[i], 2))
964 	{
965 	  process_optimisation_option (argv[i]+2);
966 	}
967       else
968 	{
969 	  /* Hmm, must be one of
970 	   * (a) A path name
971 	   * (b) A predicate
972 	   */
973 	  end_of_leading_options = i; /* Next time start with this option */
974 	  break;
975 	}
976     }
977   return end_of_leading_options;
978 }
979 
980 static struct timespec
now(void)981 now(void)
982 {
983   struct timespec retval;
984   struct timeval tv;
985   time_t t;
986 
987   if (0 == gettimeofday (&tv, NULL))
988     {
989       retval.tv_sec  = tv.tv_sec;
990       retval.tv_nsec = tv.tv_usec * 1000; /* convert unit from microseconds to nanoseconds */
991       return retval;
992     }
993   t = time (NULL);
994   assert (t != (time_t)-1);
995   retval.tv_sec = t;
996   retval.tv_nsec = 0;
997   return retval;
998 }
999 
1000 void
set_option_defaults(struct options * p)1001 set_option_defaults (struct options *p)
1002 {
1003   if (getenv ("POSIXLY_CORRECT"))
1004     p->posixly_correct = true;
1005   else
1006     p->posixly_correct = false;
1007 
1008   /* We call check_nofollow() before setlocale() because the numbers
1009    * for which we check (in the results of uname) definitiely have "."
1010    * as the decimal point indicator even under locales for which that
1011    * is not normally true.   Hence atof would do the wrong thing
1012    * if we call it after setlocale().
1013    */
1014 #ifdef O_NOFOLLOW
1015   p->open_nofollow_available = check_nofollow ();
1016 #else
1017   p->open_nofollow_available = false;
1018 #endif
1019 
1020   p->regex_options = RE_SYNTAX_EMACS;
1021 
1022   if (isatty (0))
1023     {
1024       p->warnings = true;
1025       p->literal_control_chars = false;
1026     }
1027   else
1028     {
1029       p->warnings = false;
1030       p->literal_control_chars = false; /* may change */
1031     }
1032   if (p->posixly_correct)
1033     {
1034       p->warnings = false;
1035     }
1036 
1037   p->do_dir_first = true;
1038   p->explicit_depth = false;
1039   p->maxdepth = p->mindepth = -1;
1040 
1041   p->start_time = now ();
1042   p->cur_day_start.tv_sec = p->start_time.tv_sec - DAYSECS;
1043   p->cur_day_start.tv_nsec = p->start_time.tv_nsec;
1044 
1045   p->full_days = false;
1046   p->stay_on_filesystem = false;
1047   p->ignore_readdir_race = false;
1048 
1049   if (p->posixly_correct)
1050     p->output_block_size = 512;
1051   else
1052     p->output_block_size = 1024;
1053 
1054   p->debug_options = 0uL;
1055   p->optimisation_level = 2;
1056 
1057   if (getenv ("FIND_BLOCK_SIZE"))
1058     {
1059       die (EXIT_FAILURE, 0,
1060 	   _("The environment variable FIND_BLOCK_SIZE is not supported, "
1061 	     "the only thing that affects the block size is the "
1062 	     "POSIXLY_CORRECT environment variable"));
1063     }
1064 
1065 #if LEAF_OPTIMISATION
1066   /* The leaf optimisation is enabled. */
1067   p->no_leaf_check = false;
1068 #else
1069   /* The leaf optimisation is disabled. */
1070   p->no_leaf_check = true;
1071 #endif
1072 
1073   set_follow_state (SYMLINK_NEVER_DEREF); /* The default is equivalent to -P. */
1074 
1075   p->err_quoting_style = locale_quoting_style;
1076 }
1077 
1078 
1079 /* apply_predicate
1080  *
1081  */
1082 bool
apply_predicate(const char * pathname,struct stat * stat_buf,struct predicate * p)1083 apply_predicate(const char *pathname, struct stat *stat_buf, struct predicate *p)
1084 {
1085   ++p->perf.visits;
1086 
1087   if (p->need_stat || p->need_type || p->need_inum)
1088     {
1089       /* We may need a stat here. */
1090       if (get_info(pathname, stat_buf, p) != 0)
1091 	    return false;
1092     }
1093   if ((p->pred_func)(pathname, stat_buf, p))
1094     {
1095       ++(p->perf.successes);
1096       return true;
1097     }
1098   else
1099     {
1100       return false;
1101     }
1102 }
1103 
1104 
1105 /* is_exec_in_local_dir
1106  *
1107  */
1108 bool
is_exec_in_local_dir(const PRED_FUNC pred_func)1109 is_exec_in_local_dir (const PRED_FUNC pred_func)
1110 {
1111   return pred_execdir == pred_func || pred_okdir == pred_func;
1112 }
1113 
1114 /* safely_quote_err_filename
1115  *
1116  */
1117 const char *
safely_quote_err_filename(int n,char const * arg)1118 safely_quote_err_filename (int n, char const *arg)
1119 {
1120   return quotearg_n_style (n, options.err_quoting_style, arg);
1121 }
1122 
1123 /* We have encountered an error which should affect the exit status.
1124  * This is normally used to change the exit status from 0 to 1.
1125  * However, if the exit status is already 2 for example, we don't want to
1126  * reduce it to 1.
1127  */
1128 void
error_severity(int level)1129 error_severity (int level)
1130 {
1131   if (state.exit_status < level)
1132     state.exit_status = level;
1133 }
1134 
1135 
1136 /* report_file_err
1137  */
1138 static void
report_file_err(int exitval,int errno_value,bool is_target_file,const char * name)1139 report_file_err(int exitval, int errno_value,
1140 		bool is_target_file, const char *name)
1141 {
1142   /* It is important that the errno value is passed in as a function
1143    * argument before we call safely_quote_err_filename(), because otherwise
1144    * we might find that safely_quote_err_filename() changes errno.
1145    */
1146   if (!is_target_file || !state.already_issued_stat_error_msg)
1147     {
1148       error (exitval, errno_value, "%s", safely_quote_err_filename (0, name));
1149       error_severity (1);
1150     }
1151   if (is_target_file)
1152     {
1153       state.already_issued_stat_error_msg = true;
1154     }
1155 }
1156 
1157 /* nonfatal_target_file_error
1158  */
1159 void
nonfatal_target_file_error(int errno_value,const char * name)1160 nonfatal_target_file_error (int errno_value, const char *name)
1161 {
1162   report_file_err (0, errno_value, true, name);
1163 }
1164 
1165 /* fatal_target_file_error
1166  *
1167  * Report an error on a target file (i.e. a file we are searching).
1168  * Such errors are only reported once per searched file.
1169  *
1170  */
1171 void
fatal_target_file_error(int errno_value,const char * name)1172 fatal_target_file_error(int errno_value, const char *name)
1173 {
1174   report_file_err (1, errno_value, true, name);
1175   /*NOTREACHED*/
1176   abort ();
1177 }
1178 
1179 /* nonfatal_nontarget_file_error
1180  *
1181  */
1182 void
nonfatal_nontarget_file_error(int errno_value,const char * name)1183 nonfatal_nontarget_file_error (int errno_value, const char *name)
1184 {
1185   report_file_err (0, errno_value, false, name);
1186 }
1187 
1188 /* fatal_nontarget_file_error
1189  *
1190  */
1191 void
fatal_nontarget_file_error(int errno_value,const char * name)1192 fatal_nontarget_file_error(int errno_value, const char *name)
1193 {
1194   /* We're going to exit fatally, so make sure we always isssue the error
1195    * message, even if it will be duplicate.   Motivation: otherwise it may
1196    * not be clear what went wrong.
1197    */
1198   state.already_issued_stat_error_msg = false;
1199   report_file_err (1, errno_value, false, name);
1200   /*NOTREACHED*/
1201   abort ();
1202 }
1203