Use the new find program, and the new way of locating it (/ instead of )
[findutils.git] / find / util.c
blobfb930e0efea1deaf4cacc18da8c8acdab6caf6e4
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 last_pred->literal_control_chars = options.literal_control_chars;
96 return last_pred;
99 /* Return a pointer to a new predicate, with operator check.
100 Like get_new_pred, but it checks to make sure that the previous
101 predicate is an operator. If it isn't, the AND operator is inserted. */
103 struct predicate *
104 get_new_pred_chk_op (const struct parser_table *entry)
106 struct predicate *new_pred;
107 static const struct parser_table *entry_and = NULL;
109 /* Locate the entry in the parser table for the "and" operator */
110 if (NULL == entry_and)
111 entry_and = find_parser("and");
113 /* Check that it's actually there. If not, that is a bug.*/
114 assert(entry_and != NULL);
116 if (last_pred)
117 switch (last_pred->p_type)
119 case NO_TYPE:
120 error (1, 0, _("oops -- invalid default insertion of and!"));
121 break;
123 case PRIMARY_TYPE:
124 case CLOSE_PAREN:
125 /* We need to interpose the and operator. */
126 new_pred = get_new_pred (entry_and);
127 new_pred->pred_func = pred_and;
128 #ifdef DEBUG
129 new_pred->p_name = find_pred_name (pred_and);
130 #endif /* DEBUG */
131 new_pred->p_type = BI_OP;
132 new_pred->p_prec = AND_PREC;
133 new_pred->need_stat = false;
134 new_pred->need_type = false;
135 new_pred->args.str = NULL;
136 new_pred->side_effects = false;
137 new_pred->no_default_print = false;
138 break;
140 default:
141 break;
144 new_pred = get_new_pred (entry);
145 new_pred->parser_entry = entry;
146 return new_pred;
149 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
151 Return a pointer to the predicate node just inserted.
153 Fills in the following cells of the new predicate node:
155 pred_func PRED_FUNC
156 args(.str) NULL
157 p_type PRIMARY_TYPE
158 p_prec NO_PREC
160 Other cells that need to be filled in are defaulted by
161 get_new_pred_chk_op, which is used to insure that the prior node is
162 either not there at all (we are the very first node) or is an
163 operator. */
165 struct predicate *
166 insert_primary_withpred (const struct parser_table *entry, PRED_FUNC pred_func)
168 struct predicate *new_pred;
170 new_pred = get_new_pred_chk_op (entry);
171 new_pred->pred_func = pred_func;
172 #ifdef DEBUG
173 new_pred->p_name = entry->parser_name;
174 #endif /* DEBUG */
175 new_pred->args.str = NULL;
176 new_pred->p_type = PRIMARY_TYPE;
177 new_pred->p_prec = NO_PREC;
178 return new_pred;
181 /* Add a primary described by ENTRY to the predicate input list.
183 Return a pointer to the predicate node just inserted.
185 Fills in the following cells of the new predicate node:
187 pred_func PRED_FUNC
188 args(.str) NULL
189 p_type PRIMARY_TYPE
190 p_prec NO_PREC
192 Other cells that need to be filled in are defaulted by
193 get_new_pred_chk_op, which is used to insure that the prior node is
194 either not there at all (we are the very first node) or is an
195 operator. */
196 struct predicate *
197 insert_primary (const struct parser_table *entry)
199 assert(entry->pred_func != NULL);
200 return insert_primary_withpred(entry, entry->pred_func);
205 void
206 usage (char *msg)
208 if (msg)
209 fprintf (stderr, "%s: %s\n", program_name, msg);
210 fprintf (stderr, _("\
211 Usage: %s [-H] [-L] [-P] [path...] [expression]\n"), program_name);
212 exit (1);
216 /* Get the stat information for a file, if it is
217 * not already known.
220 get_statinfo (const char *pathname, const char *name, struct stat *p)
222 if (!state.have_stat && (*options.xstat) (name, p) != 0)
224 if (!options.ignore_readdir_race || (errno != ENOENT) )
226 error (0, errno, "%s", pathname);
227 state.exit_status = 1;
229 return -1;
231 state.have_stat = true;
232 state.have_type = true;
233 state.type = p->st_mode;
234 return 0;
238 /* Get the stat/type information for a file, if it is
239 * not already known.
242 get_info (const char *pathname,
243 const char *name,
244 struct stat *p,
245 struct predicate *pred_ptr)
247 /* If we need the full stat info, or we need the type info but don't
248 * already have it, stat the file now.
250 (void) name;
251 if (pred_ptr->need_stat)
253 return get_statinfo(pathname, state.rel_pathname, p);
255 if ((pred_ptr->need_type && (0 == state.have_type)))
257 return get_statinfo(pathname, state.rel_pathname, p);
259 return 0;
262 /* Determine if we can use O_NOFOLLOW.
264 #if defined(O_NOFOLLOW)
265 boolean
266 check_nofollow(void)
268 struct utsname uts;
269 float release;
271 if (0 == uname(&uts))
273 /* POSIX requires that atof() ignore "unrecognised suffixes". */
274 release = atof(uts.release);
276 if (0 == strcmp("Linux", uts.sysname))
278 /* Linux kernels 2.1.126 and earlier ignore the O_NOFOLLOW flag. */
279 return release >= 2.2; /* close enough */
281 else if (0 == strcmp("FreeBSD", uts.sysname))
283 /* FreeBSD 3.0-CURRENT and later support it */
284 return release >= 3.1;
288 /* Well, O_NOFOLLOW was defined, so we'll try to use it. */
289 return true;
291 #endif
295 /* Examine the predicate list for instances of -execdir or -okdir
296 * which have been terminated with '+' (build argument list) rather
297 * than ';' (singles only). If there are any, run them (this will
298 * have no effect if there are no arguments waiting).
300 void
301 complete_pending_execdirs(struct predicate *p)
303 #if defined(NEW_EXEC)
304 if (NULL == p)
305 return;
307 complete_pending_execdirs(p->pred_left);
309 if (p->pred_func == pred_execdir || p->pred_func == pred_okdir)
311 /* It's an exec-family predicate. p->args.exec_val is valid. */
312 if (p->args.exec_vec.multiple)
314 struct exec_val *execp = &p->args.exec_vec;
316 /* This one was terminated by '+' and so might have some
317 * left... Run it if necessary.
319 if (execp->state.todo)
321 /* There are not-yet-executed arguments. */
322 launch (&execp->ctl, &execp->state);
327 complete_pending_execdirs(p->pred_right);
328 #else
329 /* nothing to do. */
330 return;
331 #endif
335 /* Examine the predicate list for instances of -exec which have been
336 * terminated with '+' (build argument list) rather than ';' (singles
337 * only). If there are any, run them (this will have no effect if
338 * there are no arguments waiting).
340 void
341 complete_pending_execs(struct predicate *p)
343 #if defined(NEW_EXEC)
344 if (NULL == p)
345 return;
347 complete_pending_execs(p->pred_left);
349 /* It's an exec-family predicate then p->args.exec_val is valid
350 * and we can check it.
352 if (p->pred_func == pred_exec && p->args.exec_vec.multiple)
354 struct exec_val *execp = &p->args.exec_vec;
356 /* This one was terminated by '+' and so might have some
357 * left... Run it if necessary. Set state.exit_status if
358 * there are any problems.
360 if (execp->state.todo)
362 /* There are not-yet-executed arguments. */
363 launch (&execp->ctl, &execp->state);
367 complete_pending_execs(p->pred_right);
368 #else
369 /* nothing to do. */
370 return;
371 #endif
375 /* Complete any outstanding commands.
377 void
378 cleanup(void)
380 if (eval_tree)
382 complete_pending_execs(eval_tree);
383 complete_pending_execdirs(eval_tree);
388 static int
389 fallback_stat(const char *name, struct stat *p, int prev_rv)
391 /* Our original stat() call failed. Perhaps we can't follow a
392 * symbolic link. If that might be the problem, lstat() the link.
393 * Otherwise, admit defeat.
395 switch (errno)
397 case ENOENT:
398 case ENOTDIR:
399 #ifdef DEBUG_STAT
400 fprintf(stderr, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name);
401 #endif
402 return lstat(name, p);
404 case EACCES:
405 case EIO:
406 case ELOOP:
407 case ENAMETOOLONG:
408 #ifdef EOVERFLOW
409 case EOVERFLOW: /* EOVERFLOW is not #defined on UNICOS. */
410 #endif
411 default:
412 return prev_rv;
417 /* optionh_stat() implements the stat operation when the -H option is
418 * in effect.
420 * If the item to be examined is a command-line argument, we follow
421 * symbolic links. If the stat() call fails on the command-line item,
422 * we fall back on the properties of the symbolic link.
424 * If the item to be examined is not a command-line argument, we
425 * examine the link itself.
427 int
428 optionh_stat(const char *name, struct stat *p)
430 if (0 == state.curdepth)
432 /* This file is from the command line; deference the link (if it
433 * is a link).
435 int rv = stat(name, p);
436 if (0 == rv)
437 return 0; /* success */
438 else
439 return fallback_stat(name, p, rv);
441 else
443 /* Not a file on the command line; do not dereference the link.
445 return lstat(name, p);
449 /* optionl_stat() implements the stat operation when the -L option is
450 * in effect. That option makes us examine the thing the symbolic
451 * link points to, not the symbolic link itself.
453 int
454 optionl_stat(const char *name, struct stat *p)
456 int rv = stat(name, p);
457 if (0 == rv)
458 return 0; /* normal case. */
459 else
460 return fallback_stat(name, p, rv);
463 /* optionp_stat() implements the stat operation when the -P option is
464 * in effect (this is also the default). That option makes us examine
465 * the symbolic link itself, not the thing it points to.
467 int
468 optionp_stat(const char *name, struct stat *p)
470 return lstat(name, p);
473 #ifdef DEBUG_STAT
474 static uintmax_t stat_count = 0u;
477 debug_stat (const char *file, struct stat *bufp)
479 ++stat_count;
480 fprintf (stderr, "debug_stat (%s)\n", file);
481 switch (options.symlink_handling)
483 case SYMLINK_ALWAYS_DEREF:
484 return optionl_stat(file, bufp);
485 case SYMLINK_DEREF_ARGSONLY:
486 return optionh_stat(file, bufp);
487 case SYMLINK_NEVER_DEREF:
488 return optionp_stat(file, bufp);
491 #endif /* DEBUG_STAT */
495 following_links(void)
497 switch (options.symlink_handling)
499 case SYMLINK_ALWAYS_DEREF:
500 return 1;
501 case SYMLINK_DEREF_ARGSONLY:
502 return (state.curdepth == 0);
503 case SYMLINK_NEVER_DEREF:
504 default:
505 return 0;
510 /* Take a "mode" indicator and fill in the files of 'state'.
513 digest_mode(mode_t mode,
514 const char *pathname,
515 const char *name,
516 struct stat *pstat,
517 boolean leaf)
519 /* If we know the type of the directory entry, and it is not a
520 * symbolic link, we may be able to avoid a stat() or lstat() call.
522 if (mode)
524 if (S_ISLNK(mode) && following_links())
526 /* mode is wrong because we should have followed the symlink. */
527 if (get_statinfo(pathname, name, pstat) != 0)
528 return 0;
529 mode = state.type = pstat->st_mode;
530 state.have_type = true;
532 else
534 state.have_type = true;
535 pstat->st_mode = state.type = mode;
538 else
540 /* Mode is not yet known; may have to stat the file unless we
541 * can deduce that it is not a directory (which is all we need to
542 * know at this stage)
544 if (leaf)
546 state.have_stat = false;
547 state.have_type = false;;
548 state.type = 0;
550 else
552 if (get_statinfo(pathname, name, pstat) != 0)
553 return 0;
555 /* If -L is in effect and we are dealing with a symlink,
556 * st_mode is the mode of the pointed-to file, while mode is
557 * the mode of the directory entry (S_IFLNK). Hence now
558 * that we have the stat information, override "mode".
560 state.type = pstat->st_mode;
561 state.have_type = true;
565 /* success. */
566 return 1;
570 /* Return true if there are no predicates with no_default_print in
571 predicate list PRED, false if there are any.
572 Returns true if default print should be performed */
574 boolean
575 default_prints (struct predicate *pred)
577 while (pred != NULL)
579 if (pred->no_default_print)
580 return (false);
581 pred = pred->pred_next;
583 return (true);
586 boolean
587 looks_like_expression(const char *arg)
589 switch (arg[0])
591 case '-':
592 if (arg[1]) /* "-foo" is an expression. */
593 return true;
594 else
595 return false; /* Just "-" is a filename. */
596 break;
598 /* According to the POSIX standard, we have to assume that a leading ')' is a
599 * filename argument. Hence it does not matter if the ')' is followed by any
600 * other characters.
602 case ')':
603 return false;
605 /* (, ) and ! are part of an expression,
606 * but (2, )3 and !foo are filenames.
608 case '!':
609 case '(':
610 case ',':
611 if (arg[1])
612 return false;
613 else
614 return true;
616 default:
617 return false;