1 /* pred.c -- execute the expression tree.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 2000, 2003,
3 2004, 2005 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 2, or (at your option)
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, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <sys/types.h>
37 #include "printquoted.h"
44 # define _(Text) gettext (Text)
49 # define N_(String) gettext_noop (String)
51 /* See locate.c for explanation as to why not use (String) */
52 # define N_(String) String
55 #if !defined(SIGCHLD) && defined(SIGCLD)
56 #define SIGCHLD SIGCLD
63 # define NAMLEN(dirent) strlen((dirent)->d_name)
65 # define dirent direct
66 # define NAMLEN(dirent) (dirent)->d_namlen
68 # include <sys/ndir.h>
79 /* Fake a return value. */
80 #define CLOSEDIR(d) (closedir (d), 0)
82 #define CLOSEDIR(d) closedir (d)
88 /* Get or fake the disk device blocksize.
89 Usually defined by sys/param.h (if at all). */
92 # define DEV_BSIZE BSIZE
94 # define DEV_BSIZE 4096
96 #endif /* !DEV_BSIZE */
98 /* Extract or fake data from a `struct stat'.
99 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
100 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
101 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
102 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
103 # define ST_BLKSIZE(statbuf) DEV_BSIZE
104 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
105 # define ST_NBLOCKS(statbuf) \
106 (S_ISREG ((statbuf).st_mode) \
107 || S_ISDIR ((statbuf).st_mode) \
108 ? (statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0) : 0)
109 # else /* !_POSIX_SOURCE && BSIZE */
110 # define ST_NBLOCKS(statbuf) \
111 (S_ISREG ((statbuf).st_mode) \
112 || S_ISDIR ((statbuf).st_mode) \
113 ? st_blocks ((statbuf).st_size) : 0)
114 # endif /* !_POSIX_SOURCE && BSIZE */
115 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
116 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
117 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
118 ? (statbuf).st_blksize : DEV_BSIZE)
119 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
120 /* HP-UX counts st_blocks in 1024-byte units.
121 This loses when mixing HP-UX and BSD filesystems with NFS. */
122 # define ST_NBLOCKSIZE 1024
124 # if defined(_AIX) && defined(_I386)
125 /* AIX PS/2 counts st_blocks in 4K units. */
126 # define ST_NBLOCKSIZE (4 * 1024)
127 # else /* not AIX PS/2 */
129 # define ST_NBLOCKS(statbuf) \
130 (S_ISREG ((statbuf).st_mode) \
131 || S_ISDIR ((statbuf).st_mode) \
132 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
134 # endif /* not AIX PS/2 */
136 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
139 # define ST_NBLOCKS(statbuf) \
140 (S_ISREG ((statbuf).st_mode) \
141 || S_ISDIR ((statbuf).st_mode) \
142 ? (statbuf).st_blocks : 0)
145 #ifndef ST_NBLOCKSIZE
146 # define ST_NBLOCKSIZE 512
151 #define MAX(a, b) ((a) > (b) ? (a) : (b))
153 static boolean insert_lname
PARAMS((char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
, boolean ignore_case
));
155 static char *format_date
PARAMS((time_t when
, int kind
));
156 static char *ctime_format
PARAMS((time_t when
));
165 struct pred_assoc pred_table
[] =
167 {pred_amin
, "amin "},
169 {pred_anewer
, "anewer "},
170 {pred_atime
, "atime "},
172 {pred_amin
, "cmin "},
173 {pred_cnewer
, "cnewer "},
175 {pred_ctime
, "ctime "},
176 {pred_delete
, "delete "},
177 {pred_empty
, "empty "},
178 {pred_exec
, "exec "},
179 {pred_execdir
, "execdir "},
180 {pred_false
, "false "},
181 {pred_fprint
, "fprint "},
182 {pred_fprint0
, "fprint0 "},
183 {pred_fprintf
, "fprintf "},
184 {pred_fstype
, "fstype "},
186 {pred_group
, "group "},
187 {pred_ilname
, "ilname "},
188 {pred_iname
, "iname "},
189 {pred_inum
, "inum "},
190 {pred_ipath
, "ipath "},
191 {pred_links
, "links "},
192 {pred_lname
, "lname "},
194 {pred_amin
, "mmin "},
195 {pred_mtime
, "mtime "},
196 {pred_name
, "name "},
197 {pred_negate
, "not "},
198 {pred_newer
, "newer "},
199 {pred_nogroup
, "nogroup "},
200 {pred_nouser
, "nouser "},
202 {pred_okdir
, "okdir "},
205 {pred_path
, "path "},
206 {pred_perm
, "perm "},
207 {pred_print
, "print "},
208 {pred_print0
, "print0 "},
209 {pred_prune
, "prune "},
210 {pred_regex
, "regex "},
211 {pred_samefile
,"samefile "},
212 {pred_size
, "size "},
213 {pred_true
, "true "},
214 {pred_type
, "type "},
216 {pred_used
, "used "},
217 {pred_user
, "user "},
218 {pred_xtype
, "xtype "},
228 struct op_assoc type_table
[] =
231 {PRIMARY_TYPE
, "primary "},
234 {OPEN_PAREN
, "open_paren "},
235 {CLOSE_PAREN
, "close_paren "},
245 struct prec_assoc prec_table
[] =
248 {COMMA_PREC
, "comma "},
251 {NEGATE_PREC
, "negate "},
257 /* Predicate processing routines.
259 PATHNAME is the full pathname of the file being checked.
260 *STAT_BUF contains information about PATHNAME.
261 *PRED_PTR contains information for applying the predicate.
263 Return true if the file passes this predicate, false if not. */
268 * Returns true if THE_TIME is
269 * COMP_GT: after the specified time
270 * COMP_LT: before the specified time
271 * COMP_EQ: less than WINDOW seconds after the specified time.
274 pred_timewindow(time_t the_time
, struct predicate
const *pred_ptr
, int window
)
276 switch (pred_ptr
->args
.info
.kind
)
279 if (the_time
> (time_t) pred_ptr
->args
.info
.l_val
)
283 if (the_time
< (time_t) pred_ptr
->args
.info
.l_val
)
287 if ((the_time
>= (time_t) pred_ptr
->args
.info
.l_val
)
288 && (the_time
< (time_t) pred_ptr
->args
.info
.l_val
+ window
))
297 pred_amin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
300 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, 60);
304 pred_and (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
306 if (pred_ptr
->pred_left
== NULL
307 || (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
308 pred_ptr
->pred_left
))
310 /* Check whether we need a stat here. */
311 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
313 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
314 pred_ptr
->pred_right
));
321 pred_anewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
325 if (stat_buf
->st_atime
> pred_ptr
->args
.time
)
331 pred_atime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
334 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, DAYSECS
);
338 pred_close (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
348 pred_cmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
351 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, 60);
355 pred_cnewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
359 if (stat_buf
->st_ctime
> pred_ptr
->args
.time
)
366 pred_comma (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
368 if (pred_ptr
->pred_left
!= NULL
)
369 (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
370 pred_ptr
->pred_left
);
371 /* Check whether we need a stat here. */
372 /* TODO: what about need_type? */
373 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
375 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
376 pred_ptr
->pred_right
));
380 pred_ctime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
383 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, DAYSECS
);
387 pred_delete (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
391 if (strcmp (state
.rel_pathname
, "."))
393 if (0 != remove (state
.rel_pathname
))
395 error (0, errno
, "cannot delete %s", pathname
);
409 pred_empty (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
414 if (S_ISDIR (stat_buf
->st_mode
))
418 boolean empty
= true;
421 d
= opendir (state
.rel_pathname
);
424 error (0, errno
, "%s", pathname
);
425 state
.exit_status
= 1;
428 for (dp
= readdir (d
); dp
; dp
= readdir (d
))
430 if (dp
->d_name
[0] != '.'
431 || (dp
->d_name
[1] != '\0'
432 && (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0')))
440 error (0, errno
, "%s", pathname
);
441 state
.exit_status
= 1;
446 else if (S_ISREG (stat_buf
->st_mode
))
447 return (stat_buf
->st_size
== 0);
453 new_impl_pred_exec (const char *pathname
, struct stat
*stat_buf
,
454 struct predicate
*pred_ptr
,
455 const char *prefix
, size_t pfxlen
)
457 struct exec_val
*execp
= &pred_ptr
->args
.exec_vec
;
458 size_t len
= strlen(pathname
);
464 /* Push the argument onto the current list.
465 * The command may or may not be run at this point,
466 * depending on the command line length limits.
468 bc_push_arg(&execp
->ctl
,
474 /* POSIX: If the primary expression is punctuated by a plus
475 * sign, the primary shall always evaluate as true
483 for (i
=0; i
<execp
->num_args
; ++i
)
485 bc_do_insert(&execp
->ctl
,
487 execp
->replace_vec
[i
],
488 strlen(execp
->replace_vec
[i
]),
494 /* Actually invoke the command. */
495 return execp
->ctl
.exec_callback(&execp
->ctl
,
502 pred_exec (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
504 return new_impl_pred_exec(pathname
, stat_buf
, pred_ptr
, NULL
, 0);
508 pred_execdir (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
510 const char *prefix
= (state
.rel_pathname
[0] == '/') ? NULL
: "./";
512 return new_impl_pred_exec (state
.rel_pathname
, stat_buf
, pred_ptr
,
513 prefix
, (prefix
? 2 : 0));
517 pred_false (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
528 pred_fls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
530 list_file (pathname
, state
.rel_pathname
, stat_buf
, options
.start_time
,
531 options
.output_block_size
,
532 pred_ptr
->literal_control_chars
, pred_ptr
->args
.stream
);
537 pred_fprint (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
542 print_quoted(pred_ptr
->args
.printf_vec
.stream
,
543 pred_ptr
->args
.printf_vec
.quote_opts
,
544 pred_ptr
->args
.printf_vec
.dest_is_tty
,
551 pred_fprint0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
556 fputs (pathname
, pred_ptr
->args
.stream
);
557 putc (0, pred_ptr
->args
.stream
);
564 mode_to_filetype(mode_t m
)
567 m
== S_IFSOCK
? "s" :
574 m
== S_IFDOOR
? "D" :
576 m
== S_IFIFO
? "p" : "U";
581 pred_fprintf (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
583 FILE *fp
= pred_ptr
->args
.printf_vec
.stream
;
584 const struct quoting_options
*qopts
= pred_ptr
->args
.printf_vec
.quote_opts
;
585 boolean ttyflag
= pred_ptr
->args
.printf_vec
.dest_is_tty
;
586 struct segment
*segment
;
588 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
590 for (segment
= pred_ptr
->args
.printf_vec
.segment
; segment
;
591 segment
= segment
->next
)
593 if (segment
->kind
& 0xff00) /* Component of date. */
597 switch (segment
->kind
& 0xff)
600 t
= stat_buf
->st_atime
;
603 t
= stat_buf
->st_ctime
;
606 t
= stat_buf
->st_mtime
;
611 /* We trust the output of format_date not to contain
612 * nasty characters, though the value of the date
613 * is itself untrusted data.
616 fprintf (fp
, segment
->text
,
617 format_date (t
, (segment
->kind
>> 8) & 0xff));
621 switch (segment
->kind
)
623 case KIND_PLAIN
: /* Plain text string (no % conversion). */
625 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
627 case KIND_STOP
: /* Terminate argument and flush output. */
629 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
632 case 'a': /* atime in `ctime' format. */
633 /* UNTRUSTED, probably unexploitable */
634 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_atime
));
636 case 'b': /* size in 512-byte blocks */
637 /* UNTRUSTED, probably unexploitable */
638 fprintf (fp
, segment
->text
,
639 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
641 ST_NBLOCKSIZE
, 512));
643 case 'c': /* ctime in `ctime' format */
644 /* UNTRUSTED, probably unexploitable */
645 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_ctime
));
647 case 'd': /* depth in search tree */
648 /* UNTRUSTED, probably unexploitable */
649 fprintf (fp
, segment
->text
, state
.curdepth
);
651 case 'D': /* Device on which file exists (stat.st_dev) */
653 fprintf (fp
, segment
->text
,
654 human_readable ((uintmax_t) stat_buf
->st_dev
, hbuf
,
655 human_ceiling
, 1, 1));
657 case 'f': /* base name of path */
659 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, base_name (pathname
));
661 case 'F': /* filesystem type */
663 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, filesystem_type (stat_buf
, pathname
));
665 case 'g': /* group name */
667 /* (well, the actual group is selected by the user but
668 * its name was selected by the system administrator)
673 g
= getgrgid (stat_buf
->st_gid
);
676 segment
->text
[segment
->text_len
] = 's';
677 fprintf (fp
, segment
->text
, g
->gr_name
);
682 case 'G': /* GID number */
683 /* UNTRUSTED, probably unexploitable */
684 fprintf (fp
, segment
->text
,
685 human_readable ((uintmax_t) stat_buf
->st_gid
, hbuf
,
686 human_ceiling
, 1, 1));
688 case 'h': /* leading directories part of path */
693 cp
= strrchr (pathname
, '/');
694 if (cp
== NULL
) /* No leading directories. */
696 /* If there is no slash in the pathname, we still
697 * print the string because it contains characters
698 * other than just '%s'. The %h expands to ".".
700 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, ".");
706 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, pathname
);
711 case 'H': /* ARGV element file was found under */
714 char cc
= pathname
[state
.starting_path_length
];
716 pathname
[state
.starting_path_length
] = '\0';
717 fprintf (fp
, segment
->text
, pathname
);
718 pathname
[state
.starting_path_length
] = cc
;
721 case 'i': /* inode number */
722 /* UNTRUSTED, but not exploitable I think */
723 fprintf (fp
, segment
->text
,
724 human_readable ((uintmax_t) stat_buf
->st_ino
, hbuf
,
728 case 'k': /* size in 1K blocks */
729 /* UNTRUSTED, but not exploitable I think */
730 fprintf (fp
, segment
->text
,
731 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
733 ST_NBLOCKSIZE
, 1024));
735 case 'l': /* object of symlink */
741 if (S_ISLNK (stat_buf
->st_mode
))
743 linkname
= get_link_name (pathname
, state
.rel_pathname
);
745 state
.exit_status
= 1;
749 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, linkname
);
753 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, "");
758 case 'M': /* mode as 10 chars (eg., "-rwxr-x--x" */
759 /* UNTRUSTED, probably unexploitable */
761 char modestring
[16] ;
762 mode_string (stat_buf
->st_mode
, modestring
);
763 modestring
[10] = '\0';
764 fprintf (fp
, segment
->text
, modestring
);
768 case 'm': /* mode as octal number (perms only) */
769 /* UNTRUSTED, probably unexploitable */
771 /* Output the mode portably using the traditional numbers,
772 even if the host unwisely uses some other numbering
773 scheme. But help the compiler in the common case where
774 the host uses the traditional numbering scheme. */
775 mode_t m
= stat_buf
->st_mode
;
776 boolean traditional_numbering_scheme
=
777 (S_ISUID
== 04000 && S_ISGID
== 02000 && S_ISVTX
== 01000
778 && S_IRUSR
== 00400 && S_IWUSR
== 00200 && S_IXUSR
== 00100
779 && S_IRGRP
== 00040 && S_IWGRP
== 00020 && S_IXGRP
== 00010
780 && S_IROTH
== 00004 && S_IWOTH
== 00002 && S_IXOTH
== 00001);
781 fprintf (fp
, segment
->text
,
782 (traditional_numbering_scheme
784 : ((m
& S_ISUID
? 04000 : 0)
785 | (m
& S_ISGID
? 02000 : 0)
786 | (m
& S_ISVTX
? 01000 : 0)
787 | (m
& S_IRUSR
? 00400 : 0)
788 | (m
& S_IWUSR
? 00200 : 0)
789 | (m
& S_IXUSR
? 00100 : 0)
790 | (m
& S_IRGRP
? 00040 : 0)
791 | (m
& S_IWGRP
? 00020 : 0)
792 | (m
& S_IXGRP
? 00010 : 0)
793 | (m
& S_IROTH
? 00004 : 0)
794 | (m
& S_IWOTH
? 00002 : 0)
795 | (m
& S_IXOTH
? 00001 : 0))));
799 case 'n': /* number of links */
800 /* UNTRUSTED, probably unexploitable */
801 fprintf (fp
, segment
->text
,
802 human_readable ((uintmax_t) stat_buf
->st_nlink
,
807 case 'p': /* pathname */
809 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, pathname
);
811 case 'P': /* pathname with ARGV element stripped */
813 if (state
.curdepth
> 0)
815 cp
= pathname
+ state
.starting_path_length
;
817 /* Move past the slash between the ARGV element
818 and the rest of the pathname. But if the ARGV element
819 ends in a slash, we didn't add another, so we've
820 already skipped past it. */
825 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, cp
);
827 case 's': /* size in bytes */
828 /* UNTRUSTED, probably unexploitable */
829 fprintf (fp
, segment
->text
,
830 human_readable ((uintmax_t) stat_buf
->st_size
,
831 hbuf
, human_ceiling
, 1, 1));
833 case 't': /* mtime in `ctime' format */
834 /* UNTRUSTED, probably unexploitable */
835 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_mtime
));
837 case 'u': /* user name */
839 /* (well, the actual user is selected by the user on systems
840 * where chown is not restricted, but the user name was
841 * selected by the system administrator)
846 p
= getpwuid (stat_buf
->st_uid
);
849 segment
->text
[segment
->text_len
] = 's';
850 fprintf (fp
, segment
->text
, p
->pw_name
);
856 case 'U': /* UID number */
857 /* UNTRUSTED, probably unexploitable */
858 fprintf (fp
, segment
->text
,
859 human_readable ((uintmax_t) stat_buf
->st_uid
, hbuf
,
860 human_ceiling
, 1, 1));
863 /* type of filesystem entry like `ls -l`: (d,-,l,s,p,b,c,n) n=nonexistent(symlink) */
864 case 'Y': /* in case of symlink */
868 if (S_ISLNK (stat_buf
->st_mode
))
871 /* If we would normally follow links, do not do so.
872 * If we would normally not follow links, do so.
874 if ((following_links() ? lstat
: stat
)
875 (state
.rel_pathname
, &sbuf
) != 0)
877 if ( errno
== ENOENT
) {
878 fprintf (fp
, segment
->text
, "N");
881 if ( errno
== ELOOP
) {
882 fprintf (fp
, segment
->text
, "L");
885 error (0, errno
, "%s", pathname
);
889 fprintf (fp
, segment
->text
,
890 mode_to_filetype(sbuf
.st_mode
& S_IFMT
));
895 fprintf (fp
, segment
->text
,
896 mode_to_filetype(stat_buf
->st_mode
& S_IFMT
));
904 fprintf (fp
, segment
->text
,
905 mode_to_filetype(stat_buf
->st_mode
& S_IFMT
));
914 pred_fstype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
918 if (strcmp (filesystem_type (stat_buf
, pathname
), pred_ptr
->args
.str
) == 0)
925 pred_gid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
929 switch (pred_ptr
->args
.info
.kind
)
932 if (stat_buf
->st_gid
> pred_ptr
->args
.info
.l_val
)
936 if (stat_buf
->st_gid
< pred_ptr
->args
.info
.l_val
)
940 if (stat_buf
->st_gid
== pred_ptr
->args
.info
.l_val
)
948 pred_group (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
952 if (pred_ptr
->args
.gid
== stat_buf
->st_gid
)
959 pred_ilname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
961 return insert_lname (pathname
, stat_buf
, pred_ptr
, true);
965 pred_iname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
971 /* FNM_PERIOD is not used here because POSIX requires that it not be.
972 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
974 base
= base_name (pathname
);
975 if (fnmatch (pred_ptr
->args
.str
, base
, FNM_CASEFOLD
) == 0)
981 pred_inum (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
985 switch (pred_ptr
->args
.info
.kind
)
988 if (stat_buf
->st_ino
> pred_ptr
->args
.info
.l_val
)
992 if (stat_buf
->st_ino
< pred_ptr
->args
.info
.l_val
)
996 if (stat_buf
->st_ino
== pred_ptr
->args
.info
.l_val
)
1004 pred_ipath (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1008 if (fnmatch (pred_ptr
->args
.str
, pathname
, FNM_CASEFOLD
) == 0)
1014 pred_links (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1018 switch (pred_ptr
->args
.info
.kind
)
1021 if (stat_buf
->st_nlink
> pred_ptr
->args
.info
.l_val
)
1025 if (stat_buf
->st_nlink
< pred_ptr
->args
.info
.l_val
)
1029 if (stat_buf
->st_nlink
== pred_ptr
->args
.info
.l_val
)
1037 pred_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1039 return insert_lname (pathname
, stat_buf
, pred_ptr
, false);
1043 insert_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
, boolean ignore_case
)
1045 boolean ret
= false;
1047 if (S_ISLNK (stat_buf
->st_mode
))
1049 char *linkname
= get_link_name (pathname
, state
.rel_pathname
);
1052 if (fnmatch (pred_ptr
->args
.str
, linkname
,
1053 ignore_case
? FNM_CASEFOLD
: 0) == 0)
1058 #endif /* S_ISLNK */
1063 pred_ls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1065 list_file (pathname
, state
.rel_pathname
, stat_buf
, options
.start_time
,
1066 options
.output_block_size
,
1067 pred_ptr
->literal_control_chars
,
1073 pred_mmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1076 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, 60);
1080 pred_mtime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1083 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, DAYSECS
);
1087 pred_name (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1092 base
= base_name (pathname
);
1094 /* FNM_PERIOD is not used here because POSIX requires that it not be.
1095 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
1097 if (fnmatch (pred_ptr
->args
.str
, base
, 0) == 0)
1103 pred_negate (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1105 /* Check whether we need a stat here. */
1106 /* TODO: what about need_type? */
1107 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
1109 return (!(*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1110 pred_ptr
->pred_right
));
1114 pred_newer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1118 if (stat_buf
->st_mtime
> pred_ptr
->args
.time
)
1124 pred_nogroup (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1130 extern char *gid_unused
;
1132 return gid_unused
[(unsigned) stat_buf
->st_gid
];
1134 return getgrgid (stat_buf
->st_gid
) == NULL
;
1139 pred_nouser (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1142 extern char *uid_unused
;
1149 return uid_unused
[(unsigned) stat_buf
->st_uid
];
1151 return getpwuid (stat_buf
->st_uid
) == NULL
;
1157 is_ok(const char *program
, const char *arg
)
1160 /* The draft open standard requires that, in the POSIX locale,
1161 the last non-blank character of this prompt be '?'.
1162 The exact format is not specified.
1163 This standard does not have requirements for locales other than POSIX
1165 /* XXX: printing UNTRUSTED data here. */
1166 fprintf (stderr
, _("< %s ... %s > ? "), program
, arg
);
1172 pred_ok (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1174 if (is_ok(pred_ptr
->args
.exec_vec
.replace_vec
[0], pathname
))
1175 return new_impl_pred_exec (pathname
, stat_buf
, pred_ptr
, NULL
, 0);
1181 pred_okdir (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1183 const char *prefix
= (state
.rel_pathname
[0] == '/') ? NULL
: "./";
1184 if (is_ok(pred_ptr
->args
.exec_vec
.replace_vec
[0], pathname
))
1185 return new_impl_pred_exec (state
.rel_pathname
, stat_buf
, pred_ptr
,
1186 prefix
, (prefix
? 2 : 0));
1192 pred_open (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1201 pred_or (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1203 if (pred_ptr
->pred_left
== NULL
1204 || !(*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
1205 pred_ptr
->pred_left
))
1207 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
1209 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1210 pred_ptr
->pred_right
));
1217 pred_path (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1220 if (fnmatch (pred_ptr
->args
.str
, pathname
, 0) == 0)
1226 pred_perm (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1229 switch (pred_ptr
->args
.perm
.kind
)
1232 return (stat_buf
->st_mode
& pred_ptr
->args
.perm
.val
) == pred_ptr
->args
.perm
.val
;
1236 /* True if any of the bits set in the mask are also set in the file's mode.
1239 * Otherwise, if onum is prefixed by a hyphen, the primary shall
1240 * evaluate as true if at least all of the bits specified in
1241 * onum that are also set in the octal mask 07777 are set.
1243 * Eric Blake's interpretation is that the mode argument is zero,
1246 if (0 == pred_ptr
->args
.perm
.val
)
1247 return true; /* Savannah bug 14748; we used to return false */
1249 return (stat_buf
->st_mode
& pred_ptr
->args
.perm
.val
) != 0;
1253 return (stat_buf
->st_mode
& MODE_ALL
) == pred_ptr
->args
.perm
.val
;
1264 pred_executable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1266 return 0 == access(state
.rel_pathname
, X_OK
);
1270 pred_readable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1272 return 0 == access(state
.rel_pathname
, R_OK
);
1276 pred_writable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1278 return 0 == access(state
.rel_pathname
, W_OK
);
1282 pred_print (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1286 /* puts (pathname); */
1287 print_quoted(pred_ptr
->args
.printf_vec
.stream
,
1288 pred_ptr
->args
.printf_vec
.quote_opts
,
1289 pred_ptr
->args
.printf_vec
.dest_is_tty
,
1295 pred_print0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1299 fputs (pathname
, stdout
);
1305 pred_prune (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1310 state
.stop_at_current_level
= true;
1311 return (options
.do_dir_first
); /* This is what SunOS find seems to do. */
1315 pred_quit (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1321 /* Run any cleanups. This includes executing any command lines
1322 * we have partly built but not executed.
1326 /* Since -exec and friends don't leave child processes running in the
1327 * background, there is no need to wait for them here.
1329 exit(state
.exit_status
); /* 0 for success, etc. */
1333 pred_regex (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1335 int len
= strlen (pathname
);
1337 if (re_match (pred_ptr
->args
.regex
, pathname
, len
, 0,
1338 (struct re_registers
*) NULL
) == len
)
1344 pred_size (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1349 f_val
= ((stat_buf
->st_size
/ pred_ptr
->args
.size
.blocksize
)
1350 + (stat_buf
->st_size
% pred_ptr
->args
.size
.blocksize
!= 0));
1351 switch (pred_ptr
->args
.size
.kind
)
1354 if (f_val
> pred_ptr
->args
.size
.size
)
1358 if (f_val
< pred_ptr
->args
.size
.size
)
1362 if (f_val
== pred_ptr
->args
.size
.size
)
1370 pred_samefile (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1372 /* Potential optimisation: because of the loop protection, we always
1373 * know the device of the current directory, hence the device number
1374 * of the file we're currently considering. If -L is not in effect,
1375 * and the device number of the file we're looking for is not the
1376 * same as the device number of the current directory, this
1377 * predicate cannot return true. Hence there would be no need to
1378 * stat the file we're lookingn at.
1382 return stat_buf
->st_ino
== pred_ptr
->args
.fileid
.ino
1383 && stat_buf
->st_dev
== pred_ptr
->args
.fileid
.dev
;
1387 pred_true (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1396 pred_type (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1399 mode_t type
= pred_ptr
->args
.type
;
1401 assert(state
.have_type
);
1402 assert(state
.type
!= 0);
1406 if (state
.have_stat
)
1407 mode
= stat_buf
->st_mode
;
1412 /* POSIX system; check `mode' the slow way. */
1413 if ((S_ISBLK (mode
) && type
== S_IFBLK
)
1414 || (S_ISCHR (mode
) && type
== S_IFCHR
)
1415 || (S_ISDIR (mode
) && type
== S_IFDIR
)
1416 || (S_ISREG (mode
) && type
== S_IFREG
)
1418 || (S_ISLNK (mode
) && type
== S_IFLNK
)
1421 || (S_ISFIFO (mode
) && type
== S_IFIFO
)
1424 || (S_ISSOCK (mode
) && type
== S_IFSOCK
)
1427 || (S_ISDOOR (mode
) && type
== S_IFDOOR
)
1431 /* Unix system; check `mode' the fast way. */
1432 if ((mode
& S_IFMT
) == type
)
1440 pred_uid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1443 switch (pred_ptr
->args
.info
.kind
)
1446 if (stat_buf
->st_uid
> pred_ptr
->args
.info
.l_val
)
1450 if (stat_buf
->st_uid
< pred_ptr
->args
.info
.l_val
)
1454 if (stat_buf
->st_uid
== pred_ptr
->args
.info
.l_val
)
1462 pred_used (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1467 delta
= stat_buf
->st_atime
- stat_buf
->st_ctime
; /* Use difftime? */
1468 return pred_timewindow(delta
, pred_ptr
, DAYSECS
);
1472 pred_user (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1475 if (pred_ptr
->args
.uid
== stat_buf
->st_uid
)
1482 pred_xtype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1484 struct stat sbuf
; /* local copy, not stat_buf because we're using a different stat method */
1485 int (*ystat
) (const char*, struct stat
*p
);
1487 /* If we would normally stat the link itself, stat the target instead.
1488 * If we would normally follow the link, stat the link itself instead.
1490 if (following_links())
1491 ystat
= optionp_stat
;
1493 ystat
= optionl_stat
;
1495 if ((*ystat
) (state
.rel_pathname
, &sbuf
) != 0)
1497 if (following_links() && errno
== ENOENT
)
1499 /* If we failed to follow the symlink,
1500 * fall back on looking at the symlink itself.
1502 /* Mimic behavior of ls -lL. */
1503 return (pred_type (pathname
, stat_buf
, pred_ptr
));
1507 error (0, errno
, "%s", pathname
);
1508 state
.exit_status
= 1;
1512 /* Now that we have our stat() information, query it in the same
1513 * way that -type does.
1515 return (pred_type (pathname
, &sbuf
, pred_ptr
));
1518 /* 1) fork to get a child; parent remembers the child pid
1519 2) child execs the command requested
1520 3) parent waits for child; checks for proper pid of child
1524 ret errno status(h) status(l)
1526 pid x signal# 0177 stopped
1527 pid x exit arg 0 term by _exit
1528 pid x 0 signal # term by signal
1529 -1 EINTR parent got signal
1530 -1 other some other kind of error
1532 Return true only if the pid matches, status(l) is
1533 zero, and the exit arg (status high) is 0.
1534 Otherwise return false, possibly printing an error message. */
1538 prep_child_for_exec (boolean close_stdin
)
1542 const char inputfile
[] = "/dev/null";
1543 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
1546 if (open(inputfile
, O_RDONLY
) < 0)
1548 /* This is not entirely fatal, since
1549 * executing the child with a closed
1550 * stdin is almost as good as executing it
1551 * with its stdin attached to /dev/null.
1553 error (0, errno
, "%s", inputfile
);
1561 launch (const struct buildcmd_control
*ctl
,
1562 struct buildcmd_state
*buildstate
)
1566 static int first_time
= 1;
1567 const struct exec_val
*execp
= buildstate
->usercontext
;
1569 /* Null terminate the arg list. */
1570 bc_push_arg (ctl
, buildstate
, (char *) NULL
, 0, NULL
, 0, false);
1572 /* Make sure output of command doesn't get mixed with find output. */
1576 /* Make sure to listen for the kids. */
1580 signal (SIGCHLD
, SIG_DFL
);
1583 child_pid
= fork ();
1584 if (child_pid
== -1)
1585 error (1, errno
, _("cannot fork"));
1588 /* We be the child. */
1589 prep_child_for_exec(execp
->close_stdin
);
1591 /* For -exec and -ok, change directory back to the starting directory.
1592 * for -execdir and -okdir, stay in the directory we are searching
1593 * (the latter is more secure).
1595 if (!execp
->use_current_dir
)
1597 /* Even if DEBUG_STAT is set, don't announce our change of
1598 * directory, since we're not going to emit a subsequent
1599 * announcement of a call to stat() anyway, as we're about
1600 * to exec something.
1602 if (starting_desc
< 0
1603 ? chdir (starting_dir
) != 0
1604 : fchdir (starting_desc
) != 0)
1606 error (0, errno
, "%s", starting_dir
);
1611 execvp (buildstate
->cmd_argv
[0], buildstate
->cmd_argv
);
1612 error (0, errno
, "%s", buildstate
->cmd_argv
[0]);
1617 /* In parent; set up for next time. */
1618 bc_clear_args(ctl
, buildstate
);
1621 while (waitpid (child_pid
, &wait_status
, 0) == (pid_t
) -1)
1625 error (0, errno
, _("error waiting for %s"), buildstate
->cmd_argv
[0]);
1626 state
.exit_status
= 1;
1627 return 0; /* FAIL */
1631 if (WIFSIGNALED (wait_status
))
1633 error (0, 0, _("%s terminated by signal %d"),
1634 buildstate
->cmd_argv
[0], WTERMSIG (wait_status
));
1636 if (execp
->multiple
)
1638 /* -exec \; just returns false if the invoked command fails.
1639 * -exec {} + returns true if the invoked command fails, but
1640 * sets the program exit status.
1642 state
.exit_status
= 1;
1648 if (0 == WEXITSTATUS (wait_status
))
1654 if (execp
->multiple
)
1656 /* -exec \; just returns false if the invoked command fails.
1657 * -exec {} + returns true if the invoked command fails, but
1658 * sets the program exit status.
1660 state
.exit_status
= 1;
1662 return 0; /* FAIL */
1668 /* Return a static string formatting the time WHEN according to the
1669 strftime format character KIND. */
1672 format_date (time_t when
, int kind
)
1674 static char buf
[MAX (LONGEST_HUMAN_READABLE
+ 2, 64)];
1682 strcpy (fmt
, "%F+%T");
1685 && (tm
= localtime (&when
))
1686 && strftime (buf
, sizeof buf
, fmt
, tm
))
1691 char *p
= human_readable (when
< 0 ? -w
: w
, buf
+ 1,
1692 human_ceiling
, 1, 1);
1700 ctime_format (time_t when
)
1702 char *r
= ctime (&when
);
1705 /* The time cannot be represented as a struct tm.
1706 Output it as an integer. */
1707 return format_date (when
, '@');
1711 /* Remove the trailing newline from the ctime output,
1712 being careful not to assume that the output is fixed-width. */
1713 *strchr (r
, '\n') = '\0';
1719 /* Return a pointer to the string representation of
1720 the predicate function PRED_FUNC. */
1723 find_pred_name (PRED_FUNC func
)
1727 for (i
= 0; pred_table
[i
].pred_func
!= 0; i
++)
1728 if (pred_table
[i
].pred_func
== func
)
1730 return pred_table
[i
].pred_name
;
1739 for (i
= 0; type_table
[i
].type
!= (short) -1; i
++)
1740 if (type_table
[i
].type
== type
)
1742 return (type_table
[i
].type_name
);
1751 for (i
= 0; prec_table
[i
].prec
!= (short) -1; i
++)
1752 if (prec_table
[i
].prec
== prec
)
1754 return (prec_table
[i
].prec_name
);
1757 /* Walk the expression tree NODE to stdout.
1758 INDENT is the number of levels to indent the left margin. */
1761 print_tree (FILE *fp
, struct predicate
*node
, int indent
)
1767 for (i
= 0; i
< indent
; i
++)
1769 fprintf (fp
, "pred = %s type = %s prec = %s addr = %p\n",
1770 find_pred_name (node
->pred_func
),
1771 type_name (node
->p_type
), prec_name (node
->p_prec
), node
);
1772 if (node
->need_stat
|| node
->need_type
)
1776 for (i
= 0; i
< indent
; i
++)
1779 fprintf (fp
, "Needs ");
1780 if (node
->need_stat
)
1782 fprintf (fp
, "stat");
1785 if (node
->need_type
)
1787 fprintf (fp
, "%stype", comma
? "," : "");
1792 for (i
= 0; i
< indent
; i
++)
1794 fprintf (fp
, "left:\n");
1795 print_tree (fp
, node
->pred_left
, indent
+ 1);
1796 for (i
= 0; i
< indent
; i
++)
1798 fprintf (fp
, "right:\n");
1799 print_tree (fp
, node
->pred_right
, indent
+ 1);
1802 /* Copy STR into BUF and trim blanks from the end of BUF.
1806 blank_rtrim (str
, buf
)
1815 i
= strlen (buf
) - 1;
1816 while ((i
>= 0) && ((buf
[i
] == ' ') || buf
[i
] == '\t'))
1822 /* Print out the predicate list starting at NODE. */
1825 print_list (FILE *fp
, struct predicate
*node
)
1827 struct predicate
*cur
;
1833 fprintf (fp
, "%s ", blank_rtrim (find_pred_name (cur
->pred_func
), name
));
1834 cur
= cur
->pred_next
;
1840 /* Print out the predicate list starting at NODE. */
1844 print_parenthesised(FILE *fp
, struct predicate
*node
)
1850 if ( ( (node
->pred_func
== pred_or
)
1851 || (node
->pred_func
== pred_and
) )
1852 && node
->pred_left
== NULL
)
1854 /* We print "<nothing> or X" as just "X"
1855 * We print "<nothing> and X" as just "X"
1857 print_parenthesised(fp
, node
->pred_right
);
1861 if (node
->pred_left
|| node
->pred_right
)
1865 fprintf(fp
, "%s", " ( ");
1866 print_optlist(fp
, node
);
1868 fprintf(fp
, "%s", " ) ");
1874 print_optlist (FILE *fp
, struct predicate
*p
)
1880 print_parenthesised(fp
, p
->pred_left
);
1883 p
->need_stat
? "[stat called here] " : "",
1884 p
->need_type
? "[type needed here] " : "",
1885 blank_rtrim (find_pred_name (p
->pred_func
), name
));
1886 print_parenthesised(fp
, p
->pred_right
);
1894 pred_sanity_check(const struct predicate
*predicates
)
1896 const struct predicate
*p
;
1898 for (p
=predicates
; p
!= NULL
; p
=p
->pred_next
)
1900 /* All predicates must do something. */
1901 assert(p
->pred_func
!= NULL
);
1903 /* All predicates must have a parser table entry. */
1904 assert(p
->parser_entry
!= NULL
);
1906 /* If the parser table tells us that just one predicate function is
1907 * possible, verify that that is still the one that is in effect.
1908 * If the parser has NULL for the predicate function, that means that
1909 * the parse_xxx function fills it in, so we can't check it.
1911 if (p
->parser_entry
->pred_func
)
1913 assert(p
->parser_entry
->pred_func
== p
->pred_func
);
1916 switch (p
->parser_entry
->type
)
1918 /* Options all take effect during parsing, so there should
1919 * be no predicate entries corresponding to them. Hence we
1920 * should not see any ARG_OPTION or ARG_POSITIONAL_OPTION
1923 * This is a silly way of coding this test, but it prevents
1924 * a compiler warning (i.e. otherwise it would think that
1925 * there would be case statements missing).
1928 case ARG_POSITIONAL_OPTION
:
1929 assert(p
->parser_entry
->type
!= ARG_OPTION
);
1930 assert(p
->parser_entry
->type
!= ARG_POSITIONAL_OPTION
);
1934 assert(p
->side_effects
); /* actions have side effects. */
1935 if (p
->pred_func
!= pred_prune
&& p
->pred_func
!= pred_quit
)
1937 /* actions other than -prune and -quit should
1938 * inhibit the default -print
1940 assert(p
->no_default_print
);
1944 case ARG_PUNCTUATION
:
1946 /* Punctuation and tests should have no side
1947 * effects and not inhibit default print.
1949 assert(!p
->no_default_print
);
1950 assert(!p
->side_effects
);