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 if (options
.do_dir_first
== true &&
1296 S_ISDIR(stat_buf
->st_mode
))
1297 state
.stop_at_current_level
= true;
1299 return (options
.do_dir_first
); /* This is what SunOS find seems to do. */
1303 pred_quit (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1309 /* Run any cleanups. This includes executing any command lines
1310 * we have partly built but not executed.
1314 /* Since -exec and friends don't leave child processes running in the
1315 * background, there is no need to wait for them here.
1317 exit(state
.exit_status
); /* 0 for success, etc. */
1321 pred_regex (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1323 int len
= strlen (pathname
);
1325 if (re_match (pred_ptr
->args
.regex
, pathname
, len
, 0,
1326 (struct re_registers
*) NULL
) == len
)
1332 pred_size (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1337 f_val
= ((stat_buf
->st_size
/ pred_ptr
->args
.size
.blocksize
)
1338 + (stat_buf
->st_size
% pred_ptr
->args
.size
.blocksize
!= 0));
1339 switch (pred_ptr
->args
.size
.kind
)
1342 if (f_val
> pred_ptr
->args
.size
.size
)
1346 if (f_val
< pred_ptr
->args
.size
.size
)
1350 if (f_val
== pred_ptr
->args
.size
.size
)
1358 pred_samefile (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1360 /* Potential optimisation: because of the loop protection, we always
1361 * know the device of the current directory, hence the device number
1362 * of the file we're currently considering. If -L is not in effect,
1363 * and the device number of the file we're looking for is not the
1364 * same as the device number of the current directory, this
1365 * predicate cannot return true. Hence there would be no need to
1366 * stat the file we're lookingn at.
1370 return stat_buf
->st_ino
== pred_ptr
->args
.fileid
.ino
1371 && stat_buf
->st_dev
== pred_ptr
->args
.fileid
.dev
;
1375 pred_true (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1384 pred_type (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1387 mode_t type
= pred_ptr
->args
.type
;
1389 assert(state
.have_type
);
1390 assert(state
.type
!= 0);
1394 if (state
.have_stat
)
1395 mode
= stat_buf
->st_mode
;
1400 /* POSIX system; check `mode' the slow way. */
1401 if ((S_ISBLK (mode
) && type
== S_IFBLK
)
1402 || (S_ISCHR (mode
) && type
== S_IFCHR
)
1403 || (S_ISDIR (mode
) && type
== S_IFDIR
)
1404 || (S_ISREG (mode
) && type
== S_IFREG
)
1406 || (S_ISLNK (mode
) && type
== S_IFLNK
)
1409 || (S_ISFIFO (mode
) && type
== S_IFIFO
)
1412 || (S_ISSOCK (mode
) && type
== S_IFSOCK
)
1415 || (S_ISDOOR (mode
) && type
== S_IFDOOR
)
1419 /* Unix system; check `mode' the fast way. */
1420 if ((mode
& S_IFMT
) == type
)
1428 pred_uid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1431 switch (pred_ptr
->args
.info
.kind
)
1434 if (stat_buf
->st_uid
> pred_ptr
->args
.info
.l_val
)
1438 if (stat_buf
->st_uid
< pred_ptr
->args
.info
.l_val
)
1442 if (stat_buf
->st_uid
== pred_ptr
->args
.info
.l_val
)
1450 pred_used (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1455 delta
= stat_buf
->st_atime
- stat_buf
->st_ctime
; /* Use difftime? */
1456 return pred_timewindow(delta
, pred_ptr
, DAYSECS
);
1460 pred_user (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1463 if (pred_ptr
->args
.uid
== stat_buf
->st_uid
)
1470 pred_xtype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1472 struct stat sbuf
; /* local copy, not stat_buf because we're using a different stat method */
1473 int (*ystat
) (const char*, struct stat
*p
);
1475 /* If we would normally stat the link itself, stat the target instead.
1476 * If we would normally follow the link, stat the link itself instead.
1478 if (following_links())
1479 ystat
= optionp_stat
;
1481 ystat
= optionl_stat
;
1483 if ((*ystat
) (state
.rel_pathname
, &sbuf
) != 0)
1485 if (following_links() && errno
== ENOENT
)
1487 /* If we failed to follow the symlink,
1488 * fall back on looking at the symlink itself.
1490 /* Mimic behavior of ls -lL. */
1491 return (pred_type (pathname
, stat_buf
, pred_ptr
));
1495 error (0, errno
, "%s", pathname
);
1496 state
.exit_status
= 1;
1500 /* Now that we have our stat() information, query it in the same
1501 * way that -type does.
1503 return (pred_type (pathname
, &sbuf
, pred_ptr
));
1506 /* 1) fork to get a child; parent remembers the child pid
1507 2) child execs the command requested
1508 3) parent waits for child; checks for proper pid of child
1512 ret errno status(h) status(l)
1514 pid x signal# 0177 stopped
1515 pid x exit arg 0 term by _exit
1516 pid x 0 signal # term by signal
1517 -1 EINTR parent got signal
1518 -1 other some other kind of error
1520 Return true only if the pid matches, status(l) is
1521 zero, and the exit arg (status high) is 0.
1522 Otherwise return false, possibly printing an error message. */
1526 prep_child_for_exec (boolean close_stdin
)
1530 const char inputfile
[] = "/dev/null";
1531 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
1534 if (open(inputfile
, O_RDONLY
) < 0)
1536 /* This is not entirely fatal, since
1537 * executing the child with a closed
1538 * stdin is almost as good as executing it
1539 * with its stdin attached to /dev/null.
1541 error (0, errno
, "%s", inputfile
);
1549 launch (const struct buildcmd_control
*ctl
,
1550 struct buildcmd_state
*buildstate
)
1554 static int first_time
= 1;
1555 const struct exec_val
*execp
= buildstate
->usercontext
;
1557 /* Null terminate the arg list. */
1558 bc_push_arg (ctl
, buildstate
, (char *) NULL
, 0, NULL
, 0, false);
1560 /* Make sure output of command doesn't get mixed with find output. */
1564 /* Make sure to listen for the kids. */
1568 signal (SIGCHLD
, SIG_DFL
);
1571 child_pid
= fork ();
1572 if (child_pid
== -1)
1573 error (1, errno
, _("cannot fork"));
1576 /* We be the child. */
1577 prep_child_for_exec(execp
->close_stdin
);
1579 /* For -exec and -ok, change directory back to the starting directory.
1580 * for -execdir and -okdir, stay in the directory we are searching
1581 * (the latter is more secure).
1583 if (!execp
->use_current_dir
)
1585 /* Even if DebugSearch is set, don't announce our change of
1586 * directory, since we're not going to emit a subsequent
1587 * announcement of a call to stat() anyway, as we're about
1588 * to exec something.
1590 if (starting_desc
< 0
1591 ? chdir (starting_dir
) != 0
1592 : fchdir (starting_desc
) != 0)
1594 error (0, errno
, "%s", starting_dir
);
1599 execvp (buildstate
->cmd_argv
[0], buildstate
->cmd_argv
);
1600 error (0, errno
, "%s", buildstate
->cmd_argv
[0]);
1605 /* In parent; set up for next time. */
1606 bc_clear_args(ctl
, buildstate
);
1609 while (waitpid (child_pid
, &wait_status
, 0) == (pid_t
) -1)
1613 error (0, errno
, _("error waiting for %s"), buildstate
->cmd_argv
[0]);
1614 state
.exit_status
= 1;
1615 return 0; /* FAIL */
1619 if (WIFSIGNALED (wait_status
))
1621 error (0, 0, _("%s terminated by signal %d"),
1622 buildstate
->cmd_argv
[0], WTERMSIG (wait_status
));
1624 if (execp
->multiple
)
1626 /* -exec \; just returns false if the invoked command fails.
1627 * -exec {} + returns true if the invoked command fails, but
1628 * sets the program exit status.
1630 state
.exit_status
= 1;
1636 if (0 == WEXITSTATUS (wait_status
))
1642 if (execp
->multiple
)
1644 /* -exec \; just returns false if the invoked command fails.
1645 * -exec {} + returns true if the invoked command fails, but
1646 * sets the program exit status.
1648 state
.exit_status
= 1;
1650 return 0; /* FAIL */
1656 /* Return a static string formatting the time WHEN according to the
1657 strftime format character KIND. */
1660 format_date (time_t when
, int kind
)
1662 static char buf
[MAX (LONGEST_HUMAN_READABLE
+ 2, 64)];
1670 strcpy (fmt
, "%F+%T");
1673 && (tm
= localtime (&when
))
1674 && strftime (buf
, sizeof buf
, fmt
, tm
))
1679 char *p
= human_readable (when
< 0 ? -w
: w
, buf
+ 1,
1680 human_ceiling
, 1, 1);
1688 ctime_format (time_t when
)
1690 char *r
= ctime (&when
);
1693 /* The time cannot be represented as a struct tm.
1694 Output it as an integer. */
1695 return format_date (when
, '@');
1699 /* Remove the trailing newline from the ctime output,
1700 being careful not to assume that the output is fixed-width. */
1701 *strchr (r
, '\n') = '\0';
1706 /* Copy STR into BUF and trim blanks from the end of BUF.
1710 blank_rtrim (str
, buf
)
1719 i
= strlen (buf
) - 1;
1720 while ((i
>= 0) && ((buf
[i
] == ' ') || buf
[i
] == '\t'))
1726 /* Print out the predicate list starting at NODE. */
1728 print_list (FILE *fp
, struct predicate
*node
)
1730 struct predicate
*cur
;
1736 fprintf (fp
, "%s ", blank_rtrim (cur
->p_name
, name
));
1737 cur
= cur
->pred_next
;
1742 /* Print out the predicate list starting at NODE. */
1744 print_parenthesised(FILE *fp
, struct predicate
*node
)
1750 if ( ( (node
->pred_func
== pred_or
)
1751 || (node
->pred_func
== pred_and
) )
1752 && node
->pred_left
== NULL
)
1754 /* We print "<nothing> or X" as just "X"
1755 * We print "<nothing> and X" as just "X"
1757 print_parenthesised(fp
, node
->pred_right
);
1761 if (node
->pred_left
|| node
->pred_right
)
1765 fprintf(fp
, "%s", " ( ");
1766 print_optlist(fp
, node
);
1768 fprintf(fp
, "%s", " ) ");
1774 print_optlist (FILE *fp
, const struct predicate
*p
)
1778 print_parenthesised(fp
, p
->pred_left
);
1781 p
->need_stat
? "[call stat] " : "",
1782 p
->need_type
? "[need type] " : "");
1783 print_predicate(fp
, p
);
1784 fprintf(fp
, " [%g] ", p
->est_success_rate
);
1785 print_parenthesised(fp
, p
->pred_right
);
1790 pred_sanity_check(const struct predicate
*predicates
)
1792 const struct predicate
*p
;
1794 for (p
=predicates
; p
!= NULL
; p
=p
->pred_next
)
1796 /* All predicates must do something. */
1797 assert(p
->pred_func
!= NULL
);
1799 /* All predicates must have a parser table entry. */
1800 assert(p
->parser_entry
!= NULL
);
1802 /* If the parser table tells us that just one predicate function is
1803 * possible, verify that that is still the one that is in effect.
1804 * If the parser has NULL for the predicate function, that means that
1805 * the parse_xxx function fills it in, so we can't check it.
1807 if (p
->parser_entry
->pred_func
)
1809 assert(p
->parser_entry
->pred_func
== p
->pred_func
);
1812 switch (p
->parser_entry
->type
)
1814 /* Options all take effect during parsing, so there should
1815 * be no predicate entries corresponding to them. Hence we
1816 * should not see any ARG_OPTION or ARG_POSITIONAL_OPTION
1819 * This is a silly way of coding this test, but it prevents
1820 * a compiler warning (i.e. otherwise it would think that
1821 * there would be case statements missing).
1824 case ARG_POSITIONAL_OPTION
:
1825 assert(p
->parser_entry
->type
!= ARG_OPTION
);
1826 assert(p
->parser_entry
->type
!= ARG_POSITIONAL_OPTION
);
1830 assert(p
->side_effects
); /* actions have side effects. */
1831 if (p
->pred_func
!= pred_prune
&& p
->pred_func
!= pred_quit
)
1833 /* actions other than -prune and -quit should
1834 * inhibit the default -print
1836 assert(p
->no_default_print
);
1840 case ARG_PUNCTUATION
:
1843 /* Punctuation and tests should have no side
1844 * effects and not inhibit default print.
1846 assert(!p
->no_default_print
);
1847 assert(!p
->side_effects
);