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 match_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_cmin
, "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_executable
, "executable "},
181 {pred_false
, "false "},
182 {pred_fprint
, "fprint "},
183 {pred_fprint0
, "fprint0 "},
184 {pred_fprintf
, "fprintf "},
185 {pred_fstype
, "fstype "},
187 {pred_group
, "group "},
188 {pred_ilname
, "ilname "},
189 {pred_iname
, "iname "},
190 {pred_inum
, "inum "},
191 {pred_ipath
, "ipath "},
192 {pred_links
, "links "},
193 {pred_lname
, "lname "},
195 {pred_mmin
, "mmin "},
196 {pred_mtime
, "mtime "},
197 {pred_name
, "name "},
198 {pred_negate
, "not "},
199 {pred_newer
, "newer "},
200 {pred_nogroup
, "nogroup "},
201 {pred_nouser
, "nouser "},
203 {pred_okdir
, "okdir "},
206 {pred_path
, "path "},
207 {pred_perm
, "perm "},
208 {pred_print
, "print "},
209 {pred_print0
, "print0 "},
210 {pred_prune
, "prune "},
211 {pred_quit
, "quit "},
212 {pred_readable
, "readable "},
213 {pred_regex
, "regex "},
214 {pred_samefile
,"samefile "},
215 {pred_size
, "size "},
216 {pred_true
, "true "},
217 {pred_type
, "type "},
219 {pred_used
, "used "},
220 {pred_user
, "user "},
221 {pred_writable
, "writable "},
222 {pred_xtype
, "xtype "},
227 /* Predicate processing routines.
229 PATHNAME is the full pathname of the file being checked.
230 *STAT_BUF contains information about PATHNAME.
231 *PRED_PTR contains information for applying the predicate.
233 Return true if the file passes this predicate, false if not. */
238 * Returns true if THE_TIME is
239 * COMP_GT: after the specified time
240 * COMP_LT: before the specified time
241 * COMP_EQ: less than WINDOW seconds after the specified time.
244 pred_timewindow(time_t the_time
, struct predicate
const *pred_ptr
, int window
)
246 switch (pred_ptr
->args
.info
.kind
)
249 if (the_time
> (time_t) pred_ptr
->args
.info
.l_val
)
253 if (the_time
< (time_t) pred_ptr
->args
.info
.l_val
)
257 if ((the_time
>= (time_t) pred_ptr
->args
.info
.l_val
)
258 && (the_time
< (time_t) pred_ptr
->args
.info
.l_val
+ window
))
267 pred_amin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
270 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, 60);
274 pred_and (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
276 if (pred_ptr
->pred_left
== NULL
277 || (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
278 pred_ptr
->pred_left
))
280 /* Check whether we need a stat here. */
281 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
283 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
284 pred_ptr
->pred_right
));
291 pred_anewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
295 if (stat_buf
->st_atime
> pred_ptr
->args
.time
)
301 pred_atime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
304 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, DAYSECS
);
308 pred_close (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
318 pred_cmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
321 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, 60);
325 pred_cnewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
329 if (stat_buf
->st_ctime
> pred_ptr
->args
.time
)
336 pred_comma (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
338 if (pred_ptr
->pred_left
!= NULL
)
339 (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
340 pred_ptr
->pred_left
);
341 /* Check whether we need a stat here. */
342 /* TODO: what about need_type? */
343 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
345 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
346 pred_ptr
->pred_right
));
350 pred_ctime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
353 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, DAYSECS
);
357 pred_delete (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
361 if (strcmp (state
.rel_pathname
, "."))
363 if (0 != remove (state
.rel_pathname
))
365 error (0, errno
, "cannot delete %s", pathname
);
379 pred_empty (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
384 if (S_ISDIR (stat_buf
->st_mode
))
388 boolean empty
= true;
391 d
= opendir (state
.rel_pathname
);
394 error (0, errno
, "%s", pathname
);
395 state
.exit_status
= 1;
398 for (dp
= readdir (d
); dp
; dp
= readdir (d
))
400 if (dp
->d_name
[0] != '.'
401 || (dp
->d_name
[1] != '\0'
402 && (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0')))
410 error (0, errno
, "%s", pathname
);
411 state
.exit_status
= 1;
416 else if (S_ISREG (stat_buf
->st_mode
))
417 return (stat_buf
->st_size
== 0);
423 new_impl_pred_exec (const char *pathname
, struct stat
*stat_buf
,
424 struct predicate
*pred_ptr
,
425 const char *prefix
, size_t pfxlen
)
427 struct exec_val
*execp
= &pred_ptr
->args
.exec_vec
;
428 size_t len
= strlen(pathname
);
434 /* Push the argument onto the current list.
435 * The command may or may not be run at this point,
436 * depending on the command line length limits.
438 bc_push_arg(&execp
->ctl
,
444 /* POSIX: If the primary expression is punctuated by a plus
445 * sign, the primary shall always evaluate as true
453 for (i
=0; i
<execp
->num_args
; ++i
)
455 bc_do_insert(&execp
->ctl
,
457 execp
->replace_vec
[i
],
458 strlen(execp
->replace_vec
[i
]),
464 /* Actually invoke the command. */
465 return execp
->ctl
.exec_callback(&execp
->ctl
,
472 pred_exec (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
474 return new_impl_pred_exec(pathname
, stat_buf
, pred_ptr
, NULL
, 0);
478 pred_execdir (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
480 const char *prefix
= (state
.rel_pathname
[0] == '/') ? NULL
: "./";
482 return new_impl_pred_exec (state
.rel_pathname
, stat_buf
, pred_ptr
,
483 prefix
, (prefix
? 2 : 0));
487 pred_false (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
498 pred_fls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
500 list_file (pathname
, state
.rel_pathname
, stat_buf
, options
.start_time
,
501 options
.output_block_size
,
502 pred_ptr
->literal_control_chars
, pred_ptr
->args
.stream
);
507 pred_fprint (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
512 print_quoted(pred_ptr
->args
.printf_vec
.stream
,
513 pred_ptr
->args
.printf_vec
.quote_opts
,
514 pred_ptr
->args
.printf_vec
.dest_is_tty
,
521 pred_fprint0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
526 fputs (pathname
, pred_ptr
->args
.stream
);
527 putc (0, pred_ptr
->args
.stream
);
534 mode_to_filetype(mode_t m
)
537 m
== S_IFSOCK
? "s" :
544 m
== S_IFDOOR
? "D" :
546 m
== S_IFIFO
? "p" : "U";
551 pred_fprintf (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
553 FILE *fp
= pred_ptr
->args
.printf_vec
.stream
;
554 const struct quoting_options
*qopts
= pred_ptr
->args
.printf_vec
.quote_opts
;
555 boolean ttyflag
= pred_ptr
->args
.printf_vec
.dest_is_tty
;
556 struct segment
*segment
;
558 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
560 for (segment
= pred_ptr
->args
.printf_vec
.segment
; segment
;
561 segment
= segment
->next
)
563 if (segment
->kind
& 0xff00) /* Component of date. */
567 switch (segment
->kind
& 0xff)
570 t
= stat_buf
->st_atime
;
573 t
= stat_buf
->st_ctime
;
576 t
= stat_buf
->st_mtime
;
581 /* We trust the output of format_date not to contain
582 * nasty characters, though the value of the date
583 * is itself untrusted data.
586 fprintf (fp
, segment
->text
,
587 format_date (t
, (segment
->kind
>> 8) & 0xff));
591 switch (segment
->kind
)
593 case KIND_PLAIN
: /* Plain text string (no % conversion). */
595 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
597 case KIND_STOP
: /* Terminate argument and flush output. */
599 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
602 case 'a': /* atime in `ctime' format. */
603 /* UNTRUSTED, probably unexploitable */
604 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_atime
));
606 case 'b': /* size in 512-byte blocks */
607 /* UNTRUSTED, probably unexploitable */
608 fprintf (fp
, segment
->text
,
609 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
611 ST_NBLOCKSIZE
, 512));
613 case 'c': /* ctime in `ctime' format */
614 /* UNTRUSTED, probably unexploitable */
615 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_ctime
));
617 case 'd': /* depth in search tree */
618 /* UNTRUSTED, probably unexploitable */
619 fprintf (fp
, segment
->text
, state
.curdepth
);
621 case 'D': /* Device on which file exists (stat.st_dev) */
623 fprintf (fp
, segment
->text
,
624 human_readable ((uintmax_t) stat_buf
->st_dev
, hbuf
,
625 human_ceiling
, 1, 1));
627 case 'f': /* base name of path */
629 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, base_name (pathname
));
631 case 'F': /* filesystem type */
633 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, filesystem_type (stat_buf
, pathname
));
635 case 'g': /* group name */
637 /* (well, the actual group is selected by the user but
638 * its name was selected by the system administrator)
643 g
= getgrgid (stat_buf
->st_gid
);
646 segment
->text
[segment
->text_len
] = 's';
647 fprintf (fp
, segment
->text
, g
->gr_name
);
652 case 'G': /* GID number */
653 /* UNTRUSTED, probably unexploitable */
654 fprintf (fp
, segment
->text
,
655 human_readable ((uintmax_t) stat_buf
->st_gid
, hbuf
,
656 human_ceiling
, 1, 1));
658 case 'h': /* leading directories part of path */
663 cp
= strrchr (pathname
, '/');
664 if (cp
== NULL
) /* No leading directories. */
666 /* If there is no slash in the pathname, we still
667 * print the string because it contains characters
668 * other than just '%s'. The %h expands to ".".
670 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, ".");
676 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, pathname
);
681 case 'H': /* ARGV element file was found under */
684 char cc
= pathname
[state
.starting_path_length
];
686 pathname
[state
.starting_path_length
] = '\0';
687 fprintf (fp
, segment
->text
, pathname
);
688 pathname
[state
.starting_path_length
] = cc
;
691 case 'i': /* inode number */
692 /* UNTRUSTED, but not exploitable I think */
693 fprintf (fp
, segment
->text
,
694 human_readable ((uintmax_t) stat_buf
->st_ino
, hbuf
,
698 case 'k': /* size in 1K blocks */
699 /* UNTRUSTED, but not exploitable I think */
700 fprintf (fp
, segment
->text
,
701 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
703 ST_NBLOCKSIZE
, 1024));
705 case 'l': /* object of symlink */
711 if (S_ISLNK (stat_buf
->st_mode
))
713 linkname
= get_link_name (pathname
, state
.rel_pathname
);
715 state
.exit_status
= 1;
719 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, linkname
);
723 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, "");
728 case 'M': /* mode as 10 chars (eg., "-rwxr-x--x" */
729 /* UNTRUSTED, probably unexploitable */
731 char modestring
[16] ;
732 filemodestring (stat_buf
, modestring
);
733 modestring
[10] = '\0';
734 fprintf (fp
, segment
->text
, modestring
);
738 case 'm': /* mode as octal number (perms only) */
739 /* UNTRUSTED, probably unexploitable */
741 /* Output the mode portably using the traditional numbers,
742 even if the host unwisely uses some other numbering
743 scheme. But help the compiler in the common case where
744 the host uses the traditional numbering scheme. */
745 mode_t m
= stat_buf
->st_mode
;
746 boolean traditional_numbering_scheme
=
747 (S_ISUID
== 04000 && S_ISGID
== 02000 && S_ISVTX
== 01000
748 && S_IRUSR
== 00400 && S_IWUSR
== 00200 && S_IXUSR
== 00100
749 && S_IRGRP
== 00040 && S_IWGRP
== 00020 && S_IXGRP
== 00010
750 && S_IROTH
== 00004 && S_IWOTH
== 00002 && S_IXOTH
== 00001);
751 fprintf (fp
, segment
->text
,
752 (traditional_numbering_scheme
754 : ((m
& S_ISUID
? 04000 : 0)
755 | (m
& S_ISGID
? 02000 : 0)
756 | (m
& S_ISVTX
? 01000 : 0)
757 | (m
& S_IRUSR
? 00400 : 0)
758 | (m
& S_IWUSR
? 00200 : 0)
759 | (m
& S_IXUSR
? 00100 : 0)
760 | (m
& S_IRGRP
? 00040 : 0)
761 | (m
& S_IWGRP
? 00020 : 0)
762 | (m
& S_IXGRP
? 00010 : 0)
763 | (m
& S_IROTH
? 00004 : 0)
764 | (m
& S_IWOTH
? 00002 : 0)
765 | (m
& S_IXOTH
? 00001 : 0))));
769 case 'n': /* number of links */
770 /* UNTRUSTED, probably unexploitable */
771 fprintf (fp
, segment
->text
,
772 human_readable ((uintmax_t) stat_buf
->st_nlink
,
777 case 'p': /* pathname */
779 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, pathname
);
781 case 'P': /* pathname with ARGV element stripped */
783 if (state
.curdepth
> 0)
785 cp
= pathname
+ state
.starting_path_length
;
787 /* Move past the slash between the ARGV element
788 and the rest of the pathname. But if the ARGV element
789 ends in a slash, we didn't add another, so we've
790 already skipped past it. */
795 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, cp
);
797 case 's': /* size in bytes */
798 /* UNTRUSTED, probably unexploitable */
799 fprintf (fp
, segment
->text
,
800 human_readable ((uintmax_t) stat_buf
->st_size
,
801 hbuf
, human_ceiling
, 1, 1));
803 case 't': /* mtime in `ctime' format */
804 /* UNTRUSTED, probably unexploitable */
805 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_mtime
));
807 case 'u': /* user name */
809 /* (well, the actual user is selected by the user on systems
810 * where chown is not restricted, but the user name was
811 * selected by the system administrator)
816 p
= getpwuid (stat_buf
->st_uid
);
819 segment
->text
[segment
->text_len
] = 's';
820 fprintf (fp
, segment
->text
, p
->pw_name
);
826 case 'U': /* UID number */
827 /* UNTRUSTED, probably unexploitable */
828 fprintf (fp
, segment
->text
,
829 human_readable ((uintmax_t) stat_buf
->st_uid
, hbuf
,
830 human_ceiling
, 1, 1));
833 /* type of filesystem entry like `ls -l`: (d,-,l,s,p,b,c,n) n=nonexistent(symlink) */
834 case 'Y': /* in case of symlink */
838 if (S_ISLNK (stat_buf
->st_mode
))
841 /* If we would normally follow links, do not do so.
842 * If we would normally not follow links, do so.
844 if ((following_links() ? lstat
: stat
)
845 (state
.rel_pathname
, &sbuf
) != 0)
847 if ( errno
== ENOENT
) {
848 fprintf (fp
, segment
->text
, "N");
851 if ( errno
== ELOOP
) {
852 fprintf (fp
, segment
->text
, "L");
855 error (0, errno
, "%s", pathname
);
859 fprintf (fp
, segment
->text
,
860 mode_to_filetype(sbuf
.st_mode
& S_IFMT
));
865 fprintf (fp
, segment
->text
,
866 mode_to_filetype(stat_buf
->st_mode
& S_IFMT
));
874 fprintf (fp
, segment
->text
,
875 mode_to_filetype(stat_buf
->st_mode
& S_IFMT
));
884 pred_fstype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
888 if (strcmp (filesystem_type (stat_buf
, pathname
), pred_ptr
->args
.str
) == 0)
895 pred_gid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
899 switch (pred_ptr
->args
.info
.kind
)
902 if (stat_buf
->st_gid
> pred_ptr
->args
.info
.l_val
)
906 if (stat_buf
->st_gid
< pred_ptr
->args
.info
.l_val
)
910 if (stat_buf
->st_gid
== pred_ptr
->args
.info
.l_val
)
918 pred_group (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
922 if (pred_ptr
->args
.gid
== stat_buf
->st_gid
)
929 pred_ilname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
931 return match_lname (pathname
, stat_buf
, pred_ptr
, true);
935 pred_iname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
941 /* FNM_PERIOD is not used here because POSIX requires that it not be.
942 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
944 base
= base_name (pathname
);
945 if (fnmatch (pred_ptr
->args
.str
, base
, FNM_CASEFOLD
) == 0)
951 pred_inum (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
955 switch (pred_ptr
->args
.info
.kind
)
958 if (stat_buf
->st_ino
> pred_ptr
->args
.info
.l_val
)
962 if (stat_buf
->st_ino
< pred_ptr
->args
.info
.l_val
)
966 if (stat_buf
->st_ino
== pred_ptr
->args
.info
.l_val
)
974 pred_ipath (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
978 if (fnmatch (pred_ptr
->args
.str
, pathname
, FNM_CASEFOLD
) == 0)
984 pred_links (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
988 switch (pred_ptr
->args
.info
.kind
)
991 if (stat_buf
->st_nlink
> pred_ptr
->args
.info
.l_val
)
995 if (stat_buf
->st_nlink
< pred_ptr
->args
.info
.l_val
)
999 if (stat_buf
->st_nlink
== pred_ptr
->args
.info
.l_val
)
1007 pred_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1009 return match_lname (pathname
, stat_buf
, pred_ptr
, false);
1013 match_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
, boolean ignore_case
)
1015 boolean ret
= false;
1017 if (S_ISLNK (stat_buf
->st_mode
))
1019 char *linkname
= get_link_name (pathname
, state
.rel_pathname
);
1022 if (fnmatch (pred_ptr
->args
.str
, linkname
,
1023 ignore_case
? FNM_CASEFOLD
: 0) == 0)
1028 #endif /* S_ISLNK */
1033 pred_ls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1035 list_file (pathname
, state
.rel_pathname
, stat_buf
, options
.start_time
,
1036 options
.output_block_size
,
1037 pred_ptr
->literal_control_chars
,
1043 pred_mmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1046 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, 60);
1050 pred_mtime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1053 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, DAYSECS
);
1057 pred_name (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1062 base
= base_name (pathname
);
1064 /* FNM_PERIOD is not used here because POSIX requires that it not be.
1065 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
1067 if (fnmatch (pred_ptr
->args
.str
, base
, 0) == 0)
1073 pred_negate (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1075 /* Check whether we need a stat here. */
1076 /* TODO: what about need_type? */
1077 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
1079 return (!(*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1080 pred_ptr
->pred_right
));
1084 pred_newer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1088 if (stat_buf
->st_mtime
> pred_ptr
->args
.time
)
1094 pred_nogroup (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1100 extern char *gid_unused
;
1102 return gid_unused
[(unsigned) stat_buf
->st_gid
];
1104 return getgrgid (stat_buf
->st_gid
) == NULL
;
1109 pred_nouser (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1112 extern char *uid_unused
;
1119 return uid_unused
[(unsigned) stat_buf
->st_uid
];
1121 return getpwuid (stat_buf
->st_uid
) == NULL
;
1127 is_ok(const char *program
, const char *arg
)
1130 /* The draft open standard requires that, in the POSIX locale,
1131 the last non-blank character of this prompt be '?'.
1132 The exact format is not specified.
1133 This standard does not have requirements for locales other than POSIX
1135 /* XXX: printing UNTRUSTED data here. */
1136 fprintf (stderr
, _("< %s ... %s > ? "), program
, arg
);
1142 pred_ok (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1144 if (is_ok(pred_ptr
->args
.exec_vec
.replace_vec
[0], pathname
))
1145 return new_impl_pred_exec (pathname
, stat_buf
, pred_ptr
, NULL
, 0);
1151 pred_okdir (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1153 const char *prefix
= (state
.rel_pathname
[0] == '/') ? NULL
: "./";
1154 if (is_ok(pred_ptr
->args
.exec_vec
.replace_vec
[0], pathname
))
1155 return new_impl_pred_exec (state
.rel_pathname
, stat_buf
, pred_ptr
,
1156 prefix
, (prefix
? 2 : 0));
1162 pred_open (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1171 pred_or (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1173 if (pred_ptr
->pred_left
== NULL
1174 || !(*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
1175 pred_ptr
->pred_left
))
1177 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
1179 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1180 pred_ptr
->pred_right
));
1187 pred_path (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1190 if (fnmatch (pred_ptr
->args
.str
, pathname
, 0) == 0)
1196 pred_perm (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1198 mode_t mode
= stat_buf
->st_mode
;
1199 mode_t perm_val
= pred_ptr
->args
.perm
.val
[S_ISDIR (mode
) != 0];
1201 switch (pred_ptr
->args
.perm
.kind
)
1204 return (mode
& perm_val
) == perm_val
;
1208 /* True if any of the bits set in the mask are also set in the file's mode.
1211 * Otherwise, if onum is prefixed by a hyphen, the primary shall
1212 * evaluate as true if at least all of the bits specified in
1213 * onum that are also set in the octal mask 07777 are set.
1215 * Eric Blake's interpretation is that the mode argument is zero,
1219 return true; /* Savannah bug 14748; we used to return false */
1221 return (mode
& perm_val
) != 0;
1225 return (mode
& MODE_ALL
) == perm_val
;
1236 pred_executable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1242 return 0 == access(state
.rel_pathname
, X_OK
);
1246 pred_readable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1252 return 0 == access(state
.rel_pathname
, R_OK
);
1256 pred_writable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1262 return 0 == access(state
.rel_pathname
, W_OK
);
1266 pred_print (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1270 /* puts (pathname); */
1271 print_quoted(pred_ptr
->args
.printf_vec
.stream
,
1272 pred_ptr
->args
.printf_vec
.quote_opts
,
1273 pred_ptr
->args
.printf_vec
.dest_is_tty
,
1279 pred_print0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1283 fputs (pathname
, stdout
);
1289 pred_prune (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1294 state
.stop_at_current_level
= true;
1295 return (options
.do_dir_first
); /* This is what SunOS find seems to do. */
1299 pred_quit (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1305 /* Run any cleanups. This includes executing any command lines
1306 * we have partly built but not executed.
1310 /* Since -exec and friends don't leave child processes running in the
1311 * background, there is no need to wait for them here.
1313 exit(state
.exit_status
); /* 0 for success, etc. */
1317 pred_regex (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1319 int len
= strlen (pathname
);
1321 if (re_match (pred_ptr
->args
.regex
, pathname
, len
, 0,
1322 (struct re_registers
*) NULL
) == len
)
1328 pred_size (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1333 f_val
= ((stat_buf
->st_size
/ pred_ptr
->args
.size
.blocksize
)
1334 + (stat_buf
->st_size
% pred_ptr
->args
.size
.blocksize
!= 0));
1335 switch (pred_ptr
->args
.size
.kind
)
1338 if (f_val
> pred_ptr
->args
.size
.size
)
1342 if (f_val
< pred_ptr
->args
.size
.size
)
1346 if (f_val
== pred_ptr
->args
.size
.size
)
1354 pred_samefile (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1356 /* Potential optimisation: because of the loop protection, we always
1357 * know the device of the current directory, hence the device number
1358 * of the file we're currently considering. If -L is not in effect,
1359 * and the device number of the file we're looking for is not the
1360 * same as the device number of the current directory, this
1361 * predicate cannot return true. Hence there would be no need to
1362 * stat the file we're lookingn at.
1366 return stat_buf
->st_ino
== pred_ptr
->args
.fileid
.ino
1367 && stat_buf
->st_dev
== pred_ptr
->args
.fileid
.dev
;
1371 pred_true (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1380 pred_type (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1383 mode_t type
= pred_ptr
->args
.type
;
1385 assert(state
.have_type
);
1386 assert(state
.type
!= 0);
1390 if (state
.have_stat
)
1391 mode
= stat_buf
->st_mode
;
1396 /* POSIX system; check `mode' the slow way. */
1397 if ((S_ISBLK (mode
) && type
== S_IFBLK
)
1398 || (S_ISCHR (mode
) && type
== S_IFCHR
)
1399 || (S_ISDIR (mode
) && type
== S_IFDIR
)
1400 || (S_ISREG (mode
) && type
== S_IFREG
)
1402 || (S_ISLNK (mode
) && type
== S_IFLNK
)
1405 || (S_ISFIFO (mode
) && type
== S_IFIFO
)
1408 || (S_ISSOCK (mode
) && type
== S_IFSOCK
)
1411 || (S_ISDOOR (mode
) && type
== S_IFDOOR
)
1415 /* Unix system; check `mode' the fast way. */
1416 if ((mode
& S_IFMT
) == type
)
1424 pred_uid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1427 switch (pred_ptr
->args
.info
.kind
)
1430 if (stat_buf
->st_uid
> pred_ptr
->args
.info
.l_val
)
1434 if (stat_buf
->st_uid
< pred_ptr
->args
.info
.l_val
)
1438 if (stat_buf
->st_uid
== pred_ptr
->args
.info
.l_val
)
1446 pred_used (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1451 delta
= stat_buf
->st_atime
- stat_buf
->st_ctime
; /* Use difftime? */
1452 return pred_timewindow(delta
, pred_ptr
, DAYSECS
);
1456 pred_user (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1459 if (pred_ptr
->args
.uid
== stat_buf
->st_uid
)
1466 pred_xtype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1468 struct stat sbuf
; /* local copy, not stat_buf because we're using a different stat method */
1469 int (*ystat
) (const char*, struct stat
*p
);
1471 /* If we would normally stat the link itself, stat the target instead.
1472 * If we would normally follow the link, stat the link itself instead.
1474 if (following_links())
1475 ystat
= optionp_stat
;
1477 ystat
= optionl_stat
;
1479 if ((*ystat
) (state
.rel_pathname
, &sbuf
) != 0)
1481 if (following_links() && errno
== ENOENT
)
1483 /* If we failed to follow the symlink,
1484 * fall back on looking at the symlink itself.
1486 /* Mimic behavior of ls -lL. */
1487 return (pred_type (pathname
, stat_buf
, pred_ptr
));
1491 error (0, errno
, "%s", pathname
);
1492 state
.exit_status
= 1;
1496 /* Now that we have our stat() information, query it in the same
1497 * way that -type does.
1499 return (pred_type (pathname
, &sbuf
, pred_ptr
));
1502 /* 1) fork to get a child; parent remembers the child pid
1503 2) child execs the command requested
1504 3) parent waits for child; checks for proper pid of child
1508 ret errno status(h) status(l)
1510 pid x signal# 0177 stopped
1511 pid x exit arg 0 term by _exit
1512 pid x 0 signal # term by signal
1513 -1 EINTR parent got signal
1514 -1 other some other kind of error
1516 Return true only if the pid matches, status(l) is
1517 zero, and the exit arg (status high) is 0.
1518 Otherwise return false, possibly printing an error message. */
1522 prep_child_for_exec (boolean close_stdin
)
1526 const char inputfile
[] = "/dev/null";
1527 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
1530 if (open(inputfile
, O_RDONLY
) < 0)
1532 /* This is not entirely fatal, since
1533 * executing the child with a closed
1534 * stdin is almost as good as executing it
1535 * with its stdin attached to /dev/null.
1537 error (0, errno
, "%s", inputfile
);
1545 launch (const struct buildcmd_control
*ctl
,
1546 struct buildcmd_state
*buildstate
)
1550 static int first_time
= 1;
1551 const struct exec_val
*execp
= buildstate
->usercontext
;
1553 /* Null terminate the arg list. */
1554 bc_push_arg (ctl
, buildstate
, (char *) NULL
, 0, NULL
, 0, false);
1556 /* Make sure output of command doesn't get mixed with find output. */
1560 /* Make sure to listen for the kids. */
1564 signal (SIGCHLD
, SIG_DFL
);
1567 child_pid
= fork ();
1568 if (child_pid
== -1)
1569 error (1, errno
, _("cannot fork"));
1572 /* We be the child. */
1573 prep_child_for_exec(execp
->close_stdin
);
1575 /* For -exec and -ok, change directory back to the starting directory.
1576 * for -execdir and -okdir, stay in the directory we are searching
1577 * (the latter is more secure).
1579 if (!execp
->use_current_dir
)
1581 /* Even if DebugSearch is set, don't announce our change of
1582 * directory, since we're not going to emit a subsequent
1583 * announcement of a call to stat() anyway, as we're about
1584 * to exec something.
1586 if (starting_desc
< 0
1587 ? chdir (starting_dir
) != 0
1588 : fchdir (starting_desc
) != 0)
1590 error (0, errno
, "%s", starting_dir
);
1595 execvp (buildstate
->cmd_argv
[0], buildstate
->cmd_argv
);
1596 error (0, errno
, "%s", buildstate
->cmd_argv
[0]);
1601 /* In parent; set up for next time. */
1602 bc_clear_args(ctl
, buildstate
);
1605 while (waitpid (child_pid
, &wait_status
, 0) == (pid_t
) -1)
1609 error (0, errno
, _("error waiting for %s"), buildstate
->cmd_argv
[0]);
1610 state
.exit_status
= 1;
1611 return 0; /* FAIL */
1615 if (WIFSIGNALED (wait_status
))
1617 error (0, 0, _("%s terminated by signal %d"),
1618 buildstate
->cmd_argv
[0], WTERMSIG (wait_status
));
1620 if (execp
->multiple
)
1622 /* -exec \; just returns false if the invoked command fails.
1623 * -exec {} + returns true if the invoked command fails, but
1624 * sets the program exit status.
1626 state
.exit_status
= 1;
1632 if (0 == WEXITSTATUS (wait_status
))
1638 if (execp
->multiple
)
1640 /* -exec \; just returns false if the invoked command fails.
1641 * -exec {} + returns true if the invoked command fails, but
1642 * sets the program exit status.
1644 state
.exit_status
= 1;
1646 return 0; /* FAIL */
1652 /* Return a static string formatting the time WHEN according to the
1653 strftime format character KIND. */
1656 format_date (time_t when
, int kind
)
1658 static char buf
[MAX (LONGEST_HUMAN_READABLE
+ 2, 64)];
1666 strcpy (fmt
, "%F+%T");
1669 && (tm
= localtime (&when
))
1670 && strftime (buf
, sizeof buf
, fmt
, tm
))
1675 char *p
= human_readable (when
< 0 ? -w
: w
, buf
+ 1,
1676 human_ceiling
, 1, 1);
1684 ctime_format (time_t when
)
1686 char *r
= ctime (&when
);
1689 /* The time cannot be represented as a struct tm.
1690 Output it as an integer. */
1691 return format_date (when
, '@');
1695 /* Remove the trailing newline from the ctime output,
1696 being careful not to assume that the output is fixed-width. */
1697 *strchr (r
, '\n') = '\0';
1702 /* Copy STR into BUF and trim blanks from the end of BUF.
1706 blank_rtrim (str
, buf
)
1715 i
= strlen (buf
) - 1;
1716 while ((i
>= 0) && ((buf
[i
] == ' ') || buf
[i
] == '\t'))
1722 /* Print out the predicate list starting at NODE. */
1724 print_list (FILE *fp
, struct predicate
*node
)
1726 struct predicate
*cur
;
1732 fprintf (fp
, "%s ", blank_rtrim (cur
->p_name
, name
));
1733 cur
= cur
->pred_next
;
1738 /* Print out the predicate list starting at NODE. */
1740 print_parenthesised(FILE *fp
, struct predicate
*node
)
1746 if ( ( (node
->pred_func
== pred_or
)
1747 || (node
->pred_func
== pred_and
) )
1748 && node
->pred_left
== NULL
)
1750 /* We print "<nothing> or X" as just "X"
1751 * We print "<nothing> and X" as just "X"
1753 print_parenthesised(fp
, node
->pred_right
);
1757 if (node
->pred_left
|| node
->pred_right
)
1761 fprintf(fp
, "%s", " ( ");
1762 print_optlist(fp
, node
);
1764 fprintf(fp
, "%s", " ) ");
1770 print_optlist (FILE *fp
, const struct predicate
*p
)
1774 print_parenthesised(fp
, p
->pred_left
);
1777 p
->need_stat
? "[call stat] " : "",
1778 p
->need_type
? "[need type] " : "");
1779 print_predicate(fp
, p
);
1780 fprintf(fp
, " [%g] ", p
->est_success_rate
);
1781 print_parenthesised(fp
, p
->pred_right
);
1786 pred_sanity_check(const struct predicate
*predicates
)
1788 const struct predicate
*p
;
1790 for (p
=predicates
; p
!= NULL
; p
=p
->pred_next
)
1792 /* All predicates must do something. */
1793 assert(p
->pred_func
!= NULL
);
1795 /* All predicates must have a parser table entry. */
1796 assert(p
->parser_entry
!= NULL
);
1798 /* If the parser table tells us that just one predicate function is
1799 * possible, verify that that is still the one that is in effect.
1800 * If the parser has NULL for the predicate function, that means that
1801 * the parse_xxx function fills it in, so we can't check it.
1803 if (p
->parser_entry
->pred_func
)
1805 assert(p
->parser_entry
->pred_func
== p
->pred_func
);
1808 switch (p
->parser_entry
->type
)
1810 /* Options all take effect during parsing, so there should
1811 * be no predicate entries corresponding to them. Hence we
1812 * should not see any ARG_OPTION or ARG_POSITIONAL_OPTION
1815 * This is a silly way of coding this test, but it prevents
1816 * a compiler warning (i.e. otherwise it would think that
1817 * there would be case statements missing).
1820 case ARG_POSITIONAL_OPTION
:
1821 assert(p
->parser_entry
->type
!= ARG_OPTION
);
1822 assert(p
->parser_entry
->type
!= ARG_POSITIONAL_OPTION
);
1826 assert(p
->side_effects
); /* actions have side effects. */
1827 if (p
->pred_func
!= pred_prune
&& p
->pred_func
!= pred_quit
)
1829 /* actions other than -prune and -quit should
1830 * inhibit the default -print
1832 assert(p
->no_default_print
);
1836 case ARG_PUNCTUATION
:
1839 /* Punctuation and tests should have no side
1840 * effects and not inhibit default print.
1842 assert(!p
->no_default_print
);
1843 assert(!p
->side_effects
);