Implemented %S (sparseness) printf format, and general refactoring and cleanup of...
[findutils.git] / find / pred.c
blobd7296ed59f1e46a6fe6f5adf42c66e4b2f99877a
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)
8 any later version.
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,
18 USA.
21 #include "defs.h"
23 #include <fnmatch.h>
24 #include <signal.h>
25 #include <math.h>
26 #include <pwd.h>
27 #include <grp.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <assert.h>
31 #include <fcntl.h>
32 #include "xalloc.h"
33 #include "dirname.h"
34 #include "human.h"
35 #include "modetype.h"
36 #include "filemode.h"
37 #include "wait.h"
38 #include "printquoted.h"
39 #include "buildcmd.h"
40 #include "yesno.h"
41 #include "listfile.h"
43 #if ENABLE_NLS
44 # include <libintl.h>
45 # define _(Text) gettext (Text)
46 #else
47 # define _(Text) Text
48 #endif
49 #ifdef gettext_noop
50 # define N_(String) gettext_noop (String)
51 #else
52 /* See locate.c for explanation as to why not use (String) */
53 # define N_(String) String
54 #endif
56 #if !defined(SIGCHLD) && defined(SIGCLD)
57 #define SIGCHLD SIGCLD
58 #endif
62 #if HAVE_DIRENT_H
63 # include <dirent.h>
64 # define NAMLEN(dirent) strlen((dirent)->d_name)
65 #else
66 # define dirent direct
67 # define NAMLEN(dirent) (dirent)->d_namlen
68 # if HAVE_SYS_NDIR_H
69 # include <sys/ndir.h>
70 # endif
71 # if HAVE_SYS_DIR_H
72 # include <sys/dir.h>
73 # endif
74 # if HAVE_NDIR_H
75 # include <ndir.h>
76 # endif
77 #endif
79 #ifdef CLOSEDIR_VOID
80 /* Fake a return value. */
81 #define CLOSEDIR(d) (closedir (d), 0)
82 #else
83 #define CLOSEDIR(d) closedir (d)
84 #endif
89 /* Get or fake the disk device blocksize.
90 Usually defined by sys/param.h (if at all). */
91 #ifndef DEV_BSIZE
92 # ifdef BSIZE
93 # define DEV_BSIZE BSIZE
94 # else /* !BSIZE */
95 # define DEV_BSIZE 4096
96 # endif /* !BSIZE */
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
124 # else /* !hpux */
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 */
129 # if defined(_CRAY)
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)
134 # endif /* _CRAY */
135 # endif /* not AIX PS/2 */
136 # endif /* !hpux */
137 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
139 #ifndef ST_NBLOCKS
140 # define ST_NBLOCKS(statbuf) \
141 (S_ISREG ((statbuf).st_mode) \
142 || S_ISDIR ((statbuf).st_mode) \
143 ? (statbuf).st_blocks : 0)
144 #endif
146 #ifndef ST_NBLOCKSIZE
147 # define ST_NBLOCKSIZE 512
148 #endif
151 #undef MAX
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));
159 #ifdef DEBUG
160 struct pred_assoc
162 PRED_FUNC pred_func;
163 char *pred_name;
166 struct pred_assoc pred_table[] =
168 {pred_amin, "amin "},
169 {pred_and, "and "},
170 {pred_anewer, "anewer "},
171 {pred_atime, "atime "},
172 {pred_close, ") "},
173 {pred_cmin, "cmin "},
174 {pred_cnewer, "cnewer "},
175 {pred_comma, ", "},
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 "},
187 {pred_gid, "gid "},
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 "},
195 {pred_ls, "ls "},
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 "},
203 {pred_ok, "ok "},
204 {pred_okdir, "okdir "},
205 {pred_open, "( "},
206 {pred_or, "or "},
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 "},
219 {pred_uid, "uid "},
220 {pred_used, "used "},
221 {pred_user, "user "},
222 {pred_writable, "writable "},
223 {pred_xtype, "xtype "},
224 {0, "none "}
226 #endif
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. */
237 /* pred_timewindow
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.
244 static boolean
245 pred_timewindow(time_t the_time, struct predicate const *pred_ptr, int window)
247 switch (pred_ptr->args.info.kind)
249 case COMP_GT:
250 if (the_time > (time_t) pred_ptr->args.info.l_val)
251 return true;
252 break;
253 case COMP_LT:
254 if (the_time < (time_t) pred_ptr->args.info.l_val)
255 return true;
256 break;
257 case COMP_EQ:
258 if ((the_time >= (time_t) pred_ptr->args.info.l_val)
259 && (the_time < (time_t) pred_ptr->args.info.l_val + window))
260 return true;
261 break;
263 return false;
267 boolean
268 pred_amin (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
270 (void) &pathname;
271 return pred_timewindow(stat_buf->st_atime, pred_ptr, 60);
274 boolean
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)
283 return false;
284 return ((*pred_ptr->pred_right->pred_func) (pathname, stat_buf,
285 pred_ptr->pred_right));
287 else
288 return (false);
291 boolean
292 pred_anewer (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
294 (void) &pathname;
296 if (stat_buf->st_atime > pred_ptr->args.time)
297 return (true);
298 return (false);
301 boolean
302 pred_atime (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
304 (void) &pathname;
305 return pred_timewindow(stat_buf->st_atime, pred_ptr, DAYSECS);
308 boolean
309 pred_close (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
311 (void) &pathname;
312 (void) &stat_buf;
313 (void) &pred_ptr;
315 return true;
318 boolean
319 pred_cmin (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
321 (void) pathname;
322 return pred_timewindow(stat_buf->st_ctime, pred_ptr, 60);
325 boolean
326 pred_cnewer (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
328 (void) pathname;
330 if (stat_buf->st_ctime > pred_ptr->args.time)
331 return true;
332 else
333 return false;
336 boolean
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)
345 return false;
346 return ((*pred_ptr->pred_right->pred_func) (pathname, stat_buf,
347 pred_ptr->pred_right));
350 boolean
351 pred_ctime (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
353 (void) &pathname;
354 return pred_timewindow(stat_buf->st_ctime, pred_ptr, DAYSECS);
357 boolean
358 pred_delete (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
360 (void) pred_ptr;
361 (void) stat_buf;
362 if (strcmp (state.rel_pathname, "."))
364 if (0 != remove (state.rel_pathname))
366 error (0, errno, "cannot delete %s", pathname);
367 return false;
369 else
371 return true;
375 /* nothing to do. */
376 return true;
379 boolean
380 pred_empty (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
382 (void) pathname;
383 (void) pred_ptr;
385 if (S_ISDIR (stat_buf->st_mode))
387 DIR *d;
388 struct dirent *dp;
389 boolean empty = true;
391 errno = 0;
392 d = opendir (state.rel_pathname);
393 if (d == NULL)
395 error (0, errno, "%s", pathname);
396 state.exit_status = 1;
397 return false;
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')))
405 empty = false;
406 break;
409 if (CLOSEDIR (d))
411 error (0, errno, "%s", pathname);
412 state.exit_status = 1;
413 return false;
415 return (empty);
417 else if (S_ISREG (stat_buf->st_mode))
418 return (stat_buf->st_size == 0);
419 else
420 return (false);
423 static boolean
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);
431 (void) stat_buf;
433 if (execp->multiple)
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,
440 &execp->state,
441 pathname, len+1,
442 prefix, pfxlen,
445 /* POSIX: If the primary expression is punctuated by a plus
446 * sign, the primary shall always evaluate as true
448 return true;
450 else
452 int i;
454 for (i=0; i<execp->num_args; ++i)
456 bc_do_insert(&execp->ctl,
457 &execp->state,
458 execp->replace_vec[i],
459 strlen(execp->replace_vec[i]),
460 prefix, pfxlen,
461 pathname, len,
465 /* Actually invoke the command. */
466 return execp->ctl.exec_callback(&execp->ctl,
467 &execp->state);
472 boolean
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);
478 boolean
479 pred_execdir (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
481 const char *prefix = (state.rel_pathname[0] == '/') ? NULL : "./";
482 (void) &pathname;
483 return new_impl_pred_exec (state.rel_pathname, stat_buf, pred_ptr,
484 prefix, (prefix ? 2 : 0));
487 boolean
488 pred_false (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
490 (void) &pathname;
491 (void) &stat_buf;
492 (void) &pred_ptr;
495 return (false);
498 boolean
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);
504 return true;
507 boolean
508 pred_fprint (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
510 (void) &pathname;
511 (void) &stat_buf;
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,
516 "%s\n",
517 pathname);
518 return true;
521 boolean
522 pred_fprint0 (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
524 (void) &pathname;
525 (void) &stat_buf;
527 fputs (pathname, pred_ptr->args.stream);
528 putc (0, pred_ptr->args.stream);
529 return (true);
534 static char*
535 mode_to_filetype(mode_t m)
537 return
538 m == S_IFSOCK ? "s" :
539 m == S_IFLNK ? "l" :
540 m == S_IFREG ? "f" :
541 m == S_IFBLK ? "b" :
542 m == S_IFDIR ? "d" :
543 m == S_IFCHR ? "c" :
544 #ifdef S_IFDOOR
545 m == S_IFDOOR ? "D" :
546 #endif
547 m == S_IFIFO ? "p" : "U";
550 static double
551 file_sparseness(const struct stat *p)
553 if (0 == p->st_size)
555 if (0 == p->st_blocks)
556 return 1.0;
557 else
558 return p->st_blocks < 0 ? -HUGE_VAL : HUGE_VAL;
560 else
562 double blklen = file_blocksize(p) * (double)p->st_blocks;
563 return blklen / p->st_size;
569 static boolean
570 do_fprintf(FILE *fp,
571 struct segment *segment,
572 char *pathname,
573 const struct stat *stat_buf,
574 boolean ttyflag,
575 const struct quoting_options *qopts)
577 char hbuf[LONGEST_HUMAN_READABLE + 1];
578 char *cp;
580 switch (segment->segkind)
582 case KIND_PLAIN: /* Plain text string (no % conversion). */
583 /* trusted */
584 fwrite (segment->text, 1, segment->text_len, fp);
585 break;
587 case KIND_STOP: /* Terminate argument and flush output. */
588 /* trusted */
589 fwrite (segment->text, 1, segment->text_len, fp);
590 fflush (fp);
591 return (true);
593 case KIND_FORMAT:
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));
599 break;
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),
604 hbuf, human_ceiling,
605 ST_NBLOCKSIZE, 512));
606 break;
607 case 'c': /* ctime in `ctime' format */
608 /* UNTRUSTED, probably unexploitable */
609 fprintf (fp, segment->text, ctime_format (stat_buf->st_ctime));
610 break;
611 case 'd': /* depth in search tree */
612 /* UNTRUSTED, probably unexploitable */
613 fprintf (fp, segment->text, state.curdepth);
614 break;
615 case 'D': /* Device on which file exists (stat.st_dev) */
616 /* trusted */
617 fprintf (fp, segment->text,
618 human_readable ((uintmax_t) stat_buf->st_dev, hbuf,
619 human_ceiling, 1, 1));
620 break;
621 case 'f': /* base name of path */
622 /* sanitised */
623 print_quoted (fp, qopts, ttyflag, segment->text, base_name (pathname));
624 break;
625 case 'F': /* filesystem type */
626 /* trusted */
627 print_quoted (fp, qopts, ttyflag, segment->text, filesystem_type (stat_buf, pathname));
628 break;
629 case 'g': /* group name */
630 /* trusted */
631 /* (well, the actual group is selected by the user but
632 * its name was selected by the system administrator)
635 struct group *g;
637 g = getgrgid (stat_buf->st_gid);
638 if (g)
640 segment->text[segment->text_len] = 's';
641 fprintf (fp, segment->text, g->gr_name);
642 break;
644 else
646 /* Do nothing. */
647 /*FALLTHROUGH*/
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));
655 break;
656 case 'h': /* leading directories part of path */
657 /* sanitised */
659 char cc;
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, ".");
670 else
672 cc = *cp;
673 *cp = '\0';
674 print_quoted (fp, qopts, ttyflag, segment->text, pathname);
675 *cp = cc;
677 break;
679 case 'H': /* ARGV element file was found under */
680 /* trusted */
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;
687 break;
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,
693 human_ceiling,
694 1, 1));
695 break;
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),
700 hbuf, human_ceiling,
701 ST_NBLOCKSIZE, 1024));
702 break;
703 case 'l': /* object of symlink */
704 /* sanitised */
705 #ifdef S_ISLNK
707 char *linkname = 0;
709 if (S_ISLNK (stat_buf->st_mode))
711 linkname = get_link_name (pathname, state.rel_pathname);
712 if (linkname == 0)
713 state.exit_status = 1;
715 if (linkname)
717 print_quoted (fp, qopts, ttyflag, segment->text, linkname);
718 free (linkname);
720 else
721 print_quoted (fp, qopts, ttyflag, segment->text, "");
723 #endif /* S_ISLNK */
724 break;
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);
734 break;
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
751 ? m & MODE_ALL
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))));
765 break;
767 case 'n': /* number of links */
768 /* UNTRUSTED, probably unexploitable */
769 fprintf (fp, segment->text,
770 human_readable ((uintmax_t) stat_buf->st_nlink,
771 hbuf,
772 human_ceiling,
773 1, 1));
774 break;
775 case 'p': /* pathname */
776 /* sanitised */
777 print_quoted (fp, qopts, ttyflag, segment->text, pathname);
778 break;
779 case 'P': /* pathname with ARGV element stripped */
780 /* sanitised */
781 if (state.curdepth > 0)
783 cp = pathname + state.starting_path_length;
784 if (*cp == '/')
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. */
789 cp++;
791 else
792 cp = "";
793 print_quoted (fp, qopts, ttyflag, segment->text, cp);
794 break;
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));
800 break;
802 case 'S': /* sparseness */
803 /* UNTRUSTED, probably unexploitable */
804 fprintf (fp, segment->text, file_sparseness(stat_buf));;
805 break;
807 case 't': /* mtime in `ctime' format */
808 /* UNTRUSTED, probably unexploitable */
809 fprintf (fp, segment->text, ctime_format (stat_buf->st_mtime));
810 break;
811 case 'u': /* user name */
812 /* trusted */
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)
818 struct passwd *p;
820 p = getpwuid (stat_buf->st_uid);
821 if (p)
823 segment->text[segment->text_len] = 's';
824 fprintf (fp, segment->text, p->pw_name);
825 break;
827 /* else fallthru */
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));
835 break;
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 */
839 /* trusted */
841 #ifdef S_ISLNK
842 if (S_ISLNK (stat_buf->st_mode))
844 struct stat sbuf;
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");
853 break;
855 if ( errno == ELOOP ) {
856 fprintf (fp, segment->text, "L");
857 break;
859 error (0, errno, "%s", pathname);
860 /* exit_status = 1;
861 return (false); */
863 fprintf (fp, segment->text,
864 mode_to_filetype(sbuf.st_mode & S_IFMT));
866 #endif /* S_ISLNK */
867 else
869 fprintf (fp, segment->text,
870 mode_to_filetype(stat_buf->st_mode & S_IFMT));
873 break;
875 case 'y':
876 /* trusted */
878 fprintf (fp, segment->text,
879 mode_to_filetype(stat_buf->st_mode & S_IFMT));
881 break;
883 break;
887 boolean
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. */
900 time_t t;
902 switch (segment->format_char[0])
904 case 'A':
905 t = stat_buf->st_atime;
906 break;
907 case 'C':
908 t = stat_buf->st_ctime;
909 break;
910 case 'T':
911 t = stat_buf->st_mtime;
912 break;
913 default:
914 assert(0);
915 abort ();
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.
921 /* trusted */
922 fprintf (fp, segment->text,
923 format_date (t, segment->format_char[1]));
925 else
927 do_fprintf(fp, segment, pathname, stat_buf, ttyflag, qopts);
930 return true;
933 boolean
934 pred_fstype (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
936 (void) pathname;
938 if (strcmp (filesystem_type (stat_buf, pathname), pred_ptr->args.str) == 0)
939 return true;
940 else
941 return false;
944 boolean
945 pred_gid (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
947 (void) pathname;
949 switch (pred_ptr->args.info.kind)
951 case COMP_GT:
952 if (stat_buf->st_gid > pred_ptr->args.info.l_val)
953 return (true);
954 break;
955 case COMP_LT:
956 if (stat_buf->st_gid < pred_ptr->args.info.l_val)
957 return (true);
958 break;
959 case COMP_EQ:
960 if (stat_buf->st_gid == pred_ptr->args.info.l_val)
961 return (true);
962 break;
964 return (false);
967 boolean
968 pred_group (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
970 (void) pathname;
972 if (pred_ptr->args.gid == stat_buf->st_gid)
973 return (true);
974 else
975 return (false);
978 boolean
979 pred_ilname (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
981 return match_lname (pathname, stat_buf, pred_ptr, true);
984 boolean
985 pred_iname (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
987 const char *base;
989 (void) stat_buf;
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)
996 return (true);
997 return (false);
1000 boolean
1001 pred_inum (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1003 (void) pathname;
1005 switch (pred_ptr->args.info.kind)
1007 case COMP_GT:
1008 if (stat_buf->st_ino > pred_ptr->args.info.l_val)
1009 return (true);
1010 break;
1011 case COMP_LT:
1012 if (stat_buf->st_ino < pred_ptr->args.info.l_val)
1013 return (true);
1014 break;
1015 case COMP_EQ:
1016 if (stat_buf->st_ino == pred_ptr->args.info.l_val)
1017 return (true);
1018 break;
1020 return (false);
1023 boolean
1024 pred_ipath (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1026 (void) stat_buf;
1028 if (fnmatch (pred_ptr->args.str, pathname, FNM_CASEFOLD) == 0)
1029 return (true);
1030 return (false);
1033 boolean
1034 pred_links (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1036 (void) pathname;
1038 switch (pred_ptr->args.info.kind)
1040 case COMP_GT:
1041 if (stat_buf->st_nlink > pred_ptr->args.info.l_val)
1042 return (true);
1043 break;
1044 case COMP_LT:
1045 if (stat_buf->st_nlink < pred_ptr->args.info.l_val)
1046 return (true);
1047 break;
1048 case COMP_EQ:
1049 if (stat_buf->st_nlink == pred_ptr->args.info.l_val)
1050 return (true);
1051 break;
1053 return (false);
1056 boolean
1057 pred_lname (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1059 return match_lname (pathname, stat_buf, pred_ptr, false);
1062 static boolean
1063 match_lname (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr, boolean ignore_case)
1065 boolean ret = false;
1066 #ifdef S_ISLNK
1067 if (S_ISLNK (stat_buf->st_mode))
1069 char *linkname = get_link_name (pathname, state.rel_pathname);
1070 if (linkname)
1072 if (fnmatch (pred_ptr->args.str, linkname,
1073 ignore_case ? FNM_CASEFOLD : 0) == 0)
1074 ret = true;
1075 free (linkname);
1078 #endif /* S_ISLNK */
1079 return ret;
1082 boolean
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,
1088 stdout);
1089 return true;
1092 boolean
1093 pred_mmin (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1095 (void) &pathname;
1096 return pred_timewindow(stat_buf->st_mtime, pred_ptr, 60);
1099 boolean
1100 pred_mtime (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1102 (void) pathname;
1103 return pred_timewindow(stat_buf->st_mtime, pred_ptr, DAYSECS);
1106 boolean
1107 pred_name (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1109 const char *base;
1111 (void) stat_buf;
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)
1118 return (true);
1119 return (false);
1122 boolean
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)
1128 return false;
1129 return (!(*pred_ptr->pred_right->pred_func) (pathname, stat_buf,
1130 pred_ptr->pred_right));
1133 boolean
1134 pred_newer (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1136 (void) pathname;
1138 if (stat_buf->st_mtime > pred_ptr->args.time)
1139 return (true);
1140 return (false);
1143 boolean
1144 pred_nogroup (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1146 (void) pathname;
1147 (void) pred_ptr;
1149 #ifdef CACHE_IDS
1150 extern char *gid_unused;
1152 return gid_unused[(unsigned) stat_buf->st_gid];
1153 #else
1154 return getgrgid (stat_buf->st_gid) == NULL;
1155 #endif
1158 boolean
1159 pred_nouser (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1161 #ifdef CACHE_IDS
1162 extern char *uid_unused;
1163 #endif
1165 (void) pathname;
1166 (void) pred_ptr;
1168 #ifdef CACHE_IDS
1169 return uid_unused[(unsigned) stat_buf->st_uid];
1170 #else
1171 return getpwuid (stat_buf->st_uid) == NULL;
1172 #endif
1176 static boolean
1177 is_ok(const char *program, const char *arg)
1179 fflush (stdout);
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);
1187 fflush (stderr);
1188 return yesno();
1191 boolean
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);
1196 else
1197 return false;
1200 boolean
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));
1207 else
1208 return false;
1211 boolean
1212 pred_open (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1214 (void) pathname;
1215 (void) stat_buf;
1216 (void) pred_ptr;
1217 return true;
1220 boolean
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)
1228 return false;
1229 return ((*pred_ptr->pred_right->pred_func) (pathname, stat_buf,
1230 pred_ptr->pred_right));
1232 else
1233 return true;
1236 boolean
1237 pred_path (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1239 (void) stat_buf;
1240 if (fnmatch (pred_ptr->args.str, pathname, 0) == 0)
1241 return (true);
1242 return (false);
1245 boolean
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];
1250 (void) pathname;
1251 switch (pred_ptr->args.perm.kind)
1253 case PERM_AT_LEAST:
1254 return (mode & perm_val) == perm_val;
1255 break;
1257 case PERM_ANY:
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,
1268 if (0 == perm_val)
1269 return true; /* Savannah bug 14748; we used to return false */
1270 else
1271 return (mode & perm_val) != 0;
1272 break;
1274 case PERM_EXACT:
1275 return (mode & MODE_ALL) == perm_val;
1276 break;
1278 default:
1279 abort ();
1280 break;
1285 boolean
1286 pred_executable (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1288 (void) pathname;
1289 (void) stat_buf;
1290 (void) pred_ptr;
1292 return 0 == access(state.rel_pathname, X_OK);
1295 boolean
1296 pred_readable (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1298 (void) pathname;
1299 (void) stat_buf;
1300 (void) pred_ptr;
1302 return 0 == access(state.rel_pathname, R_OK);
1305 boolean
1306 pred_writable (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1308 (void) pathname;
1309 (void) stat_buf;
1310 (void) pred_ptr;
1312 return 0 == access(state.rel_pathname, W_OK);
1315 boolean
1316 pred_print (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1318 (void) stat_buf;
1319 (void) 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,
1324 "%s\n", pathname);
1325 return true;
1328 boolean
1329 pred_print0 (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1331 (void) stat_buf;
1332 (void) pred_ptr;
1333 fputs (pathname, stdout);
1334 putc (0, stdout);
1335 return (true);
1338 boolean
1339 pred_prune (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1341 (void) pathname;
1342 (void) pred_ptr;
1344 if (options.do_dir_first == true &&
1345 stat_buf != NULL &&
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. */
1352 boolean
1353 pred_quit (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1355 (void) pathname;
1356 (void) stat_buf;
1357 (void) pred_ptr;
1359 /* Run any cleanups. This includes executing any command lines
1360 * we have partly built but not executed.
1362 cleanup();
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. */
1370 boolean
1371 pred_regex (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1373 int len = strlen (pathname);
1374 (void) stat_buf;
1375 if (re_match (pred_ptr->args.regex, pathname, len, 0,
1376 (struct re_registers *) NULL) == len)
1377 return (true);
1378 return (false);
1381 boolean
1382 pred_size (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1384 uintmax_t f_val;
1386 (void) pathname;
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)
1391 case COMP_GT:
1392 if (f_val > pred_ptr->args.size.size)
1393 return (true);
1394 break;
1395 case COMP_LT:
1396 if (f_val < pred_ptr->args.size.size)
1397 return (true);
1398 break;
1399 case COMP_EQ:
1400 if (f_val == pred_ptr->args.size.size)
1401 return (true);
1402 break;
1404 return (false);
1407 boolean
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.
1418 (void) pathname;
1420 return stat_buf->st_ino == pred_ptr->args.fileid.ino
1421 && stat_buf->st_dev == pred_ptr->args.fileid.dev;
1424 boolean
1425 pred_true (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1427 (void) pathname;
1428 (void) stat_buf;
1429 (void) pred_ptr;
1430 return true;
1433 boolean
1434 pred_type (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1436 mode_t mode;
1437 mode_t type = pred_ptr->args.type;
1439 assert(state.have_type);
1440 assert(state.type != 0);
1442 (void) pathname;
1444 if (state.have_stat)
1445 mode = stat_buf->st_mode;
1446 else
1447 mode = state.type;
1449 #ifndef S_IFMT
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)
1455 #ifdef S_IFLNK
1456 || (S_ISLNK (mode) && type == S_IFLNK)
1457 #endif
1458 #ifdef S_IFIFO
1459 || (S_ISFIFO (mode) && type == S_IFIFO)
1460 #endif
1461 #ifdef S_IFSOCK
1462 || (S_ISSOCK (mode) && type == S_IFSOCK)
1463 #endif
1464 #ifdef S_IFDOOR
1465 || (S_ISDOOR (mode) && type == S_IFDOOR)
1466 #endif
1468 #else /* S_IFMT */
1469 /* Unix system; check `mode' the fast way. */
1470 if ((mode & S_IFMT) == type)
1471 #endif /* S_IFMT */
1472 return (true);
1473 else
1474 return (false);
1477 boolean
1478 pred_uid (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1480 (void) pathname;
1481 switch (pred_ptr->args.info.kind)
1483 case COMP_GT:
1484 if (stat_buf->st_uid > pred_ptr->args.info.l_val)
1485 return (true);
1486 break;
1487 case COMP_LT:
1488 if (stat_buf->st_uid < pred_ptr->args.info.l_val)
1489 return (true);
1490 break;
1491 case COMP_EQ:
1492 if (stat_buf->st_uid == pred_ptr->args.info.l_val)
1493 return (true);
1494 break;
1496 return (false);
1499 boolean
1500 pred_used (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1502 time_t delta;
1504 (void) pathname;
1505 delta = stat_buf->st_atime - stat_buf->st_ctime; /* Use difftime? */
1506 return pred_timewindow(delta, pred_ptr, DAYSECS);
1509 boolean
1510 pred_user (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
1512 (void) pathname;
1513 if (pred_ptr->args.uid == stat_buf->st_uid)
1514 return (true);
1515 else
1516 return (false);
1519 boolean
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;
1530 else
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));
1543 else
1545 error (0, errno, "%s", pathname);
1546 state.exit_status = 1;
1548 return false;
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
1560 Possible returns:
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. */
1575 static void
1576 prep_child_for_exec (boolean close_stdin)
1578 if (close_stdin)
1580 const char inputfile[] = "/dev/null";
1581 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
1583 close(0);
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)
1602 int wait_status;
1603 pid_t child_pid;
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. */
1611 fflush (stdout);
1612 fflush (stderr);
1614 /* Make sure to listen for the kids. */
1615 if (first_time)
1617 first_time = 0;
1618 signal (SIGCHLD, SIG_DFL);
1621 child_pid = fork ();
1622 if (child_pid == -1)
1623 error (1, errno, _("cannot fork"));
1624 if (child_pid == 0)
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);
1645 _exit (1);
1649 execvp (buildstate->cmd_argv[0], buildstate->cmd_argv);
1650 error (0, errno, "%s", buildstate->cmd_argv[0]);
1651 _exit (1);
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)
1661 if (errno != EINTR)
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;
1683 return 1; /* OK */
1686 if (0 == WEXITSTATUS (wait_status))
1688 return 1; /* OK */
1690 else
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. */
1709 static char *
1710 format_date (time_t when, int kind)
1712 static char buf[MAX (LONGEST_HUMAN_READABLE + 2, 64)];
1713 struct tm *tm;
1714 char fmt[6];
1716 fmt[0] = '%';
1717 fmt[1] = kind;
1718 fmt[2] = '\0';
1719 if (kind == '+')
1720 strcpy (fmt, "%F+%T");
1722 if (kind != '@'
1723 && (tm = localtime (&when))
1724 && strftime (buf, sizeof buf, fmt, tm))
1725 return buf;
1726 else
1728 uintmax_t w = when;
1729 char *p = human_readable (when < 0 ? -w : w, buf + 1,
1730 human_ceiling, 1, 1);
1731 if (when < 0)
1732 *--p = '-';
1733 return p;
1737 static char *
1738 ctime_format (time_t when)
1740 char *r = ctime (&when);
1741 if (!r)
1743 /* The time cannot be represented as a struct tm.
1744 Output it as an integer. */
1745 return format_date (when, '@');
1747 else
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';
1752 return r;
1756 /* Copy STR into BUF and trim blanks from the end of BUF.
1757 Return BUF. */
1759 static char *
1760 blank_rtrim (str, buf)
1761 char *str;
1762 char *buf;
1764 int i;
1766 if (str == NULL)
1767 return (NULL);
1768 strcpy (buf, str);
1769 i = strlen (buf) - 1;
1770 while ((i >= 0) && ((buf[i] == ' ') || buf[i] == '\t'))
1771 i--;
1772 buf[++i] = '\0';
1773 return (buf);
1776 /* Print out the predicate list starting at NODE. */
1777 void
1778 print_list (FILE *fp, struct predicate *node)
1780 struct predicate *cur;
1781 char name[256];
1783 cur = node;
1784 while (cur != NULL)
1786 fprintf (fp, "%s ", blank_rtrim (cur->p_name, name));
1787 cur = cur->pred_next;
1789 fprintf (fp, "\n");
1792 /* Print out the predicate list starting at NODE. */
1793 static void
1794 print_parenthesised(FILE *fp, struct predicate *node)
1796 int parens = 0;
1798 if (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);
1809 else
1811 if (node->pred_left || node->pred_right)
1812 parens = 1;
1814 if (parens)
1815 fprintf(fp, "%s", " ( ");
1816 print_optlist(fp, node);
1817 if (parens)
1818 fprintf(fp, "%s", " ) ");
1823 void
1824 print_optlist (FILE *fp, const struct predicate *p)
1826 if (p)
1828 print_parenthesised(fp, p->pred_left);
1829 fprintf (fp,
1830 "%s%s",
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);
1839 void
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
1867 * items.
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).
1873 case ARG_OPTION:
1874 case ARG_POSITIONAL_OPTION:
1875 assert(p->parser_entry->type != ARG_OPTION);
1876 assert(p->parser_entry->type != ARG_POSITIONAL_OPTION);
1877 break;
1879 case ARG_ACTION:
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);
1888 break;
1890 case ARG_PUNCTUATION:
1891 case ARG_TEST:
1892 case ARG_NOOP:
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);
1898 break;