Assume unistd.h is present - avoid using HAVE_UNISTD_H
[findutils.git] / find / defs.h
blobfe2c684627aba1a65e48ccdce1ac9e40bd34c705
1 /* defs.h -- data types and declarations.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2004 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>
24 #include <sys/stat.h>
25 #include <stdio.h>
27 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
28 #include <string.h>
29 #else
30 #include <strings.h>
31 #ifndef strchr
32 #define strchr index
33 #endif
34 #ifndef strrchr
35 #define strrchr rindex
36 #endif
37 #endif
39 #include <errno.h>
40 #ifndef errno
41 extern int errno;
42 #endif
44 #ifdef STDC_HEADERS
45 #include <stdlib.h>
46 #endif
48 /* The presence of unistd.h is assumed by gnulib these days, so we
49 * might as well assume it too.
51 #include <unistd.h>
53 #include <time.h>
55 #if HAVE_LIMITS_H
56 # include <limits.h>
57 #endif
58 #ifndef CHAR_BIT
59 # define CHAR_BIT 8
60 #endif
62 #if HAVE_INTTYPES_H
63 # include <inttypes.h>
64 #endif
66 #include "regex.h"
68 #ifndef S_IFLNK
69 #define lstat stat
70 #endif
72 # ifndef PARAMS
73 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
74 # define PARAMS(Args) Args
75 # else
76 # define PARAMS(Args) ()
77 # endif
78 # endif
80 int lstat PARAMS((const char *__path, struct stat *__statbuf));
81 int stat PARAMS((const char *__path, struct stat *__statbuf));
83 int optionl_stat PARAMS((const char *name, struct stat *p));
84 int optionp_stat PARAMS((const char *name, struct stat *p));
85 int optionh_stat PARAMS((const char *name, struct stat *p));
87 int get_statinfo PARAMS((const char *pathname, const char *name, struct stat *p));
91 #ifndef S_ISUID
92 # define S_ISUID 0004000
93 #endif
94 #ifndef S_ISGID
95 # define S_ISGID 0002000
96 #endif
97 #ifndef S_ISVTX
98 # define S_ISVTX 0001000
99 #endif
100 #ifndef S_IRUSR
101 # define S_IRUSR 0000400
102 #endif
103 #ifndef S_IWUSR
104 # define S_IWUSR 0000200
105 #endif
106 #ifndef S_IXUSR
107 # define S_IXUSR 0000100
108 #endif
109 #ifndef S_IRGRP
110 # define S_IRGRP 0000040
111 #endif
112 #ifndef S_IWGRP
113 # define S_IWGRP 0000020
114 #endif
115 #ifndef S_IXGRP
116 # define S_IXGRP 0000010
117 #endif
118 #ifndef S_IROTH
119 # define S_IROTH 0000004
120 #endif
121 #ifndef S_IWOTH
122 # define S_IWOTH 0000002
123 #endif
124 #ifndef S_IXOTH
125 # define S_IXOTH 0000001
126 #endif
128 #define MODE_WXUSR (S_IWUSR | S_IXUSR)
129 #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
130 #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
131 #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
132 #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
134 #if 1
135 #include <stdbool.h>
136 typedef bool boolean;
137 #else
138 /* Not char because of type promotion; NeXT gcc can't handle it. */
139 typedef int boolean;
140 #define true 1
141 #define false 0
142 #endif
144 struct predicate;
146 /* Pointer to a predicate function. */
147 typedef boolean (*PRED_FUNC)(char *pathname, struct stat *stat_buf, struct predicate *pred_ptr);
149 /* The number of seconds in a day. */
150 #define DAYSECS 86400
152 /* Argument structures for predicates. */
154 enum comparison_type
156 COMP_GT,
157 COMP_LT,
158 COMP_EQ
161 enum permissions_type
163 PERM_AT_LEAST,
164 PERM_ANY,
165 PERM_EXACT
168 enum predicate_type
170 NO_TYPE,
171 PRIMARY_TYPE,
172 UNI_OP,
173 BI_OP,
174 OPEN_PAREN,
175 CLOSE_PAREN
178 enum predicate_precedence
180 NO_PREC,
181 COMMA_PREC,
182 OR_PREC,
183 AND_PREC,
184 NEGATE_PREC,
185 MAX_PREC
188 struct long_val
190 enum comparison_type kind;
191 boolean negative; /* Defined only when representing time_t. */
192 uintmax_t l_val;
195 struct perm_val
197 enum permissions_type kind;
198 mode_t val;
201 /* dir_id is used to support loop detection in find.c and
202 * also to support the -samefile test.
204 struct dir_id
206 ino_t ino;
207 dev_t dev;
210 struct size_val
212 enum comparison_type kind;
213 int blocksize;
214 uintmax_t size;
217 #define NEW_EXEC 1
219 #undef NEW_EXEC
222 #if !defined(NEW_EXEC)
223 struct path_arg
225 short offset; /* Offset in `vec' of this arg. */
226 short count; /* Number of path replacements in this arg. */
227 char *origarg; /* Arg with "{}" intact. */
229 #endif
231 #include "buildcmd.h"
233 struct exec_val
235 #if defined(NEW_EXEC)
236 /* new-style */
237 boolean multiple; /* -exec {} \+ denotes multiple argument. */
238 struct buildcmd_control ctl;
239 struct buildcmd_state state;
240 char **replace_vec; /* Command arguments (for ";" style) */
241 int num_args;
242 boolean use_current_dir; /* If nonzero, don't chdir to start dir */
243 #else
244 struct path_arg *paths; /* Array of args with path replacements. */
245 char **vec; /* Array of args to pass to program. */
246 #endif
249 /* The format string for a -printf or -fprintf is chopped into one or
250 more `struct segment', linked together into a list.
251 Each stretch of plain text is a segment, and
252 each \c and `%' conversion is a segment. */
254 /* Special values for the `kind' field of `struct segment'. */
255 #define KIND_PLAIN 0 /* Segment containing just plain text. */
256 #define KIND_STOP 1 /* \c -- stop printing and flush output. */
258 struct segment
260 int kind; /* Format chars or KIND_{PLAIN,STOP}. */
261 char *text; /* Plain text or `%' format string. */
262 int text_len; /* Length of `text'. */
263 struct segment *next; /* Next segment for this predicate. */
266 struct format_val
268 struct segment *segment; /* Linked list of segments. */
269 FILE *stream; /* Output stream to print on. */
270 boolean dest_is_tty; /* True if the destination is a terminal. */
271 struct quoting_options *quote_opts;
274 struct predicate
276 /* Pointer to the function that implements this predicate. */
277 PRED_FUNC pred_func;
279 /* Only used for debugging, but defined unconditionally so individual
280 modules can be compiled with -DDEBUG. */
281 char *p_name;
283 /* The type of this node. There are two kinds. The first is real
284 predicates ("primaries") such as -perm, -print, or -exec. The
285 other kind is operators for combining predicates. */
286 enum predicate_type p_type;
288 /* The precedence of this node. Only has meaning for operators. */
289 enum predicate_precedence p_prec;
291 /* True if this predicate node produces side effects.
292 If side_effects are produced
293 then optimization will not be performed */
294 boolean side_effects;
296 /* True if this predicate node requires default print be turned off. */
297 boolean no_default_print;
299 /* True if this predicate node requires a stat system call to execute. */
300 boolean need_stat;
302 /* True if this predicate node requires knowledge of the file type. */
303 boolean need_type;
305 /* Information needed by the predicate processor.
306 Next to each member are listed the predicates that use it. */
307 union
309 char *str; /* fstype [i]lname [i]name [i]path */
310 struct re_pattern_buffer *regex; /* regex */
311 struct exec_val exec_vec; /* exec ok */
312 struct long_val info; /* atime ctime gid inum links mtime
313 size uid */
314 struct size_val size; /* size */
315 uid_t uid; /* user */
316 gid_t gid; /* group */
317 time_t time; /* newer */
318 struct perm_val perm; /* perm */
319 struct dir_id fileid; /* samefile */
320 mode_t type; /* type */
321 FILE *stream; /* ls fls fprint0 */
322 struct format_val printf_vec; /* printf fprintf fprint */
323 } args;
325 /* The next predicate in the user input sequence,
326 which represents the order in which the user supplied the
327 predicates on the command line. */
328 struct predicate *pred_next;
330 /* The right and left branches from this node in the expression
331 tree, which represents the order in which the nodes should be
332 processed. */
333 struct predicate *pred_left;
334 struct predicate *pred_right;
336 const struct parser_table* parser_entry;
339 /* find.c. */
340 int get_info PARAMS((const char *pathname, const char *name, struct stat *p, struct predicate *pred_ptr));
341 int following_links(void);
344 /* find library function declarations. */
346 /* dirname.c */
347 char *dirname PARAMS((char *path));
349 /* error.c */
350 void error PARAMS((int status, int errnum, char *message, ...));
352 /* listfile.c */
353 void list_file PARAMS((char *name, char *relname, struct stat *statp, time_t current_time, int output_block_size, FILE *stream));
354 char *get_link_name PARAMS((char *name, char *relname));
356 /* stpcpy.c */
357 #if !HAVE_STPCPY
358 char *stpcpy PARAMS((char *dest, const char *src));
359 #endif
361 /* xgetcwd.c */
362 char *xgetcwd PARAMS((void));
364 /* xmalloc.c */
365 #if __STDC__
366 #define VOID void
367 #else
368 #define VOID char
369 #endif
371 /* find global function declarations. */
373 /* find.c */
374 /* SymlinkOption represents the choice of
375 * -P, -L or -P (default) on the command line.
377 enum SymlinkOption
379 SYMLINK_NEVER_DEREF, /* Option -P */
380 SYMLINK_ALWAYS_DEREF, /* Option -L */
381 SYMLINK_DEREF_ARGSONLY /* Option -H */
383 extern enum SymlinkOption symlink_handling; /* defined in find.c. */
385 void set_follow_state PARAMS((enum SymlinkOption opt));
386 void cleanup(void);
388 /* fstype.c */
389 char *filesystem_type PARAMS((const struct stat *statp));
390 char * get_mounted_filesystems (void);
391 dev_t * get_mounted_devices PARAMS((size_t *));
395 enum arg_type
397 ARG_OPTION, /* regular options like -maxdepth */
398 ARG_POSITIONAL_OPTION, /* options whose position is important (-follow) */
399 ARG_TEST, /* a like -name */
400 ARG_PUNCTUATION, /* like -o or ( */
401 ARG_ACTION /* like -print */
405 struct parser_table;
406 /* Pointer to a parser function. */
407 typedef boolean (*PARSE_FUNC)(const struct parser_table *p,
408 char *argv[], int *arg_ptr);
409 struct parser_table
411 enum arg_type type;
412 char *parser_name;
413 PARSE_FUNC parser_func;
414 PRED_FUNC pred_func;
417 /* parser.c */
418 const struct parser_table* find_parser PARAMS((char *search_name));
419 boolean parse_open PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
420 boolean parse_close PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
421 boolean parse_print PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
422 void pred_sanity_check PARAMS((const struct predicate *predicates));
425 /* pred.c */
426 boolean pred_amin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
427 boolean pred_and PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
428 boolean pred_anewer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
429 boolean pred_atime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
430 boolean pred_close PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
431 boolean pred_cmin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
432 boolean pred_cnewer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
433 boolean pred_comma PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
434 boolean pred_ctime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
435 boolean pred_delete PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
436 boolean pred_empty PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
437 boolean pred_exec PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
438 boolean pred_execdir PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
439 boolean pred_false PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
440 boolean pred_fls PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
441 boolean pred_fprint PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
442 boolean pred_fprint0 PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
443 boolean pred_fprintf PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
444 boolean pred_fstype PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
445 boolean pred_gid PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
446 boolean pred_group PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
447 boolean pred_ilname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
448 boolean pred_iname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
449 boolean pred_inum PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
450 boolean pred_ipath PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
451 boolean pred_links PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
452 boolean pred_lname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
453 boolean pred_ls PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
454 boolean pred_mmin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
455 boolean pred_mtime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
456 boolean pred_name PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
457 boolean pred_negate PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
458 boolean pred_newer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
459 boolean pred_nogroup PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
460 boolean pred_nouser PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
461 boolean pred_ok PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
462 boolean pred_okdir PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
463 boolean pred_open PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
464 boolean pred_or PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
465 boolean pred_path PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
466 boolean pred_perm PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
467 boolean pred_print PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
468 boolean pred_print0 PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
469 boolean pred_prune PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
470 boolean pred_quit PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
471 boolean pred_regex PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
472 boolean pred_samefile PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
473 boolean pred_size PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
474 boolean pred_true PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
475 boolean pred_type PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
476 boolean pred_uid PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
477 boolean pred_used PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
478 boolean pred_user PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
479 boolean pred_xtype PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
483 int launch PARAMS((const struct buildcmd_control *ctl,
484 struct buildcmd_state *buildstate));
487 char *find_pred_name PARAMS((PRED_FUNC pred_func));
491 #ifdef DEBUG
492 void print_tree PARAMS((FILE*, struct predicate *node, int indent));
493 void print_list PARAMS((FILE*, struct predicate *node));
494 void print_optlist PARAMS((FILE *fp, struct predicate *node));
495 #endif /* DEBUG */
497 /* tree.c */
498 struct predicate *
499 get_expr PARAMS((struct predicate **input, short int prev_prec));
500 boolean opt_expr PARAMS((struct predicate **eval_treep));
501 boolean mark_stat PARAMS((struct predicate *tree));
502 boolean mark_type PARAMS((struct predicate *tree));
504 /* util.c */
505 struct predicate *get_new_pred PARAMS((const struct parser_table *entry));
506 struct predicate *get_new_pred_chk_op PARAMS((const struct parser_table *entry));
507 struct predicate *insert_primary PARAMS((const struct parser_table *entry));
508 struct predicate *insert_primary_withpred PARAMS((const struct parser_table *entry, PRED_FUNC fptr));
509 void usage PARAMS((char *msg));
511 extern char *program_name;
512 extern struct predicate *predicates;
513 extern struct predicate *last_pred;
515 struct options
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. */
521 int maxdepth;
523 /* If >=0, don't process files above this level. */
524 int mindepth;
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, we issue warning messages
540 boolean warnings;
541 time_t start_time; /* Time at start of execution. */
543 /* Seconds between 00:00 1/1/70 and either one day before now
544 (the default), or the start of today (if -daystart is given). */
545 time_t cur_day_start;
547 /* If true, cur_day_start has been adjusted to the start of the day. */
548 boolean full_days;
550 int output_block_size; /* Output block size. */
552 enum SymlinkOption symlink_handling;
555 /* Pointer to the function used to stat files. */
556 int (*xstat) (const char *name, struct stat *statbuf);
559 /* Indicate if we can implement safely_chdir() using the O_NOFOLLOW
560 * flag to open(2).
562 boolean open_nofollow_available;
565 /* Which regular expression syntex we currently use. This can be
566 * changed with the positional option -regextype, which takes the
567 * arguments ...
569 * posix-basic, posix-extended, posix-grep, posix-egrep, posix-awk
570 * emacs, gnu-awk.
572 * The default is
574 int regex_options;
576 extern struct options options;
579 struct state
581 /* Current depth; 0 means current path is a command line arg. */
582 int curdepth;
584 /* If true, we have called stat on the current path. */
585 boolean have_stat;
587 /* If true, we know the type of the current path. */
588 boolean have_type;
589 mode_t type; /* this is the actual type */
591 /* The file being operated on, relative to the current directory.
592 Used for stat, readlink, remove, and opendir. */
593 char *rel_pathname;
595 /* Length of current path. */
596 int path_length;
598 /* If true, don't descend past current directory.
599 Can be set by -prune, -maxdepth, and -xdev/-mount. */
600 boolean stop_at_current_level;
602 /* Status value to return to system. */
603 int exit_status;
605 extern struct state state;
607 extern char const *starting_dir;
608 extern int starting_desc;
609 #if ! defined HAVE_FCHDIR && ! defined fchdir
610 # define fchdir(fd) (-1)
611 #endif
613 #endif