cvsimport
[findutils.git] / find / defs.h
blob1708d839be1777202543c3c3f23818b030711916
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/>.
20 #ifndef INC_DEFS_H
21 #define INC_DEFS_H 1
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
31 * thing included.
33 #error "<config.h> should be #included before defs.h, and indeed before any other header"
34 Please stop compiling the program now
35 #endif
38 #include <sys/types.h>
40 /* XXX: some of these includes probably don't belong in a common header file */
41 #include <sys/stat.h>
42 #include <stdio.h> /* for FILE* */
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <time.h>
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. */
54 #ifndef CHAR_BIT
55 # define CHAR_BIT 8
56 #endif
58 #if HAVE_INTTYPES_H
59 # include <inttypes.h>
60 #endif
61 typedef bool boolean;
63 #include "regex.h"
64 #include "timespec.h"
65 #include "buildcmd.h"
66 #include "quotearg.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__))
74 # else
75 # define ATTRIBUTE_NORETURN /* nothing */
76 # endif
77 #endif
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)
95 struct predicate;
96 struct options;
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. */
106 enum comparison_type
108 COMP_GT,
109 COMP_LT,
110 COMP_EQ
113 enum permissions_type
115 PERM_AT_LEAST,
116 PERM_ANY,
117 PERM_EXACT
120 enum predicate_type
122 NO_TYPE,
123 PRIMARY_TYPE,
124 UNI_OP,
125 BI_OP,
126 OPEN_PAREN,
127 CLOSE_PAREN
130 enum predicate_precedence
132 NO_PREC,
133 COMMA_PREC,
134 OR_PREC,
135 AND_PREC,
136 NEGATE_PREC,
137 MAX_PREC
140 struct long_val
142 enum comparison_type kind;
143 boolean negative; /* Defined only when representing time_t. */
144 uintmax_t l_val;
147 struct perm_val
149 enum permissions_type kind;
150 mode_t val[2];
153 /* dir_id is used to support loop detection in find.c
155 struct dir_id
157 ino_t ino;
158 dev_t dev;
161 /* samefile_file_id is used to support the -samefile test.
163 struct samefile_file_id
165 ino_t ino;
166 dev_t dev;
167 int fd;
170 struct size_val
172 enum comparison_type kind;
173 int blocksize;
174 uintmax_t size;
178 enum xval
180 XVAL_ATIME, XVAL_BIRTHTIME, XVAL_CTIME, XVAL_MTIME, XVAL_TIME
183 struct time_val
185 enum xval xval;
186 enum comparison_type kind;
187 struct timespec ts;
191 struct exec_val
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) */
197 int num_args;
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'. */
209 enum SegmentKind
211 KIND_PLAIN=0, /* Segment containing just plain text. */
212 KIND_STOP=1, /* \c -- stop printing and flush output. */
213 KIND_FORMAT, /* Regular format */
216 struct segment
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. */
225 struct format_val
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 */
242 enum EvaluationCost
244 NeedsNothing,
245 NeedsType,
246 NeedsStatInfo,
247 NeedsLinkName,
248 NeedsAccessInfo,
249 NeedsSyncDiskHit,
250 NeedsEventualExec,
251 NeedsImmediateExec,
252 NeedsUserInteraction,
253 NeedsUnknown,
254 NumEvaluationCosts
257 struct predicate
259 /* Pointer to the function that implements this predicate. */
260 PRED_FUNC pred_func;
262 /* Only used for debugging, but defined unconditionally so individual
263 modules can be compiled with -DDEBUG. */
264 char *p_name;
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. */
283 boolean need_stat;
285 /* True if this predicate node requires knowledge of the file type. */
286 boolean need_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. */
297 boolean artificial;
299 /* The raw text of the argument of this predicate. */
300 char *arg_text;
302 /* Information needed by the predicate processor.
303 Next to each member are listed the predicates that use it. */
304 union
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 */
318 } args;
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
327 processed. */
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. */
345 /* find.c */
346 /* SymlinkOption represents the choice of
347 * -P, -L or -P (default) on the command line.
349 enum SymlinkOption
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));
358 void cleanup(void);
360 /* fstype.c */
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 *));
367 enum arg_type
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 */
379 struct parser_table;
380 /* Pointer to a parser function. */
381 typedef boolean (*PARSE_FUNC)(const struct parser_table *p,
382 char *argv[], int *arg_ptr);
383 struct parser_table
385 enum arg_type type;
386 char *parser_name;
387 PARSE_FUNC parser_func;
388 PRED_FUNC pred_func;
391 /* parser.c */
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));
401 /* pred.c */
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);
480 /* tree.c */
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));
487 /* util.c */
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));
501 #if 0
502 #define apply_predicate(pathname, stat_buf_ptr, node) \
503 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
504 #else
505 boolean apply_predicate(const char *pathname, struct stat *stat_buf, struct predicate *p);
506 #endif
508 #define pred_is(node, fn) ( ((node)->pred_func) == (fn) )
511 /* find.c. */
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));
519 enum DebugOption
521 DebugNone = 0,
522 DebugExpressionTree = 1,
523 DebugStat = 2,
524 DebugSearch = 4,
525 DebugTreeOpt = 8,
526 DebugHelp = 16,
527 DebugExec = 32,
528 DebugSuccessRates = 64
531 struct options
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. */
541 int maxdepth;
543 /* If >=0, don't process files above this level. */
544 int mindepth;
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
565 boolean warnings;
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. */
579 boolean full_days;
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
594 * flag to open(2).
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.
602 int regex_options;
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;
616 struct state
618 /* Current depth; 0 means current path is a command line arg. */
619 int curdepth;
621 /* If true, we have called stat on the current path. */
622 boolean have_stat;
624 /* If true, we know the type of the current path. */
625 boolean have_type;
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. */
630 char *rel_pathname;
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.
634 int cwd_dir_fd;
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. */
644 int exit_status;
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;
653 /* finddata.c */
654 extern struct state state;
655 extern char const *starting_dir;
656 extern int starting_desc;
657 extern char *program_name;
660 #endif