1 /* defs.h -- data types and declarations.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #if !defined(ALREADY_INCLUDED_CONFIG_H)
25 * Savannah bug #20128: if we include some system header and it
26 * includes some othersecond system header, the second system header
27 * may in fact turn out to be a file provided by gnulib. For that
28 * situation, we need to have already included <config.h> so that the
29 * Gnulib files have access to the information probed by their
30 * configure script fragments. So <config.h> should be the first
33 #error "<config.h> should be #included before defs.h, and indeed before any other header"
34 Please stop compiling the program now
38 #include <sys/types.h>
40 /* XXX: some of these includes probably don't belong in a common header file */
42 #include <stdio.h> /* for FILE* */
47 #include <limits.h> /* for CHAR_BIT */
48 #include <stdbool.h> /* for bool/boolean */
49 #include <stdint.h> /* for uintmax_t */
50 #include <sys/stat.h> /* S_ISUID etc. */
59 # include <inttypes.h>
68 /* These days we will assume ANSI/ISO C protootypes work on our compiler. */
69 #define PARAMS(Args) Args
71 #ifndef ATTRIBUTE_NORETURN
72 # if HAVE_ATTRIBUTE_NORETURN
73 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
75 # define ATTRIBUTE_NORETURN /* nothing */
79 int optionl_stat
PARAMS((const char *name
, struct stat
*p
));
80 int optionp_stat
PARAMS((const char *name
, struct stat
*p
));
81 int optionh_stat
PARAMS((const char *name
, struct stat
*p
));
82 int debug_stat
PARAMS((const char *file
, struct stat
*bufp
));
84 void set_stat_placeholders
PARAMS((struct stat
*p
));
85 int get_statinfo
PARAMS((const char *pathname
, const char *name
, struct stat
*p
));
88 #define MODE_WXUSR (S_IWUSR | S_IXUSR)
89 #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
90 #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
91 #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
92 #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
98 /* Pointer to a predicate function. */
99 typedef boolean (*PRED_FUNC
)(const char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
);
101 /* The number of seconds in a day. */
102 #define DAYSECS 86400
104 /* Argument structures for predicates. */
113 enum permissions_type
130 enum predicate_precedence
142 enum comparison_type kind
;
143 boolean negative
; /* Defined only when representing time_t. */
149 enum permissions_type kind
;
153 /* dir_id is used to support loop detection in find.c
161 /* samefile_file_id is used to support the -samefile test.
163 struct samefile_file_id
172 enum comparison_type kind
;
180 XVAL_ATIME
, XVAL_BIRTHTIME
, XVAL_CTIME
, XVAL_MTIME
, XVAL_TIME
186 enum comparison_type kind
;
193 boolean multiple
; /* -exec {} \+ denotes multiple argument. */
194 struct buildcmd_control ctl
;
195 struct buildcmd_state state
;
196 char **replace_vec
; /* Command arguments (for ";" style) */
198 boolean use_current_dir
; /* If nonzero, don't chdir to start dir */
199 boolean close_stdin
; /* If true, close stdin in the child. */
200 int dir_fd
; /* The directory to do the exec in. */
203 /* The format string for a -printf or -fprintf is chopped into one or
204 more `struct segment', linked together into a list.
205 Each stretch of plain text is a segment, and
206 each \c and `%' conversion is a segment. */
208 /* Special values for the `kind' field of `struct segment'. */
211 KIND_PLAIN
=0, /* Segment containing just plain text. */
212 KIND_STOP
=1, /* \c -- stop printing and flush output. */
213 KIND_FORMAT
, /* Regular format */
218 enum SegmentKind segkind
; /* KIND_FORMAT, KIND_PLAIN, KIND_STOP */
219 char format_char
[2]; /* Format chars if kind is KIND_FORMAT */
220 char *text
; /* Plain text or `%' format string. */
221 int text_len
; /* Length of `text'. */
222 struct segment
*next
; /* Next segment for this predicate. */
227 struct segment
*segment
; /* Linked list of segments. */
228 FILE *stream
; /* Output stream to print on. */
229 const char *filename
; /* We need the filename for error messages. */
230 boolean dest_is_tty
; /* True if the destination is a terminal. */
231 struct quoting_options
*quote_opts
;
234 /* Profiling information for a predicate */
235 struct predicate_performance_info
237 unsigned long visits
;
238 unsigned long successes
;
241 /* evaluation cost of a predicate */
252 NeedsUserInteraction
,
259 /* Pointer to the function that implements this predicate. */
262 /* Only used for debugging, but defined unconditionally so individual
263 modules can be compiled with -DDEBUG. */
266 /* The type of this node. There are two kinds. The first is real
267 predicates ("primaries") such as -perm, -print, or -exec. The
268 other kind is operators for combining predicates. */
269 enum predicate_type p_type
;
271 /* The precedence of this node. Only has meaning for operators. */
272 enum predicate_precedence p_prec
;
274 /* True if this predicate node produces side effects.
275 If side_effects are produced
276 then optimization will not be performed */
277 boolean side_effects
;
279 /* True if this predicate node requires default print be turned off. */
280 boolean no_default_print
;
282 /* True if this predicate node requires a stat system call to execute. */
285 /* True if this predicate node requires knowledge of the file type. */
288 enum EvaluationCost p_cost
;
290 /* est_success_rate is a number between 0.0 and 1.0 */
291 float est_success_rate
;
293 /* True if this predicate should display control characters literally */
294 boolean literal_control_chars
;
296 /* True if this predicate didn't originate from the user. */
299 /* The raw text of the argument of this predicate. */
302 /* Information needed by the predicate processor.
303 Next to each member are listed the predicates that use it. */
306 const char *str
; /* fstype [i]lname [i]name [i]path */
307 struct re_pattern_buffer
*regex
; /* regex */
308 struct exec_val exec_vec
; /* exec ok */
309 struct long_val numinfo
; /* gid inum links uid */
310 struct size_val size
; /* size */
311 uid_t uid
; /* user */
312 gid_t gid
; /* group */
313 struct time_val reftime
; /* newer newerXY anewer cnewer mtime atime ctime mmin amin cmin */
314 struct perm_val perm
; /* perm */
315 struct samefile_file_id samefileid
; /* samefile */
316 mode_t type
; /* type */
317 struct format_val printf_vec
; /* printf fprintf fprint ls fls print0 fprint0 print */
320 /* The next predicate in the user input sequence,
321 which represents the order in which the user supplied the
322 predicates on the command line. */
323 struct predicate
*pred_next
;
325 /* The right and left branches from this node in the expression
326 tree, which represents the order in which the nodes should be
328 struct predicate
*pred_left
;
329 struct predicate
*pred_right
;
331 struct predicate_performance_info perf
;
333 const struct parser_table
* parser_entry
;
336 /* find.c, ftsfind.c */
337 boolean
is_fts_enabled(int *ftsoptions
);
338 int get_start_dirfd(void);
339 int get_current_dirfd(void);
341 /* find library function declarations. */
343 /* find global function declarations. */
346 /* SymlinkOption represents the choice of
347 * -P, -L or -P (default) on the command line.
351 SYMLINK_NEVER_DEREF
, /* Option -P */
352 SYMLINK_ALWAYS_DEREF
, /* Option -L */
353 SYMLINK_DEREF_ARGSONLY
/* Option -H */
355 extern enum SymlinkOption symlink_handling
; /* defined in find.c. */
357 void set_follow_state
PARAMS((enum SymlinkOption opt
));
361 char *filesystem_type
PARAMS((const struct stat
*statp
, const char *path
));
362 char * get_mounted_filesystems (void);
363 dev_t
* get_mounted_devices
PARAMS((size_t *));
369 ARG_OPTION
, /* regular options like -maxdepth */
370 ARG_NOOP
, /* does nothing, returns true, internal use only */
371 ARG_POSITIONAL_OPTION
, /* options whose position is important (-follow) */
372 ARG_TEST
, /* a like -name */
373 ARG_SPECIAL_PARSE
, /* complex to parse, don't eat the test name before calling parse_xx(). */
374 ARG_PUNCTUATION
, /* like -o or ( */
375 ARG_ACTION
/* like -print */
380 /* Pointer to a parser function. */
381 typedef boolean (*PARSE_FUNC
)(const struct parser_table
*p
,
382 char *argv
[], int *arg_ptr
);
387 PARSE_FUNC parser_func
;
392 const struct parser_table
* find_parser
PARAMS((char *search_name
));
393 boolean parse_print
PARAMS((const struct parser_table
*, char *argv
[], int *arg_ptr
));
394 void pred_sanity_check
PARAMS((const struct predicate
*predicates
));
395 void check_option_combinations (const struct predicate
*p
);
396 void parse_begin_user_args
PARAMS((char **args
, int argno
, const struct predicate
*last
, const struct predicate
*predicates
));
397 void parse_end_user_args
PARAMS((char **args
, int argno
, const struct predicate
*last
, const struct predicate
*predicates
));
398 boolean parse_openparen
PARAMS((const struct parser_table
* entry
, char *argv
[], int *arg_ptr
));
399 boolean parse_closeparen
PARAMS((const struct parser_table
* entry
, char *argv
[], int *arg_ptr
));
403 typedef boolean
PREDICATEFUNCTION(const char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
);
404 PREDICATEFUNCTION pred_amin
;
405 PREDICATEFUNCTION pred_and
;
406 PREDICATEFUNCTION pred_anewer
;
407 PREDICATEFUNCTION pred_atime
;
408 PREDICATEFUNCTION pred_closeparen
;
409 PREDICATEFUNCTION pred_cmin
;
410 PREDICATEFUNCTION pred_cnewer
;
411 PREDICATEFUNCTION pred_comma
;
412 PREDICATEFUNCTION pred_ctime
;
413 PREDICATEFUNCTION pred_delete
;
414 PREDICATEFUNCTION pred_empty
;
415 PREDICATEFUNCTION pred_exec
;
416 PREDICATEFUNCTION pred_execdir
;
417 PREDICATEFUNCTION pred_executable
;
418 PREDICATEFUNCTION pred_false
;
419 PREDICATEFUNCTION pred_fls
;
420 PREDICATEFUNCTION pred_fprint
;
421 PREDICATEFUNCTION pred_fprint0
;
422 PREDICATEFUNCTION pred_fprintf
;
423 PREDICATEFUNCTION pred_fstype
;
424 PREDICATEFUNCTION pred_gid
;
425 PREDICATEFUNCTION pred_group
;
426 PREDICATEFUNCTION pred_ilname
;
427 PREDICATEFUNCTION pred_iname
;
428 PREDICATEFUNCTION pred_inum
;
429 PREDICATEFUNCTION pred_ipath
;
430 PREDICATEFUNCTION pred_links
;
431 PREDICATEFUNCTION pred_lname
;
432 PREDICATEFUNCTION pred_ls
;
433 PREDICATEFUNCTION pred_mmin
;
434 PREDICATEFUNCTION pred_mtime
;
435 PREDICATEFUNCTION pred_name
;
436 PREDICATEFUNCTION pred_negate
;
437 PREDICATEFUNCTION pred_newer
;
438 PREDICATEFUNCTION pred_newerXY
;
439 PREDICATEFUNCTION pred_nogroup
;
440 PREDICATEFUNCTION pred_nouser
;
441 PREDICATEFUNCTION pred_ok
;
442 PREDICATEFUNCTION pred_okdir
;
443 PREDICATEFUNCTION pred_openparen
;
444 PREDICATEFUNCTION pred_or
;
445 PREDICATEFUNCTION pred_path
;
446 PREDICATEFUNCTION pred_perm
;
447 PREDICATEFUNCTION pred_print
;
448 PREDICATEFUNCTION pred_print0
;
449 PREDICATEFUNCTION pred_prune
;
450 PREDICATEFUNCTION pred_quit
;
451 PREDICATEFUNCTION pred_readable
;
452 PREDICATEFUNCTION pred_regex
;
453 PREDICATEFUNCTION pred_samefile
;
454 PREDICATEFUNCTION pred_size
;
455 PREDICATEFUNCTION pred_true
;
456 PREDICATEFUNCTION pred_type
;
457 PREDICATEFUNCTION pred_uid
;
458 PREDICATEFUNCTION pred_used
;
459 PREDICATEFUNCTION pred_user
;
460 PREDICATEFUNCTION pred_writable
;
461 PREDICATEFUNCTION pred_xtype
;
465 int launch
PARAMS((const struct buildcmd_control
*ctl
,
466 struct buildcmd_state
*buildstate
));
469 char *find_pred_name
PARAMS((PRED_FUNC pred_func
));
473 void print_predicate
PARAMS((FILE *fp
, const struct predicate
*p
));
474 void print_tree
PARAMS((FILE*, struct predicate
*node
, int indent
));
475 void print_list
PARAMS((FILE*, struct predicate
*node
));
476 void print_optlist
PARAMS((FILE *fp
, const struct predicate
*node
));
477 void show_success_rates(const struct predicate
*node
);
481 struct predicate
* build_expression_tree
PARAMS((int argc
, char *argv
[], int end_of_leading_options
));
482 struct predicate
* get_eval_tree
PARAMS((void));
483 struct predicate
*get_new_pred
PARAMS((const struct parser_table
*entry
));
484 struct predicate
*get_new_pred_chk_op
PARAMS((const struct parser_table
*entry
));
485 float calculate_derived_rates
PARAMS((struct predicate
*p
));
488 struct predicate
*insert_primary
PARAMS((const struct parser_table
*entry
));
489 struct predicate
*insert_primary_withpred
PARAMS((const struct parser_table
*entry
, PRED_FUNC fptr
));
490 void usage
PARAMS((FILE *fp
, int status
, char *msg
));
491 extern boolean
check_nofollow(void);
492 void complete_pending_execs(struct predicate
*p
);
493 void complete_pending_execdirs(int dir_fd
); /* Passing dir_fd is an unpleasant CodeSmell. */
494 const char *safely_quote_err_filename (int n
, char const *arg
);
495 void fatal_file_error(const char *name
) ATTRIBUTE_NORETURN
;
496 void nonfatal_file_error(const char *name
);
498 int process_leading_options
PARAMS((int argc
, char *argv
[]));
499 void set_option_defaults
PARAMS((struct options
*p
));
502 #define apply_predicate(pathname, stat_buf_ptr, node) \
503 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
505 boolean
apply_predicate(const char *pathname
, struct stat
*stat_buf
, struct predicate
*p
);
508 #define pred_is(node, fn) ( ((node)->pred_func) == (fn) )
512 int get_info
PARAMS((const char *pathname
, struct stat
*p
, struct predicate
*pred_ptr
));
513 int following_links
PARAMS((void));
514 int digest_mode
PARAMS((mode_t mode
, const char *pathname
, const char *name
, struct stat
*pstat
, boolean leaf
));
515 boolean default_prints
PARAMS((struct predicate
*pred
));
516 boolean looks_like_expression
PARAMS((const char *arg
, boolean leading
));
522 DebugExpressionTree
= 1,
528 DebugSuccessRates
= 64
533 /* If true, process directory before contents. True unless -depth given. */
534 boolean do_dir_first
;
535 /* If true, -depth was EXPLICITLY set (as opposed to having been turned
536 * on by -delete, for example).
538 boolean explicit_depth
;
540 /* If >=0, don't descend more than this many levels of subdirectories. */
543 /* If >=0, don't process files above this level. */
546 /* If true, do not assume that files in directories with nlink == 2
547 are non-directories. */
548 boolean no_leaf_check
;
550 /* If true, don't cross filesystem boundaries. */
551 boolean stay_on_filesystem
;
553 /* If true, we ignore the problem where we find that a directory entry
554 * no longer exists by the time we get around to processing it.
556 boolean ignore_readdir_race
;
558 /* If true, pass control characters through. If false, escape them
559 * or turn them into harmless things.
561 boolean literal_control_chars
;
563 /* If true, we issue warning messages
567 /* If true, avoid POSIX-incompatible behaviours
568 * (this functionality is currently incomplete
569 * and at the moment affects mainly warning messages).
571 boolean posixly_correct
;
573 struct timespec start_time
; /* Time at start of execution. */
575 /* Either one day before now (the default), or the start of today (if -daystart is given). */
576 struct timespec cur_day_start
;
578 /* If true, cur_day_start has been adjusted to the start of the day. */
581 int output_block_size
; /* Output block size. */
583 /* bitmask for debug options */
584 unsigned long debug_options
;
586 enum SymlinkOption symlink_handling
;
589 /* Pointer to the function used to stat files. */
590 int (*xstat
) (const char *name
, struct stat
*statbuf
);
593 /* Indicate if we can implement safely_chdir() using the O_NOFOLLOW
596 boolean open_nofollow_available
;
598 /* The variety of regular expression that we support.
599 * The default is POSIX Basic Regular Expressions, but this
600 * can be changed with the positional option, -regextype.
604 /* Optimisation level. One is the default.
606 unsigned short optimisation_level
;
609 /* How should we quote filenames in error messages and so forth?
611 enum quoting_style err_quoting_style
;
613 extern struct options options
;
618 /* Current depth; 0 means current path is a command line arg. */
621 /* If true, we have called stat on the current path. */
624 /* If true, we know the type of the current path. */
626 mode_t type
; /* this is the actual type */
628 /* The file being operated on, relative to the current directory.
629 Used for stat, readlink, remove, and opendir. */
631 /* The directory fd to which rel_pathname is relative. Thsi is relevant
632 * when we're navigating the hierarchy with fts() and using FTS_CWDFD.
636 /* Length of starting path. */
637 int starting_path_length
;
639 /* If true, don't descend past current directory.
640 Can be set by -prune, -maxdepth, and -xdev/-mount. */
641 boolean stop_at_current_level
;
643 /* Status value to return to system. */
646 /* True if there are any execdirs. This saves us a pair of fchdir()
647 * calls for every directory we leave if it is false. This is just
648 * an optimisation. Set to true if you want to be conservative.
650 boolean execdirs_outstanding
;
654 extern struct state state
;
655 extern char const *starting_dir
;
656 extern int starting_desc
;
657 extern char *program_name
;