Do not use __attribute__((__noreturn__)) if it produces a compilation error
[findutils.git] / find / defs.h
blobd70396c87367fdf2e9106ec1e86070df24a1c061
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)
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 #include <config.h>
23 #include <sys/types.h>
25 /* XXX: some of these includes probably don't belong in a common header file */
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <limits.h>
34 #include <stdbool.h>
38 #ifndef errno
39 extern int errno;
40 #endif
42 #ifndef CHAR_BIT
43 # define CHAR_BIT 8
44 #endif
46 #if HAVE_INTTYPES_H
47 # include <inttypes.h>
48 #endif
49 typedef bool boolean;
51 #include "regex.h"
52 #include "timespec.h"
53 #include "buildcmd.h"
54 #include "quotearg.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__))
63 # else
64 # define ATTRIBUTE_NORETURN /* nothing */
65 # endif
66 #endif
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)
84 struct predicate;
85 struct options;
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. */
91 #define DAYSECS 86400
93 /* Argument structures for predicates. */
95 enum comparison_type
97 COMP_GT,
98 COMP_LT,
99 COMP_EQ
102 enum permissions_type
104 PERM_AT_LEAST,
105 PERM_ANY,
106 PERM_EXACT
109 enum predicate_type
111 NO_TYPE,
112 PRIMARY_TYPE,
113 UNI_OP,
114 BI_OP,
115 OPEN_PAREN,
116 CLOSE_PAREN
119 enum predicate_precedence
121 NO_PREC,
122 COMMA_PREC,
123 OR_PREC,
124 AND_PREC,
125 NEGATE_PREC,
126 MAX_PREC
129 struct long_val
131 enum comparison_type kind;
132 boolean negative; /* Defined only when representing time_t. */
133 uintmax_t l_val;
136 struct perm_val
138 enum permissions_type kind;
139 mode_t val[2];
142 /* dir_id is used to support loop detection in find.c
144 struct dir_id
146 ino_t ino;
147 dev_t dev;
150 /* samefile_file_id is used to support the -samefile test.
152 struct samefile_file_id
154 ino_t ino;
155 dev_t dev;
156 int fd;
159 struct size_val
161 enum comparison_type kind;
162 int blocksize;
163 uintmax_t size;
167 enum xval
169 XVAL_ATIME, XVAL_BIRTHTIME, XVAL_CTIME, XVAL_MTIME, XVAL_TIME
172 struct time_val
174 enum xval xval;
175 enum comparison_type kind;
176 struct timespec ts;
180 struct exec_val
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) */
186 int num_args;
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'. */
198 enum SegmentKind
200 KIND_PLAIN=0, /* Segment containing just plain text. */
201 KIND_STOP=1, /* \c -- stop printing and flush output. */
202 KIND_FORMAT, /* Regular format */
205 struct segment
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. */
214 struct format_val
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 */
231 enum EvaluationCost
233 NeedsNothing,
234 NeedsType,
235 NeedsStatInfo,
236 NeedsLinkName,
237 NeedsAccessInfo,
238 NeedsSyncDiskHit,
239 NeedsEventualExec,
240 NeedsImmediateExec,
241 NeedsUserInteraction,
242 NeedsUnknown,
243 NumEvaluationCosts
246 struct predicate
248 /* Pointer to the function that implements this predicate. */
249 PRED_FUNC pred_func;
251 /* Only used for debugging, but defined unconditionally so individual
252 modules can be compiled with -DDEBUG. */
253 char *p_name;
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. */
272 boolean need_stat;
274 /* True if this predicate node requires knowledge of the file type. */
275 boolean need_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. */
286 boolean artificial;
288 /* The raw text of the argument of this predicate. */
289 char *arg_text;
291 /* Information needed by the predicate processor.
292 Next to each member are listed the predicates that use it. */
293 union
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 */
307 } args;
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
316 processed. */
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. */
334 /* find.c */
335 /* SymlinkOption represents the choice of
336 * -P, -L or -P (default) on the command line.
338 enum SymlinkOption
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));
347 void cleanup(void);
349 /* fstype.c */
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 *));
356 enum arg_type
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 */
368 struct parser_table;
369 /* Pointer to a parser function. */
370 typedef boolean (*PARSE_FUNC)(const struct parser_table *p,
371 char *argv[], int *arg_ptr);
372 struct parser_table
374 enum arg_type type;
375 char *parser_name;
376 PARSE_FUNC parser_func;
377 PRED_FUNC pred_func;
380 /* parser.c */
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));
389 /* pred.c */
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);
468 /* tree.c */
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));
475 /* util.c */
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));
489 #if 0
490 #define apply_predicate(pathname, stat_buf_ptr, node) \
491 (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
492 #else
493 boolean apply_predicate(const char *pathname, struct stat *stat_buf, struct predicate *p);
494 #endif
496 #define pred_is(node, fn) ( ((node)->pred_func) == (fn) )
499 /* find.c. */
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));
507 enum DebugOption
509 DebugNone = 0,
510 DebugExpressionTree = 1,
511 DebugStat = 2,
512 DebugSearch = 4,
513 DebugTreeOpt = 8,
514 DebugHelp = 16,
515 DebugExec = 32,
516 DebugSuccessRates = 64
519 struct options
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. */
525 int maxdepth;
527 /* If >=0, don't process files above this level. */
528 int mindepth;
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
549 boolean warnings;
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. */
558 boolean full_days;
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
573 * flag to open(2).
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.
581 int regex_options;
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;
595 struct state
597 /* Current depth; 0 means current path is a command line arg. */
598 int curdepth;
600 /* If true, we have called stat on the current path. */
601 boolean have_stat;
603 /* If true, we know the type of the current path. */
604 boolean have_type;
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. */
609 char *rel_pathname;
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.
613 int cwd_dir_fd;
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. */
623 int exit_status;
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;
632 /* finddata.c */
633 extern struct state state;
634 extern char const *starting_dir;
635 extern int starting_desc;
636 extern char *program_name;
639 #endif