1 /* pred.c -- execute the expression tree.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
26 #include "../gnulib/lib/dirname.h"
27 #include "../gnulib/lib/human.h"
34 # define _(Text) gettext (Text)
39 # define N_(String) gettext_noop (String)
41 /* See locate.c for explanation as to why not use (String) */
42 # define N_(String) String
45 #if !defined(SIGCHLD) && defined(SIGCLD)
46 #define SIGCHLD SIGCLD
51 # define NAMLEN(dirent) strlen((dirent)->d_name)
53 # define dirent direct
54 # define NAMLEN(dirent) (dirent)->d_namlen
56 # include <sys/ndir.h>
67 /* Fake a return value. */
68 #define CLOSEDIR(d) (closedir (d), 0)
70 #define CLOSEDIR(d) closedir (d)
76 /* Get or fake the disk device blocksize.
77 Usually defined by sys/param.h (if at all). */
80 # define DEV_BSIZE BSIZE
82 # define DEV_BSIZE 4096
84 #endif /* !DEV_BSIZE */
86 /* Extract or fake data from a `struct stat'.
87 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
88 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
89 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
90 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
91 # define ST_BLKSIZE(statbuf) DEV_BSIZE
92 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
93 # define ST_NBLOCKS(statbuf) \
94 (S_ISREG ((statbuf).st_mode) \
95 || S_ISDIR ((statbuf).st_mode) \
96 ? (statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0) : 0)
97 # else /* !_POSIX_SOURCE && BSIZE */
98 # define ST_NBLOCKS(statbuf) \
99 (S_ISREG ((statbuf).st_mode) \
100 || S_ISDIR ((statbuf).st_mode) \
101 ? st_blocks ((statbuf).st_size) : 0)
102 # endif /* !_POSIX_SOURCE && BSIZE */
103 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
104 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
105 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
106 ? (statbuf).st_blksize : DEV_BSIZE)
107 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
108 /* HP-UX counts st_blocks in 1024-byte units.
109 This loses when mixing HP-UX and BSD filesystems with NFS. */
110 # define ST_NBLOCKSIZE 1024
112 # if defined(_AIX) && defined(_I386)
113 /* AIX PS/2 counts st_blocks in 4K units. */
114 # define ST_NBLOCKSIZE (4 * 1024)
115 # else /* not AIX PS/2 */
117 # define ST_NBLOCKS(statbuf) \
118 (S_ISREG ((statbuf).st_mode) \
119 || S_ISDIR ((statbuf).st_mode) \
120 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
122 # endif /* not AIX PS/2 */
124 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
127 # define ST_NBLOCKS(statbuf) \
128 (S_ISREG ((statbuf).st_mode) \
129 || S_ISDIR ((statbuf).st_mode) \
130 ? (statbuf).st_blocks : 0)
133 #ifndef ST_NBLOCKSIZE
134 # define ST_NBLOCKSIZE 512
138 #define MAX(a, b) ((a) > (b) ? (a) : (b))
140 static boolean insert_lname
PARAMS((char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
, boolean ignore_case
));
141 static boolean launch
PARAMS((struct predicate
*pred_ptr
));
142 static char *format_date
PARAMS((time_t when
, int kind
));
143 static char *ctime_format
PARAMS((time_t when
));
152 struct pred_assoc pred_table
[] =
154 {pred_amin
, "amin "},
156 {pred_anewer
, "anewer "},
157 {pred_atime
, "atime "},
159 {pred_amin
, "cmin "},
160 {pred_cnewer
, "cnewer "},
162 {pred_ctime
, "ctime "},
163 {pred_delete
, "delete "},
164 {pred_empty
, "empty "},
165 {pred_exec
, "exec "},
166 {pred_false
, "false "},
167 {pred_fprint
, "fprint "},
168 {pred_fprint0
, "fprint0 "},
169 {pred_fprintf
, "fprintf "},
170 {pred_fstype
, "fstype "},
172 {pred_group
, "group "},
173 {pred_ilname
, "ilname "},
174 {pred_iname
, "iname "},
175 {pred_inum
, "inum "},
176 {pred_ipath
, "ipath "},
177 {pred_links
, "links "},
178 {pred_lname
, "lname "},
180 {pred_amin
, "mmin "},
181 {pred_mtime
, "mtime "},
182 {pred_name
, "name "},
183 {pred_negate
, "not "},
184 {pred_newer
, "newer "},
185 {pred_nogroup
, "nogroup "},
186 {pred_nouser
, "nouser "},
190 {pred_path
, "path "},
191 {pred_perm
, "perm "},
192 {pred_print
, "print "},
193 {pred_print0
, "print0 "},
194 {pred_prune
, "prune "},
195 {pred_regex
, "regex "},
196 {pred_size
, "size "},
197 {pred_true
, "true "},
198 {pred_type
, "type "},
200 {pred_used
, "used "},
201 {pred_user
, "user "},
202 {pred_xtype
, "xtype "},
212 struct op_assoc type_table
[] =
215 {PRIMARY_TYPE
, "primary "},
218 {OPEN_PAREN
, "open_paren "},
219 {CLOSE_PAREN
, "close_paren "},
229 struct prec_assoc prec_table
[] =
232 {COMMA_PREC
, "comma "},
235 {NEGATE_PREC
, "negate "},
241 /* Predicate processing routines.
243 PATHNAME is the full pathname of the file being checked.
244 *STAT_BUF contains information about PATHNAME.
245 *PRED_PTR contains information for applying the predicate.
247 Return true if the file passes this predicate, false if not. */
252 * Returns true if THE_TIME is
253 * COMP_GT: after the specified time
254 * COMP_LT: before the specified time
255 * COMP_EQ: less than WINDOW seconds after the specified time.
258 pred_timewindow(time_t the_time
, struct predicate
const *pred_ptr
, int window
)
260 switch (pred_ptr
->args
.info
.kind
)
263 if (the_time
> (time_t) pred_ptr
->args
.info
.l_val
)
267 if (the_time
< (time_t) pred_ptr
->args
.info
.l_val
)
271 if ((the_time
>= (time_t) pred_ptr
->args
.info
.l_val
)
272 && (the_time
< (time_t) pred_ptr
->args
.info
.l_val
+ window
))
281 pred_amin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
284 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, 60);
288 pred_and (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
290 if (pred_ptr
->pred_left
== NULL
291 || (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
292 pred_ptr
->pred_left
))
294 /* Check whether we need a stat here. */
295 if (pred_ptr
->need_stat
)
297 if (!have_stat
&& (*xstat
) (rel_pathname
, stat_buf
) != 0)
299 error (0, errno
, "%s", pathname
);
305 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
306 pred_ptr
->pred_right
));
313 pred_anewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
317 if (stat_buf
->st_atime
> pred_ptr
->args
.time
)
323 pred_atime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
326 return pred_timewindow(stat_buf
->st_atime
, pred_ptr
, DAYSECS
);
330 pred_close (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
340 pred_cmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
343 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, 60);
347 pred_cnewer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
351 if (stat_buf
->st_ctime
> pred_ptr
->args
.time
)
358 pred_comma (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
360 if (pred_ptr
->pred_left
!= NULL
)
361 (*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
362 pred_ptr
->pred_left
);
363 /* Check whether we need a stat here. */
364 if (pred_ptr
->need_stat
)
366 if (!have_stat
&& (*xstat
) (rel_pathname
, stat_buf
) != 0)
368 error (0, errno
, "%s", pathname
);
374 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
375 pred_ptr
->pred_right
));
379 pred_ctime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
382 return pred_timewindow(stat_buf
->st_ctime
, pred_ptr
, DAYSECS
);
386 pred_delete (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
390 if (strcmp (rel_pathname
, "."))
392 if (0 != remove (rel_pathname
))
394 error (0, errno
, "cannot delete %s", pathname
);
408 pred_empty (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
413 if (S_ISDIR (stat_buf
->st_mode
))
417 boolean empty
= true;
420 d
= opendir (rel_pathname
);
423 error (0, errno
, "%s", pathname
);
427 for (dp
= readdir (d
); dp
; dp
= readdir (d
))
429 if (dp
->d_name
[0] != '.'
430 || (dp
->d_name
[1] != '\0'
431 && (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0')))
439 error (0, errno
, "%s", pathname
);
445 else if (S_ISREG (stat_buf
->st_mode
))
446 return (stat_buf
->st_size
== 0);
452 pred_exec (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
456 struct exec_val
*execp
; /* Pointer for efficiency. */
461 execp
= &pred_ptr
->args
.exec_vec
;
463 /* Replace "{}" with the real path in each affected arg. */
464 for (path_pos
= 0; execp
->paths
[path_pos
].offset
>= 0; path_pos
++)
466 register char *from
, *to
;
468 i
= execp
->paths
[path_pos
].offset
;
470 xmalloc (strlen (execp
->paths
[path_pos
].origarg
) + 1
471 + (strlen (pathname
) - 2) * execp
->paths
[path_pos
].count
);
472 for (from
= execp
->paths
[path_pos
].origarg
, to
= execp
->vec
[i
]; *from
; )
473 if (from
[0] == '{' && from
[1] == '}')
475 to
= stpcpy (to
, pathname
);
480 *to
= *from
; /* Copy null. */
483 i
= launch (pred_ptr
);
485 /* Free the temporary args. */
486 for (path_pos
= 0; execp
->paths
[path_pos
].offset
>= 0; path_pos
++)
487 free (execp
->vec
[execp
->paths
[path_pos
].offset
]);
493 pred_false (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
504 pred_fls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
506 list_file (pathname
, rel_pathname
, stat_buf
, start_time
,
507 output_block_size
, pred_ptr
->args
.stream
);
512 pred_fprint (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
517 fputs (pathname
, pred_ptr
->args
.stream
);
518 putc ('\n', pred_ptr
->args
.stream
);
523 pred_fprint0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
528 fputs (pathname
, pred_ptr
->args
.stream
);
529 putc (0, pred_ptr
->args
.stream
);
534 pred_fprintf (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
536 FILE *fp
= pred_ptr
->args
.printf_vec
.stream
;
537 struct segment
*segment
;
539 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
541 for (segment
= pred_ptr
->args
.printf_vec
.segment
; segment
;
542 segment
= segment
->next
)
544 if (segment
->kind
& 0xff00) /* Component of date. */
548 switch (segment
->kind
& 0xff)
551 t
= stat_buf
->st_atime
;
554 t
= stat_buf
->st_ctime
;
557 t
= stat_buf
->st_mtime
;
562 fprintf (fp
, segment
->text
,
563 format_date (t
, (segment
->kind
>> 8) & 0xff));
567 switch (segment
->kind
)
569 case KIND_PLAIN
: /* Plain text string (no % conversion). */
570 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
572 case KIND_STOP
: /* Terminate argument and flush output. */
573 fwrite (segment
->text
, 1, segment
->text_len
, fp
);
576 case 'a': /* atime in `ctime' format. */
577 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_atime
));
579 case 'b': /* size in 512-byte blocks */
580 fprintf (fp
, segment
->text
,
581 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
583 ST_NBLOCKSIZE
, 512));
585 case 'c': /* ctime in `ctime' format */
586 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_ctime
));
588 case 'd': /* depth in search tree */
589 fprintf (fp
, segment
->text
, curdepth
);
591 case 'D': /* Device on which file exists (stat.st_dev) */
592 fprintf (fp
, segment
->text
,
593 human_readable ((uintmax_t) stat_buf
->st_dev
, hbuf
,
594 human_ceiling
, 1, 1));
596 case 'f': /* basename of path */
597 fprintf (fp
, segment
->text
, base_name (pathname
));
599 case 'F': /* filesystem type */
600 fprintf (fp
, segment
->text
,
601 filesystem_type (pathname
, rel_pathname
, stat_buf
));
603 case 'g': /* group name */
607 g
= getgrgid (stat_buf
->st_gid
);
610 segment
->text
[segment
->text_len
] = 's';
611 fprintf (fp
, segment
->text
, g
->gr_name
);
616 case 'G': /* GID number */
617 fprintf (fp
, segment
->text
,
618 human_readable ((uintmax_t) stat_buf
->st_gid
, hbuf
,
619 human_ceiling
, 1, 1));
621 case 'h': /* leading directories part of path */
625 cp
= strrchr (pathname
, '/');
626 if (cp
== NULL
) /* No leading directories. */
630 fprintf (fp
, segment
->text
, pathname
);
634 case 'H': /* ARGV element file was found under */
636 char cc
= pathname
[path_length
];
638 pathname
[path_length
] = '\0';
639 fprintf (fp
, segment
->text
, pathname
);
640 pathname
[path_length
] = cc
;
643 case 'i': /* inode number */
644 fprintf (fp
, segment
->text
,
645 human_readable ((uintmax_t) stat_buf
->st_ino
, hbuf
,
649 case 'k': /* size in 1K blocks */
650 fprintf (fp
, segment
->text
,
651 human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf
),
653 ST_NBLOCKSIZE
, 1024));
655 case 'l': /* object of symlink */
660 if (S_ISLNK (stat_buf
->st_mode
))
662 linkname
= get_link_name (pathname
, rel_pathname
);
668 fprintf (fp
, segment
->text
, linkname
);
672 fprintf (fp
, segment
->text
, "");
677 case 'M': /* mode as 10 chars (eg., "-rwxr-x--x" */
679 char modestring
[16] ;
680 mode_string (stat_buf
->st_mode
, modestring
);
681 modestring
[10] = '\0';
682 fprintf (fp
, segment
->text
, modestring
);
686 case 'm': /* mode as octal number (perms only) */
688 /* Output the mode portably using the traditional numbers,
689 even if the host unwisely uses some other numbering
690 scheme. But help the compiler in the common case where
691 the host uses the traditional numbering scheme. */
692 mode_t m
= stat_buf
->st_mode
;
693 boolean traditional_numbering_scheme
=
694 (S_ISUID
== 04000 && S_ISGID
== 02000 && S_ISVTX
== 01000
695 && S_IRUSR
== 00400 && S_IWUSR
== 00200 && S_IXUSR
== 00100
696 && S_IRGRP
== 00040 && S_IWGRP
== 00020 && S_IXGRP
== 00010
697 && S_IROTH
== 00004 && S_IWOTH
== 00002 && S_IXOTH
== 00001);
698 fprintf (fp
, segment
->text
,
699 (traditional_numbering_scheme
701 : ((m
& S_ISUID
? 04000 : 0)
702 | (m
& S_ISGID
? 02000 : 0)
703 | (m
& S_ISVTX
? 01000 : 0)
704 | (m
& S_IRUSR
? 00400 : 0)
705 | (m
& S_IWUSR
? 00200 : 0)
706 | (m
& S_IXUSR
? 00100 : 0)
707 | (m
& S_IRGRP
? 00040 : 0)
708 | (m
& S_IWGRP
? 00020 : 0)
709 | (m
& S_IXGRP
? 00010 : 0)
710 | (m
& S_IROTH
? 00004 : 0)
711 | (m
& S_IWOTH
? 00002 : 0)
712 | (m
& S_IXOTH
? 00001 : 0))));
716 case 'n': /* number of links */
717 fprintf (fp
, segment
->text
,
718 human_readable ((uintmax_t) stat_buf
->st_nlink
,
723 case 'p': /* pathname */
724 fprintf (fp
, segment
->text
, pathname
);
726 case 'P': /* pathname with ARGV element stripped */
729 cp
= pathname
+ path_length
;
731 /* Move past the slash between the ARGV element
732 and the rest of the pathname. But if the ARGV element
733 ends in a slash, we didn't add another, so we've
734 already skipped past it. */
739 fprintf (fp
, segment
->text
, cp
);
741 case 's': /* size in bytes */
742 fprintf (fp
, segment
->text
,
743 human_readable ((uintmax_t) stat_buf
->st_size
,
744 hbuf
, human_ceiling
, 1, 1));
746 case 't': /* mtime in `ctime' format */
747 fprintf (fp
, segment
->text
, ctime_format (stat_buf
->st_mtime
));
749 case 'u': /* user name */
753 p
= getpwuid (stat_buf
->st_uid
);
756 segment
->text
[segment
->text_len
] = 's';
757 fprintf (fp
, segment
->text
, p
->pw_name
);
763 case 'U': /* UID number */
764 fprintf (fp
, segment
->text
,
765 human_readable ((uintmax_t) stat_buf
->st_uid
, hbuf
,
766 human_ceiling
, 1, 1));
769 /* type of filesystem entry like `ls -l`: (d,-,l,s,p,b,c,n) n=nonexistent(symlink) */
770 case 'Y': /* in case of symlink */
773 if (S_ISLNK (stat_buf
->st_mode
))
777 ystat
= xstat
== lstat
? stat
: lstat
;
778 if ((*ystat
) (rel_pathname
, &sbuf
) != 0)
780 if ( errno
== ENOENT
) {
781 fprintf (fp
, segment
->text
, "N");
784 if ( errno
== ELOOP
) {
785 fprintf (fp
, segment
->text
, "L");
788 error (0, errno
, "%s", pathname
);
792 stat_buf
->st_mode
= sbuf
.st_mode
;
799 mode_t m
= stat_buf
->st_mode
& S_IFMT
;
801 fprintf (fp
, segment
->text
,
802 ( m
== S_IFSOCK
? "s" :
809 m
== S_IFDOOR
? "D" :
811 m
== S_IFIFO
? "p" : "U" ) );
821 pred_fstype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
823 if (strcmp (filesystem_type (pathname
, rel_pathname
, stat_buf
),
824 pred_ptr
->args
.str
) == 0)
830 pred_gid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
834 switch (pred_ptr
->args
.info
.kind
)
837 if (stat_buf
->st_gid
> pred_ptr
->args
.info
.l_val
)
841 if (stat_buf
->st_gid
< pred_ptr
->args
.info
.l_val
)
845 if (stat_buf
->st_gid
== pred_ptr
->args
.info
.l_val
)
853 pred_group (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
857 if (pred_ptr
->args
.gid
== stat_buf
->st_gid
)
864 pred_ilname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
866 return insert_lname (pathname
, stat_buf
, pred_ptr
, true);
870 pred_iname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
876 /* FNM_PERIOD is not used here because POSIX requires that it not be.
877 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
879 base
= base_name (pathname
);
880 if (fnmatch (pred_ptr
->args
.str
, base
, FNM_CASEFOLD
) == 0)
886 pred_inum (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
890 switch (pred_ptr
->args
.info
.kind
)
893 if (stat_buf
->st_ino
> pred_ptr
->args
.info
.l_val
)
897 if (stat_buf
->st_ino
< pred_ptr
->args
.info
.l_val
)
901 if (stat_buf
->st_ino
== pred_ptr
->args
.info
.l_val
)
909 pred_ipath (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
913 if (fnmatch (pred_ptr
->args
.str
, pathname
, FNM_CASEFOLD
) == 0)
919 pred_links (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
923 switch (pred_ptr
->args
.info
.kind
)
926 if (stat_buf
->st_nlink
> pred_ptr
->args
.info
.l_val
)
930 if (stat_buf
->st_nlink
< pred_ptr
->args
.info
.l_val
)
934 if (stat_buf
->st_nlink
== pred_ptr
->args
.info
.l_val
)
942 pred_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
944 return insert_lname (pathname
, stat_buf
, pred_ptr
, false);
948 insert_lname (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
, boolean ignore_case
)
952 if (S_ISLNK (stat_buf
->st_mode
))
954 char *linkname
= get_link_name (pathname
, rel_pathname
);
957 if (fnmatch (pred_ptr
->args
.str
, linkname
,
958 ignore_case
? FNM_CASEFOLD
: 0) == 0)
968 pred_ls (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
972 list_file (pathname
, rel_pathname
, stat_buf
, start_time
,
973 output_block_size
, stdout
);
978 pred_mmin (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
981 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, 60);
985 pred_mtime (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
988 return pred_timewindow(stat_buf
->st_mtime
, pred_ptr
, DAYSECS
);
992 pred_name (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
997 base
= base_name (pathname
);
999 /* FNM_PERIOD is not used here because POSIX requires that it not be.
1000 * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html
1002 if (fnmatch (pred_ptr
->args
.str
, base
, 0) == 0)
1008 pred_negate (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1010 /* Check whether we need a stat here. */
1011 if (pred_ptr
->need_stat
)
1013 if (!have_stat
&& (*xstat
) (rel_pathname
, stat_buf
) != 0)
1015 error (0, errno
, "%s", pathname
);
1021 return (!(*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1022 pred_ptr
->pred_right
));
1026 pred_newer (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1030 if (stat_buf
->st_mtime
> pred_ptr
->args
.time
)
1036 pred_nogroup (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1042 extern char *gid_unused
;
1044 return gid_unused
[(unsigned) stat_buf
->st_gid
];
1046 return getgrgid (stat_buf
->st_gid
) == NULL
;
1051 pred_nouser (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1054 extern char *uid_unused
;
1061 return uid_unused
[(unsigned) stat_buf
->st_uid
];
1063 return getpwuid (stat_buf
->st_uid
) == NULL
;
1068 pred_ok (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1071 /* The draft open standard requires that, in the POSIX locale,
1072 the last non-blank character of this prompt be '?'.
1073 The exact format is not specified.
1074 This standard does not have requirements for locales other than POSIX
1076 fprintf (stderr
, _("< %s ... %s > ? "),
1077 pred_ptr
->args
.exec_vec
.vec
[0], pathname
);
1080 return pred_exec (pathname
, stat_buf
, pred_ptr
);
1086 pred_open (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1095 pred_or (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1097 if (pred_ptr
->pred_left
== NULL
1098 || !(*pred_ptr
->pred_left
->pred_func
) (pathname
, stat_buf
,
1099 pred_ptr
->pred_left
))
1101 /* Check whether we need a stat here. */
1102 if (pred_ptr
->need_stat
)
1104 if (!have_stat
&& (*xstat
) (rel_pathname
, stat_buf
) != 0)
1106 error (0, errno
, "%s", pathname
);
1112 return ((*pred_ptr
->pred_right
->pred_func
) (pathname
, stat_buf
,
1113 pred_ptr
->pred_right
));
1120 pred_path (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1123 if (fnmatch (pred_ptr
->args
.str
, pathname
, 0) == 0)
1129 pred_perm (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1132 switch (pred_ptr
->args
.perm
.kind
)
1135 return (stat_buf
->st_mode
& pred_ptr
->args
.perm
.val
) == pred_ptr
->args
.perm
.val
;
1139 return (stat_buf
->st_mode
& pred_ptr
->args
.perm
.val
) != 0;
1143 return (stat_buf
->st_mode
& MODE_ALL
) == pred_ptr
->args
.perm
.val
;
1153 pred_print (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1162 pred_print0 (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1166 fputs (pathname
, stdout
);
1172 pred_prune (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1177 stop_at_current_level
= true;
1178 return (do_dir_first
); /* This is what SunOS find seems to do. */
1182 pred_quit (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1191 pred_regex (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1193 int len
= strlen (pathname
);
1195 if (re_match (pred_ptr
->args
.regex
, pathname
, len
, 0,
1196 (struct re_registers
*) NULL
) == len
)
1202 pred_size (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1207 f_val
= ((stat_buf
->st_size
/ pred_ptr
->args
.size
.blocksize
)
1208 + (stat_buf
->st_size
% pred_ptr
->args
.size
.blocksize
!= 0));
1209 switch (pred_ptr
->args
.size
.kind
)
1212 if (f_val
> pred_ptr
->args
.size
.size
)
1216 if (f_val
< pred_ptr
->args
.size
.size
)
1220 if (f_val
== pred_ptr
->args
.size
.size
)
1228 pred_true (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1237 pred_type (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1239 mode_t mode
= stat_buf
->st_mode
;
1240 mode_t type
= pred_ptr
->args
.type
;
1245 /* POSIX system; check `mode' the slow way. */
1246 if ((S_ISBLK (mode
) && type
== S_IFBLK
)
1247 || (S_ISCHR (mode
) && type
== S_IFCHR
)
1248 || (S_ISDIR (mode
) && type
== S_IFDIR
)
1249 || (S_ISREG (mode
) && type
== S_IFREG
)
1251 || (S_ISLNK (mode
) && type
== S_IFLNK
)
1254 || (S_ISFIFO (mode
) && type
== S_IFIFO
)
1257 || (S_ISSOCK (mode
) && type
== S_IFSOCK
)
1260 || (S_ISDOOR (mode
) && type
== S_IFDOOR
)
1264 /* Unix system; check `mode' the fast way. */
1265 if ((mode
& S_IFMT
) == type
)
1273 pred_uid (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1276 switch (pred_ptr
->args
.info
.kind
)
1279 if (stat_buf
->st_uid
> pred_ptr
->args
.info
.l_val
)
1283 if (stat_buf
->st_uid
< pred_ptr
->args
.info
.l_val
)
1287 if (stat_buf
->st_uid
== pred_ptr
->args
.info
.l_val
)
1295 pred_used (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1300 delta
= stat_buf
->st_atime
- stat_buf
->st_ctime
; /* Use difftime? */
1301 return pred_timewindow(delta
, pred_ptr
, DAYSECS
);
1305 pred_user (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1308 if (pred_ptr
->args
.uid
== stat_buf
->st_uid
)
1315 pred_xtype (char *pathname
, struct stat
*stat_buf
, struct predicate
*pred_ptr
)
1317 struct stat sbuf
; /* local copy, not stat_buf because we're using a different stat method */
1318 int (*ystat
) (const char*, struct stat
*p
);
1320 switch (symlink_handling
)
1322 case SYMLINK_ALWAYS_DEREF
:
1323 ystat
= optionl_stat
;
1324 case SYMLINK_DEREF_ARGSONLY
:
1325 case SYMLINK_NEVER_DEREF
:
1326 ystat
= optionp_stat
;
1329 if ((*ystat
) (rel_pathname
, &sbuf
) != 0)
1331 if (ystat
== optionl_stat
&& errno
== ENOENT
)
1333 /* If we failed to follow the symlink,
1334 * fall back on looking at the symlink itself.
1336 /* Mimic behavior of ls -lL. */
1337 return (pred_type (pathname
, stat_buf
, pred_ptr
));
1341 error (0, errno
, "%s", pathname
);
1346 /* Now that we have our stat() information, query it in the same
1347 * way that -type does.
1349 return (pred_type (pathname
, &sbuf
, pred_ptr
));
1352 /* 1) fork to get a child; parent remembers the child pid
1353 2) child execs the command requested
1354 3) parent waits for child; checks for proper pid of child
1358 ret errno status(h) status(l)
1360 pid x signal# 0177 stopped
1361 pid x exit arg 0 term by _exit
1362 pid x 0 signal # term by signal
1363 -1 EINTR parent got signal
1364 -1 other some other kind of error
1366 Return true only if the pid matches, status(l) is
1367 zero, and the exit arg (status high) is 0.
1368 Otherwise return false, possibly printing an error message. */
1371 launch (struct predicate
*pred_ptr
)
1375 struct exec_val
*execp
; /* Pointer for efficiency. */
1376 static int first_time
= 1;
1378 execp
= &pred_ptr
->args
.exec_vec
;
1380 /* Make sure output of command doesn't get mixed with find output. */
1384 /* Make sure to listen for the kids. */
1388 signal (SIGCHLD
, SIG_DFL
);
1391 child_pid
= fork ();
1392 if (child_pid
== -1)
1393 error (1, errno
, _("cannot fork"));
1396 /* We be the child. */
1397 if (starting_desc
< 0
1398 ? chdir (starting_dir
) != 0
1399 : fchdir (starting_desc
) != 0)
1401 error (0, errno
, "%s", starting_dir
);
1404 execvp (execp
->vec
[0], execp
->vec
);
1405 error (0, errno
, "%s", execp
->vec
[0]);
1410 while (waitpid (child_pid
, &status
, 0) == (pid_t
) -1)
1413 error (0, errno
, _("error waiting for %s"), execp
->vec
[0]);
1417 if (WIFSIGNALED (status
))
1419 error (0, 0, _("%s terminated by signal %d"),
1420 execp
->vec
[0], WTERMSIG (status
));
1424 return (!WEXITSTATUS (status
));
1427 /* Return a static string formatting the time WHEN according to the
1428 strftime format character KIND. */
1431 format_date (time_t when
, int kind
)
1433 static char buf
[MAX (LONGEST_HUMAN_READABLE
+ 2, 64)];
1441 strcpy (fmt
, "%F+%T");
1444 && (tm
= localtime (&when
))
1445 && strftime (buf
, sizeof buf
, fmt
, tm
))
1450 char *p
= human_readable (when
< 0 ? -w
: w
, buf
+ 1,
1451 human_ceiling
, 1, 1);
1462 char *r
= ctime (&when
);
1465 /* The time cannot be represented as a struct tm.
1466 Output it as an integer. */
1467 return format_date (when
, '@');
1471 /* Remove the trailing newline from the ctime output,
1472 being careful not to assume that the output is fixed-width. */
1473 *strchr (r
, '\n') = '\0';
1479 /* Return a pointer to the string representation of
1480 the predicate function PRED_FUNC. */
1483 find_pred_name (pred_func
)
1488 for (i
= 0; pred_table
[i
].pred_func
!= 0; i
++)
1489 if (pred_table
[i
].pred_func
== pred_func
)
1491 return (pred_table
[i
].pred_name
);
1500 for (i
= 0; type_table
[i
].type
!= (short) -1; i
++)
1501 if (type_table
[i
].type
== type
)
1503 return (type_table
[i
].type_name
);
1512 for (i
= 0; prec_table
[i
].prec
!= (short) -1; i
++)
1513 if (prec_table
[i
].prec
== prec
)
1515 return (prec_table
[i
].prec_name
);
1518 /* Walk the expression tree NODE to stdout.
1519 INDENT is the number of levels to indent the left margin. */
1522 print_tree (node
, indent
)
1523 struct predicate
*node
;
1530 for (i
= 0; i
< indent
; i
++)
1532 printf ("pred = %s type = %s prec = %s addr = %x\n",
1533 find_pred_name (node
->pred_func
),
1534 type_name (node
->p_type
), prec_name (node
->p_prec
), node
);
1535 for (i
= 0; i
< indent
; i
++)
1537 printf (_("left:\n"));
1538 print_tree (node
->pred_left
, indent
+ 1);
1539 for (i
= 0; i
< indent
; i
++)
1541 printf (_("right:\n"));
1542 print_tree (node
->pred_right
, indent
+ 1);
1545 /* Copy STR into BUF and trim blanks from the end of BUF.
1549 blank_rtrim (str
, buf
)
1558 i
= strlen (buf
) - 1;
1559 while ((i
>= 0) && ((buf
[i
] == ' ') || buf
[i
] == '\t'))
1565 /* Print out the predicate list starting at NODE. */
1569 struct predicate
*node
;
1571 struct predicate
*cur
;
1577 printf ("%s ", blank_rtrim (find_pred_name (cur
->pred_func
), name
));
1578 cur
= cur
->pred_next
;