1/* defs.h -- data types and declarations.
2   Copyright (C) 1990, 91, 92, 93, 94, 2000, 2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3
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 3 of the License, or
7   (at your option) any later version.
8
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.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16*/
17
18
19#ifndef INC_DEFS_H
20#define INC_DEFS_H 1
21
22#include <config.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <stdio.h>
26
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
38
39#include <errno.h>
40#ifndef errno
41extern int errno;
42#endif
43
44#ifdef STDC_HEADERS
45#include <stdlib.h>
46#endif
47
48/* The presence of unistd.h is assumed by gnulib these days, so we
49 * might as well assume it too.
50 */
51#include <unistd.h>
52
53#include <time.h>
54
55#if HAVE_LIMITS_H
56# include <limits.h>
57#endif
58#ifndef CHAR_BIT
59# define CHAR_BIT 8
60#endif
61
62#if HAVE_INTTYPES_H
63# include <inttypes.h>
64#endif
65
66#include "regex.h"
67
68#ifndef S_IFLNK
69#define lstat stat
70#endif
71
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
79
80int lstat PARAMS((const char *__path, struct stat *__statbuf));
81int stat PARAMS((const char *__path, struct stat *__statbuf));
82
83int optionl_stat PARAMS((const char *name, struct stat *p));
84int optionp_stat PARAMS((const char *name, struct stat *p));
85int optionh_stat PARAMS((const char *name, struct stat *p));
86
87int get_statinfo PARAMS((const char *pathname, const char *name, struct stat *p));
88
89
90
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
127
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)
133
134#if 1
135#include <stdbool.h>
136typedef bool boolean;
137#else
138/* Not char because of type promotion; NeXT gcc can't handle it.  */
139typedef int boolean;
140#define		true    1
141#define		false	0
142#endif
143
144struct predicate;
145
146/* Pointer to a predicate function. */
147typedef boolean (*PRED_FUNC)(char *pathname, struct stat *stat_buf, struct predicate *pred_ptr);
148
149/* The number of seconds in a day. */
150#define		DAYSECS	    86400
151
152/* Argument structures for predicates. */
153
154enum comparison_type
155{
156  COMP_GT,
157  COMP_LT,
158  COMP_EQ
159};
160
161enum permissions_type
162{
163  PERM_AT_LEAST,
164  PERM_ANY,
165  PERM_EXACT
166};
167
168enum predicate_type
169{
170  NO_TYPE,
171  PRIMARY_TYPE,
172  UNI_OP,
173  BI_OP,
174  OPEN_PAREN,
175  CLOSE_PAREN
176};
177
178enum predicate_precedence
179{
180  NO_PREC,
181  COMMA_PREC,
182  OR_PREC,
183  AND_PREC,
184  NEGATE_PREC,
185  MAX_PREC
186};
187
188struct long_val
189{
190  enum comparison_type kind;
191  boolean negative;		/* Defined only when representing time_t.  */
192  uintmax_t l_val;
193};
194
195struct perm_val
196{
197  enum permissions_type kind;
198  mode_t val[2];
199};
200
201/* dir_id is used to support loop detection in find.c and
202 * also to support the -samefile test.
203 */
204struct dir_id
205{
206  ino_t ino;
207  dev_t dev;
208};
209
210struct size_val
211{
212  enum comparison_type kind;
213  int blocksize;
214  uintmax_t size;
215};
216
217#define NEW_EXEC 1
218/*
219#undef NEW_EXEC
220*/
221
222#if !defined(NEW_EXEC)
223struct path_arg
224{
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. */
228};
229#endif
230
231#include "buildcmd.h"
232
233struct exec_val
234{
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  boolean close_stdin;		/* If true, close stdin in the child. */
244#else
245  struct path_arg *paths;	/* Array of args with path replacements. */
246  char **vec;			/* Array of args to pass to program. */
247#endif
248};
249
250/* The format string for a -printf or -fprintf is chopped into one or
251   more `struct segment', linked together into a list.
252   Each stretch of plain text is a segment, and
253   each \c and `%' conversion is a segment. */
254
255/* Special values for the `kind' field of `struct segment'. */
256#define KIND_PLAIN 0		/* Segment containing just plain text. */
257#define KIND_STOP 1		/* \c -- stop printing and flush output. */
258
259struct segment
260{
261  int kind;			/* Format chars or KIND_{PLAIN,STOP}. */
262  char *text;			/* Plain text or `%' format string. */
263  int text_len;			/* Length of `text'. */
264  struct segment *next;		/* Next segment for this predicate. */
265};
266
267struct format_val
268{
269  struct segment *segment;	/* Linked list of segments. */
270  FILE *stream;			/* Output stream to print on. */
271  boolean dest_is_tty;		/* True if the destination is a terminal. */
272  struct quoting_options *quote_opts;
273};
274
275struct predicate
276{
277  /* Pointer to the function that implements this predicate.  */
278  PRED_FUNC pred_func;
279
280  /* Only used for debugging, but defined unconditionally so individual
281     modules can be compiled with -DDEBUG.  */
282  char *p_name;
283
284  /* The type of this node.  There are two kinds.  The first is real
285     predicates ("primaries") such as -perm, -print, or -exec.  The
286     other kind is operators for combining predicates. */
287  enum predicate_type p_type;
288
289  /* The precedence of this node.  Only has meaning for operators. */
290  enum predicate_precedence p_prec;
291
292  /* True if this predicate node produces side effects.
293     If side_effects are produced
294     then optimization will not be performed */
295  boolean side_effects;
296
297  /* True if this predicate node requires default print be turned off. */
298  boolean no_default_print;
299
300  /* True if this predicate node requires a stat system call to execute. */
301  boolean need_stat;
302
303  /* True if this predicate node requires knowledge of the file type. */
304  boolean need_type;
305
306  /* Information needed by the predicate processor.
307     Next to each member are listed the predicates that use it. */
308  union
309  {
310    char *str;			/* fstype [i]lname [i]name [i]path */
311    struct re_pattern_buffer *regex; /* regex */
312    struct exec_val exec_vec;	/* exec ok */
313    struct long_val info;	/* atime ctime gid inum links mtime
314                                   size uid */
315    struct size_val size;	/* size */
316    uid_t uid;			/* user */
317    gid_t gid;			/* group */
318    time_t time;		/* newer */
319    struct perm_val perm;	/* perm */
320    struct dir_id   fileid;	/* samefile */
321    mode_t type;		/* type */
322    FILE *stream;		/* ls fls fprint0 */
323    struct format_val printf_vec; /* printf fprintf fprint  */
324  } args;
325
326  /* The next predicate in the user input sequence,
327     which represents the order in which the user supplied the
328     predicates on the command line. */
329  struct predicate *pred_next;
330
331  /* The right and left branches from this node in the expression
332     tree, which represents the order in which the nodes should be
333     processed. */
334  struct predicate *pred_left;
335  struct predicate *pred_right;
336
337  const struct parser_table* parser_entry;
338};
339
340/* find.c. */
341int get_info PARAMS((const char *pathname, const char *name, struct stat *p, struct predicate *pred_ptr));
342int following_links(void);
343
344
345/* find library function declarations.  */
346
347/* dirname.c */
348char *dirname PARAMS((char *path));
349
350/* error.c */
351void error PARAMS((int status, int errnum, char *message, ...));
352
353/* listfile.c */
354void list_file PARAMS((char *name, char *relname, struct stat *statp, time_t current_time, int output_block_size, FILE *stream));
355char *get_link_name PARAMS((char *name, char *relname));
356
357/* stpcpy.c */
358#if !HAVE_STPCPY
359char *stpcpy PARAMS((char *dest, const char *src));
360#endif
361
362/* xgetcwd.c */
363char *xgetcwd PARAMS((void));
364
365/* xmalloc.c */
366#if __STDC__
367#define VOID void
368#else
369#define VOID char
370#endif
371
372/* find global function declarations.  */
373
374/* find.c */
375/* SymlinkOption represents the choice of
376 * -P, -L or -P (default) on the command line.
377 */
378enum SymlinkOption
379  {
380    SYMLINK_NEVER_DEREF,	/* Option -P */
381    SYMLINK_ALWAYS_DEREF,	/* Option -L */
382    SYMLINK_DEREF_ARGSONLY	/* Option -H */
383  };
384extern enum SymlinkOption symlink_handling; /* defined in find.c. */
385
386void set_follow_state PARAMS((enum SymlinkOption opt));
387void cleanup(void);
388
389/* fstype.c */
390char *filesystem_type PARAMS((const struct stat *statp, const char *path));
391char * get_mounted_filesystems (void);
392dev_t * get_mounted_devices PARAMS((size_t *));
393
394
395
396enum arg_type
397  {
398    ARG_OPTION,			/* regular options like -maxdepth */
399    ARG_POSITIONAL_OPTION,	/* options whose position is important (-follow) */
400    ARG_TEST,			/* a like -name */
401    ARG_PUNCTUATION,		/* like -o or ( */
402    ARG_ACTION			/* like -print */
403  };
404
405
406struct parser_table;
407/* Pointer to a parser function. */
408typedef boolean (*PARSE_FUNC)(const struct parser_table *p,
409			      char *argv[], int *arg_ptr);
410struct parser_table
411{
412  enum arg_type type;
413  char *parser_name;
414  PARSE_FUNC parser_func;
415  PRED_FUNC    pred_func;
416};
417
418/* parser.c */
419const struct parser_table* find_parser PARAMS((char *search_name));
420boolean parse_open  PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
421boolean parse_close PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
422boolean parse_print PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
423void pred_sanity_check PARAMS((const struct predicate *predicates));
424void parse_begin_user_args PARAMS((char **args, int argno, const struct predicate *last, const struct predicate *predicates));
425void parse_end_user_args PARAMS((char **args, int argno, const struct predicate *last, const struct predicate *predicates));
426boolean parse_openparen              PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
427boolean parse_closeparen             PARAMS((const struct parser_table* entry, char *argv[], int *arg_ptr));
428
429/* pred.c */
430boolean pred_amin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
431boolean pred_and PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
432boolean pred_anewer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
433boolean pred_atime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
434boolean pred_closeparen PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
435boolean pred_cmin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
436boolean pred_cnewer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
437boolean pred_comma PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
438boolean pred_ctime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
439boolean pred_delete PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
440boolean pred_empty PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
441boolean pred_exec PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
442boolean pred_execdir PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
443boolean pred_false PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
444boolean pred_fls PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
445boolean pred_fprint PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
446boolean pred_fprint0 PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
447boolean pred_fprintf PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
448boolean pred_fstype PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
449boolean pred_gid PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
450boolean pred_group PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
451boolean pred_ilname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
452boolean pred_iname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
453boolean pred_inum PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
454boolean pred_ipath PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
455boolean pred_links PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
456boolean pred_lname PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
457boolean pred_ls PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
458boolean pred_mmin PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
459boolean pred_mtime PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
460boolean pred_name PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
461boolean pred_negate PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
462boolean pred_newer PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
463boolean pred_nogroup PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
464boolean pred_nouser PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
465boolean pred_ok PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
466boolean pred_okdir PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
467boolean pred_openparen PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
468boolean pred_or PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
469boolean pred_path PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
470boolean pred_perm PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
471boolean pred_print PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
472boolean pred_print0 PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
473boolean pred_prune PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
474boolean pred_quit PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
475boolean pred_regex PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
476boolean pred_samefile PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
477boolean pred_size PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
478boolean pred_true PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
479boolean pred_type PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
480boolean pred_uid PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
481boolean pred_used PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
482boolean pred_user PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
483boolean pred_xtype PARAMS((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr));
484
485
486
487int launch PARAMS((const struct buildcmd_control *ctl,
488		   struct buildcmd_state *buildstate));
489
490
491char *find_pred_name PARAMS((PRED_FUNC pred_func));
492
493
494
495#ifdef DEBUG
496void print_tree PARAMS((FILE*, struct predicate *node, int indent));
497void print_list PARAMS((FILE*, struct predicate *node));
498void print_optlist PARAMS((FILE *fp, struct predicate *node));
499#endif /* DEBUG */
500
501/* tree.c */
502struct predicate *
503get_expr PARAMS((struct predicate **input, short int prev_prec));
504boolean opt_expr PARAMS((struct predicate **eval_treep));
505boolean mark_stat PARAMS((struct predicate *tree));
506boolean mark_type PARAMS((struct predicate *tree));
507
508/* util.c */
509struct predicate *get_new_pred PARAMS((const struct parser_table *entry));
510struct predicate *get_new_pred_chk_op PARAMS((const struct parser_table *entry));
511struct predicate *insert_primary PARAMS((const struct parser_table *entry));
512struct predicate *insert_primary_withpred PARAMS((const struct parser_table *entry, PRED_FUNC fptr));
513void usage PARAMS((char *msg));
514
515extern char *program_name;
516extern struct predicate *predicates;
517extern struct predicate *last_pred;
518
519struct options
520{
521  /* If true, process directory before contents.  True unless -depth given. */
522  boolean do_dir_first;
523
524  /* If >=0, don't descend more than this many levels of subdirectories. */
525  int maxdepth;
526
527  /* If >=0, don't process files above this level. */
528  int mindepth;
529
530  /* If true, do not assume that files in directories with nlink == 2
531     are non-directories. */
532  boolean no_leaf_check;
533
534  /* If true, don't cross filesystem boundaries. */
535  boolean stay_on_filesystem;
536
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.
539   */
540  boolean ignore_readdir_race;
541
542/* If true, we issue warning messages
543 */
544  boolean warnings;
545  time_t start_time;		/* Time at start of execution.  */
546
547  /* Seconds between 00:00 1/1/70 and either one day before now
548     (the default), or the start of today (if -daystart is given). */
549  time_t cur_day_start;
550
551  /* If true, cur_day_start has been adjusted to the start of the day. */
552  boolean full_days;
553
554  int output_block_size;	/* Output block size.  */
555
556  enum SymlinkOption symlink_handling;
557
558
559  /* Pointer to the function used to stat files. */
560  int (*xstat) (const char *name, struct stat *statbuf);
561
562
563  /* Indicate if we can implement safely_chdir() using the O_NOFOLLOW
564   * flag to open(2).
565   */
566  boolean open_nofollow_available;
567
568  /* The variety of regular expression that we support.
569   * The default is POSIX Basic Regular Expressions, but this
570   * can be changed with the positional option, -regextype.
571   */
572  int regex_options;
573};
574extern struct options options;
575
576
577struct state
578{
579  /* Current depth; 0 means current path is a command line arg. */
580  int curdepth;
581
582  /* If true, we have called stat on the current path. */
583  boolean have_stat;
584
585  /* If true, we know the type of the current path. */
586  boolean have_type;
587  mode_t type;			/* this is the actual type */
588
589  /* The file being operated on, relative to the current directory.
590     Used for stat, readlink, remove, and opendir.  */
591  char *rel_pathname;
592
593  /* Length of current path. */
594  int path_length;
595
596  /* If true, don't descend past current directory.
597     Can be set by -prune, -maxdepth, and -xdev/-mount. */
598  boolean stop_at_current_level;
599
600  /* Status value to return to system. */
601  int exit_status;
602};
603extern struct state state;
604
605extern char const *starting_dir;
606extern int starting_desc;
607#if ! defined HAVE_FCHDIR && ! defined fchdir
608# define fchdir(fd) (-1)
609#endif
610
611#endif
612