1*9f61b804Splunky /* $NetBSD: sel_subs.c,v 1.24 2011/08/31 16:24:54 plunky Exp $ */
2b5b29542Sagc
3b5b29542Sagc /*-
4ed6ed8e6Sagc * Copyright (c) 1992 Keith Muller.
5b5b29542Sagc * Copyright (c) 1992, 1993
6b5b29542Sagc * The Regents of the University of California. All rights reserved.
7b5b29542Sagc *
8b5b29542Sagc * This code is derived from software contributed to Berkeley by
9b5b29542Sagc * Keith Muller of the University of California, San Diego.
10b5b29542Sagc *
11b5b29542Sagc * Redistribution and use in source and binary forms, with or without
12b5b29542Sagc * modification, are permitted provided that the following conditions
13b5b29542Sagc * are met:
14b5b29542Sagc * 1. Redistributions of source code must retain the above copyright
15b5b29542Sagc * notice, this list of conditions and the following disclaimer.
16b5b29542Sagc * 2. Redistributions in binary form must reproduce the above copyright
17b5b29542Sagc * notice, this list of conditions and the following disclaimer in the
18b5b29542Sagc * documentation and/or other materials provided with the distribution.
19b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
20b5b29542Sagc * may be used to endorse or promote products derived from this software
21b5b29542Sagc * without specific prior written permission.
22b5b29542Sagc *
23b5b29542Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24b5b29542Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b5b29542Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b5b29542Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27b5b29542Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b5b29542Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b5b29542Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b5b29542Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b5b29542Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b5b29542Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b5b29542Sagc * SUCH DAMAGE.
34b5b29542Sagc */
3549f0ad86Scgd
36171d6532Slukem #if HAVE_NBTOOL_CONFIG_H
37171d6532Slukem #include "nbtool_config.h"
38171d6532Slukem #endif
39171d6532Slukem
40f3cd6022Schristos #include <sys/cdefs.h>
41171d6532Slukem #if !defined(lint)
4249f0ad86Scgd #if 0
4349f0ad86Scgd static char sccsid[] = "@(#)sel_subs.c 8.1 (Berkeley) 5/31/93";
4449f0ad86Scgd #else
45*9f61b804Splunky __RCSID("$NetBSD: sel_subs.c,v 1.24 2011/08/31 16:24:54 plunky Exp $");
4649f0ad86Scgd #endif
478b35abe2Sjtc #endif /* not lint */
488b35abe2Sjtc
498b35abe2Sjtc #include <sys/types.h>
508b35abe2Sjtc #include <sys/time.h>
518b35abe2Sjtc #include <sys/stat.h>
528b35abe2Sjtc #include <sys/param.h>
53f8c64f21Smycroft
548b35abe2Sjtc #include <pwd.h>
558b35abe2Sjtc #include <grp.h>
568b35abe2Sjtc #include <stdio.h>
578b35abe2Sjtc #include <ctype.h>
588b35abe2Sjtc #include <string.h>
598b35abe2Sjtc #include <strings.h>
60461522b4Skleink #include <time.h>
618b35abe2Sjtc #include <unistd.h>
628b35abe2Sjtc #include <stdlib.h>
63f8c64f21Smycroft #include <tzfile.h>
64f8c64f21Smycroft
658b35abe2Sjtc #include "pax.h"
668b35abe2Sjtc #include "sel_subs.h"
678b35abe2Sjtc #include "extern.h"
688b35abe2Sjtc
69c1bd745cSlukem static int str_sec(const char *, time_t *);
70c1bd745cSlukem static int usr_match(ARCHD *);
71c1bd745cSlukem static int grp_match(ARCHD *);
72c1bd745cSlukem static int trng_match(ARCHD *);
738b35abe2Sjtc
748b35abe2Sjtc static TIME_RNG *trhead = NULL; /* time range list head */
758b35abe2Sjtc static TIME_RNG *trtail = NULL; /* time range list tail */
768b35abe2Sjtc static USRT **usrtb = NULL; /* user selection table */
778b35abe2Sjtc static GRPT **grptb = NULL; /* group selection table */
788b35abe2Sjtc
798b35abe2Sjtc /*
808b35abe2Sjtc * Routines for selection of archive members
818b35abe2Sjtc */
828b35abe2Sjtc
838b35abe2Sjtc /*
848b35abe2Sjtc * sel_chk()
858ce1f4ffSmsaitoh * check if this file matches a specified uid, gid or time range
868b35abe2Sjtc * Return:
878b35abe2Sjtc * 0 if this archive member should be processed, 1 if it should be skipped
888b35abe2Sjtc */
898b35abe2Sjtc
908b35abe2Sjtc int
sel_chk(ARCHD * arcn)9148250187Stls sel_chk(ARCHD *arcn)
928b35abe2Sjtc {
938b35abe2Sjtc if (((usrtb != NULL) && usr_match(arcn)) ||
948b35abe2Sjtc ((grptb != NULL) && grp_match(arcn)) ||
958b35abe2Sjtc ((trhead != NULL) && trng_match(arcn)))
96cdec4ac1Sdsl return 1;
97cdec4ac1Sdsl return 0;
988b35abe2Sjtc }
998b35abe2Sjtc
1008b35abe2Sjtc /*
1018b35abe2Sjtc * User/group selection routines
1028b35abe2Sjtc *
1038b35abe2Sjtc * Routines to handle user selection of files based on the file uid/gid. To
104b4371d47Swiz * add an entry, the user supplies either the name or the uid/gid starting with
105b4371d47Swiz * a # on the command line. A \# will escape the #.
1068b35abe2Sjtc */
1078b35abe2Sjtc
1088b35abe2Sjtc /*
1098b35abe2Sjtc * usr_add()
1108b35abe2Sjtc * add a user match to the user match hash table
1118b35abe2Sjtc * Return:
1128b35abe2Sjtc * 0 if added ok, -1 otherwise;
1138b35abe2Sjtc */
1148b35abe2Sjtc
1158b35abe2Sjtc int
usr_add(char * str)11648250187Stls usr_add(char *str)
1178b35abe2Sjtc {
11848250187Stls u_int indx;
11948250187Stls USRT *pt;
12048250187Stls struct passwd *pw;
12148250187Stls uid_t uid;
1228b35abe2Sjtc
1238b35abe2Sjtc /*
1248b35abe2Sjtc * create the table if it doesn't exist
1258b35abe2Sjtc */
1268b35abe2Sjtc if ((str == NULL) || (*str == '\0'))
127cdec4ac1Sdsl return -1;
1288b35abe2Sjtc if ((usrtb == NULL) &&
1298b35abe2Sjtc ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
130f3cd6022Schristos tty_warn(1,
131f3cd6022Schristos "Unable to allocate memory for user selection table");
132cdec4ac1Sdsl return -1;
1338b35abe2Sjtc }
1348b35abe2Sjtc
1358b35abe2Sjtc /*
1368b35abe2Sjtc * figure out user spec
1378b35abe2Sjtc */
1388b35abe2Sjtc if (str[0] != '#') {
1398b35abe2Sjtc /*
1408b35abe2Sjtc * it is a user name, \# escapes # as first char in user name
1418b35abe2Sjtc */
1428b35abe2Sjtc if ((str[0] == '\\') && (str[1] == '#'))
1438b35abe2Sjtc ++str;
1448b35abe2Sjtc if ((pw = getpwnam(str)) == NULL) {
145f3cd6022Schristos tty_warn(1, "Unable to find uid for user: %s", str);
146cdec4ac1Sdsl return -1;
1478b35abe2Sjtc }
1488b35abe2Sjtc uid = (uid_t)pw->pw_uid;
1498b35abe2Sjtc } else
150*9f61b804Splunky uid = (uid_t)strtoul(str+1, NULL, 10);
1518b35abe2Sjtc endpwent();
1528b35abe2Sjtc
1538b35abe2Sjtc /*
1548b35abe2Sjtc * hash it and go down the hash chain (if any) looking for it
1558b35abe2Sjtc */
1568b35abe2Sjtc indx = ((unsigned)uid) % USR_TB_SZ;
1578b35abe2Sjtc if ((pt = usrtb[indx]) != NULL) {
1588b35abe2Sjtc while (pt != NULL) {
1598b35abe2Sjtc if (pt->uid == uid)
160cdec4ac1Sdsl return 0;
1618b35abe2Sjtc pt = pt->fow;
1628b35abe2Sjtc }
1638b35abe2Sjtc }
1648b35abe2Sjtc
1658b35abe2Sjtc /*
1668b35abe2Sjtc * uid is not yet in the table, add it to the front of the chain
1678b35abe2Sjtc */
1688b35abe2Sjtc if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
1698b35abe2Sjtc pt->uid = uid;
1708b35abe2Sjtc pt->fow = usrtb[indx];
1718b35abe2Sjtc usrtb[indx] = pt;
172cdec4ac1Sdsl return 0;
1738b35abe2Sjtc }
174f3cd6022Schristos tty_warn(1, "User selection table out of memory");
175cdec4ac1Sdsl return -1;
1768b35abe2Sjtc }
1778b35abe2Sjtc
1788b35abe2Sjtc /*
1798b35abe2Sjtc * usr_match()
1808b35abe2Sjtc * check if this files uid matches a selected uid.
1818b35abe2Sjtc * Return:
1828b35abe2Sjtc * 0 if this archive member should be processed, 1 if it should be skipped
1838b35abe2Sjtc */
1848b35abe2Sjtc
1858b35abe2Sjtc static int
usr_match(ARCHD * arcn)18648250187Stls usr_match(ARCHD *arcn)
1878b35abe2Sjtc {
18848250187Stls USRT *pt;
1898b35abe2Sjtc
1908b35abe2Sjtc /*
1918b35abe2Sjtc * hash and look for it in the table
1928b35abe2Sjtc */
1938b35abe2Sjtc pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
1948b35abe2Sjtc while (pt != NULL) {
1958b35abe2Sjtc if (pt->uid == arcn->sb.st_uid)
196cdec4ac1Sdsl return 0;
1978b35abe2Sjtc pt = pt->fow;
1988b35abe2Sjtc }
1998b35abe2Sjtc
2008b35abe2Sjtc /*
2018b35abe2Sjtc * not found
2028b35abe2Sjtc */
203cdec4ac1Sdsl return 1;
2048b35abe2Sjtc }
2058b35abe2Sjtc
2068b35abe2Sjtc /*
2078b35abe2Sjtc * grp_add()
2088b35abe2Sjtc * add a group match to the group match hash table
2098b35abe2Sjtc * Return:
2108b35abe2Sjtc * 0 if added ok, -1 otherwise;
2118b35abe2Sjtc */
2128b35abe2Sjtc
2138b35abe2Sjtc int
grp_add(char * str)21448250187Stls grp_add(char *str)
2158b35abe2Sjtc {
21648250187Stls u_int indx;
21748250187Stls GRPT *pt;
21848250187Stls struct group *gr;
21948250187Stls gid_t gid;
2208b35abe2Sjtc
2218b35abe2Sjtc /*
2228b35abe2Sjtc * create the table if it doesn't exist
2238b35abe2Sjtc */
2248b35abe2Sjtc if ((str == NULL) || (*str == '\0'))
225cdec4ac1Sdsl return -1;
2268b35abe2Sjtc if ((grptb == NULL) &&
2278b35abe2Sjtc ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
228f3cd6022Schristos tty_warn(1,
229f3cd6022Schristos "Unable to allocate memory fo group selection table");
230cdec4ac1Sdsl return -1;
2318b35abe2Sjtc }
2328b35abe2Sjtc
2338b35abe2Sjtc /*
2348b35abe2Sjtc * figure out user spec
2358b35abe2Sjtc */
2368b35abe2Sjtc if (str[0] != '#') {
2378b35abe2Sjtc /*
2388b35abe2Sjtc * it is a group name, \# escapes # as first char in group name
2398b35abe2Sjtc */
2408b35abe2Sjtc if ((str[0] == '\\') && (str[1] == '#'))
2418b35abe2Sjtc ++str;
2428b35abe2Sjtc if ((gr = getgrnam(str)) == NULL) {
243f3cd6022Schristos tty_warn(1,
244f3cd6022Schristos "Cannot determine gid for group name: %s", str);
245cdec4ac1Sdsl return -1;
2468b35abe2Sjtc }
2478b35abe2Sjtc gid = (gid_t)gr->gr_gid;
2488b35abe2Sjtc } else
249*9f61b804Splunky gid = (gid_t)strtoul(str+1, NULL, 10);
2508b35abe2Sjtc endgrent();
2518b35abe2Sjtc
2528b35abe2Sjtc /*
2538b35abe2Sjtc * hash it and go down the hash chain (if any) looking for it
2548b35abe2Sjtc */
2558b35abe2Sjtc indx = ((unsigned)gid) % GRP_TB_SZ;
2568b35abe2Sjtc if ((pt = grptb[indx]) != NULL) {
2578b35abe2Sjtc while (pt != NULL) {
2588b35abe2Sjtc if (pt->gid == gid)
259cdec4ac1Sdsl return 0;
2608b35abe2Sjtc pt = pt->fow;
2618b35abe2Sjtc }
2628b35abe2Sjtc }
2638b35abe2Sjtc
2648b35abe2Sjtc /*
2658b35abe2Sjtc * gid not in the table, add it to the front of the chain
2668b35abe2Sjtc */
2678b35abe2Sjtc if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
2688b35abe2Sjtc pt->gid = gid;
2698b35abe2Sjtc pt->fow = grptb[indx];
2708b35abe2Sjtc grptb[indx] = pt;
271cdec4ac1Sdsl return 0;
2728b35abe2Sjtc }
273f3cd6022Schristos tty_warn(1, "Group selection table out of memory");
274cdec4ac1Sdsl return -1;
2758b35abe2Sjtc }
2768b35abe2Sjtc
2778b35abe2Sjtc /*
2788b35abe2Sjtc * grp_match()
2798b35abe2Sjtc * check if this files gid matches a selected gid.
2808b35abe2Sjtc * Return:
2818b35abe2Sjtc * 0 if this archive member should be processed, 1 if it should be skipped
2828b35abe2Sjtc */
2838b35abe2Sjtc
2848b35abe2Sjtc static int
grp_match(ARCHD * arcn)28548250187Stls grp_match(ARCHD *arcn)
2868b35abe2Sjtc {
28748250187Stls GRPT *pt;
2888b35abe2Sjtc
2898b35abe2Sjtc /*
2908b35abe2Sjtc * hash and look for it in the table
2918b35abe2Sjtc */
2928b35abe2Sjtc pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
2938b35abe2Sjtc while (pt != NULL) {
2948b35abe2Sjtc if (pt->gid == arcn->sb.st_gid)
295cdec4ac1Sdsl return 0;
2968b35abe2Sjtc pt = pt->fow;
2978b35abe2Sjtc }
2988b35abe2Sjtc
2998b35abe2Sjtc /*
3008b35abe2Sjtc * not found
3018b35abe2Sjtc */
302cdec4ac1Sdsl return 1;
3038b35abe2Sjtc }
3048b35abe2Sjtc
3058b35abe2Sjtc /*
3068b35abe2Sjtc * Time range selection routines
3078b35abe2Sjtc *
3088b35abe2Sjtc * Routines to handle user selection of files based on the modification and/or
3098b35abe2Sjtc * inode change time falling within a specified time range (the non-standard
3108b35abe2Sjtc * -T flag). The user may specify any number of different file time ranges.
3118b35abe2Sjtc * Time ranges are checked one at a time until a match is found (if at all).
3128b35abe2Sjtc * If the file has a mtime (and/or ctime) which lies within one of the time
3138b35abe2Sjtc * ranges, the file is selected. Time ranges may have a lower and/or a upper
3148b35abe2Sjtc * value. These ranges are inclusive. When no time ranges are supplied to pax
3158b35abe2Sjtc * with the -T option, all members in the archive will be selected by the time
3168b35abe2Sjtc * range routines. When only a lower range is supplied, only files with a
3178b35abe2Sjtc * mtime (and/or ctime) equal to or younger are selected. When only a upper
3188b35abe2Sjtc * range is supplied, only files with a mtime (and/or ctime) equal to or older
3198b35abe2Sjtc * are selected. When the lower time range is equal to the upper time range,
3208b35abe2Sjtc * only files with a mtime (or ctime) of exactly that time are selected.
3218b35abe2Sjtc */
3228b35abe2Sjtc
3238b35abe2Sjtc /*
3248b35abe2Sjtc * trng_add()
3258b35abe2Sjtc * add a time range match to the time range list.
3268b35abe2Sjtc * This is a non-standard pax option. Lower and upper ranges are in the
3278b35abe2Sjtc * format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
3288b35abe2Sjtc * Time ranges are based on current time, so 1234 would specify a time of
3298b35abe2Sjtc * 12:34 today.
3308b35abe2Sjtc * Return:
3318b35abe2Sjtc * 0 if the time range was added to the list, -1 otherwise
3328b35abe2Sjtc */
3338b35abe2Sjtc
3348b35abe2Sjtc int
trng_add(char * str)33548250187Stls trng_add(char *str)
3368b35abe2Sjtc {
33748250187Stls TIME_RNG *pt;
33848250187Stls char *up_pt = NULL;
33948250187Stls char *stpt;
34048250187Stls char *flgpt;
34148250187Stls int dot = 0;
3428b35abe2Sjtc
3438b35abe2Sjtc /*
3448b35abe2Sjtc * throw out the badly formed time ranges
3458b35abe2Sjtc */
3468b35abe2Sjtc if ((str == NULL) || (*str == '\0')) {
347f3cd6022Schristos tty_warn(1, "Empty time range string");
348cdec4ac1Sdsl return -1;
3498b35abe2Sjtc }
3508b35abe2Sjtc
3518b35abe2Sjtc /*
3528b35abe2Sjtc * locate optional flags suffix /{cm}.
3538b35abe2Sjtc */
35406f53b68Smycroft if ((flgpt = strrchr(str, '/')) != NULL)
3558b35abe2Sjtc *flgpt++ = '\0';
3568b35abe2Sjtc
3578b35abe2Sjtc for (stpt = str; *stpt != '\0'; ++stpt) {
3588b35abe2Sjtc if ((*stpt >= '0') && (*stpt <= '9'))
3598b35abe2Sjtc continue;
3608b35abe2Sjtc if ((*stpt == ',') && (up_pt == NULL)) {
3618b35abe2Sjtc *stpt = '\0';
3628b35abe2Sjtc up_pt = stpt + 1;
3638b35abe2Sjtc dot = 0;
3648b35abe2Sjtc continue;
3658b35abe2Sjtc }
3668b35abe2Sjtc
3678b35abe2Sjtc /*
3688b35abe2Sjtc * allow only one dot per range (secs)
3698b35abe2Sjtc */
3708b35abe2Sjtc if ((*stpt == '.') && (!dot)) {
3718b35abe2Sjtc ++dot;
3728b35abe2Sjtc continue;
3738b35abe2Sjtc }
374f3cd6022Schristos tty_warn(1, "Improperly specified time range: %s", str);
3758b35abe2Sjtc goto out;
3768b35abe2Sjtc }
3778b35abe2Sjtc
3788b35abe2Sjtc /*
3798b35abe2Sjtc * allocate space for the time range and store the limits
3808b35abe2Sjtc */
3817b9eb38dSchristos if ((pt = malloc(sizeof(TIME_RNG))) == NULL) {
382f3cd6022Schristos tty_warn(1, "Unable to allocate memory for time range");
383cdec4ac1Sdsl return -1;
3848b35abe2Sjtc }
3858b35abe2Sjtc
3868b35abe2Sjtc /*
3873ac7ce18Swiz * by default we only will check file mtime, but user can specify
3888b35abe2Sjtc * mtime, ctime (inode change time) or both.
3898b35abe2Sjtc */
3908b35abe2Sjtc if ((flgpt == NULL) || (*flgpt == '\0'))
3918b35abe2Sjtc pt->flgs = CMPMTME;
3928b35abe2Sjtc else {
3938b35abe2Sjtc pt->flgs = 0;
3948b35abe2Sjtc while (*flgpt != '\0') {
3958b35abe2Sjtc switch(*flgpt) {
3968b35abe2Sjtc case 'M':
3978b35abe2Sjtc case 'm':
3988b35abe2Sjtc pt->flgs |= CMPMTME;
3998b35abe2Sjtc break;
4008b35abe2Sjtc case 'C':
4018b35abe2Sjtc case 'c':
4028b35abe2Sjtc pt->flgs |= CMPCTME;
4038b35abe2Sjtc break;
4048b35abe2Sjtc default:
405f3cd6022Schristos tty_warn(1, "Bad option %c with time range %s",
4068b35abe2Sjtc *flgpt, str);
4077b9eb38dSchristos free(pt);
4088b35abe2Sjtc goto out;
4098b35abe2Sjtc }
4108b35abe2Sjtc ++flgpt;
4118b35abe2Sjtc }
4128b35abe2Sjtc }
4138b35abe2Sjtc
4148b35abe2Sjtc /*
4158b35abe2Sjtc * start off with the current time
4168b35abe2Sjtc */
417*9f61b804Splunky pt->low_time = pt->high_time = time(NULL);
4188b35abe2Sjtc if (*str != '\0') {
4198b35abe2Sjtc /*
4208b35abe2Sjtc * add lower limit
4218b35abe2Sjtc */
4228b35abe2Sjtc if (str_sec(str, &(pt->low_time)) < 0) {
423f3cd6022Schristos tty_warn(1, "Illegal lower time range %s", str);
4247b9eb38dSchristos free(pt);
4258b35abe2Sjtc goto out;
4268b35abe2Sjtc }
4278b35abe2Sjtc pt->flgs |= HASLOW;
4288b35abe2Sjtc }
4298b35abe2Sjtc
4308b35abe2Sjtc if ((up_pt != NULL) && (*up_pt != '\0')) {
4318b35abe2Sjtc /*
4328b35abe2Sjtc * add upper limit
4338b35abe2Sjtc */
4348b35abe2Sjtc if (str_sec(up_pt, &(pt->high_time)) < 0) {
435f3cd6022Schristos tty_warn(1, "Illegal upper time range %s", up_pt);
4367b9eb38dSchristos free(pt);
4378b35abe2Sjtc goto out;
4388b35abe2Sjtc }
4398b35abe2Sjtc pt->flgs |= HASHIGH;
4408b35abe2Sjtc
4418b35abe2Sjtc /*
4428b35abe2Sjtc * check that the upper and lower do not overlap
4438b35abe2Sjtc */
4448b35abe2Sjtc if (pt->flgs & HASLOW) {
4458b35abe2Sjtc if (pt->low_time > pt->high_time) {
446f3cd6022Schristos tty_warn(1,
447f3cd6022Schristos "Upper %s and lower %s time overlap",
4488b35abe2Sjtc up_pt, str);
4497b9eb38dSchristos free(pt);
450cdec4ac1Sdsl return -1;
4518b35abe2Sjtc }
4528b35abe2Sjtc }
4538b35abe2Sjtc }
4548b35abe2Sjtc
4558b35abe2Sjtc pt->fow = NULL;
4568b35abe2Sjtc if (trhead == NULL) {
4578b35abe2Sjtc trtail = trhead = pt;
458cdec4ac1Sdsl return 0;
4598b35abe2Sjtc }
4608b35abe2Sjtc trtail->fow = pt;
4618b35abe2Sjtc trtail = pt;
462cdec4ac1Sdsl return 0;
4638b35abe2Sjtc
4648b35abe2Sjtc out:
465f3cd6022Schristos tty_warn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
466cdec4ac1Sdsl return -1;
4678b35abe2Sjtc }
4688b35abe2Sjtc
4698b35abe2Sjtc /*
4708b35abe2Sjtc * trng_match()
4718b35abe2Sjtc * check if this files mtime/ctime falls within any supplied time range.
4728b35abe2Sjtc * Return:
4738b35abe2Sjtc * 0 if this archive member should be processed, 1 if it should be skipped
4748b35abe2Sjtc */
4758b35abe2Sjtc
4768b35abe2Sjtc static int
trng_match(ARCHD * arcn)47748250187Stls trng_match(ARCHD *arcn)
4788b35abe2Sjtc {
47948250187Stls TIME_RNG *pt;
4808b35abe2Sjtc
4818b35abe2Sjtc /*
4828b35abe2Sjtc * have to search down the list one at a time looking for a match.
4838b35abe2Sjtc * remember time range limits are inclusive.
4848b35abe2Sjtc */
4858b35abe2Sjtc pt = trhead;
4868b35abe2Sjtc while (pt != NULL) {
4878b35abe2Sjtc switch(pt->flgs & CMPBOTH) {
4888b35abe2Sjtc case CMPBOTH:
4898b35abe2Sjtc /*
4908b35abe2Sjtc * user wants both mtime and ctime checked for this
4918b35abe2Sjtc * time range
4928b35abe2Sjtc */
4938b35abe2Sjtc if (((pt->flgs & HASLOW) &&
4948b35abe2Sjtc (arcn->sb.st_mtime < pt->low_time) &&
4958b35abe2Sjtc (arcn->sb.st_ctime < pt->low_time)) ||
4968b35abe2Sjtc ((pt->flgs & HASHIGH) &&
4978b35abe2Sjtc (arcn->sb.st_mtime > pt->high_time) &&
4988b35abe2Sjtc (arcn->sb.st_ctime > pt->high_time))) {
4998b35abe2Sjtc pt = pt->fow;
5008b35abe2Sjtc continue;
5018b35abe2Sjtc }
5028b35abe2Sjtc break;
5038b35abe2Sjtc case CMPCTME:
5048b35abe2Sjtc /*
5058b35abe2Sjtc * user wants only ctime checked for this time range
5068b35abe2Sjtc */
5078b35abe2Sjtc if (((pt->flgs & HASLOW) &&
5088b35abe2Sjtc (arcn->sb.st_ctime < pt->low_time)) ||
5098b35abe2Sjtc ((pt->flgs & HASHIGH) &&
5108b35abe2Sjtc (arcn->sb.st_ctime > pt->high_time))) {
5118b35abe2Sjtc pt = pt->fow;
5128b35abe2Sjtc continue;
5138b35abe2Sjtc }
5148b35abe2Sjtc break;
5158b35abe2Sjtc case CMPMTME:
5168b35abe2Sjtc default:
5178b35abe2Sjtc /*
5188b35abe2Sjtc * user wants only mtime checked for this time range
5198b35abe2Sjtc */
5208b35abe2Sjtc if (((pt->flgs & HASLOW) &&
5218b35abe2Sjtc (arcn->sb.st_mtime < pt->low_time)) ||
5228b35abe2Sjtc ((pt->flgs & HASHIGH) &&
5238b35abe2Sjtc (arcn->sb.st_mtime > pt->high_time))) {
5248b35abe2Sjtc pt = pt->fow;
5258b35abe2Sjtc continue;
5268b35abe2Sjtc }
5278b35abe2Sjtc break;
5288b35abe2Sjtc }
5298b35abe2Sjtc break;
5308b35abe2Sjtc }
5318b35abe2Sjtc
5328b35abe2Sjtc if (pt == NULL)
533cdec4ac1Sdsl return 1;
534cdec4ac1Sdsl return 0;
5358b35abe2Sjtc }
5368b35abe2Sjtc
5378b35abe2Sjtc /*
5388b35abe2Sjtc * str_sec()
5398b35abe2Sjtc * Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
5408b35abe2Sjtc * seconds. Tval already has current time loaded into it at entry.
5418b35abe2Sjtc * Return:
5428b35abe2Sjtc * 0 if converted ok, -1 otherwise
5438b35abe2Sjtc */
5448b35abe2Sjtc
545f8c64f21Smycroft #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
546f8c64f21Smycroft
5478b35abe2Sjtc static int
str_sec(const char * p,time_t * tval)548dbfa4f81Smycroft str_sec(const char *p, time_t *tval)
5498b35abe2Sjtc {
55048250187Stls struct tm *lt;
551dbfa4f81Smycroft const char *dot, *t;
552dbfa4f81Smycroft int yearset, len;
553dbfa4f81Smycroft
554dbfa4f81Smycroft for (t = p, dot = NULL; *t; ++t) {
5553c85b6c0Schristos if (isdigit((unsigned char)*t))
556dbfa4f81Smycroft continue;
557dbfa4f81Smycroft if (*t == '.' && dot == NULL) {
558dbfa4f81Smycroft dot = t;
559dbfa4f81Smycroft continue;
560dbfa4f81Smycroft }
561cdec4ac1Sdsl return -1;
562dbfa4f81Smycroft }
5638b35abe2Sjtc
5648b35abe2Sjtc lt = localtime(tval);
565dbfa4f81Smycroft
566dbfa4f81Smycroft if (dot != NULL) {
567dbfa4f81Smycroft len = strlen(dot);
568dbfa4f81Smycroft if (len != 3)
569cdec4ac1Sdsl return -1;
570dbfa4f81Smycroft ++dot;
571f8c64f21Smycroft lt->tm_sec = ATOI2(dot);
572dbfa4f81Smycroft } else {
573dbfa4f81Smycroft len = 0;
5748b35abe2Sjtc lt->tm_sec = 0;
575dbfa4f81Smycroft }
5768b35abe2Sjtc
577f8c64f21Smycroft yearset = 0;
578dbfa4f81Smycroft switch (strlen(p) - len) {
579f8c64f21Smycroft case 12:
580dbfa4f81Smycroft lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
581f8c64f21Smycroft yearset = 1;
582f8c64f21Smycroft /* FALLTHROUGH */
5838b35abe2Sjtc case 10:
584f8c64f21Smycroft if (yearset) {
585dbfa4f81Smycroft lt->tm_year += ATOI2(p);
586f8c64f21Smycroft } else {
587dbfa4f81Smycroft yearset = ATOI2(p);
588f8c64f21Smycroft if (yearset < 69)
589f8c64f21Smycroft lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
590f8c64f21Smycroft else
591f8c64f21Smycroft lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
592f8c64f21Smycroft }
5938b35abe2Sjtc /* FALLTHROUGH */
5948b35abe2Sjtc case 8:
595dbfa4f81Smycroft lt->tm_mon = ATOI2(p);
5968b35abe2Sjtc --lt->tm_mon;
5978b35abe2Sjtc /* FALLTHROUGH */
5988b35abe2Sjtc case 6:
599dbfa4f81Smycroft lt->tm_mday = ATOI2(p);
6008b35abe2Sjtc /* FALLTHROUGH */
6018b35abe2Sjtc case 4:
602dbfa4f81Smycroft lt->tm_hour = ATOI2(p);
6038b35abe2Sjtc /* FALLTHROUGH */
6048b35abe2Sjtc case 2:
605dbfa4f81Smycroft lt->tm_min = ATOI2(p);
6068b35abe2Sjtc break;
6078b35abe2Sjtc default:
608cdec4ac1Sdsl return -1;
6098b35abe2Sjtc }
610f8c64f21Smycroft
6118b35abe2Sjtc /*
6128b35abe2Sjtc * convert broken-down time to GMT clock time seconds
6138b35abe2Sjtc */
6148b35abe2Sjtc if ((*tval = mktime(lt)) == -1)
615cdec4ac1Sdsl return -1;
616cdec4ac1Sdsl return 0;
6178b35abe2Sjtc }
618