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,
28 #include <sys/types.h>
38 #include "printquoted.h"
45 # define _(Text) gettext (Text)
50 # define N_(String) gettext_noop (String)
52 /* See locate.c for explanation as to why not use (String) */
53 # define N_(String) String
56 #if !defined(SIGCHLD) && defined(SIGCLD)
57 #define SIGCHLD SIGCLD
64 # define NAMLEN(dirent) strlen((dirent)->d_name)
66 # define dirent direct
67 # define NAMLEN(dirent) (dirent)->d_namlen
69 # include <sys/ndir.h>
80 /* Fake a return value. */
81 #define CLOSEDIR(d) (closedir (d), 0)
83 #define CLOSEDIR(d) closedir (d)
89 /* Get or fake the disk device blocksize.
90 Usually defined by sys/param.h (if at all). */
93 # define DEV_BSIZE BSIZE
95 # define DEV_BSIZE 4096
97 #endif /* !DEV_BSIZE */
99 /* Extract or fake data from a `struct stat'.
100 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
101 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
102 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
103 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
104 # define ST_BLKSIZE(statbuf) DEV_BSIZE
105 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
106 # define ST_NBLOCKS(statbuf) \
107 (S_ISREG ((statbuf).st_mode) \
108 || S_ISDIR ((statbuf).st_mode) \
109 ? (statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0) : 0)
110 # else /* !_POSIX_SOURCE && BSIZE */
111 # define ST_NBLOCKS(statbuf) \
112 (S_ISREG ((statbuf).st_mode) \
113 || S_ISDIR ((statbuf).st_mode) \
114 ? st_blocks ((statbuf).st_size) : 0)
115 # endif /* !_POSIX_SOURCE && BSIZE */
116 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
117 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
118 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
119 ? (statbuf).st_blksize : DEV_BSIZE)
120 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
121 /* HP-UX counts st_blocks in 1024-byte units.
122 This loses when mixing HP-UX and BSD filesystems with NFS. */
123 # define ST_NBLOCKSIZE 1024
125 # if defined(_AIX) && defined(_I386)
126 /* AIX PS/2 counts st_blocks in 4K units. */
127 # define ST_NBLOCKSIZE (4 * 1024)
128 # else /* not AIX PS/2 */
130 # define ST_NBLOCKS(statbuf) \
131 (S_ISREG ((statbuf).st_mode) \
132 || S_ISDIR ((statbuf).st_mode) \
133 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
135 # endif /* not AIX PS/2 */
137 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
140 # define ST_NBLOCKS(statbuf) \
141 (S_ISREG ((statbuf).st_mode) \
142 || S_ISDIR ((statbuf).st_mode) \
143 ? (statbuf).st_blocks : 0)
146 #ifndef ST_NBLOCKSIZE
147 # define ST_NBLOCKSIZE 512
152 #define MAX(a, b) ((a) > (b) ? (a) : (b))
154 static boolean match_lname
PARAMS((char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
, boolean ignore_case
));
156 static char *format_date
PARAMS((time_t when
, int kind
));
157 static char *ctime_format
PARAMS((time_t when
));
166 struct pred_assoc pred_table
[] =
168 {pred_amin
, "amin "},
170 {pred_anewer
, "anewer "},
171 {pred_atime
, "atime "},
173 {pred_cmin
, "cmin "},
174 {pred_cnewer
, "cnewer "},
176 {pred_ctime
, "ctime "},
177 {pred_delete
, "delete "},
178 {pred_empty
, "empty "},
179 {pred_exec
, "exec "},
180 {pred_execdir
, "execdir "},
181 {pred_executable
, "executable "},
182 {pred_false
, "false "},
183 {pred_fprint
, "fprint "},
184 {pred_fprint0
, "fprint0 "},
185 {pred_fprintf
, "fprintf "},
186 {pred_fstype
, "fstype "},
188 {pred_group
, "group "},
189 {pred_ilname
, "ilname "},
190 {pred_iname
, "iname "},
191 {pred_inum
, "inum "},
192 {pred_ipath
, "ipath "},
193 {pred_links
, "links "},
194 {pred_lname
, "lname "},
196 {pred_mmin
, "mmin "},
197 {pred_mtime
, "mtime "},
198 {pred_name
, "name "},
199 {pred_negate
, "not "},
200 {pred_newer
, "newer "},
201 {pred_nogroup
, "nogroup "},
202 {pred_nouser
, "nouser "},
204 {pred_okdir
, "okdir "},
207 {pred_path
, "path "},
208 {pred_perm
, "perm "},
209 {pred_print
, "print "},
210 {pred_print0
, "print0 "},
211 {pred_prune
, "prune "},
212 {pred_quit
, "quit "},
213 {pred_readable
, "readable "},
214 {pred_regex
, "regex "},
215 {pred_samefile
,"samefile "},
216 {pred_size
, "size "},
217 {pred_true
, "true "},
218 {pred_type
, "type "},
220 {pred_used
, "used "},
221 {pred_user
, "user "},
222 {pred_writable
, "writable "},
223 {pred_xtype
, "xtype "},
228 /* Predicate processing routines.
230 PATHNAME is the full pathname of the file being checked.
231 *STAT_BUF contains information about PATHNAME.
232 *PRED_PTR contains information for applying the predicate.
234 Return true if the file passes this predicate, false if not. */
239 * Returns true if THE_TIME is
240 * COMP_GT: after the specified time
241 * COMP_LT: before the specified time
242 * COMP_EQ: less than WINDOW seconds after the specified time.
245 pred_timewindow(time_t the_time
, struct predicate
const *pred_ptr
, int window
)
247 switch (pred_ptr
->args
.info
.kind
)
250 if (the_time
> (time_t) pred_ptr
->args
.info
.l_val
)
254 if (the_time
< (time_t) pred_ptr
->args
.info
.l_val
)
258 if ((the_time
>= (time_t) pred_ptr
->args
.info
.l_val
)
259 && (the_time
< (time_t) pred_ptr
->args
.info
.l_val
+ window
))
268 pred_amin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
271 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, 60);
275 pred_and (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
277 if (pred_ptr
->pred_left
== NULL
278 || (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
279 pred_ptr
->pred_left
))
281 /* Check whether we need a stat here. */
282 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
284 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
285 pred_ptr
->pred_right
));
292 pred_anewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
296 if (stat_buf
->st_atime
> pred_ptr
->args
.time
)
302 pred_atime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
305 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, DAYSECS
);
309 pred_close (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
319 pred_cmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
322 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, 60);
326 pred_cnewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
330 if (stat_buf
->st_ctime
> pred_ptr
->args
.time
)
337 pred_comma (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
339 if (pred_ptr
->pred_left
!= NULL
)
340 (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
341 pred_ptr
->pred_left
);
342 /* Check whether we need a stat here. */
343 /* TODO: what about need_type? */
344 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
346 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
347 pred_ptr
->pred_right
));
351 pred_ctime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
354 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, DAYSECS
);
358 pred_delete (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
362 if (strcmp (state
.rel_pathname
, "."))
364 if (0 != remove (state
.rel_pathname
))
366 error (0, errno
, "cannot delete %s", pathname
);
380 pred_empty (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
385 if (S_ISDIR (stat_buf
->st_mode
))
389 boolean empty
= true;
392 d
= opendir (state
.rel_pathname
);
395 error (0, errno
, "%s", pathname
);
396 state
.exit_status
= 1;
399 for (dp
= readdir (d
); dp
; dp
= readdir (d
))
401 if (dp
->d_name
[0] != '.'
402 || (dp
->d_name
[1] != '\0'
403 && (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0')))
411 error (0, errno
, "%s", pathname
);
412 state
.exit_status
= 1;
417 else if (S_ISREG (stat_buf
->st_mode
))
418 return (stat_buf
->st_size
== 0);
424 new_impl_pred_exec (const char *pathname
, struct stat
*stat_buf
,
425 struct predicate
*pred_ptr
,
426 const char *prefix
, size_t pfxlen
)
428 struct exec_val
*execp
= &pred_ptr
->args
.exec_vec
;
429 size_t len
= strlen(pathname
);
435 /* Push the argument onto the current list.
436 * The command may or may not be run at this point,
437 * depending on the command line length limits.
439 bc_push_arg(&execp
->ctl
,
445 /* POSIX: If the primary expression is punctuated by a plus
446 * sign, the primary shall always evaluate as true
454 for (i
=0; i
<execp
->num_args
; ++i
)
456 bc_do_insert(&execp
->ctl
,
458 execp
->replace_vec
[i
],
459 strlen(execp
->replace_vec
[i
]),
465 /* Actually invoke the command. */
466 return execp
->ctl
.exec_callback(&execp
->ctl
,
473 pred_exec (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
475 return new_impl_pred_exec(pathname
, stat_buf
, pred_ptr
, NULL
, 0);
479 pred_execdir (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
481 const char *prefix
= (state
.rel_pathname
[0] == '/') ? NULL
: "./";
483 return new_impl_pred_exec (state
.rel_pathname
, stat_buf
, pred_ptr
,
484 prefix
, (prefix
? 2 : 0));
488 pred_false (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
499 pred_fls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
501 list_file (pathname
, state
.rel_pathname
, stat_buf
, options
.start_time
,
502 options
.output_block_size
,
503 pred_ptr
->literal_control_chars
, pred_ptr
->args
.stream
);
508 pred_fprint (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
513 print_quoted(pred_ptr
->args
.printf_vec
.stream
,
514 pred_ptr
->args
.printf_vec
.quote_opts
,
515 pred_ptr
->args
.printf_vec
.dest_is_tty
,
522 pred_fprint0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
527 fputs (pathname
, pred_ptr
->args
.stream
);
528 putc (0, pred_ptr
->args
.stream
);
535 mode_to_filetype(mode_t m
)
538 m
== S_IFSOCK
? "s" :
545 m
== S_IFDOOR
? "D" :
547 m
== S_IFIFO
? "p" : "U";
551 file_sparseness(const struct stat
*p
)
555 if (0 == p
->st_blocks
)
558 return p
->st_blocks
< 0 ? -HUGE_VAL
: HUGE_VAL
;
562 double blklen
= file_blocksize(p
) * (double)p
->st_blocks
;
563 return blklen
/ p
->st_size
;
571 struct segment
*segment
,
573 const struct stat
*stat_buf
,
575 const struct quoting_options
*qopts
)
577 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
580 switch (segment
->segkind
)
582 case KIND_PLAIN
: /* Plain text string (no % conversion). */
584 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
587 case KIND_STOP
: /* Terminate argument and flush output. */
589 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
594 switch (segment
->format_char
[0])
596 case 'a': /* atime in `ctime' format. */
597 /* UNTRUSTED, probably unexploitable */
598 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_atime
));
600 case 'b': /* size in 512-byte blocks */
601 /* UNTRUSTED, probably unexploitable */
602 fprintf (fp
, segment
->text
,
603 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
605 ST_NBLOCKSIZE
, 512));
607 case 'c': /* ctime in `ctime' format */
608 /* UNTRUSTED, probably unexploitable */
609 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_ctime
));
611 case 'd': /* depth in search tree */
612 /* UNTRUSTED, probably unexploitable */
613 fprintf (fp
, segment
->text
, state
.curdepth
);
615 case 'D': /* Device on which file exists (stat.st_dev) */
617 fprintf (fp
, segment
->text
,
618 human_readable ((uintmax_t) stat_buf
->st_dev
, hbuf
,
619 human_ceiling
, 1, 1));
621 case 'f': /* base name of path */
623 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, base_name (pathname
));
625 case 'F': /* filesystem type */
627 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, filesystem_type (stat_buf
, pathname
));
629 case 'g': /* group name */
631 /* (well, the actual group is selected by the user but
632 * its name was selected by the system administrator)
637 g
= getgrgid (stat_buf
->st_gid
);
640 segment
->text
[segment
->text_len
] = 's';
641 fprintf (fp
, segment
->text
, g
->gr_name
);
650 case 'G': /* GID number */
651 /* UNTRUSTED, probably unexploitable */
652 fprintf (fp
, segment
->text
,
653 human_readable ((uintmax_t) stat_buf
->st_gid
, hbuf
,
654 human_ceiling
, 1, 1));
656 case 'h': /* leading directories part of path */
661 cp
= strrchr (pathname
, '/');
662 if (cp
== NULL
) /* No leading directories. */
664 /* If there is no slash in the pathname, we still
665 * print the string because it contains characters
666 * other than just '%s'. The %h expands to ".".
668 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, ".");
674 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, pathname
);
679 case 'H': /* ARGV element file was found under */
682 char cc
= pathname
[state
.starting_path_length
];
684 pathname
[state
.starting_path_length
] = '\0';
685 fprintf (fp
, segment
->text
, pathname
);
686 pathname
[state
.starting_path_length
] = cc
;
689 case 'i': /* inode number */
690 /* UNTRUSTED, but not exploitable I think */
691 fprintf (fp
, segment
->text
,
692 human_readable ((uintmax_t) stat_buf
->st_ino
, hbuf
,
696 case 'k': /* size in 1K blocks */
697 /* UNTRUSTED, but not exploitable I think */
698 fprintf (fp
, segment
->text
,
699 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
701 ST_NBLOCKSIZE
, 1024));
703 case 'l': /* object of symlink */
709 if (S_ISLNK (stat_buf
->st_mode
))
711 linkname
= get_link_name (pathname
, state
.rel_pathname
);
713 state
.exit_status
= 1;
717 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, linkname
);
721 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, "");
726 case 'M': /* mode as 10 chars (eg., "-rwxr-x--x" */
727 /* UNTRUSTED, probably unexploitable */
729 char modestring
[16] ;
730 filemodestring (stat_buf
, modestring
);
731 modestring
[10] = '\0';
732 fprintf (fp
, segment
->text
, modestring
);
736 case 'm': /* mode as octal number (perms only) */
737 /* UNTRUSTED, probably unexploitable */
739 /* Output the mode portably using the traditional numbers,
740 even if the host unwisely uses some other numbering
741 scheme. But help the compiler in the common case where
742 the host uses the traditional numbering scheme. */
743 mode_t m
= stat_buf
->st_mode
;
744 boolean traditional_numbering_scheme
=
745 (S_ISUID
== 04000 && S_ISGID
== 02000 && S_ISVTX
== 01000
746 && S_IRUSR
== 00400 && S_IWUSR
== 00200 && S_IXUSR
== 00100
747 && S_IRGRP
== 00040 && S_IWGRP
== 00020 && S_IXGRP
== 00010
748 && S_IROTH
== 00004 && S_IWOTH
== 00002 && S_IXOTH
== 00001);
749 fprintf (fp
, segment
->text
,
750 (traditional_numbering_scheme
752 : ((m
& S_ISUID
? 04000 : 0)
753 | (m
& S_ISGID
? 02000 : 0)
754 | (m
& S_ISVTX
? 01000 : 0)
755 | (m
& S_IRUSR
? 00400 : 0)
756 | (m
& S_IWUSR
? 00200 : 0)
757 | (m
& S_IXUSR
? 00100 : 0)
758 | (m
& S_IRGRP
? 00040 : 0)
759 | (m
& S_IWGRP
? 00020 : 0)
760 | (m
& S_IXGRP
? 00010 : 0)
761 | (m
& S_IROTH
? 00004 : 0)
762 | (m
& S_IWOTH
? 00002 : 0)
763 | (m
& S_IXOTH
? 00001 : 0))));
767 case 'n': /* number of links */
768 /* UNTRUSTED, probably unexploitable */
769 fprintf (fp
, segment
->text
,
770 human_readable ((uintmax_t) stat_buf
->st_nlink
,
775 case 'p': /* pathname */
777 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, pathname
);
779 case 'P': /* pathname with ARGV element stripped */
781 if (state
.curdepth
> 0)
783 cp
= pathname
+ state
.starting_path_length
;
785 /* Move past the slash between the ARGV element
786 and the rest of the pathname. But if the ARGV element
787 ends in a slash, we didn't add another, so we've
788 already skipped past it. */
793 print_quoted (fp
, qopts
, ttyflag
, segment
->text
, cp
);
795 case 's': /* size in bytes */
796 /* UNTRUSTED, probably unexploitable */
797 fprintf (fp
, segment
->text
,
798 human_readable ((uintmax_t) stat_buf
->st_size
,
799 hbuf
, human_ceiling
, 1, 1));
802 case 'S': /* sparseness */
803 /* UNTRUSTED, probably unexploitable */
804 fprintf (fp
, segment
->text
, file_sparseness(stat_buf
));;
807 case 't': /* mtime in `ctime' format */
808 /* UNTRUSTED, probably unexploitable */
809 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_mtime
));
811 case 'u': /* user name */
813 /* (well, the actual user is selected by the user on systems
814 * where chown is not restricted, but the user name was
815 * selected by the system administrator)
820 p
= getpwuid (stat_buf
->st_uid
);
823 segment
->text
[segment
->text_len
] = 's';
824 fprintf (fp
, segment
->text
, p
->pw_name
);
830 case 'U': /* UID number */
831 /* UNTRUSTED, probably unexploitable */
832 fprintf (fp
, segment
->text
,
833 human_readable ((uintmax_t) stat_buf
->st_uid
, hbuf
,
834 human_ceiling
, 1, 1));
837 /* type of filesystem entry like `ls -l`: (d,-,l,s,p,b,c,n) n=nonexistent(symlink) */
838 case 'Y': /* in case of symlink */
842 if (S_ISLNK (stat_buf
->st_mode
))
845 /* If we would normally follow links, do not do so.
846 * If we would normally not follow links, do so.
848 if ((following_links() ? lstat
: stat
)
849 (state
.rel_pathname
, &sbuf
) != 0)
851 if ( errno
== ENOENT
) {
852 fprintf (fp
, segment
->text
, "N");
855 if ( errno
== ELOOP
) {
856 fprintf (fp
, segment
->text
, "L");
859 error (0, errno
, "%s", pathname
);
863 fprintf (fp
, segment
->text
,
864 mode_to_filetype(sbuf
.st_mode
& S_IFMT
));
869 fprintf (fp
, segment
->text
,
870 mode_to_filetype(stat_buf
->st_mode
& S_IFMT
));
878 fprintf (fp
, segment
->text
,
879 mode_to_filetype(stat_buf
->st_mode
& S_IFMT
));
888 pred_fprintf (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
890 FILE *fp
= pred_ptr
->args
.printf_vec
.stream
;
891 struct segment
*segment
;
892 boolean ttyflag
= pred_ptr
->args
.printf_vec
.dest_is_tty
;
893 const struct quoting_options
*qopts
= pred_ptr
->args
.printf_vec
.quote_opts
;
895 for (segment
= pred_ptr
->args
.printf_vec
.segment
; segment
;
896 segment
= segment
->next
)
898 if ( (KIND_FORMAT
== segment
->segkind
) && segment
->format_char
[1]) /* Component of date. */
902 switch (segment
->format_char
[0])
905 t
= stat_buf
->st_atime
;
908 t
= stat_buf
->st_ctime
;
911 t
= stat_buf
->st_mtime
;
917 /* We trust the output of format_date not to contain
918 * nasty characters, though the value of the date
919 * is itself untrusted data.
922 fprintf (fp
, segment
->text
,
923 format_date (t
, segment
->format_char
[1]));
927 do_fprintf(fp
, segment
, pathname
, stat_buf
, ttyflag
, qopts
);
934 pred_fstype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
938 if (strcmp (filesystem_type (stat_buf
, pathname
), pred_ptr
->args
.str
) == 0)
945 pred_gid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
949 switch (pred_ptr
->args
.info
.kind
)
952 if (stat_buf
->st_gid
> pred_ptr
->args
.info
.l_val
)
956 if (stat_buf
->st_gid
< pred_ptr
->args
.info
.l_val
)
960 if (stat_buf
->st_gid
== pred_ptr
->args
.info
.l_val
)
968 pred_group (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
972 if (pred_ptr
->args
.gid
== stat_buf
->st_gid
)
979 pred_ilname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
981 return match_lname (pathname
, stat_buf
, pred_ptr
, true);
985 pred_iname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
991 /* FNM_PERIOD is not used here because POSIX requires that it not be.
992 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
994 base
= base_name (pathname
);
995 if (fnmatch (pred_ptr
->args
.str
, base
, FNM_CASEFOLD
) == 0)
1001 pred_inum (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1005 switch (pred_ptr
->args
.info
.kind
)
1008 if (stat_buf
->st_ino
> pred_ptr
->args
.info
.l_val
)
1012 if (stat_buf
->st_ino
< pred_ptr
->args
.info
.l_val
)
1016 if (stat_buf
->st_ino
== pred_ptr
->args
.info
.l_val
)
1024 pred_ipath (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1028 if (fnmatch (pred_ptr
->args
.str
, pathname
, FNM_CASEFOLD
) == 0)
1034 pred_links (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1038 switch (pred_ptr
->args
.info
.kind
)
1041 if (stat_buf
->st_nlink
> pred_ptr
->args
.info
.l_val
)
1045 if (stat_buf
->st_nlink
< pred_ptr
->args
.info
.l_val
)
1049 if (stat_buf
->st_nlink
== pred_ptr
->args
.info
.l_val
)
1057 pred_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1059 return match_lname (pathname
, stat_buf
, pred_ptr
, false);
1063 match_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
, boolean ignore_case
)
1065 boolean ret
= false;
1067 if (S_ISLNK (stat_buf
->st_mode
))
1069 char *linkname
= get_link_name (pathname
, state
.rel_pathname
);
1072 if (fnmatch (pred_ptr
->args
.str
, linkname
,
1073 ignore_case
? FNM_CASEFOLD
: 0) == 0)
1078 #endif /* S_ISLNK */
1083 pred_ls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1085 list_file (pathname
, state
.rel_pathname
, stat_buf
, options
.start_time
,
1086 options
.output_block_size
,
1087 pred_ptr
->literal_control_chars
,
1093 pred_mmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1096 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, 60);
1100 pred_mtime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1103 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, DAYSECS
);
1107 pred_name (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1112 base
= base_name (pathname
);
1114 /* FNM_PERIOD is not used here because POSIX requires that it not be.
1115 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
1117 if (fnmatch (pred_ptr
->args
.str
, base
, 0) == 0)
1123 pred_negate (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1125 /* Check whether we need a stat here. */
1126 /* TODO: what about need_type? */
1127 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
1129 return (!(*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1130 pred_ptr
->pred_right
));
1134 pred_newer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1138 if (stat_buf
->st_mtime
> pred_ptr
->args
.time
)
1144 pred_nogroup (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1150 extern char *gid_unused
;
1152 return gid_unused
[(unsigned) stat_buf
->st_gid
];
1154 return getgrgid (stat_buf
->st_gid
) == NULL
;
1159 pred_nouser (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1162 extern char *uid_unused
;
1169 return uid_unused
[(unsigned) stat_buf
->st_uid
];
1171 return getpwuid (stat_buf
->st_uid
) == NULL
;
1177 is_ok(const char *program
, const char *arg
)
1180 /* The draft open standard requires that, in the POSIX locale,
1181 the last non-blank character of this prompt be '?'.
1182 The exact format is not specified.
1183 This standard does not have requirements for locales other than POSIX
1185 /* XXX: printing UNTRUSTED data here. */
1186 fprintf (stderr
, _("< %s ... %s > ? "), program
, arg
);
1192 pred_ok (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1194 if (is_ok(pred_ptr
->args
.exec_vec
.replace_vec
[0], pathname
))
1195 return new_impl_pred_exec (pathname
, stat_buf
, pred_ptr
, NULL
, 0);
1201 pred_okdir (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1203 const char *prefix
= (state
.rel_pathname
[0] == '/') ? NULL
: "./";
1204 if (is_ok(pred_ptr
->args
.exec_vec
.replace_vec
[0], pathname
))
1205 return new_impl_pred_exec (state
.rel_pathname
, stat_buf
, pred_ptr
,
1206 prefix
, (prefix
? 2 : 0));
1212 pred_open (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1221 pred_or (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1223 if (pred_ptr
->pred_left
== NULL
1224 || !(*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
1225 pred_ptr
->pred_left
))
1227 if (get_info(pathname
, state
.rel_pathname
, stat_buf
, pred_ptr
) != 0)
1229 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1230 pred_ptr
->pred_right
));
1237 pred_path (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1240 if (fnmatch (pred_ptr
->args
.str
, pathname
, 0) == 0)
1246 pred_perm (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1248 mode_t mode
= stat_buf
->st_mode
;
1249 mode_t perm_val
= pred_ptr
->args
.perm
.val
[S_ISDIR (mode
) != 0];
1251 switch (pred_ptr
->args
.perm
.kind
)
1254 return (mode
& perm_val
) == perm_val
;
1258 /* True if any of the bits set in the mask are also set in the file's mode.
1261 * Otherwise, if onum is prefixed by a hyphen, the primary shall
1262 * evaluate as true if at least all of the bits specified in
1263 * onum that are also set in the octal mask 07777 are set.
1265 * Eric Blake's interpretation is that the mode argument is zero,
1269 return true; /* Savannah bug 14748; we used to return false */
1271 return (mode
& perm_val
) != 0;
1275 return (mode
& MODE_ALL
) == perm_val
;
1286 pred_executable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1292 return 0 == access(state
.rel_pathname
, X_OK
);
1296 pred_readable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1302 return 0 == access(state
.rel_pathname
, R_OK
);
1306 pred_writable (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1312 return 0 == access(state
.rel_pathname
, W_OK
);
1316 pred_print (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1320 /* puts (pathname); */
1321 print_quoted(pred_ptr
->args
.printf_vec
.stream
,
1322 pred_ptr
->args
.printf_vec
.quote_opts
,
1323 pred_ptr
->args
.printf_vec
.dest_is_tty
,
1329 pred_print0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1333 fputs (pathname
, stdout
);
1339 pred_prune (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1344 if (options
.do_dir_first
== true &&
1346 S_ISDIR(stat_buf
->st_mode
))
1347 state
.stop_at_current_level
= true;
1349 return (options
.do_dir_first
); /* This is what SunOS find seems to do. */
1353 pred_quit (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1359 /* Run any cleanups. This includes executing any command lines
1360 * we have partly built but not executed.
1364 /* Since -exec and friends don't leave child processes running in the
1365 * background, there is no need to wait for them here.
1367 exit(state
.exit_status
); /* 0 for success, etc. */
1371 pred_regex (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1373 int len
= strlen (pathname
);
1375 if (re_match (pred_ptr
->args
.regex
, pathname
, len
, 0,
1376 (struct re_registers
*) NULL
) == len
)
1382 pred_size (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1387 f_val
= ((stat_buf
->st_size
/ pred_ptr
->args
.size
.blocksize
)
1388 + (stat_buf
->st_size
% pred_ptr
->args
.size
.blocksize
!= 0));
1389 switch (pred_ptr
->args
.size
.kind
)
1392 if (f_val
> pred_ptr
->args
.size
.size
)
1396 if (f_val
< pred_ptr
->args
.size
.size
)
1400 if (f_val
== pred_ptr
->args
.size
.size
)
1408 pred_samefile (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1410 /* Potential optimisation: because of the loop protection, we always
1411 * know the device of the current directory, hence the device number
1412 * of the file we're currently considering. If -L is not in effect,
1413 * and the device number of the file we're looking for is not the
1414 * same as the device number of the current directory, this
1415 * predicate cannot return true. Hence there would be no need to
1416 * stat the file we're looking at.
1420 return stat_buf
->st_ino
== pred_ptr
->args
.fileid
.ino
1421 && stat_buf
->st_dev
== pred_ptr
->args
.fileid
.dev
;
1425 pred_true (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1434 pred_type (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1437 mode_t type
= pred_ptr
->args
.type
;
1439 assert(state
.have_type
);
1440 assert(state
.type
!= 0);
1444 if (state
.have_stat
)
1445 mode
= stat_buf
->st_mode
;
1450 /* POSIX system; check `mode' the slow way. */
1451 if ((S_ISBLK (mode
) && type
== S_IFBLK
)
1452 || (S_ISCHR (mode
) && type
== S_IFCHR
)
1453 || (S_ISDIR (mode
) && type
== S_IFDIR
)
1454 || (S_ISREG (mode
) && type
== S_IFREG
)
1456 || (S_ISLNK (mode
) && type
== S_IFLNK
)
1459 || (S_ISFIFO (mode
) && type
== S_IFIFO
)
1462 || (S_ISSOCK (mode
) && type
== S_IFSOCK
)
1465 || (S_ISDOOR (mode
) && type
== S_IFDOOR
)
1469 /* Unix system; check `mode' the fast way. */
1470 if ((mode
& S_IFMT
) == type
)
1478 pred_uid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1481 switch (pred_ptr
->args
.info
.kind
)
1484 if (stat_buf
->st_uid
> pred_ptr
->args
.info
.l_val
)
1488 if (stat_buf
->st_uid
< pred_ptr
->args
.info
.l_val
)
1492 if (stat_buf
->st_uid
== pred_ptr
->args
.info
.l_val
)
1500 pred_used (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1505 delta
= stat_buf
->st_atime
- stat_buf
->st_ctime
; /* Use difftime? */
1506 return pred_timewindow(delta
, pred_ptr
, DAYSECS
);
1510 pred_user (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1513 if (pred_ptr
->args
.uid
== stat_buf
->st_uid
)
1520 pred_xtype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1522 struct stat sbuf
; /* local copy, not stat_buf because we're using a different stat method */
1523 int (*ystat
) (const char*, struct stat
*p
);
1525 /* If we would normally stat the link itself, stat the target instead.
1526 * If we would normally follow the link, stat the link itself instead.
1528 if (following_links())
1529 ystat
= optionp_stat
;
1531 ystat
= optionl_stat
;
1533 if ((*ystat
) (state
.rel_pathname
, &sbuf
) != 0)
1535 if (following_links() && errno
== ENOENT
)
1537 /* If we failed to follow the symlink,
1538 * fall back on looking at the symlink itself.
1540 /* Mimic behavior of ls -lL. */
1541 return (pred_type (pathname
, stat_buf
, pred_ptr
));
1545 error (0, errno
, "%s", pathname
);
1546 state
.exit_status
= 1;
1550 /* Now that we have our stat() information, query it in the same
1551 * way that -type does.
1553 return (pred_type (pathname
, &sbuf
, pred_ptr
));
1556 /* 1) fork to get a child; parent remembers the child pid
1557 2) child execs the command requested
1558 3) parent waits for child; checks for proper pid of child
1562 ret errno status(h) status(l)
1564 pid x signal# 0177 stopped
1565 pid x exit arg 0 term by _exit
1566 pid x 0 signal # term by signal
1567 -1 EINTR parent got signal
1568 -1 other some other kind of error
1570 Return true only if the pid matches, status(l) is
1571 zero, and the exit arg (status high) is 0.
1572 Otherwise return false, possibly printing an error message. */
1576 prep_child_for_exec (boolean close_stdin
)
1580 const char inputfile
[] = "/dev/null";
1581 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
1584 if (open(inputfile
, O_RDONLY
) < 0)
1586 /* This is not entirely fatal, since
1587 * executing the child with a closed
1588 * stdin is almost as good as executing it
1589 * with its stdin attached to /dev/null.
1591 error (0, errno
, "%s", inputfile
);
1599 launch (const struct buildcmd_control
*ctl
,
1600 struct buildcmd_state
*buildstate
)
1604 static int first_time
= 1;
1605 const struct exec_val
*execp
= buildstate
->usercontext
;
1607 /* Null terminate the arg list. */
1608 bc_push_arg (ctl
, buildstate
, (char *) NULL
, 0, NULL
, 0, false);
1610 /* Make sure output of command doesn't get mixed with find output. */
1614 /* Make sure to listen for the kids. */
1618 signal (SIGCHLD
, SIG_DFL
);
1621 child_pid
= fork ();
1622 if (child_pid
== -1)
1623 error (1, errno
, _("cannot fork"));
1626 /* We be the child. */
1627 prep_child_for_exec(execp
->close_stdin
);
1629 /* For -exec and -ok, change directory back to the starting directory.
1630 * for -execdir and -okdir, stay in the directory we are searching
1631 * (the latter is more secure).
1633 if (!execp
->use_current_dir
)
1635 /* Even if DebugSearch is set, don't announce our change of
1636 * directory, since we're not going to emit a subsequent
1637 * announcement of a call to stat() anyway, as we're about
1638 * to exec something.
1640 if (starting_desc
< 0
1641 ? chdir (starting_dir
) != 0
1642 : fchdir (starting_desc
) != 0)
1644 error (0, errno
, "%s", starting_dir
);
1649 execvp (buildstate
->cmd_argv
[0], buildstate
->cmd_argv
);
1650 error (0, errno
, "%s", buildstate
->cmd_argv
[0]);
1655 /* In parent; set up for next time. */
1656 bc_clear_args(ctl
, buildstate
);
1659 while (waitpid (child_pid
, &wait_status
, 0) == (pid_t
) -1)
1663 error (0, errno
, _("error waiting for %s"), buildstate
->cmd_argv
[0]);
1664 state
.exit_status
= 1;
1665 return 0; /* FAIL */
1669 if (WIFSIGNALED (wait_status
))
1671 error (0, 0, _("%s terminated by signal %d"),
1672 buildstate
->cmd_argv
[0], WTERMSIG (wait_status
));
1674 if (execp
->multiple
)
1676 /* -exec \; just returns false if the invoked command fails.
1677 * -exec {} + returns true if the invoked command fails, but
1678 * sets the program exit status.
1680 state
.exit_status
= 1;
1686 if (0 == WEXITSTATUS (wait_status
))
1692 if (execp
->multiple
)
1694 /* -exec \; just returns false if the invoked command fails.
1695 * -exec {} + returns true if the invoked command fails, but
1696 * sets the program exit status.
1698 state
.exit_status
= 1;
1700 return 0; /* FAIL */
1706 /* Return a static string formatting the time WHEN according to the
1707 strftime format character KIND. */
1710 format_date (time_t when
, int kind
)
1712 static char buf
[MAX (LONGEST_HUMAN_READABLE
+ 2, 64)];
1720 strcpy (fmt
, "%F+%T");
1723 && (tm
= localtime (&when
))
1724 && strftime (buf
, sizeof buf
, fmt
, tm
))
1729 char *p
= human_readable (when
< 0 ? -w
: w
, buf
+ 1,
1730 human_ceiling
, 1, 1);
1738 ctime_format (time_t when
)
1740 char *r
= ctime (&when
);
1743 /* The time cannot be represented as a struct tm.
1744 Output it as an integer. */
1745 return format_date (when
, '@');
1749 /* Remove the trailing newline from the ctime output,
1750 being careful not to assume that the output is fixed-width. */
1751 *strchr (r
, '\n') = '\0';
1756 /* Copy STR into BUF and trim blanks from the end of BUF.
1760 blank_rtrim (str
, buf
)
1769 i
= strlen (buf
) - 1;
1770 while ((i
>= 0) && ((buf
[i
] == ' ') || buf
[i
] == '\t'))
1776 /* Print out the predicate list starting at NODE. */
1778 print_list (FILE *fp
, struct predicate
*node
)
1780 struct predicate
*cur
;
1786 fprintf (fp
, "%s ", blank_rtrim (cur
->p_name
, name
));
1787 cur
= cur
->pred_next
;
1792 /* Print out the predicate list starting at NODE. */
1794 print_parenthesised(FILE *fp
, struct predicate
*node
)
1800 if ( ( (node
->pred_func
== pred_or
)
1801 || (node
->pred_func
== pred_and
) )
1802 && node
->pred_left
== NULL
)
1804 /* We print "<nothing> or X" as just "X"
1805 * We print "<nothing> and X" as just "X"
1807 print_parenthesised(fp
, node
->pred_right
);
1811 if (node
->pred_left
|| node
->pred_right
)
1815 fprintf(fp
, "%s", " ( ");
1816 print_optlist(fp
, node
);
1818 fprintf(fp
, "%s", " ) ");
1824 print_optlist (FILE *fp
, const struct predicate
*p
)
1828 print_parenthesised(fp
, p
->pred_left
);
1831 p
->need_stat
? "[call stat] " : "",
1832 p
->need_type
? "[need type] " : "");
1833 print_predicate(fp
, p
);
1834 fprintf(fp
, " [%g] ", p
->est_success_rate
);
1835 print_parenthesised(fp
, p
->pred_right
);
1840 pred_sanity_check(const struct predicate
*predicates
)
1842 const struct predicate
*p
;
1844 for (p
=predicates
; p
!= NULL
; p
=p
->pred_next
)
1846 /* All predicates must do something. */
1847 assert(p
->pred_func
!= NULL
);
1849 /* All predicates must have a parser table entry. */
1850 assert(p
->parser_entry
!= NULL
);
1852 /* If the parser table tells us that just one predicate function is
1853 * possible, verify that that is still the one that is in effect.
1854 * If the parser has NULL for the predicate function, that means that
1855 * the parse_xxx function fills it in, so we can't check it.
1857 if (p
->parser_entry
->pred_func
)
1859 assert(p
->parser_entry
->pred_func
== p
->pred_func
);
1862 switch (p
->parser_entry
->type
)
1864 /* Options all take effect during parsing, so there should
1865 * be no predicate entries corresponding to them. Hence we
1866 * should not see any ARG_OPTION or ARG_POSITIONAL_OPTION
1869 * This is a silly way of coding this test, but it prevents
1870 * a compiler warning (i.e. otherwise it would think that
1871 * there would be case statements missing).
1874 case ARG_POSITIONAL_OPTION
:
1875 assert(p
->parser_entry
->type
!= ARG_OPTION
);
1876 assert(p
->parser_entry
->type
!= ARG_POSITIONAL_OPTION
);
1880 assert(p
->side_effects
); /* actions have side effects. */
1881 if (p
->pred_func
!= pred_prune
&& p
->pred_func
!= pred_quit
)
1883 /* actions other than -prune and -quit should
1884 * inhibit the default -print
1886 assert(p
->no_default_print
);
1890 case ARG_PUNCTUATION
:
1893 /* Punctuation and tests should have no side
1894 * effects and not inhibit default print.
1896 assert(!p
->no_default_print
);
1897 assert(!p
->side_effects
);