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)
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,
29 #ifdef HAVE_SYS_UTSNAME_H
30 #include <sys/utsname.h>
36 # define _(Text) gettext (Text)
41 # define N_(String) gettext_noop (String)
43 /* See locate.c for explanation as to why not use (String) */
44 # define N_(String) String
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. */
59 get_new_pred (const struct parser_table
*entry
)
61 register struct predicate
*new_pred
;
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
;
76 new_pred
= (struct predicate
*) xmalloc (sizeof (struct predicate
));
77 last_pred
->pred_next
= new_pred
;
80 last_pred
->parser_entry
= entry
;
81 last_pred
->pred_func
= NULL
;
83 last_pred
->p_name
= NULL
;
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
;
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. */
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
);
117 switch (last_pred
->p_type
)
120 error (1, 0, _("oops -- invalid default insertion of and!"));
125 /* We need to interpose the and operator. */
126 new_pred
= get_new_pred (entry_and
);
127 new_pred
->pred_func
= pred_and
;
129 new_pred
->p_name
= find_pred_name (pred_and
);
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;
144 new_pred
= get_new_pred (entry
);
145 new_pred
->parser_entry
= entry
;
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:
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
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
;
173 new_pred
->p_name
= entry
->parser_name
;
175 new_pred
->args
.str
= NULL
;
176 new_pred
->p_type
= PRIMARY_TYPE
;
177 new_pred
->p_prec
= NO_PREC
;
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:
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
197 insert_primary (const struct parser_table
*entry
)
199 assert(entry
->pred_func
!= NULL
);
200 return insert_primary_withpred(entry
, entry
->pred_func
);
209 fprintf (stderr
, "%s: %s\n", program_name
, msg
);
210 fprintf (stderr
, _("\
211 Usage: %s [-H] [-L] [-P] [path...] [expression]\n"), program_name
);
216 /* Get the stat information for a file, if it is
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;
231 state
.have_stat
= true;
232 state
.have_type
= true;
233 state
.type
= p
->st_mode
;
238 /* Get the stat/type information for a file, if it is
242 get_info (const char *pathname
,
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.
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
);
262 /* Determine if we can use O_NOFOLLOW.
264 #if defined(O_NOFOLLOW)
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. */
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).
301 complete_pending_execdirs(struct predicate
*p
)
303 #if defined(NEW_EXEC)
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
);
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).
341 complete_pending_execs(struct predicate
*p
)
343 #if defined(NEW_EXEC)
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
);
375 /* Complete any outstanding commands.
382 complete_pending_execs(eval_tree
);
383 complete_pending_execdirs(eval_tree
);
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.
400 fprintf(stderr
, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name
);
402 return lstat(name
, p
);
409 case EOVERFLOW
: /* EOVERFLOW is not #defined on UNICOS. */
417 /* optionh_stat() implements the stat operation when the -H option is
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.
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
435 int rv
= stat(name
, p
);
437 return 0; /* success */
439 return fallback_stat(name
, p
, rv
);
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.
454 optionl_stat(const char *name
, struct stat
*p
)
456 int rv
= stat(name
, p
);
458 return 0; /* normal case. */
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.
468 optionp_stat(const char *name
, struct stat
*p
)
470 return lstat(name
, p
);
474 static uintmax_t stat_count
= 0u;
477 debug_stat (const char *file
, struct stat
*bufp
)
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
:
501 case SYMLINK_DEREF_ARGSONLY
:
502 return (state
.curdepth
== 0);
503 case SYMLINK_NEVER_DEREF
:
510 /* Take a "mode" indicator and fill in the files of 'state'.
513 digest_mode(mode_t mode
,
514 const char *pathname
,
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.
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)
529 mode
= state
.type
= pstat
->st_mode
;
530 state
.have_type
= true;
534 state
.have_type
= true;
535 pstat
->st_mode
= state
.type
= mode
;
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)
546 state
.have_stat
= false;
547 state
.have_type
= false;;
552 if (get_statinfo(pathname
, name
, pstat
) != 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;
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 */
575 default_prints (struct predicate
*pred
)
579 if (pred
->no_default_print
)
581 pred
= pred
->pred_next
;
587 looks_like_expression(const char *arg
)
592 if (arg
[1]) /* "-foo" is an expression. */
595 return false; /* Just "-" is a filename. */
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
605 /* (, ) and ! are part of an expression,
606 * but (2, )3 and !foo are filenames.