Fixed savannah bug #11625 Wrong return status for -exec ... \; when command fails...
[findutils.git] / find / defs.h
blob2295a82df9cd52ef4ac2b9219acc4cf820735b46
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
52 #include <time.h>
54 #if HAVE_LIMITS_H
55 # include <limits.h>
56 #endif
57 #ifndef CHAR_BIT
58 # define CHAR_BIT 8
59 #endif
61 #if HAVE_INTTYPES_H
62 # include <inttypes.h>
63 #endif
65 #include "regex.h"
67 #ifndef S_IFLNK
68 #define lstat stat
69 #endif
71 # ifndef PARAMS
72 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
73 # define PARAMS(Args) Args
74 # else
75 # define PARAMS(Args) ()
76 # endif
77 # endif
79 int lstat PARAMS((const char *__path, struct stat *__statbuf));
80 int stat PARAMS((const char *__path, struct stat *__statbuf));
82 int optionl_stat PARAMS((const char *name, struct stat *p));
83 int optionp_stat PARAMS((const char *name, struct stat *p));
84 int optionh_stat PARAMS((const char *name, struct stat *p));
86 #ifndef S_ISUID
87 # define S_ISUID 0004000
88 #endif
89 #ifndef S_ISGID
90 # define S_ISGID 0002000
91 #endif
92 #ifndef S_ISVTX
93 # define S_ISVTX 0001000
94 #endif
95 #ifndef S_IRUSR
96 # define S_IRUSR 0000400
97 #endif
98 #ifndef S_IWUSR
99 # define S_IWUSR 0000200
100 #endif
101 #ifndef S_IXUSR
102 # define S_IXUSR 0000100
103 #endif
104 #ifndef S_IRGRP
105 # define S_IRGRP 0000040
106 #endif
107 #ifndef S_IWGRP
108 # define S_IWGRP 0000020
109 #endif
110 #ifndef S_IXGRP
111 # define S_IXGRP 0000010
112 #endif
113 #ifndef S_IROTH
114 # define S_IROTH 0000004
115 #endif
116 #ifndef S_IWOTH
117 # define S_IWOTH 0000002
118 #endif
119 #ifndef S_IXOTH
120 # define S_IXOTH 0000001
121 #endif
123 #define MODE_WXUSR (S_IWUSR | S_IXUSR)
124 #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
125 #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
126 #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
127 #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
129 #if 1
130 #include <stdbool.h>
131 typedef bool boolean;
132 #else
133 /* Not char because of type promotion; NeXT gcc can't handle it. */
134 typedef int boolean;
135 #define true 1
136 #define false 0
137 #endif
139 /* Pointer to function returning boolean. */
140 typedef boolean (*PFB)();
142 /* The number of seconds in a day. */
143 #define DAYSECS 86400
145 /* Argument structures for predicates. */
147 enum comparison_type
149 COMP_GT,
150 COMP_LT,
151 COMP_EQ
154 enum permissions_type
156 PERM_AT_LEAST,
157 PERM_ANY,
158 PERM_EXACT
161 enum predicate_type
163 NO_TYPE,
164 PRIMARY_TYPE,
165 UNI_OP,
166 BI_OP,
167 OPEN_PAREN,
168 CLOSE_PAREN
171 enum predicate_precedence
173 NO_PREC,
174 COMMA_PREC,
175 OR_PREC,
176 AND_PREC,
177 NEGATE_PREC,
178 MAX_PREC
181 struct long_val
183 enum comparison_type kind;
184 boolean negative; /* Defined only when representing time_t. */
185 uintmax_t l_val;
188 struct perm_val
190 enum permissions_type kind;
191 mode_t val;
194 /* dir_id is used to support loop detection in find.c and
195 * also to support the -samefile test.
197 struct dir_id
199 ino_t ino;
200 dev_t dev;
203 struct size_val
205 enum comparison_type kind;
206 int blocksize;
207 uintmax_t size;
210 #define NEW_EXEC 1
212 #undef NEW_EXEC
215 #if !defined(NEW_EXEC)
216 struct path_arg
218 short offset; /* Offset in `vec' of this arg. */
219 short count; /* Number of path replacements in this arg. */
220 char *origarg; /* Arg with "{}" intact. */
222 #endif
224 #include "buildcmd.h"
226 struct exec_val
228 #if defined(NEW_EXEC)
229 /* new-style */
230 boolean multiple; /* -exec {} \+ denotes multiple argument. */
231 struct buildcmd_control ctl;
232 struct buildcmd_state state;
233 char **replace_vec; /* Command arguments (for ";" style) */
234 int num_args;
235 boolean use_current_dir; /* If nonzero, don't chdir to start dir */
236 #else
237 struct path_arg *paths; /* Array of args with path replacements. */
238 char **vec; /* Array of args to pass to program. */
239 #endif
242 /* The format string for a -printf or -fprintf is chopped into one or
243 more `struct segment', linked together into a list.
244 Each stretch of plain text is a segment, and
245 each \c and `%' conversion is a segment. */
247 /* Special values for the `kind' field of `struct segment'. */
248 #define KIND_PLAIN 0 /* Segment containing just plain text. */
249 #define KIND_STOP 1 /* \c -- stop printing and flush output. */
251 struct segment
253 int kind; /* Format chars or KIND_{PLAIN,STOP}. */
254 char *text; /* Plain text or `%' format string. */
255 int text_len; /* Length of `text'. */
256 struct segment *next; /* Next segment for this predicate. */
259 struct format_val
261 struct segment *segment; /* Linked list of segments. */
262 FILE *stream; /* Output stream to print on. */
265 struct predicate
267 /* Pointer to the function that implements this predicate. */
268 PFB pred_func;
270 /* Only used for debugging, but defined unconditionally so individual
271 modules can be compiled with -DDEBUG. */
272 char *p_name;
274 /* The type of this node. There are two kinds. The first is real
275 predicates ("primaries") such as -perm, -print, or -exec. The
276 other kind is operators for combining predicates. */
277 enum predicate_type p_type;
279 /* The precedence of this node. Only has meaning for operators. */
280 enum predicate_precedence p_prec;
282 /* True if this predicate node produces side effects.
283 If side_effects are produced
284 then optimization will not be performed */
285 boolean side_effects;
287 /* True if this predicate node requires default print be turned off. */
288 boolean no_default_print;
290 /* True if this predicate node requires a stat system call to execute. */
291 boolean need_stat;
295 /* Information needed by the predicate processor.
296 Next to each member are listed the predicates that use it. */
297 union
299 char *str; /* fstype [i]lname [i]name [i]path */
300 struct re_pattern_buffer *regex; /* regex */
301 struct exec_val exec_vec; /* exec ok */
302 struct long_val info; /* atime ctime gid inum links mtime
303 size uid */
304 struct size_val size; /* size */
305 uid_t uid; /* user */
306 gid_t gid; /* group */
307 time_t time; /* newer */
308 struct perm_val perm; /* perm */
309 struct dir_id fileid; /* samefile */
310 mode_t type; /* type */
311 FILE *stream; /* fprint fprint0 */
312 struct format_val printf_vec; /* printf fprintf */
313 } args;
315 /* The next predicate in the user input sequence,
316 which repesents the order in which the user supplied the
317 predicates on the command line. */
318 struct predicate *pred_next;
320 /* The right and left branches from this node in the expression
321 tree, which represents the order in which the nodes should be
322 processed. */
323 struct predicate *pred_left;
324 struct predicate *pred_right;
327 /* find library function declarations. */
329 /* dirname.c */
330 char *dirname PARAMS((char *path));
332 /* error.c */
333 void error PARAMS((int status, int errnum, char *message, ...));
335 /* listfile.c */
336 void list_file PARAMS((char *name, char *relname, struct stat *statp, time_t current_time, int output_block_size, FILE *stream));
337 char *get_link_name PARAMS((char *name, char *relname));
339 /* stpcpy.c */
340 #if !HAVE_STPCPY
341 char *stpcpy PARAMS((char *dest, const char *src));
342 #endif
344 /* xgetcwd.c */
345 char *xgetcwd PARAMS((void));
347 /* xmalloc.c */
348 #if __STDC__
349 #define VOID void
350 #else
351 #define VOID char
352 #endif
354 /* find global function declarations. */
356 /* find.c */
357 /* SymlinkOption represents the choice of
358 * -P, -L or -P (default) on the command line.
360 enum SymlinkOption
362 SYMLINK_NEVER_DEREF, /* Option -P */
363 SYMLINK_ALWAYS_DEREF, /* Option -L */
364 SYMLINK_DEREF_ARGSONLY /* Option -H */
366 extern enum SymlinkOption symlink_handling; /* defined in find.c. */
368 void set_follow_state PARAMS((enum SymlinkOption opt));
369 void cleanup(void);
371 /* fstype.c */
372 char *filesystem_type PARAMS((const struct stat *statp));
373 char * get_mounted_filesystems (void);
374 dev_t * get_mounted_devices PARAMS((size_t *));
376 /* parser.c */
377 PFB find_parser PARAMS((char *search_name));
378 boolean parse_close PARAMS((char *argv[], int *arg_ptr));
379 boolean parse_open PARAMS((char *argv[], int *arg_ptr));
380 boolean parse_print PARAMS((char *argv[], int *arg_ptr));
382 /* pred.c */
383 boolean pred_amin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
384 boolean pred_and PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
385 boolean pred_anewer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
386 boolean pred_atime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
387 boolean pred_close PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
388 boolean pred_cmin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
389 boolean pred_cnewer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
390 boolean pred_comma PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
391 boolean pred_ctime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
392 boolean pred_delete PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
393 boolean pred_empty PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
394 boolean pred_exec PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
395 boolean pred_execdir PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
396 boolean pred_false PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
397 boolean pred_fls PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
398 boolean pred_fprint PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
399 boolean pred_fprint0 PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
400 boolean pred_fprintf PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
401 boolean pred_fstype PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
402 boolean pred_gid PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
403 boolean pred_group PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
404 boolean pred_ilname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
405 boolean pred_iname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
406 boolean pred_inum PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
407 boolean pred_ipath PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
408 boolean pred_links PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
409 boolean pred_lname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
410 boolean pred_ls PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
411 boolean pred_mmin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
412 boolean pred_mtime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
413 boolean pred_name PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
414 boolean pred_negate PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
415 boolean pred_newer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
416 boolean pred_nogroup PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
417 boolean pred_nouser PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
418 boolean pred_ok PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
419 boolean pred_okdir PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
420 boolean pred_open PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
421 boolean pred_or PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
422 boolean pred_path PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
423 boolean pred_perm PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
424 boolean pred_print PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
425 boolean pred_print0 PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
426 boolean pred_prune PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
427 boolean pred_quit PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
428 boolean pred_regex PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
429 boolean pred_samefile PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
430 boolean pred_size PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
431 boolean pred_true PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
432 boolean pred_type PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
433 boolean pred_uid PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
434 boolean pred_used PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
435 boolean pred_user PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
436 boolean pred_xtype PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
438 int launch PARAMS((const struct buildcmd_control *ctl,
439 struct buildcmd_state *buildstate));
442 char *find_pred_name PARAMS((PFB pred_func));
446 #ifdef DEBUG
447 void print_tree PARAMS((FILE*, struct predicate *node, int indent));
448 void print_list PARAMS((FILE*, struct predicate *node));
449 void print_optlist PARAMS((FILE *fp, struct predicate *node));
450 #endif /* DEBUG */
452 /* tree.c */
453 struct predicate *
454 get_expr PARAMS((struct predicate **input, short int prev_prec));
455 boolean opt_expr PARAMS((struct predicate **eval_treep));
456 boolean mark_stat PARAMS((struct predicate *tree));
458 /* util.c */
459 struct predicate *get_new_pred PARAMS((void));
460 struct predicate *get_new_pred_chk_op PARAMS((void));
461 struct predicate *insert_primary PARAMS((boolean (*pred_func )()));
462 void usage PARAMS((char *msg));
464 extern char *program_name;
465 extern struct predicate *predicates;
466 extern struct predicate *last_pred;
468 struct options
470 /* If true, process directory before contents. True unless -depth given. */
471 boolean do_dir_first;
473 /* If >=0, don't descend more than this many levels of subdirectories. */
474 int maxdepth;
476 /* If >=0, don't process files above this level. */
477 int mindepth;
479 /* If true, do not assume that files in directories with nlink == 2
480 are non-directories. */
481 boolean no_leaf_check;
483 /* If true, don't cross filesystem boundaries. */
484 boolean stay_on_filesystem;
486 /* If true, we ignore the problem where we find that a directory entry
487 * no longer exists by the time we get around to processing it.
489 boolean ignore_readdir_race;
491 /* If true, we issue warning messages
493 boolean warnings;
494 time_t start_time; /* Time at start of execution. */
496 /* Seconds between 00:00 1/1/70 and either one day before now
497 (the default), or the start of today (if -daystart is given). */
498 time_t cur_day_start;
500 /* If true, cur_day_start has been adjusted to the start of the day. */
501 boolean full_days;
503 int output_block_size; /* Output block size. */
505 enum SymlinkOption symlink_handling;
508 /* Pointer to the function used to stat files. */
509 int (*xstat) (const char *name, struct stat *statbuf);
511 extern struct options options;
514 struct state
516 /* Current depth; 0 means current path is a command line arg. */
517 int curdepth;
519 /* If true, we have called stat on the current path. */
520 boolean have_stat;
522 /* The file being operated on, relative to the current directory.
523 Used for stat, readlink, remove, and opendir. */
524 char *rel_pathname;
526 /* Length of current path. */
527 int path_length;
529 /* If true, don't descend past current directory.
530 Can be set by -prune, -maxdepth, and -xdev/-mount. */
531 boolean stop_at_current_level;
533 /* Status value to return to system. */
534 int exit_status;
536 extern struct state state;
538 extern char const *starting_dir;
539 extern int starting_desc;
540 #if ! defined HAVE_FCHDIR && ! defined fchdir
541 # define fchdir(fd) (-1)
542 #endif
544 #endif