find/tree.c (get_pred_cost): Eliminate unused variable
[findutils.git] / find / defs.h
blob9ddf43f050a42ac83996c1d42d5d1c41ecbe9a64
1 /* defs.h -- data types and declarations.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2004, 2005, 2006, 2007 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)
7 any later version.
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,
17 USA.
19 #ifndef INC_DEFS_H
20 #define INC_DEFS_H 1
22 #if !defined(ALREADY_INCLUDED_CONFIG_H)
24 * Savannah bug #20128: if we include some system header and it
25 * includes some othersecond system header, the second system header
26 * may in fact turn out to be a file provided by gnulib. For that
27 * situation, we need to have already included <config.h> so that the
28 * Gnulib files have access to the information probed by their
29 * configure script fragments. So <config.h> should be the first
30 * thing included.
32 #error "<config.h> should be #included before defs.h, and indeed before any other header"
33 Please stop compiling the program now
34 #endif
37 #include <sys/types.h>
39 /* XXX: some of these includes probably don't belong in a common header file */
40 #include <sys/stat.h>
41 #include <stdio.h> /* for FILE* */
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <time.h>
46 #include <limits.h> /* for CHAR_BIT */
47 #include <stdbool.h> /* for bool/boolean */
48 #include <stdint.h> /* for uintmax_t */
49 #include <sys/stat.h> /* S_ISUID etc. */
53 #ifndef CHAR_BIT
54 # define CHAR_BIT 8
55 #endif
57 #if HAVE_INTTYPES_H
58 # include <inttypes.h>
59 #endif
60 typedef bool boolean;
62 #include "regex.h"
63 #include "timespec.h"
64 #include "buildcmd.h"
65 #include "quotearg.h"
67 /* These days we will assume ANSI/ISO C protootypes work on our compiler. */
68 #define PARAMS(Args) Args
70 #ifndef ATTRIBUTE_NORETURN
71 # if HAVE_ATTRIBUTE_NORETURN
72 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
73 # else
74 # define ATTRIBUTE_NORETURN /* nothing */
75 # endif
76 #endif
78 int optionl_stat PARAMS((const char *name, struct stat *p));
79 int optionp_stat PARAMS((const char *name, struct stat *p));
80 int optionh_stat PARAMS((const char *name, struct stat *p));
81 int debug_stat PARAMS((const char *file, struct stat *bufp));
83 void set_stat_placeholders PARAMS((struct stat *p));
84 int get_statinfo PARAMS((const char *pathname, const char *name, struct stat *p));
87 #define MODE_WXUSR (S_IWUSR | S_IXUSR)
88 #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
89 #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
90 #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
91 #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
94 struct predicate;
95 struct options;
97 /* Pointer to a predicate function. */
98 typedef boolean (*PRED_FUNC)(const char *pathname, struct stat *stat_buf, struct predicate *pred_ptr);
100 /* The number of seconds in a day. */
101 #define DAYSECS 86400
103 /* Argument structures for predicates. */
105 enum comparison_type
107 COMP_GT,
108 COMP_LT,
109 COMP_EQ
112 enum permissions_type
114 PERM_AT_LEAST,
115 PERM_ANY,
116 PERM_EXACT
119 enum predicate_type
121 NO_TYPE,
122 PRIMARY_TYPE,
123 UNI_OP,
124 BI_OP,
125 OPEN_PAREN,
126 CLOSE_PAREN
129 enum predicate_precedence
131 NO_PREC,
132 COMMA_PREC,
133 OR_PREC,
134 AND_PREC,
135 NEGATE_PREC,
136 MAX_PREC
139 struct long_val
141 enum comparison_type kind;
142 boolean negative; /* Defined only when representing time_t. */
143 uintmax_t l_val;
146 struct perm_val
148 enum permissions_type kind;
149 mode_t val[2];
152 /* dir_id is used to support loop detection in find.c
154 struct dir_id
156 ino_t ino;
157 dev_t dev;
160 /* samefile_file_id is used to support the -samefile test.
162 struct samefile_file_id
164 ino_t ino;
165 dev_t dev;
166 int fd;
169 struct size_val
171 enum comparison_type kind;
172 int blocksize;
173 uintmax_t size;
177 enum xval
179 XVAL_ATIME, XVAL_BIRTHTIME, XVAL_CTIME, XVAL_MTIME, XVAL_TIME
182 struct time_val
184 enum xval xval;
185 enum comparison_type kind;
186 struct timespec ts;
190 struct exec_val
192 boolean multiple; /* -exec {} \+ denotes multiple argument. */
193 struct buildcmd_control ctl;
194 struct buildcmd_state state;
195 char **replace_vec; /* Command arguments (for ";" style) */
196 int num_args;
197 boolean use_current_dir; /* If nonzero, don't chdir to start dir */
198 boolean close_stdin; /* If true, close stdin in the child. */
199 int dirfd; /* The directory to do the exec in. */
202 /* The format string for a -printf or -fprintf is chopped into one or
203 more `struct segment', linked together into a list.
204 Each stretch of plain text is a segment, and
205 each \c and `%' conversion is a segment. */
207 /* Special values for the `kind' field of `struct segment'. */
208 enum SegmentKind
210 KIND_PLAIN=0, /* Segment containing just plain text. */
211 KIND_STOP=1, /* \c -- stop printing and flush output. */
212 KIND_FORMAT, /* Regular format */
215 struct segment
217 enum SegmentKind segkind; /* KIND_FORMAT, KIND_PLAIN, KIND_STOP */
218 char format_char[2]; /* Format chars if kind is KIND_FORMAT */
219 char *text; /* Plain text or `%' format string. */
220 int text_len; /* Length of `text'. */
221 struct segment *next; /* Next segment for this predicate. */
224 struct format_val
226 struct segment *segment; /* Linked list of segments. */
227 FILE *stream; /* Output stream to print on. */
228 const char *filename; /* We need the filename for error messages. */
229 boolean dest_is_tty; /* True if the destination is a terminal. */
230 struct quoting_options *quote_opts;
233 /* Profiling information for a predicate */
234 struct predicate_performance_info
236 unsigned long visits;
237 unsigned long successes;
240 /* evaluation cost of a predicate */
241 enum EvaluationCost
243 NeedsNothing,
244 NeedsType,
245 NeedsStatInfo,
246 NeedsLinkName,
247 NeedsAccessInfo,
248 NeedsSyncDiskHit,
249 NeedsEventualExec,
250 NeedsImmediateExec,
251 NeedsUserInteraction,
252 NeedsUnknown,
253 NumEvaluationCosts
256 struct predicate
258 /* Pointer to the function that implements this predicate. */
259 PRED_FUNC pred_func;
261 /* Only used for debugging, but defined unconditionally so individual
262 modules can be compiled with -DDEBUG. */
263 char *p_name;
265 /* The type of this node. There are two kinds. The first is real
266 predicates ("primaries") such as -perm, -print, or -exec. The
267 other kind is operators for combining predicates. */
268 enum predicate_type p_type;
270 /* The precedence of this node. Only has meaning for operators. */
271 enum predicate_precedence p_prec;
273 /* True if this predicate node produces side effects.
274 If side_effects are produced
275 then optimization will not be performed */
276 boolean side_effects;
278 /* True if this predicate node requires default print be turned off. */
279 boolean no_default_print;
281 /* True if this predicate node requires a stat system call to execute. */
282 boolean need_stat;
284 /* True if this predicate node requires knowledge of the file type. */
285 boolean need_type;
287 enum EvaluationCost p_cost;
289 /* est_success_rate is a number between 0.0 and 1.0 */
290 float est_success_rate;
292 /* True if this predicate should display control characters literally */
293 boolean literal_control_chars;
295 /* True if this predicate didn't originate from the user. */
296 boolean artificial;
298 /* The raw text of the argument of this predicate. */
299 char *arg_text;
301 /* Information needed by the predicate processor.
302 Next to each member are listed the predicates that use it. */
303 union
305 const char *str; /* fstype [i]lname [i]name [i]path */
306 struct re_pattern_buffer *regex; /* regex */
307 struct exec_val exec_vec; /* exec ok */
308 struct long_val numinfo; /* gid inum links uid */
309 struct size_val size; /* size */
310 uid_t uid; /* user */
311 gid_t gid; /* group */
312 struct time_val reftime; /* newer newerXY anewer cnewer mtime atime ctime mmin amin cmin */
313 struct perm_val perm; /* perm */
314 struct samefile_file_id samefileid; /* samefile */
315 mode_t type; /* type */
316 struct format_val printf_vec; /* printf fprintf fprint ls fls print0 fprint0 print */
317 } args;
319 /* The next predicate in the user input sequence,
320 which represents the order in which the user supplied the
321 predicates on the command line. */
322 struct predicate *pred_next;
324 /* The right and left branches from this node in the expression
325 tree, which represents the order in which the nodes should be
326 processed. */
327 struct predicate *pred_left;
328 struct predicate *pred_right;
330 struct predicate_performance_info perf;
332 const struct parser_table* parser_entry;
335 /* find.c, ftsfind.c */
336 boolean is_fts_enabled(int *ftsoptions);
337 int get_start_dirfd(void);
338 int get_current_dirfd(void);
340 /* find library function declarations. */
342 /* find global function declarations. */
344 /* find.c */
345 /* SymlinkOption represents the choice of
346 * -P, -L or -P (default) on the command line.
348 enum SymlinkOption
350 SYMLINK_NEVER_DEREF, /* Option -P */
351 SYMLINK_ALWAYS_DEREF, /* Option -L */
352 SYMLINK_DEREF_ARGSONLY /* Option -H */
354 extern enum SymlinkOption symlink_handling; /* defined in find.c. */
356 void set_follow_state PARAMS((enum SymlinkOption opt));
357 void cleanup(void);
359 /* fstype.c */
360 char *filesystem_type PARAMS((const struct stat *statp, const char *path));
361 char * get_mounted_filesystems (void);
362 dev_t * get_mounted_devices PARAMS((size_t *));
366 enum arg_type
368 ARG_OPTION, /* regular options like -maxdepth */
369 ARG_NOOP, /* does nothing, returns true, internal use only */
370 ARG_POSITIONAL_OPTION, /* options whose position is important (-follow) */
371 ARG_TEST, /* a like -name */
372 ARG_SPECIAL_PARSE, /* complex to parse, don't eat the test name before calling parse_xx(). */
373 ARG_PUNCTUATION, /* like -o or ( */
374 ARG_ACTION /* like -print */
378 struct parser_table;
379 /* Pointer to a parser function. */
380 typedef boolean (*PARSE_FUNC)(const struct parser_table *p,
381 char *argv[], int *arg_ptr);
382 struct parser_table
384 enum arg_type type;
385 char *parser_name;
386 PARSE_FUNC parser_func;
387 PRED_FUNC pred_func;
390 /* parser.c */
391 const struct parser_table* find_parser PARAMS((char *search_name));
392 boolean parse_print PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
393 void pred_sanity_check PARAMS((const struct predicate *predicates));
394 void parse_begin_user_args PARAMS((char **args, int argno, const struct predicate *last, const struct predicate *predicates));
395 void parse_end_user_args PARAMS((char **args, int argno, const struct predicate *last, const struct predicate *predicates));
396 boolean parse_openparen PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
397 boolean parse_closeparen PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
399 /* pred.c */
401 typedef boolean PREDICATEFUNCTION(const char *pathname, struct stat *stat_buf, struct predicate *pred_ptr);
402 PREDICATEFUNCTION pred_amin;
403 PREDICATEFUNCTION pred_and;
404 PREDICATEFUNCTION pred_anewer;
405 PREDICATEFUNCTION pred_atime;
406 PREDICATEFUNCTION pred_closeparen;
407 PREDICATEFUNCTION pred_cmin;
408 PREDICATEFUNCTION pred_cnewer;
409 PREDICATEFUNCTION pred_comma;
410 PREDICATEFUNCTION pred_ctime;
411 PREDICATEFUNCTION pred_delete;
412 PREDICATEFUNCTION pred_empty;
413 PREDICATEFUNCTION pred_exec;
414 PREDICATEFUNCTION pred_execdir;
415 PREDICATEFUNCTION pred_executable;
416 PREDICATEFUNCTION pred_false;
417 PREDICATEFUNCTION pred_fls;
418 PREDICATEFUNCTION pred_fprint;
419 PREDICATEFUNCTION pred_fprint0;
420 PREDICATEFUNCTION pred_fprintf;
421 PREDICATEFUNCTION pred_fstype;
422 PREDICATEFUNCTION pred_gid;
423 PREDICATEFUNCTION pred_group;
424 PREDICATEFUNCTION pred_ilname;
425 PREDICATEFUNCTION pred_iname;
426 PREDICATEFUNCTION pred_inum;
427 PREDICATEFUNCTION pred_ipath;
428 PREDICATEFUNCTION pred_links;
429 PREDICATEFUNCTION pred_lname;
430 PREDICATEFUNCTION pred_ls;
431 PREDICATEFUNCTION pred_mmin;
432 PREDICATEFUNCTION pred_mtime;
433 PREDICATEFUNCTION pred_name;
434 PREDICATEFUNCTION pred_negate;
435 PREDICATEFUNCTION pred_newer;
436 PREDICATEFUNCTION pred_newerXY;
437 PREDICATEFUNCTION pred_nogroup;
438 PREDICATEFUNCTION pred_nouser;
439 PREDICATEFUNCTION pred_ok;
440 PREDICATEFUNCTION pred_okdir;
441 PREDICATEFUNCTION pred_openparen;
442 PREDICATEFUNCTION pred_or;
443 PREDICATEFUNCTION pred_path;
444 PREDICATEFUNCTION pred_perm;
445 PREDICATEFUNCTION pred_print;
446 PREDICATEFUNCTION pred_print0;
447 PREDICATEFUNCTION pred_prune;
448 PREDICATEFUNCTION pred_quit;
449 PREDICATEFUNCTION pred_readable;
450 PREDICATEFUNCTION pred_regex;
451 PREDICATEFUNCTION pred_samefile;
452 PREDICATEFUNCTION pred_size;
453 PREDICATEFUNCTION pred_true;
454 PREDICATEFUNCTION pred_type;
455 PREDICATEFUNCTION pred_uid;
456 PREDICATEFUNCTION pred_used;
457 PREDICATEFUNCTION pred_user;
458 PREDICATEFUNCTION pred_writable;
459 PREDICATEFUNCTION pred_xtype;
463 int launch PARAMS((const struct buildcmd_control *ctl,
464 struct buildcmd_state *buildstate));
467 char *find_pred_name PARAMS((PRED_FUNC pred_func));
471 void print_predicate PARAMS((FILE *fp, const struct predicate *p));
472 void print_tree PARAMS((FILE*, struct predicate *node, int indent));
473 void print_list PARAMS((FILE*, struct predicate *node));
474 void print_optlist PARAMS((FILE *fp, const struct predicate *node));
475 void show_success_rates(const struct predicate *node);
478 /* tree.c */
479 struct predicate * build_expression_tree PARAMS((int argc, char *argv[], int end_of_leading_options));
480 struct predicate * get_eval_tree PARAMS((void));
481 struct predicate *get_new_pred PARAMS((const struct parser_table *entry));
482 struct predicate *get_new_pred_chk_op PARAMS((const struct parser_table *entry));
483 float calculate_derived_rates PARAMS((struct predicate *p));
485 /* util.c */
486 struct predicate *insert_primary PARAMS((const struct parser_table *entry));
487 struct predicate *insert_primary_withpred PARAMS((const struct parser_table *entry, PRED_FUNC fptr));
488 void usage PARAMS((FILE *fp, int status, char *msg));
489 extern boolean check_nofollow(void);
490 void complete_pending_execs(struct predicate *p);
491 void complete_pending_execdirs(int dirfd); /* Passing dirfd is an unpleasant CodeSmell. */
492 const char *safely_quote_err_filename (int n, char const *arg);
493 void fatal_file_error(const char *name) ATTRIBUTE_NORETURN;
494 void nonfatal_file_error(const char *name);
496 int process_leading_options PARAMS((int argc, char *argv[]));
497 void set_option_defaults PARAMS((struct options *p));
499 #if 0
500 #define apply_predicate(pathname, stat_buf_ptr, node) \
501 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
502 #else
503 boolean apply_predicate(const char *pathname, struct stat *stat_buf, struct predicate *p);
504 #endif
506 #define pred_is(node, fn) ( ((node)->pred_func) == (fn) )
509 /* find.c. */
510 int get_info PARAMS((const char *pathname, struct stat *p, struct predicate *pred_ptr));
511 int following_links PARAMS((void));
512 int digest_mode PARAMS((mode_t mode, const char *pathname, const char *name, struct stat *pstat, boolean leaf));
513 boolean default_prints PARAMS((struct predicate *pred));
514 boolean looks_like_expression PARAMS((const char *arg, boolean leading));
517 enum DebugOption
519 DebugNone = 0,
520 DebugExpressionTree = 1,
521 DebugStat = 2,
522 DebugSearch = 4,
523 DebugTreeOpt = 8,
524 DebugHelp = 16,
525 DebugExec = 32,
526 DebugSuccessRates = 64
529 struct options
531 /* If true, process directory before contents. True unless -depth given. */
532 boolean do_dir_first;
534 /* If >=0, don't descend more than this many levels of subdirectories. */
535 int maxdepth;
537 /* If >=0, don't process files above this level. */
538 int mindepth;
540 /* If true, do not assume that files in directories with nlink == 2
541 are non-directories. */
542 boolean no_leaf_check;
544 /* If true, don't cross filesystem boundaries. */
545 boolean stay_on_filesystem;
547 /* If true, we ignore the problem where we find that a directory entry
548 * no longer exists by the time we get around to processing it.
550 boolean ignore_readdir_race;
552 /* If true, pass control characters through. If false, escape them
553 * or turn them into harmless things.
555 boolean literal_control_chars;
557 /* If true, we issue warning messages
559 boolean warnings;
561 struct timespec start_time; /* Time at start of execution. */
563 /* Seconds between 00:00 1/1/70 and either one day before now
564 (the default), or the start of today (if -daystart is given). */
565 time_t cur_day_start;
567 /* If true, cur_day_start has been adjusted to the start of the day. */
568 boolean full_days;
570 int output_block_size; /* Output block size. */
572 /* bitmask for debug options */
573 unsigned long debug_options;
575 enum SymlinkOption symlink_handling;
578 /* Pointer to the function used to stat files. */
579 int (*xstat) (const char *name, struct stat *statbuf);
582 /* Indicate if we can implement safely_chdir() using the O_NOFOLLOW
583 * flag to open(2).
585 boolean open_nofollow_available;
587 /* The variety of regular expression that we support.
588 * The default is POSIX Basic Regular Expressions, but this
589 * can be changed with the positional option, -regextype.
591 int regex_options;
593 /* Optimisation level. One is the default.
595 unsigned short optimisation_level;
598 /* How should we quote filenames in error messages and so forth?
600 enum quoting_style err_quoting_style;
602 extern struct options options;
605 struct state
607 /* Current depth; 0 means current path is a command line arg. */
608 int curdepth;
610 /* If true, we have called stat on the current path. */
611 boolean have_stat;
613 /* If true, we know the type of the current path. */
614 boolean have_type;
615 mode_t type; /* this is the actual type */
617 /* The file being operated on, relative to the current directory.
618 Used for stat, readlink, remove, and opendir. */
619 char *rel_pathname;
620 /* The directory fd to which rel_pathname is relative. Thsi is relevant
621 * when we're navigating the hierarchy with fts() and using FTS_CWDFD.
623 int cwd_dir_fd;
625 /* Length of starting path. */
626 int starting_path_length;
628 /* If true, don't descend past current directory.
629 Can be set by -prune, -maxdepth, and -xdev/-mount. */
630 boolean stop_at_current_level;
632 /* Status value to return to system. */
633 int exit_status;
635 /* True if there are any execdirs. This saves us a pair of fchdir()
636 * calls for every directory we leave if it is false. This is just
637 * an optimisation. Set to true if you want to be conservative.
639 boolean execdirs_outstanding;
642 /* finddata.c */
643 extern struct state state;
644 extern char const *starting_dir;
645 extern int starting_desc;
646 extern char *program_name;
649 #endif