Findutils 4.3.x defaults to using the the FTS implementation of find.
[findutils.git] / find / util.c
blob4d81f3e097444055fec50039d2804fe556b485d8
1 /* util.c -- functions for initializing new tree elements, and other things.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004 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"
21 #include "xalloc.h"
24 #ifdef HAVE_FCNTL_H
25 #include <fcntl.h>
26 #else
27 #include <sys/file.h>
28 #endif
29 #ifdef HAVE_SYS_UTSNAME_H
30 #include <sys/utsname.h>
31 #endif
34 #if ENABLE_NLS
35 # include <libintl.h>
36 # define _(Text) gettext (Text)
37 #else
38 # define _(Text) Text
39 #endif
40 #ifdef gettext_noop
41 # define N_(String) gettext_noop (String)
42 #else
43 /* See locate.c for explanation as to why not use (String) */
44 # define N_(String) String
45 #endif
47 #include <assert.h>
50 /* Return a pointer to a new predicate structure, which has been
51 linked in as the last one in the predicates list.
53 Set `predicates' to point to the start of the predicates list.
54 Set `last_pred' to point to the new last predicate in the list.
56 Set all cells in the new structure to the default values. */
58 struct predicate *
59 get_new_pred (const struct parser_table *entry)
61 register struct predicate *new_pred;
62 (void) entry;
64 /* Options should not be turned into predicates. */
65 assert(entry->type != ARG_OPTION);
66 assert(entry->type != ARG_POSITIONAL_OPTION);
68 if (predicates == NULL)
70 predicates = (struct predicate *)
71 xmalloc (sizeof (struct predicate));
72 last_pred = predicates;
74 else
76 new_pred = (struct predicate *) xmalloc (sizeof (struct predicate));
77 last_pred->pred_next = new_pred;
78 last_pred = new_pred;
80 last_pred->parser_entry = entry;
81 last_pred->pred_func = NULL;
82 #ifdef DEBUG
83 last_pred->p_name = NULL;
84 #endif /* DEBUG */
85 last_pred->p_type = NO_TYPE;
86 last_pred->p_prec = NO_PREC;
87 last_pred->side_effects = false;
88 last_pred->no_default_print = false;
89 last_pred->need_stat = true;
90 last_pred->need_type = true;
91 last_pred->args.str = NULL;
92 last_pred->pred_next = NULL;
93 last_pred->pred_left = NULL;
94 last_pred->pred_right = NULL;
95 return (last_pred);
98 /* Return a pointer to a new predicate, with operator check.
99 Like get_new_pred, but it checks to make sure that the previous
100 predicate is an operator. If it isn't, the AND operator is inserted. */
102 struct predicate *
103 get_new_pred_chk_op (const struct parser_table *entry)
105 struct predicate *new_pred;
106 static const struct parser_table *entry_and = NULL;
108 /* Locate the entry in the parser table for the "and" operator */
109 if (NULL == entry_and)
110 entry_and = find_parser("and");
112 /* Check that it's actually there. If not, that is a bug.*/
113 assert(entry_and != NULL);
115 if (last_pred)
116 switch (last_pred->p_type)
118 case NO_TYPE:
119 error (1, 0, _("oops -- invalid default insertion of and!"));
120 break;
122 case PRIMARY_TYPE:
123 case CLOSE_PAREN:
124 /* We need to interpose the and operator. */
125 new_pred = get_new_pred (entry_and);
126 new_pred->pred_func = pred_and;
127 #ifdef DEBUG
128 new_pred->p_name = find_pred_name (pred_and);
129 #endif /* DEBUG */
130 new_pred->p_type = BI_OP;
131 new_pred->p_prec = AND_PREC;
132 new_pred->need_stat = false;
133 new_pred->need_type = false;
134 new_pred->args.str = NULL;
135 new_pred->side_effects = false;
136 new_pred->no_default_print = false;
137 break;
139 default:
140 break;
143 new_pred = get_new_pred (entry);
144 new_pred->parser_entry = entry;
145 return new_pred;
148 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
150 Return a pointer to the predicate node just inserted.
152 Fills in the following cells of the new predicate node:
154 pred_func PRED_FUNC
155 args(.str) NULL
156 p_type PRIMARY_TYPE
157 p_prec NO_PREC
159 Other cells that need to be filled in are defaulted by
160 get_new_pred_chk_op, which is used to insure that the prior node is
161 either not there at all (we are the very first node) or is an
162 operator. */
164 struct predicate *
165 insert_primary_withpred (const struct parser_table *entry, PRED_FUNC pred_func)
167 struct predicate *new_pred;
169 new_pred = get_new_pred_chk_op (entry);
170 new_pred->pred_func = pred_func;
171 #ifdef DEBUG
172 new_pred->p_name = entry->parser_name;
173 #endif /* DEBUG */
174 new_pred->args.str = NULL;
175 new_pred->p_type = PRIMARY_TYPE;
176 new_pred->p_prec = NO_PREC;
177 return new_pred;
180 /* Add a primary described by ENTRY to the predicate input list.
182 Return a pointer to the predicate node just inserted.
184 Fills in the following cells of the new predicate node:
186 pred_func PRED_FUNC
187 args(.str) NULL
188 p_type PRIMARY_TYPE
189 p_prec NO_PREC
191 Other cells that need to be filled in are defaulted by
192 get_new_pred_chk_op, which is used to insure that the prior node is
193 either not there at all (we are the very first node) or is an
194 operator. */
195 struct predicate *
196 insert_primary (const struct parser_table *entry)
198 assert(entry->pred_func != NULL);
199 return insert_primary_withpred(entry, entry->pred_func);
204 void
205 usage (char *msg)
207 if (msg)
208 fprintf (stderr, "%s: %s\n", program_name, msg);
209 fprintf (stderr, _("\
210 Usage: %s [-H] [-L] [-P] [path...] [expression]\n"), program_name);
211 exit (1);
215 /* Get the stat information for a file, if it is
216 * not already known.
219 get_statinfo (const char *pathname, const char *name, struct stat *p)
221 if (!state.have_stat && (*options.xstat) (name, p) != 0)
223 if (!options.ignore_readdir_race || (errno != ENOENT) )
225 error (0, errno, "%s", pathname);
226 state.exit_status = 1;
228 return -1;
230 state.have_stat = true;
231 state.have_type = true;
232 state.type = p->st_mode;
233 return 0;
237 /* Get the stat/type information for a file, if it is
238 * not already known.
241 get_info (const char *pathname,
242 const char *name,
243 struct stat *p,
244 struct predicate *pred_ptr)
246 /* If we need the full stat info, or we need the type info but don't
247 * already have it, stat the file now.
249 (void) name;
250 if (pred_ptr->need_stat)
252 return get_statinfo(pathname, state.rel_pathname, p);
254 if ((pred_ptr->need_type && (0 == state.have_type)))
256 return get_statinfo(pathname, state.rel_pathname, p);
258 return 0;
261 /* Determine if we can use O_NOFOLLOW.
263 #if defined(O_NOFOLLOW)
264 boolean
265 check_nofollow(void)
267 struct utsname uts;
268 float release;
270 if (0 == uname(&uts))
272 /* POSIX requires that atof() ignore "unrecognised suffixes". */
273 release = atof(uts.release);
275 if (0 == strcmp("Linux", uts.sysname))
277 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
278 return release >= 2.2; /* close enough */
280 else if (0 == strcmp("FreeBSD", uts.sysname))
282 /* FreeBSD 3.0-CURRENT and later support it */
283 return release >= 3.1;
287 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
288 return true;
290 #endif
294 /* Examine the predicate list for instances of -execdir or -okdir
295 * which have been terminated with '+' (build argument list) rather
296 * than ';' (singles only). If there are any, run them (this will
297 * have no effect if there are no arguments waiting).
299 void
300 complete_pending_execdirs(struct predicate *p)
302 #if defined(NEW_EXEC)
303 if (NULL == p)
304 return;
306 complete_pending_execdirs(p->pred_left);
308 if (p->pred_func == pred_execdir || p->pred_func == pred_okdir)
310 /* It's an exec-family predicate. p->args.exec_val is valid. */
311 if (p->args.exec_vec.multiple)
313 struct exec_val *execp = &p->args.exec_vec;
315 /* This one was terminated by '+' and so might have some
316 * left... Run it if necessary.
318 if (execp->state.todo)
320 /* There are not-yet-executed arguments. */
321 launch (&execp->ctl, &execp->state);
326 complete_pending_execdirs(p->pred_right);
327 #else
328 /* nothing to do. */
329 return;
330 #endif
334 /* Examine the predicate list for instances of -exec which have been
335 * terminated with '+' (build argument list) rather than ';' (singles
336 * only). If there are any, run them (this will have no effect if
337 * there are no arguments waiting).
339 void
340 complete_pending_execs(struct predicate *p)
342 #if defined(NEW_EXEC)
343 if (NULL == p)
344 return;
346 complete_pending_execs(p->pred_left);
348 /* It's an exec-family predicate then p->args.exec_val is valid
349 * and we can check it.
351 if (p->pred_func == pred_exec && p->args.exec_vec.multiple)
353 struct exec_val *execp = &p->args.exec_vec;
355 /* This one was terminated by '+' and so might have some
356 * left... Run it if necessary. Set state.exit_status if
357 * there are any problems.
359 if (execp->state.todo)
361 /* There are not-yet-executed arguments. */
362 launch (&execp->ctl, &execp->state);
366 complete_pending_execs(p->pred_right);
367 #else
368 /* nothing to do. */
369 return;
370 #endif
374 /* Complete any outstanding commands.
376 void
377 cleanup(void)
379 if (eval_tree)
381 complete_pending_execs(eval_tree);
382 complete_pending_execdirs(eval_tree);
387 static int
388 fallback_stat(const char *name, struct stat *p, int prev_rv)
390 /* Our original stat() call failed. Perhaps we can't follow a
391 * symbolic link. If that might be the problem, lstat() the link.
392 * Otherwise, admit defeat.
394 switch (errno)
396 case ENOENT:
397 case ENOTDIR:
398 #ifdef DEBUG_STAT
399 fprintf(stderr, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name);
400 #endif
401 return lstat(name, p);
403 case EACCES:
404 case EIO:
405 case ELOOP:
406 case ENAMETOOLONG:
407 #ifdef EOVERFLOW
408 case EOVERFLOW: /* EOVERFLOW is not #defined on UNICOS. */
409 #endif
410 default:
411 return prev_rv;
416 /* optionh_stat() implements the stat operation when the -H option is
417 * in effect.
419 * If the item to be examined is a command-line argument, we follow
420 * symbolic links. If the stat() call fails on the command-line item,
421 * we fall back on the properties of the symbolic link.
423 * If the item to be examined is not a command-line argument, we
424 * examine the link itself.
426 int
427 optionh_stat(const char *name, struct stat *p)
429 if (0 == state.curdepth)
431 /* This file is from the command line; deference the link (if it
432 * is a link).
434 int rv = stat(name, p);
435 if (0 == rv)
436 return 0; /* success */
437 else
438 return fallback_stat(name, p, rv);
440 else
442 /* Not a file on the command line; do not dereference the link.
444 return lstat(name, p);
448 /* optionl_stat() implements the stat operation when the -L option is
449 * in effect. That option makes us examine the thing the symbolic
450 * link points to, not the symbolic link itself.
452 int
453 optionl_stat(const char *name, struct stat *p)
455 int rv = stat(name, p);
456 if (0 == rv)
457 return 0; /* normal case. */
458 else
459 return fallback_stat(name, p, rv);
462 /* optionp_stat() implements the stat operation when the -P option is
463 * in effect (this is also the default). That option makes us examine
464 * the symbolic link itself, not the thing it points to.
466 int
467 optionp_stat(const char *name, struct stat *p)
469 return lstat(name, p);
472 #ifdef DEBUG_STAT
473 static uintmax_t stat_count = 0u;
476 debug_stat (const char *file, struct stat *bufp)
478 ++stat_count;
479 fprintf (stderr, "debug_stat (%s)\n", file);
480 switch (options.symlink_handling)
482 case SYMLINK_ALWAYS_DEREF:
483 return optionl_stat(file, bufp);
484 case SYMLINK_DEREF_ARGSONLY:
485 return optionh_stat(file, bufp);
486 case SYMLINK_NEVER_DEREF:
487 return optionp_stat(file, bufp);
490 #endif /* DEBUG_STAT */
494 following_links(void)
496 switch (options.symlink_handling)
498 case SYMLINK_ALWAYS_DEREF:
499 return 1;
500 case SYMLINK_DEREF_ARGSONLY:
501 return (state.curdepth == 0);
502 case SYMLINK_NEVER_DEREF:
503 default:
504 return 0;
509 /* Take a "mode" indicator and fill in the files of 'state'.
512 digest_mode(mode_t mode,
513 const char *pathname,
514 const char *name,
515 struct stat *pstat,
516 boolean leaf)
518 /* If we know the type of the directory entry, and it is not a
519 * symbolic link, we may be able to avoid a stat() or lstat() call.
521 if (mode)
523 if (S_ISLNK(mode) && following_links())
525 /* mode is wrong because we should have followed the symlink. */
526 if (get_statinfo(pathname, name, pstat) != 0)
527 return 0;
528 mode = state.type = pstat->st_mode;
529 state.have_type = true;
531 else
533 state.have_type = true;
534 pstat->st_mode = state.type = mode;
537 else
539 /* Mode is not yet known; may have to stat the file unless we
540 * can deduce that it is not a directory (which is all we need to
541 * know at this stage)
543 if (leaf)
545 state.have_stat = false;
546 state.have_type = false;;
547 state.type = 0;
549 else
551 if (get_statinfo(pathname, name, pstat) != 0)
552 return 0;
554 /* If -L is in effect and we are dealing with a symlink,
555 * st_mode is the mode of the pointed-to file, while mode is
556 * the mode of the directory entry (S_IFLNK). Hence now
557 * that we have the stat information, override "mode".
559 state.type = pstat->st_mode;
560 state.have_type = true;
564 /* success. */
565 return 1;
569 /* Return true if there are no predicates with no_default_print in
570 predicate list PRED, false if there are any.
571 Returns true if default print should be performed */
573 boolean
574 default_prints (struct predicate *pred)
576 while (pred != NULL)
578 if (pred->no_default_print)
579 return (false);
580 pred = pred->pred_next;
582 return (true);