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
;
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. */
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
);
116 switch (last_pred
->p_type
)
119 error (1, 0, _("oops -- invalid default insertion of and!"));
124 /* We need to interpose the and operator. */
125 new_pred
= get_new_pred (entry_and
);
126 new_pred
->pred_func
= pred_and
;
128 new_pred
->p_name
= find_pred_name (pred_and
);
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;
143 new_pred
= get_new_pred (entry
);
144 new_pred
->parser_entry
= entry
;
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:
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
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
;
172 new_pred
->p_name
= entry
->parser_name
;
174 new_pred
->args
.str
= NULL
;
175 new_pred
->p_type
= PRIMARY_TYPE
;
176 new_pred
->p_prec
= NO_PREC
;
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:
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
196 insert_primary (const struct parser_table
*entry
)
198 assert(entry
->pred_func
!= NULL
);
199 return insert_primary_withpred(entry
, entry
->pred_func
);
208 fprintf (stderr
, "%s: %s\n", program_name
, msg
);
209 fprintf (stderr
, _("\
210 Usage: %s [-H] [-L] [-P] [path...] [expression]\n"), program_name
);
215 /* Get the stat information for a file, if it is
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;
230 state
.have_stat
= true;
231 state
.have_type
= true;
232 state
.type
= p
->st_mode
;
237 /* Get the stat/type information for a file, if it is
241 get_info (const char *pathname
,
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.
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
);
261 /* Determine if we can use O_NOFOLLOW.
263 #if defined(O_NOFOLLOW)
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. */
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).
300 complete_pending_execdirs(struct predicate
*p
)
302 #if defined(NEW_EXEC)
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
);
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).
340 complete_pending_execs(struct predicate
*p
)
342 #if defined(NEW_EXEC)
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
);
374 /* Complete any outstanding commands.
381 complete_pending_execs(eval_tree
);
382 complete_pending_execdirs(eval_tree
);
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.
399 fprintf(stderr
, "fallback_stat(): stat(%s) failed; falling back on lstat()\n", name
);
401 return lstat(name
, p
);
408 case EOVERFLOW
: /* EOVERFLOW is not #defined on UNICOS. */
416 /* optionh_stat() implements the stat operation when the -H option is
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.
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
434 int rv
= stat(name
, p
);
436 return 0; /* success */
438 return fallback_stat(name
, p
, rv
);
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.
453 optionl_stat(const char *name
, struct stat
*p
)
455 int rv
= stat(name
, p
);
457 return 0; /* normal case. */
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.
467 optionp_stat(const char *name
, struct stat
*p
)
469 return lstat(name
, p
);
473 static uintmax_t stat_count
= 0u;
476 debug_stat (const char *file
, struct stat
*bufp
)
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
:
500 case SYMLINK_DEREF_ARGSONLY
:
501 return (state
.curdepth
== 0);
502 case SYMLINK_NEVER_DEREF
:
509 /* Take a "mode" indicator and fill in the files of 'state'.
512 digest_mode(mode_t mode
,
513 const char *pathname
,
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.
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)
528 mode
= state
.type
= pstat
->st_mode
;
529 state
.have_type
= true;
533 state
.have_type
= true;
534 pstat
->st_mode
= state
.type
= mode
;
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)
545 state
.have_stat
= false;
546 state
.have_type
= false;;
551 if (get_statinfo(pathname
, name
, pstat
) != 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;
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 */
574 default_prints (struct predicate
*pred
)
578 if (pred
->no_default_print
)
580 pred
= pred
->pred_next
;