]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/sel_subs.c
a4e1c2271ef8436abeec0709427073dcda57088e
1 /* $OpenBSD: sel_subs.c,v 1.7 1997/08/17 23:05:09 millert Exp $ */
2 /* $NetBSD: sel_subs.c,v 1.5 1995/03/21 09:07:42 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid
[] = "@(#)sel_subs.c 8.1 (Berkeley) 5/31/93";
45 static char rcsid
[] __attribute__((__unused__
)) = "$OpenBSD: sel_subs.c,v 1.7 1997/08/17 23:05:09 millert Exp $";
49 #include <sys/types.h>
52 #include <sys/param.h>
63 static int str_sec
__P((register char *, time_t *));
64 static int usr_match
__P((register ARCHD
*));
65 static int grp_match
__P((register ARCHD
*));
66 static int trng_match
__P((register ARCHD
*));
68 static TIME_RNG
*trhead
= NULL
; /* time range list head */
69 static TIME_RNG
*trtail
= NULL
; /* time range list tail */
70 static USRT
**usrtb
= NULL
; /* user selection table */
71 static GRPT
**grptb
= NULL
; /* group selection table */
74 * Routines for selection of archive members
79 * check if this file matches a specfied uid, gid or time range
81 * 0 if this archive member should be processed, 1 if it should be skipped
86 sel_chk(register ARCHD
*arcn
)
93 if (((usrtb
!= NULL
) && usr_match(arcn
)) ||
94 ((grptb
!= NULL
) && grp_match(arcn
)) ||
95 ((trhead
!= NULL
) && trng_match(arcn
)))
101 * User/group selection routines
103 * Routines to handle user selection of files based on the file uid/gid. To
104 * add an entry, the user supplies either then name or the uid/gid starting with
105 * a # on the command line. A \# will eascape the #.
110 * add a user match to the user match hash table
112 * 0 if added ok, -1 otherwise;
117 usr_add(register char *str
)
126 register struct passwd
*pw
;
130 * create the table if it doesn't exist
132 if ((str
== NULL
) || (*str
== '\0'))
134 if ((usrtb
== NULL
) &&
135 ((usrtb
= (USRT
**)calloc(USR_TB_SZ
, sizeof(USRT
*))) == NULL
)) {
136 paxwarn(1, "Unable to allocate memory for user selection table");
141 * figure out user spec
145 * it is a user name, \# escapes # as first char in user name
147 if ((str
[0] == '\\') && (str
[1] == '#'))
149 if ((pw
= getpwnam(str
)) == NULL
) {
150 paxwarn(1, "Unable to find uid for user: %s", str
);
153 uid
= (uid_t
)pw
->pw_uid
;
156 uid
= (uid_t
)atoi(str
+1);
158 uid
= (uid_t
)strtoul(str
+1, NULL
, 10);
163 * hash it and go down the hash chain (if any) looking for it
165 indx
= ((unsigned)uid
) % USR_TB_SZ
;
166 if ((pt
= usrtb
[indx
]) != NULL
) {
175 * uid is not yet in the table, add it to the front of the chain
177 if ((pt
= (USRT
*)malloc(sizeof(USRT
))) != NULL
) {
179 pt
->fow
= usrtb
[indx
];
183 paxwarn(1, "User selection table out of memory");
189 * check if this files uid matches a selected uid.
191 * 0 if this archive member should be processed, 1 if it should be skipped
196 usr_match(register ARCHD
*arcn
)
200 register ARCHD
*arcn
;
206 * hash and look for it in the table
208 pt
= usrtb
[((unsigned)arcn
->sb
.st_uid
) % USR_TB_SZ
];
210 if (pt
->uid
== arcn
->sb
.st_uid
)
223 * add a group match to the group match hash table
225 * 0 if added ok, -1 otherwise;
230 grp_add(register char *str
)
239 register struct group
*gr
;
243 * create the table if it doesn't exist
245 if ((str
== NULL
) || (*str
== '\0'))
247 if ((grptb
== NULL
) &&
248 ((grptb
= (GRPT
**)calloc(GRP_TB_SZ
, sizeof(GRPT
*))) == NULL
)) {
249 paxwarn(1, "Unable to allocate memory fo group selection table");
254 * figure out user spec
258 * it is a group name, \# escapes # as first char in group name
260 if ((str
[0] == '\\') && (str
[1] == '#'))
262 if ((gr
= getgrnam(str
)) == NULL
) {
263 paxwarn(1,"Cannot determine gid for group name: %s", str
);
266 gid
= (gid_t
)gr
->gr_gid
;
269 gid
= (gid_t
)atoi(str
+1);
271 gid
= (gid_t
)strtoul(str
+1, NULL
, 10);
276 * hash it and go down the hash chain (if any) looking for it
278 indx
= ((unsigned)gid
) % GRP_TB_SZ
;
279 if ((pt
= grptb
[indx
]) != NULL
) {
288 * gid not in the table, add it to the front of the chain
290 if ((pt
= (GRPT
*)malloc(sizeof(GRPT
))) != NULL
) {
292 pt
->fow
= grptb
[indx
];
296 paxwarn(1, "Group selection table out of memory");
302 * check if this files gid matches a selected gid.
304 * 0 if this archive member should be processed, 1 if it should be skipped
309 grp_match(register ARCHD
*arcn
)
313 register ARCHD
*arcn
;
319 * hash and look for it in the table
321 pt
= grptb
[((unsigned)arcn
->sb
.st_gid
) % GRP_TB_SZ
];
323 if (pt
->gid
== arcn
->sb
.st_gid
)
335 * Time range selection routines
337 * Routines to handle user selection of files based on the modification and/or
338 * inode change time falling within a specified time range (the non-standard
339 * -T flag). The user may specify any number of different file time ranges.
340 * Time ranges are checked one at a time until a match is found (if at all).
341 * If the file has a mtime (and/or ctime) which lies within one of the time
342 * ranges, the file is selected. Time ranges may have a lower and/or a upper
343 * value. These ranges are inclusive. When no time ranges are supplied to pax
344 * with the -T option, all members in the archive will be selected by the time
345 * range routines. When only a lower range is supplied, only files with a
346 * mtime (and/or ctime) equal to or younger are selected. When only a upper
347 * range is supplied, only files with a mtime (and/or ctime) equal to or older
348 * are selected. When the lower time range is equal to the upper time range,
349 * only files with a mtime (or ctime) of exactly that time are selected.
354 * add a time range match to the time range list.
355 * This is a non-standard pax option. Lower and upper ranges are in the
356 * format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
357 * Time ranges are based on current time, so 1234 would specify a time of
360 * 0 if the time range was added to the list, -1 otherwise
365 trng_add(register char *str
)
372 register TIME_RNG
*pt
;
373 register char *up_pt
= NULL
;
375 register char *flgpt
;
376 register int dot
= 0;
379 * throw out the badly formed time ranges
381 if ((str
== NULL
) || (*str
== '\0')) {
382 paxwarn(1, "Empty time range string");
387 * locate optional flags suffix /{cm}.
389 if ((flgpt
= strrchr(str
, '/')) != NULL
)
392 for (stpt
= str
; *stpt
!= '\0'; ++stpt
) {
393 if ((*stpt
>= '0') && (*stpt
<= '9'))
395 if ((*stpt
== ',') && (up_pt
== NULL
)) {
403 * allow only one dot per range (secs)
405 if ((*stpt
== '.') && (!dot
)) {
409 paxwarn(1, "Improperly specified time range: %s", str
);
414 * allocate space for the time range and store the limits
416 if ((pt
= (TIME_RNG
*)malloc(sizeof(TIME_RNG
))) == NULL
) {
417 paxwarn(1, "Unable to allocate memory for time range");
422 * by default we only will check file mtime, but usee can specify
423 * mtime, ctime (inode change time) or both.
425 if ((flgpt
== NULL
) || (*flgpt
== '\0'))
429 while (*flgpt
!= '\0') {
440 paxwarn(1, "Bad option %c with time range %s",
449 * start off with the current time
451 pt
->low_time
= pt
->high_time
= time(NULL
);
456 if (str_sec(str
, &(pt
->low_time
)) < 0) {
457 paxwarn(1, "Illegal lower time range %s", str
);
458 (void)free((char *)pt
);
464 if ((up_pt
!= NULL
) && (*up_pt
!= '\0')) {
468 if (str_sec(up_pt
, &(pt
->high_time
)) < 0) {
469 paxwarn(1, "Illegal upper time range %s", up_pt
);
470 (void)free((char *)pt
);
476 * check that the upper and lower do not overlap
478 if (pt
->flgs
& HASLOW
) {
479 if (pt
->low_time
> pt
->high_time
) {
480 paxwarn(1, "Upper %s and lower %s time overlap",
482 (void)free((char *)pt
);
489 if (trhead
== NULL
) {
490 trtail
= trhead
= pt
;
498 paxwarn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
504 * check if this files mtime/ctime falls within any supplied time range.
506 * 0 if this archive member should be processed, 1 if it should be skipped
511 trng_match(register ARCHD
*arcn
)
515 register ARCHD
*arcn
;
518 register TIME_RNG
*pt
;
521 * have to search down the list one at a time looking for a match.
522 * remember time range limits are inclusive.
526 switch(pt
->flgs
& CMPBOTH
) {
529 * user wants both mtime and ctime checked for this
532 if (((pt
->flgs
& HASLOW
) &&
533 (arcn
->sb
.st_mtime
< pt
->low_time
) &&
534 (arcn
->sb
.st_ctime
< pt
->low_time
)) ||
535 ((pt
->flgs
& HASHIGH
) &&
536 (arcn
->sb
.st_mtime
> pt
->high_time
) &&
537 (arcn
->sb
.st_ctime
> pt
->high_time
))) {
544 * user wants only ctime checked for this time range
546 if (((pt
->flgs
& HASLOW
) &&
547 (arcn
->sb
.st_ctime
< pt
->low_time
)) ||
548 ((pt
->flgs
& HASHIGH
) &&
549 (arcn
->sb
.st_ctime
> pt
->high_time
))) {
557 * user wants only mtime checked for this time range
559 if (((pt
->flgs
& HASLOW
) &&
560 (arcn
->sb
.st_mtime
< pt
->low_time
)) ||
561 ((pt
->flgs
& HASHIGH
) &&
562 (arcn
->sb
.st_mtime
> pt
->high_time
))) {
578 * Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
579 * seconds. Tval already has current time loaded into it at entry.
581 * 0 if converted ok, -1 otherwise
586 str_sec(register char *str
, time_t *tval
)
594 register struct tm
*lt
;
595 register char *dot
= NULL
;
597 lt
= localtime(tval
);
598 if ((dot
= strchr(str
, '.')) != NULL
) {
603 if (strlen(dot
) != 2)
605 if ((lt
->tm_sec
= ATOI2(dot
)) > 61)
610 switch (strlen(str
)) {
614 * watch out for year 2000
616 if ((lt
->tm_year
= ATOI2(str
)) < 69)
623 * watch out months are from 0 - 11 internally
625 if ((lt
->tm_mon
= ATOI2(str
)) > 12)
634 if ((lt
->tm_mday
= ATOI2(str
)) > 31)
642 if ((lt
->tm_hour
= ATOI2(str
)) > 23)
650 if ((lt
->tm_min
= ATOI2(str
)) > 59)
657 * convert broken-down time to GMT clock time seconds
659 if ((*tval
= mktime(lt
)) == -1)