1 /* defs.h -- data types and declarations.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2004, 2005, 2006 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,
23 #include <sys/types.h>
25 /* XXX: some of these includes probably don't belong in a common header file */
47 # include <inttypes.h>
55 #include "stat_.h" /* S_ISUID etc. */
57 /* These days we will assume ANSI/ISO C protootypes work on our compiler. */
58 #define PARAMS(Args) Args
60 #ifndef ATTRIBUTE_NORETURN
61 # if HAVE_ATTRIBUTE_NORETURN
62 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
64 # define ATTRIBUTE_NORETURN /* nothing */
68 int optionl_stat
PARAMS((const char *name
, struct stat
*p
));
69 int optionp_stat
PARAMS((const char *name
, struct stat
*p
));
70 int optionh_stat
PARAMS((const char *name
, struct stat
*p
));
71 int debug_stat
PARAMS((const char *file
, struct stat
*bufp
));
73 void set_stat_placeholders
PARAMS((struct stat
*p
));
74 int get_statinfo
PARAMS((const char *pathname
, const char *name
, struct stat
*p
));
77 #define MODE_WXUSR (S_IWUSR | S_IXUSR)
78 #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
79 #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
80 #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
81 #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
87 /* Pointer to a predicate function. */
88 typedef boolean (*PRED_FUNC
)(const char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
);
90 /* The number of seconds in a day. */
93 /* Argument structures for predicates. */
102 enum permissions_type
119 enum predicate_precedence
131 enum comparison_type kind
;
132 boolean negative
; /* Defined only when representing time_t. */
138 enum permissions_type kind
;
142 /* dir_id is used to support loop detection in find.c
150 /* samefile_file_id is used to support the -samefile test.
152 struct samefile_file_id
161 enum comparison_type kind
;
169 XVAL_ATIME
, XVAL_BIRTHTIME
, XVAL_CTIME
, XVAL_MTIME
, XVAL_TIME
175 enum comparison_type kind
;
182 boolean multiple
; /* -exec {} \+ denotes multiple argument. */
183 struct buildcmd_control ctl
;
184 struct buildcmd_state state
;
185 char **replace_vec
; /* Command arguments (for ";" style) */
187 boolean use_current_dir
; /* If nonzero, don't chdir to start dir */
188 boolean close_stdin
; /* If true, close stdin in the child. */
189 int dirfd
; /* The directory to do the exec in. */
192 /* The format string for a -printf or -fprintf is chopped into one or
193 more `struct segment', linked together into a list.
194 Each stretch of plain text is a segment, and
195 each \c and `%' conversion is a segment. */
197 /* Special values for the `kind' field of `struct segment'. */
200 KIND_PLAIN
=0, /* Segment containing just plain text. */
201 KIND_STOP
=1, /* \c -- stop printing and flush output. */
202 KIND_FORMAT
, /* Regular format */
207 enum SegmentKind segkind
; /* KIND_FORMAT, KIND_PLAIN, KIND_STOP */
208 char format_char
[2]; /* Format chars if kind is KIND_FORMAT */
209 char *text
; /* Plain text or `%' format string. */
210 int text_len
; /* Length of `text'. */
211 struct segment
*next
; /* Next segment for this predicate. */
216 struct segment
*segment
; /* Linked list of segments. */
217 FILE *stream
; /* Output stream to print on. */
218 const char *filename
; /* We need the filename for error messages. */
219 boolean dest_is_tty
; /* True if the destination is a terminal. */
220 struct quoting_options
*quote_opts
;
223 /* Profiling information for a predicate */
224 struct predicate_performance_info
226 unsigned long visits
;
227 unsigned long successes
;
230 /* evaluation cost of a predicate */
241 NeedsUserInteraction
,
248 /* Pointer to the function that implements this predicate. */
251 /* Only used for debugging, but defined unconditionally so individual
252 modules can be compiled with -DDEBUG. */
255 /* The type of this node. There are two kinds. The first is real
256 predicates ("primaries") such as -perm, -print, or -exec. The
257 other kind is operators for combining predicates. */
258 enum predicate_type p_type
;
260 /* The precedence of this node. Only has meaning for operators. */
261 enum predicate_precedence p_prec
;
263 /* True if this predicate node produces side effects.
264 If side_effects are produced
265 then optimization will not be performed */
266 boolean side_effects
;
268 /* True if this predicate node requires default print be turned off. */
269 boolean no_default_print
;
271 /* True if this predicate node requires a stat system call to execute. */
274 /* True if this predicate node requires knowledge of the file type. */
277 enum EvaluationCost p_cost
;
279 /* est_success_rate is a number between 0.0 and 1.0 */
280 float est_success_rate
;
282 /* True if this predicate should display control characters literally */
283 boolean literal_control_chars
;
285 /* True if this predicate didn't originate from the user. */
288 /* The raw text of the argument of this predicate. */
291 /* Information needed by the predicate processor.
292 Next to each member are listed the predicates that use it. */
295 const char *str
; /* fstype [i]lname [i]name [i]path */
296 struct re_pattern_buffer
*regex
; /* regex */
297 struct exec_val exec_vec
; /* exec ok */
298 struct long_val numinfo
; /* gid inum links uid */
299 struct size_val size
; /* size */
300 uid_t uid
; /* user */
301 gid_t gid
; /* group */
302 struct time_val reftime
; /* newer newerXY anewer cnewer mtime atime ctime mmin amin cmin */
303 struct perm_val perm
; /* perm */
304 struct samefile_file_id samefileid
; /* samefile */
305 mode_t type
; /* type */
306 struct format_val printf_vec
; /* printf fprintf fprint ls fls print0 fprint0 print */
309 /* The next predicate in the user input sequence,
310 which represents the order in which the user supplied the
311 predicates on the command line. */
312 struct predicate
*pred_next
;
314 /* The right and left branches from this node in the expression
315 tree, which represents the order in which the nodes should be
317 struct predicate
*pred_left
;
318 struct predicate
*pred_right
;
320 struct predicate_performance_info perf
;
322 const struct parser_table
* parser_entry
;
325 /* find.c, ftsfind.c */
326 boolean
is_fts_enabled(int *ftsoptions
);
327 int get_start_dirfd(void);
328 int get_current_dirfd(void);
330 /* find library function declarations. */
332 /* find global function declarations. */
335 /* SymlinkOption represents the choice of
336 * -P, -L or -P (default) on the command line.
340 SYMLINK_NEVER_DEREF
, /* Option -P */
341 SYMLINK_ALWAYS_DEREF
, /* Option -L */
342 SYMLINK_DEREF_ARGSONLY
/* Option -H */
344 extern enum SymlinkOption symlink_handling
; /* defined in find.c. */
346 void set_follow_state
PARAMS((enum SymlinkOption opt
));
350 char *filesystem_type
PARAMS((const struct stat
*statp
, const char *path
));
351 char * get_mounted_filesystems (void);
352 dev_t
* get_mounted_devices
PARAMS((size_t *));
358 ARG_OPTION
, /* regular options like -maxdepth */
359 ARG_NOOP
, /* does nothing, returns true, internal use only */
360 ARG_POSITIONAL_OPTION
, /* options whose position is important (-follow) */
361 ARG_TEST
, /* a like -name */
362 ARG_SPECIAL_PARSE
, /* complex to parse, don't eat the test name before calling parse_xx(). */
363 ARG_PUNCTUATION
, /* like -o or ( */
364 ARG_ACTION
/* like -print */
369 /* Pointer to a parser function. */
370 typedef boolean (*PARSE_FUNC
)(const struct parser_table
*p
,
371 char *argv
[], int *arg_ptr
);
376 PARSE_FUNC parser_func
;
381 const struct parser_table
* find_parser
PARAMS((char *search_name
));
382 boolean parse_print
PARAMS((const struct parser_table
*, char *argv
[], int *arg_ptr
));
383 void pred_sanity_check
PARAMS((const struct predicate
*predicates
));
384 void parse_begin_user_args
PARAMS((char **args
, int argno
, const struct predicate
*last
, const struct predicate
*predicates
));
385 void parse_end_user_args
PARAMS((char **args
, int argno
, const struct predicate
*last
, const struct predicate
*predicates
));
386 boolean parse_openparen
PARAMS((const struct parser_table
* entry
, char *argv
[], int *arg_ptr
));
387 boolean parse_closeparen
PARAMS((const struct parser_table
* entry
, char *argv
[], int *arg_ptr
));
391 typedef boolean
PREDICATEFUNCTION(const char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
);
392 PREDICATEFUNCTION pred_amin
;
393 PREDICATEFUNCTION pred_and
;
394 PREDICATEFUNCTION pred_anewer
;
395 PREDICATEFUNCTION pred_atime
;
396 PREDICATEFUNCTION pred_closeparen
;
397 PREDICATEFUNCTION pred_cmin
;
398 PREDICATEFUNCTION pred_cnewer
;
399 PREDICATEFUNCTION pred_comma
;
400 PREDICATEFUNCTION pred_ctime
;
401 PREDICATEFUNCTION pred_delete
;
402 PREDICATEFUNCTION pred_empty
;
403 PREDICATEFUNCTION pred_exec
;
404 PREDICATEFUNCTION pred_execdir
;
405 PREDICATEFUNCTION pred_executable
;
406 PREDICATEFUNCTION pred_false
;
407 PREDICATEFUNCTION pred_fls
;
408 PREDICATEFUNCTION pred_fprint
;
409 PREDICATEFUNCTION pred_fprint0
;
410 PREDICATEFUNCTION pred_fprintf
;
411 PREDICATEFUNCTION pred_fstype
;
412 PREDICATEFUNCTION pred_gid
;
413 PREDICATEFUNCTION pred_group
;
414 PREDICATEFUNCTION pred_ilname
;
415 PREDICATEFUNCTION pred_iname
;
416 PREDICATEFUNCTION pred_inum
;
417 PREDICATEFUNCTION pred_ipath
;
418 PREDICATEFUNCTION pred_links
;
419 PREDICATEFUNCTION pred_lname
;
420 PREDICATEFUNCTION pred_ls
;
421 PREDICATEFUNCTION pred_mmin
;
422 PREDICATEFUNCTION pred_mtime
;
423 PREDICATEFUNCTION pred_name
;
424 PREDICATEFUNCTION pred_negate
;
425 PREDICATEFUNCTION pred_newer
;
426 PREDICATEFUNCTION pred_newerXY
;
427 PREDICATEFUNCTION pred_nogroup
;
428 PREDICATEFUNCTION pred_nouser
;
429 PREDICATEFUNCTION pred_ok
;
430 PREDICATEFUNCTION pred_okdir
;
431 PREDICATEFUNCTION pred_openparen
;
432 PREDICATEFUNCTION pred_or
;
433 PREDICATEFUNCTION pred_path
;
434 PREDICATEFUNCTION pred_perm
;
435 PREDICATEFUNCTION pred_print
;
436 PREDICATEFUNCTION pred_print0
;
437 PREDICATEFUNCTION pred_prune
;
438 PREDICATEFUNCTION pred_quit
;
439 PREDICATEFUNCTION pred_readable
;
440 PREDICATEFUNCTION pred_regex
;
441 PREDICATEFUNCTION pred_samefile
;
442 PREDICATEFUNCTION pred_size
;
443 PREDICATEFUNCTION pred_true
;
444 PREDICATEFUNCTION pred_type
;
445 PREDICATEFUNCTION pred_uid
;
446 PREDICATEFUNCTION pred_used
;
447 PREDICATEFUNCTION pred_user
;
448 PREDICATEFUNCTION pred_writable
;
449 PREDICATEFUNCTION pred_xtype
;
453 int launch
PARAMS((const struct buildcmd_control
*ctl
,
454 struct buildcmd_state
*buildstate
));
457 char *find_pred_name
PARAMS((PRED_FUNC pred_func
));
461 void print_predicate
PARAMS((FILE *fp
, const struct predicate
*p
));
462 void print_tree
PARAMS((FILE*, struct predicate
*node
, int indent
));
463 void print_list
PARAMS((FILE*, struct predicate
*node
));
464 void print_optlist
PARAMS((FILE *fp
, const struct predicate
*node
));
465 void show_success_rates(const struct predicate
*node
);
469 struct predicate
* build_expression_tree
PARAMS((int argc
, char *argv
[], int end_of_leading_options
));
470 struct predicate
* get_eval_tree
PARAMS((void));
471 struct predicate
*get_new_pred
PARAMS((const struct parser_table
*entry
));
472 struct predicate
*get_new_pred_chk_op
PARAMS((const struct parser_table
*entry
));
473 float calculate_derived_rates
PARAMS((struct predicate
*p
));
476 struct predicate
*insert_primary
PARAMS((const struct parser_table
*entry
));
477 struct predicate
*insert_primary_withpred
PARAMS((const struct parser_table
*entry
, PRED_FUNC fptr
));
478 void usage
PARAMS((FILE *fp
, int status
, char *msg
));
479 extern boolean
check_nofollow(void);
480 void complete_pending_execs(struct predicate
*p
);
481 void complete_pending_execdirs(int dirfd
); /* Passing dirfd is an unpleasant CodeSmell. */
482 const char *safely_quote_err_filename (int n
, char const *arg
);
483 void fatal_file_error(const char *name
) ATTRIBUTE_NORETURN
;
484 void nonfatal_file_error(const char *name
);
486 int process_leading_options
PARAMS((int argc
, char *argv
[]));
487 void set_option_defaults
PARAMS((struct options
*p
));
490 #define apply_predicate(pathname, stat_buf_ptr, node) \
491 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
493 boolean
apply_predicate(const char *pathname
, struct stat
*stat_buf
, struct predicate
*p
);
496 #define pred_is(node, fn) ( ((node)->pred_func) == (fn) )
500 int get_info
PARAMS((const char *pathname
, struct stat
*p
, struct predicate
*pred_ptr
));
501 int following_links
PARAMS((void));
502 int digest_mode
PARAMS((mode_t mode
, const char *pathname
, const char *name
, struct stat
*pstat
, boolean leaf
));
503 boolean default_prints
PARAMS((struct predicate
*pred
));
504 boolean looks_like_expression
PARAMS((const char *arg
, boolean leading
));
510 DebugExpressionTree
= 1,
516 DebugSuccessRates
= 64
521 /* If true, process directory before contents. True unless -depth given. */
522 boolean do_dir_first
;
524 /* If >=0, don't descend more than this many levels of subdirectories. */
527 /* If >=0, don't process files above this level. */
530 /* If true, do not assume that files in directories with nlink == 2
531 are non-directories. */
532 boolean no_leaf_check
;
534 /* If true, don't cross filesystem boundaries. */
535 boolean stay_on_filesystem
;
537 /* If true, we ignore the problem where we find that a directory entry
538 * no longer exists by the time we get around to processing it.
540 boolean ignore_readdir_race
;
542 /* If true, pass control characters through. If false, escape them
543 * or turn them into harmless things.
545 boolean literal_control_chars
;
547 /* If true, we issue warning messages
551 struct timespec start_time
; /* Time at start of execution. */
553 /* Seconds between 00:00 1/1/70 and either one day before now
554 (the default), or the start of today (if -daystart is given). */
555 time_t cur_day_start
;
557 /* If true, cur_day_start has been adjusted to the start of the day. */
560 int output_block_size
; /* Output block size. */
562 /* bitmask for debug options */
563 unsigned long debug_options
;
565 enum SymlinkOption symlink_handling
;
568 /* Pointer to the function used to stat files. */
569 int (*xstat
) (const char *name
, struct stat
*statbuf
);
572 /* Indicate if we can implement safely_chdir() using the O_NOFOLLOW
575 boolean open_nofollow_available
;
577 /* The variety of regular expression that we support.
578 * The default is POSIX Basic Regular Expressions, but this
579 * can be changed with the positional option, -regextype.
583 /* Optimisation level. One is the default.
585 unsigned short optimisation_level
;
588 /* How should we quote filenames in error messages and so forth?
590 enum quoting_style err_quoting_style
;
592 extern struct options options
;
597 /* Current depth; 0 means current path is a command line arg. */
600 /* If true, we have called stat on the current path. */
603 /* If true, we know the type of the current path. */
605 mode_t type
; /* this is the actual type */
607 /* The file being operated on, relative to the current directory.
608 Used for stat, readlink, remove, and opendir. */
610 /* The directory fd to which rel_pathname is relative. Thsi is relevant
611 * when we're navigating the hierarchy with fts() and using FTS_CWDFD.
615 /* Length of starting path. */
616 int starting_path_length
;
618 /* If true, don't descend past current directory.
619 Can be set by -prune, -maxdepth, and -xdev/-mount. */
620 boolean stop_at_current_level
;
622 /* Status value to return to system. */
625 /* True if there are any execdirs. This saves us a pair of fchdir()
626 * calls for every directory we leave if it is false. This is just
627 * an optimisation. Set to true if you want to be conservative.
629 boolean execdirs_outstanding
;
633 extern struct state state
;
634 extern char const *starting_dir
;
635 extern int starting_desc
;
636 extern char *program_name
;