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 */
27 #include <stdio.h> /* for FILE* */
32 #include <limits.h> /* for CHAR_BIT */
33 #include <stdbool.h> /* for bool/boolean */
34 #include <stdint.h> /* for uintmax_t */
35 #include <sys/stat.h> /* S_ISUID etc. */
44 # include <inttypes.h>
53 /* These days we will assume ANSI/ISO C protootypes work on our compiler. */
54 #define PARAMS(Args) Args
56 #ifndef ATTRIBUTE_NORETURN
57 # if HAVE_ATTRIBUTE_NORETURN
58 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
60 # define ATTRIBUTE_NORETURN /* nothing */
64 int optionl_stat
PARAMS((const char *name
, struct stat
*p
));
65 int optionp_stat
PARAMS((const char *name
, struct stat
*p
));
66 int optionh_stat
PARAMS((const char *name
, struct stat
*p
));
67 int debug_stat
PARAMS((const char *file
, struct stat
*bufp
));
69 void set_stat_placeholders
PARAMS((struct stat
*p
));
70 int get_statinfo
PARAMS((const char *pathname
, const char *name
, struct stat
*p
));
73 #define MODE_WXUSR (S_IWUSR | S_IXUSR)
74 #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
75 #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
76 #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
77 #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
83 /* Pointer to a predicate function. */
84 typedef boolean (*PRED_FUNC
)(const char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
);
86 /* The number of seconds in a day. */
89 /* Argument structures for predicates. */
115 enum predicate_precedence
127 enum comparison_type kind
;
128 boolean negative
; /* Defined only when representing time_t. */
134 enum permissions_type kind
;
138 /* dir_id is used to support loop detection in find.c
146 /* samefile_file_id is used to support the -samefile test.
148 struct samefile_file_id
157 enum comparison_type kind
;
165 XVAL_ATIME
, XVAL_BIRTHTIME
, XVAL_CTIME
, XVAL_MTIME
, XVAL_TIME
171 enum comparison_type kind
;
178 boolean multiple
; /* -exec {} \+ denotes multiple argument. */
179 struct buildcmd_control ctl
;
180 struct buildcmd_state state
;
181 char **replace_vec
; /* Command arguments (for ";" style) */
183 boolean use_current_dir
; /* If nonzero, don't chdir to start dir */
184 boolean close_stdin
; /* If true, close stdin in the child. */
185 int dirfd
; /* The directory to do the exec in. */
188 /* The format string for a -printf or -fprintf is chopped into one or
189 more `struct segment', linked together into a list.
190 Each stretch of plain text is a segment, and
191 each \c and `%' conversion is a segment. */
193 /* Special values for the `kind' field of `struct segment'. */
196 KIND_PLAIN
=0, /* Segment containing just plain text. */
197 KIND_STOP
=1, /* \c -- stop printing and flush output. */
198 KIND_FORMAT
, /* Regular format */
203 enum SegmentKind segkind
; /* KIND_FORMAT, KIND_PLAIN, KIND_STOP */
204 char format_char
[2]; /* Format chars if kind is KIND_FORMAT */
205 char *text
; /* Plain text or `%' format string. */
206 int text_len
; /* Length of `text'. */
207 struct segment
*next
; /* Next segment for this predicate. */
212 struct segment
*segment
; /* Linked list of segments. */
213 FILE *stream
; /* Output stream to print on. */
214 const char *filename
; /* We need the filename for error messages. */
215 boolean dest_is_tty
; /* True if the destination is a terminal. */
216 struct quoting_options
*quote_opts
;
219 /* Profiling information for a predicate */
220 struct predicate_performance_info
222 unsigned long visits
;
223 unsigned long successes
;
226 /* evaluation cost of a predicate */
237 NeedsUserInteraction
,
244 /* Pointer to the function that implements this predicate. */
247 /* Only used for debugging, but defined unconditionally so individual
248 modules can be compiled with -DDEBUG. */
251 /* The type of this node. There are two kinds. The first is real
252 predicates ("primaries") such as -perm, -print, or -exec. The
253 other kind is operators for combining predicates. */
254 enum predicate_type p_type
;
256 /* The precedence of this node. Only has meaning for operators. */
257 enum predicate_precedence p_prec
;
259 /* True if this predicate node produces side effects.
260 If side_effects are produced
261 then optimization will not be performed */
262 boolean side_effects
;
264 /* True if this predicate node requires default print be turned off. */
265 boolean no_default_print
;
267 /* True if this predicate node requires a stat system call to execute. */
270 /* True if this predicate node requires knowledge of the file type. */
273 enum EvaluationCost p_cost
;
275 /* est_success_rate is a number between 0.0 and 1.0 */
276 float est_success_rate
;
278 /* True if this predicate should display control characters literally */
279 boolean literal_control_chars
;
281 /* True if this predicate didn't originate from the user. */
284 /* The raw text of the argument of this predicate. */
287 /* Information needed by the predicate processor.
288 Next to each member are listed the predicates that use it. */
291 const char *str
; /* fstype [i]lname [i]name [i]path */
292 struct re_pattern_buffer
*regex
; /* regex */
293 struct exec_val exec_vec
; /* exec ok */
294 struct long_val numinfo
; /* gid inum links uid */
295 struct size_val size
; /* size */
296 uid_t uid
; /* user */
297 gid_t gid
; /* group */
298 struct time_val reftime
; /* newer newerXY anewer cnewer mtime atime ctime mmin amin cmin */
299 struct perm_val perm
; /* perm */
300 struct samefile_file_id samefileid
; /* samefile */
301 mode_t type
; /* type */
302 struct format_val printf_vec
; /* printf fprintf fprint ls fls print0 fprint0 print */
305 /* The next predicate in the user input sequence,
306 which represents the order in which the user supplied the
307 predicates on the command line. */
308 struct predicate
*pred_next
;
310 /* The right and left branches from this node in the expression
311 tree, which represents the order in which the nodes should be
313 struct predicate
*pred_left
;
314 struct predicate
*pred_right
;
316 struct predicate_performance_info perf
;
318 const struct parser_table
* parser_entry
;
321 /* find.c, ftsfind.c */
322 boolean
is_fts_enabled(int *ftsoptions
);
323 int get_start_dirfd(void);
324 int get_current_dirfd(void);
326 /* find library function declarations. */
328 /* find global function declarations. */
331 /* SymlinkOption represents the choice of
332 * -P, -L or -P (default) on the command line.
336 SYMLINK_NEVER_DEREF
, /* Option -P */
337 SYMLINK_ALWAYS_DEREF
, /* Option -L */
338 SYMLINK_DEREF_ARGSONLY
/* Option -H */
340 extern enum SymlinkOption symlink_handling
; /* defined in find.c. */
342 void set_follow_state
PARAMS((enum SymlinkOption opt
));
346 char *filesystem_type
PARAMS((const struct stat
*statp
, const char *path
));
347 char * get_mounted_filesystems (void);
348 dev_t
* get_mounted_devices
PARAMS((size_t *));
354 ARG_OPTION
, /* regular options like -maxdepth */
355 ARG_NOOP
, /* does nothing, returns true, internal use only */
356 ARG_POSITIONAL_OPTION
, /* options whose position is important (-follow) */
357 ARG_TEST
, /* a like -name */
358 ARG_SPECIAL_PARSE
, /* complex to parse, don't eat the test name before calling parse_xx(). */
359 ARG_PUNCTUATION
, /* like -o or ( */
360 ARG_ACTION
/* like -print */
365 /* Pointer to a parser function. */
366 typedef boolean (*PARSE_FUNC
)(const struct parser_table
*p
,
367 char *argv
[], int *arg_ptr
);
372 PARSE_FUNC parser_func
;
377 const struct parser_table
* find_parser
PARAMS((char *search_name
));
378 boolean parse_print
PARAMS((const struct parser_table
*, char *argv
[], int *arg_ptr
));
379 void pred_sanity_check
PARAMS((const struct predicate
*predicates
));
380 void parse_begin_user_args
PARAMS((char **args
, int argno
, const struct predicate
*last
, const struct predicate
*predicates
));
381 void parse_end_user_args
PARAMS((char **args
, int argno
, const struct predicate
*last
, const struct predicate
*predicates
));
382 boolean parse_openparen
PARAMS((const struct parser_table
* entry
, char *argv
[], int *arg_ptr
));
383 boolean parse_closeparen
PARAMS((const struct parser_table
* entry
, char *argv
[], int *arg_ptr
));
387 typedef boolean
PREDICATEFUNCTION(const char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
);
388 PREDICATEFUNCTION pred_amin
;
389 PREDICATEFUNCTION pred_and
;
390 PREDICATEFUNCTION pred_anewer
;
391 PREDICATEFUNCTION pred_atime
;
392 PREDICATEFUNCTION pred_closeparen
;
393 PREDICATEFUNCTION pred_cmin
;
394 PREDICATEFUNCTION pred_cnewer
;
395 PREDICATEFUNCTION pred_comma
;
396 PREDICATEFUNCTION pred_ctime
;
397 PREDICATEFUNCTION pred_delete
;
398 PREDICATEFUNCTION pred_empty
;
399 PREDICATEFUNCTION pred_exec
;
400 PREDICATEFUNCTION pred_execdir
;
401 PREDICATEFUNCTION pred_executable
;
402 PREDICATEFUNCTION pred_false
;
403 PREDICATEFUNCTION pred_fls
;
404 PREDICATEFUNCTION pred_fprint
;
405 PREDICATEFUNCTION pred_fprint0
;
406 PREDICATEFUNCTION pred_fprintf
;
407 PREDICATEFUNCTION pred_fstype
;
408 PREDICATEFUNCTION pred_gid
;
409 PREDICATEFUNCTION pred_group
;
410 PREDICATEFUNCTION pred_ilname
;
411 PREDICATEFUNCTION pred_iname
;
412 PREDICATEFUNCTION pred_inum
;
413 PREDICATEFUNCTION pred_ipath
;
414 PREDICATEFUNCTION pred_links
;
415 PREDICATEFUNCTION pred_lname
;
416 PREDICATEFUNCTION pred_ls
;
417 PREDICATEFUNCTION pred_mmin
;
418 PREDICATEFUNCTION pred_mtime
;
419 PREDICATEFUNCTION pred_name
;
420 PREDICATEFUNCTION pred_negate
;
421 PREDICATEFUNCTION pred_newer
;
422 PREDICATEFUNCTION pred_newerXY
;
423 PREDICATEFUNCTION pred_nogroup
;
424 PREDICATEFUNCTION pred_nouser
;
425 PREDICATEFUNCTION pred_ok
;
426 PREDICATEFUNCTION pred_okdir
;
427 PREDICATEFUNCTION pred_openparen
;
428 PREDICATEFUNCTION pred_or
;
429 PREDICATEFUNCTION pred_path
;
430 PREDICATEFUNCTION pred_perm
;
431 PREDICATEFUNCTION pred_print
;
432 PREDICATEFUNCTION pred_print0
;
433 PREDICATEFUNCTION pred_prune
;
434 PREDICATEFUNCTION pred_quit
;
435 PREDICATEFUNCTION pred_readable
;
436 PREDICATEFUNCTION pred_regex
;
437 PREDICATEFUNCTION pred_samefile
;
438 PREDICATEFUNCTION pred_size
;
439 PREDICATEFUNCTION pred_true
;
440 PREDICATEFUNCTION pred_type
;
441 PREDICATEFUNCTION pred_uid
;
442 PREDICATEFUNCTION pred_used
;
443 PREDICATEFUNCTION pred_user
;
444 PREDICATEFUNCTION pred_writable
;
445 PREDICATEFUNCTION pred_xtype
;
449 int launch
PARAMS((const struct buildcmd_control
*ctl
,
450 struct buildcmd_state
*buildstate
));
453 char *find_pred_name
PARAMS((PRED_FUNC pred_func
));
457 void print_predicate
PARAMS((FILE *fp
, const struct predicate
*p
));
458 void print_tree
PARAMS((FILE*, struct predicate
*node
, int indent
));
459 void print_list
PARAMS((FILE*, struct predicate
*node
));
460 void print_optlist
PARAMS((FILE *fp
, const struct predicate
*node
));
461 void show_success_rates(const struct predicate
*node
);
465 struct predicate
* build_expression_tree
PARAMS((int argc
, char *argv
[], int end_of_leading_options
));
466 struct predicate
* get_eval_tree
PARAMS((void));
467 struct predicate
*get_new_pred
PARAMS((const struct parser_table
*entry
));
468 struct predicate
*get_new_pred_chk_op
PARAMS((const struct parser_table
*entry
));
469 float calculate_derived_rates
PARAMS((struct predicate
*p
));
472 struct predicate
*insert_primary
PARAMS((const struct parser_table
*entry
));
473 struct predicate
*insert_primary_withpred
PARAMS((const struct parser_table
*entry
, PRED_FUNC fptr
));
474 void usage
PARAMS((FILE *fp
, int status
, char *msg
));
475 extern boolean
check_nofollow(void);
476 void complete_pending_execs(struct predicate
*p
);
477 void complete_pending_execdirs(int dirfd
); /* Passing dirfd is an unpleasant CodeSmell. */
478 const char *safely_quote_err_filename (int n
, char const *arg
);
479 void fatal_file_error(const char *name
) ATTRIBUTE_NORETURN
;
480 void nonfatal_file_error(const char *name
);
482 int process_leading_options
PARAMS((int argc
, char *argv
[]));
483 void set_option_defaults
PARAMS((struct options
*p
));
486 #define apply_predicate(pathname, stat_buf_ptr, node) \
487 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
489 boolean
apply_predicate(const char *pathname
, struct stat
*stat_buf
, struct predicate
*p
);
492 #define pred_is(node, fn) ( ((node)->pred_func) == (fn) )
496 int get_info
PARAMS((const char *pathname
, struct stat
*p
, struct predicate
*pred_ptr
));
497 int following_links
PARAMS((void));
498 int digest_mode
PARAMS((mode_t mode
, const char *pathname
, const char *name
, struct stat
*pstat
, boolean leaf
));
499 boolean default_prints
PARAMS((struct predicate
*pred
));
500 boolean looks_like_expression
PARAMS((const char *arg
, boolean leading
));
506 DebugExpressionTree
= 1,
512 DebugSuccessRates
= 64
517 /* If true, process directory before contents. True unless -depth given. */
518 boolean do_dir_first
;
520 /* If >=0, don't descend more than this many levels of subdirectories. */
523 /* If >=0, don't process files above this level. */
526 /* If true, do not assume that files in directories with nlink == 2
527 are non-directories. */
528 boolean no_leaf_check
;
530 /* If true, don't cross filesystem boundaries. */
531 boolean stay_on_filesystem
;
533 /* If true, we ignore the problem where we find that a directory entry
534 * no longer exists by the time we get around to processing it.
536 boolean ignore_readdir_race
;
538 /* If true, pass control characters through. If false, escape them
539 * or turn them into harmless things.
541 boolean literal_control_chars
;
543 /* If true, we issue warning messages
547 struct timespec start_time
; /* Time at start of execution. */
549 /* Seconds between 00:00 1/1/70 and either one day before now
550 (the default), or the start of today (if -daystart is given). */
551 time_t cur_day_start
;
553 /* If true, cur_day_start has been adjusted to the start of the day. */
556 int output_block_size
; /* Output block size. */
558 /* bitmask for debug options */
559 unsigned long debug_options
;
561 enum SymlinkOption symlink_handling
;
564 /* Pointer to the function used to stat files. */
565 int (*xstat
) (const char *name
, struct stat
*statbuf
);
568 /* Indicate if we can implement safely_chdir() using the O_NOFOLLOW
571 boolean open_nofollow_available
;
573 /* The variety of regular expression that we support.
574 * The default is POSIX Basic Regular Expressions, but this
575 * can be changed with the positional option, -regextype.
579 /* Optimisation level. One is the default.
581 unsigned short optimisation_level
;
584 /* How should we quote filenames in error messages and so forth?
586 enum quoting_style err_quoting_style
;
588 extern struct options options
;
593 /* Current depth; 0 means current path is a command line arg. */
596 /* If true, we have called stat on the current path. */
599 /* If true, we know the type of the current path. */
601 mode_t type
; /* this is the actual type */
603 /* The file being operated on, relative to the current directory.
604 Used for stat, readlink, remove, and opendir. */
606 /* The directory fd to which rel_pathname is relative. Thsi is relevant
607 * when we're navigating the hierarchy with fts() and using FTS_CWDFD.
611 /* Length of starting path. */
612 int starting_path_length
;
614 /* If true, don't descend past current directory.
615 Can be set by -prune, -maxdepth, and -xdev/-mount. */
616 boolean stop_at_current_level
;
618 /* Status value to return to system. */
621 /* True if there are any execdirs. This saves us a pair of fchdir()
622 * calls for every directory we leave if it is false. This is just
623 * an optimisation. Set to true if you want to be conservative.
625 boolean execdirs_outstanding
;
629 extern struct state state
;
630 extern char const *starting_dir
;
631 extern int starting_desc
;
632 extern char *program_name
;