14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1992 Keith Muller.
54b88c807SRodney W. Grimes * Copyright (c) 1992, 1993
64b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
74b88c807SRodney W. Grimes *
84b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by
94b88c807SRodney W. Grimes * Keith Muller of the University of California, San Diego.
104b88c807SRodney W. Grimes *
114b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes * are met:
144b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes * without specific prior written permission.
224b88c807SRodney W. Grimes *
234b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes * SUCH DAMAGE.
344b88c807SRodney W. Grimes */
354b88c807SRodney W. Grimes
364b88c807SRodney W. Grimes #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/time.h>
384b88c807SRodney W. Grimes #include <sys/stat.h>
39*d05e43bcSDag-Erling Smørgrav
40*d05e43bcSDag-Erling Smørgrav #include <ctype.h>
414b88c807SRodney W. Grimes #include <grp.h>
42*d05e43bcSDag-Erling Smørgrav #include <pwd.h>
434b88c807SRodney W. Grimes #include <stdio.h>
44*d05e43bcSDag-Erling Smørgrav #include <stdlib.h>
454b88c807SRodney W. Grimes #include <string.h>
464b88c807SRodney W. Grimes #include <strings.h>
47*d05e43bcSDag-Erling Smørgrav
484b88c807SRodney W. Grimes #include "pax.h"
494b88c807SRodney W. Grimes #include "sel_subs.h"
504b88c807SRodney W. Grimes #include "extern.h"
514b88c807SRodney W. Grimes
52*d05e43bcSDag-Erling Smørgrav static int str_sec(const char *, time_t *);
53f789b261SWarner Losh static int usr_match(ARCHD *);
54f789b261SWarner Losh static int grp_match(ARCHD *);
55f789b261SWarner Losh static int trng_match(ARCHD *);
564b88c807SRodney W. Grimes
574b88c807SRodney W. Grimes static TIME_RNG *trhead = NULL; /* time range list head */
584b88c807SRodney W. Grimes static TIME_RNG *trtail = NULL; /* time range list tail */
594b88c807SRodney W. Grimes static USRT **usrtb = NULL; /* user selection table */
604b88c807SRodney W. Grimes static GRPT **grptb = NULL; /* group selection table */
614b88c807SRodney W. Grimes
624b88c807SRodney W. Grimes /*
634b88c807SRodney W. Grimes * Routines for selection of archive members
644b88c807SRodney W. Grimes */
654b88c807SRodney W. Grimes
664b88c807SRodney W. Grimes /*
674b88c807SRodney W. Grimes * sel_chk()
6846be34b9SKris Kennaway * check if this file matches a specified uid, gid or time range
694b88c807SRodney W. Grimes * Return:
704b88c807SRodney W. Grimes * 0 if this archive member should be processed, 1 if it should be skipped
714b88c807SRodney W. Grimes */
724b88c807SRodney W. Grimes
734b88c807SRodney W. Grimes int
sel_chk(ARCHD * arcn)74f789b261SWarner Losh sel_chk(ARCHD *arcn)
754b88c807SRodney W. Grimes {
764b88c807SRodney W. Grimes if (((usrtb != NULL) && usr_match(arcn)) ||
774b88c807SRodney W. Grimes ((grptb != NULL) && grp_match(arcn)) ||
784b88c807SRodney W. Grimes ((trhead != NULL) && trng_match(arcn)))
794b88c807SRodney W. Grimes return(1);
804b88c807SRodney W. Grimes return(0);
814b88c807SRodney W. Grimes }
824b88c807SRodney W. Grimes
834b88c807SRodney W. Grimes /*
844b88c807SRodney W. Grimes * User/group selection routines
854b88c807SRodney W. Grimes *
864b88c807SRodney W. Grimes * Routines to handle user selection of files based on the file uid/gid. To
870266a5d6SDag-Erling Smørgrav * add an entry, the user supplies either the name or the uid/gid starting with
8846be34b9SKris Kennaway * a # on the command line. A \# will escape the #.
894b88c807SRodney W. Grimes */
904b88c807SRodney W. Grimes
914b88c807SRodney W. Grimes /*
924b88c807SRodney W. Grimes * usr_add()
934b88c807SRodney W. Grimes * add a user match to the user match hash table
944b88c807SRodney W. Grimes * Return:
954b88c807SRodney W. Grimes * 0 if added ok, -1 otherwise;
964b88c807SRodney W. Grimes */
974b88c807SRodney W. Grimes
984b88c807SRodney W. Grimes int
usr_add(char * str)99f789b261SWarner Losh usr_add(char *str)
1004b88c807SRodney W. Grimes {
101f789b261SWarner Losh u_int indx;
102f789b261SWarner Losh USRT *pt;
103f789b261SWarner Losh struct passwd *pw;
104f789b261SWarner Losh uid_t uid;
1054b88c807SRodney W. Grimes
1064b88c807SRodney W. Grimes /*
1074b88c807SRodney W. Grimes * create the table if it doesn't exist
1084b88c807SRodney W. Grimes */
1094b88c807SRodney W. Grimes if ((str == NULL) || (*str == '\0'))
1104b88c807SRodney W. Grimes return(-1);
1114b88c807SRodney W. Grimes if ((usrtb == NULL) &&
1124b88c807SRodney W. Grimes ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
113778766feSKris Kennaway paxwarn(1, "Unable to allocate memory for user selection table");
1144b88c807SRodney W. Grimes return(-1);
1154b88c807SRodney W. Grimes }
1164b88c807SRodney W. Grimes
1174b88c807SRodney W. Grimes /*
1184b88c807SRodney W. Grimes * figure out user spec
1194b88c807SRodney W. Grimes */
1204b88c807SRodney W. Grimes if (str[0] != '#') {
1214b88c807SRodney W. Grimes /*
1224b88c807SRodney W. Grimes * it is a user name, \# escapes # as first char in user name
1234b88c807SRodney W. Grimes */
1244b88c807SRodney W. Grimes if ((str[0] == '\\') && (str[1] == '#'))
1254b88c807SRodney W. Grimes ++str;
1264b88c807SRodney W. Grimes if ((pw = getpwnam(str)) == NULL) {
127778766feSKris Kennaway paxwarn(1, "Unable to find uid for user: %s", str);
1284b88c807SRodney W. Grimes return(-1);
1294b88c807SRodney W. Grimes }
1304b88c807SRodney W. Grimes uid = (uid_t)pw->pw_uid;
1314b88c807SRodney W. Grimes } else
132778766feSKris Kennaway uid = (uid_t)strtoul(str+1, NULL, 10);
1334b88c807SRodney W. Grimes endpwent();
1344b88c807SRodney W. Grimes
1354b88c807SRodney W. Grimes /*
1364b88c807SRodney W. Grimes * hash it and go down the hash chain (if any) looking for it
1374b88c807SRodney W. Grimes */
1384b88c807SRodney W. Grimes indx = ((unsigned)uid) % USR_TB_SZ;
1394b88c807SRodney W. Grimes if ((pt = usrtb[indx]) != NULL) {
1404b88c807SRodney W. Grimes while (pt != NULL) {
1414b88c807SRodney W. Grimes if (pt->uid == uid)
1424b88c807SRodney W. Grimes return(0);
1434b88c807SRodney W. Grimes pt = pt->fow;
1444b88c807SRodney W. Grimes }
1454b88c807SRodney W. Grimes }
1464b88c807SRodney W. Grimes
1474b88c807SRodney W. Grimes /*
1484b88c807SRodney W. Grimes * uid is not yet in the table, add it to the front of the chain
1494b88c807SRodney W. Grimes */
1504b88c807SRodney W. Grimes if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
1514b88c807SRodney W. Grimes pt->uid = uid;
1524b88c807SRodney W. Grimes pt->fow = usrtb[indx];
1534b88c807SRodney W. Grimes usrtb[indx] = pt;
1544b88c807SRodney W. Grimes return(0);
1554b88c807SRodney W. Grimes }
156778766feSKris Kennaway paxwarn(1, "User selection table out of memory");
1574b88c807SRodney W. Grimes return(-1);
1584b88c807SRodney W. Grimes }
1594b88c807SRodney W. Grimes
1604b88c807SRodney W. Grimes /*
1614b88c807SRodney W. Grimes * usr_match()
1624b88c807SRodney W. Grimes * check if this files uid matches a selected uid.
1634b88c807SRodney W. Grimes * Return:
1644b88c807SRodney W. Grimes * 0 if this archive member should be processed, 1 if it should be skipped
1654b88c807SRodney W. Grimes */
1664b88c807SRodney W. Grimes
1674b88c807SRodney W. Grimes static int
usr_match(ARCHD * arcn)168f789b261SWarner Losh usr_match(ARCHD *arcn)
1694b88c807SRodney W. Grimes {
170f789b261SWarner Losh USRT *pt;
1714b88c807SRodney W. Grimes
1724b88c807SRodney W. Grimes /*
1734b88c807SRodney W. Grimes * hash and look for it in the table
1744b88c807SRodney W. Grimes */
1754b88c807SRodney W. Grimes pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
1764b88c807SRodney W. Grimes while (pt != NULL) {
1774b88c807SRodney W. Grimes if (pt->uid == arcn->sb.st_uid)
1784b88c807SRodney W. Grimes return(0);
1794b88c807SRodney W. Grimes pt = pt->fow;
1804b88c807SRodney W. Grimes }
1814b88c807SRodney W. Grimes
1824b88c807SRodney W. Grimes /*
1834b88c807SRodney W. Grimes * not found
1844b88c807SRodney W. Grimes */
1854b88c807SRodney W. Grimes return(1);
1864b88c807SRodney W. Grimes }
1874b88c807SRodney W. Grimes
1884b88c807SRodney W. Grimes /*
1894b88c807SRodney W. Grimes * grp_add()
1904b88c807SRodney W. Grimes * add a group match to the group match hash table
1914b88c807SRodney W. Grimes * Return:
1924b88c807SRodney W. Grimes * 0 if added ok, -1 otherwise;
1934b88c807SRodney W. Grimes */
1944b88c807SRodney W. Grimes
1954b88c807SRodney W. Grimes int
grp_add(char * str)196f789b261SWarner Losh grp_add(char *str)
1974b88c807SRodney W. Grimes {
198f789b261SWarner Losh u_int indx;
199f789b261SWarner Losh GRPT *pt;
200f789b261SWarner Losh struct group *gr;
201f789b261SWarner Losh gid_t gid;
2024b88c807SRodney W. Grimes
2034b88c807SRodney W. Grimes /*
2044b88c807SRodney W. Grimes * create the table if it doesn't exist
2054b88c807SRodney W. Grimes */
2064b88c807SRodney W. Grimes if ((str == NULL) || (*str == '\0'))
2074b88c807SRodney W. Grimes return(-1);
2084b88c807SRodney W. Grimes if ((grptb == NULL) &&
2094b88c807SRodney W. Grimes ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
210778766feSKris Kennaway paxwarn(1, "Unable to allocate memory fo group selection table");
2114b88c807SRodney W. Grimes return(-1);
2124b88c807SRodney W. Grimes }
2134b88c807SRodney W. Grimes
2144b88c807SRodney W. Grimes /*
2154b88c807SRodney W. Grimes * figure out user spec
2164b88c807SRodney W. Grimes */
2174b88c807SRodney W. Grimes if (str[0] != '#') {
2184b88c807SRodney W. Grimes /*
2194b88c807SRodney W. Grimes * it is a group name, \# escapes # as first char in group name
2204b88c807SRodney W. Grimes */
2214b88c807SRodney W. Grimes if ((str[0] == '\\') && (str[1] == '#'))
2224b88c807SRodney W. Grimes ++str;
2234b88c807SRodney W. Grimes if ((gr = getgrnam(str)) == NULL) {
224778766feSKris Kennaway paxwarn(1,"Cannot determine gid for group name: %s", str);
2254b88c807SRodney W. Grimes return(-1);
2264b88c807SRodney W. Grimes }
227d080dfa5SMark Murray gid = gr->gr_gid;
2284b88c807SRodney W. Grimes } else
229778766feSKris Kennaway gid = (gid_t)strtoul(str+1, NULL, 10);
2304b88c807SRodney W. Grimes endgrent();
2314b88c807SRodney W. Grimes
2324b88c807SRodney W. Grimes /*
2334b88c807SRodney W. Grimes * hash it and go down the hash chain (if any) looking for it
2344b88c807SRodney W. Grimes */
2354b88c807SRodney W. Grimes indx = ((unsigned)gid) % GRP_TB_SZ;
2364b88c807SRodney W. Grimes if ((pt = grptb[indx]) != NULL) {
2374b88c807SRodney W. Grimes while (pt != NULL) {
2384b88c807SRodney W. Grimes if (pt->gid == gid)
2394b88c807SRodney W. Grimes return(0);
2404b88c807SRodney W. Grimes pt = pt->fow;
2414b88c807SRodney W. Grimes }
2424b88c807SRodney W. Grimes }
2434b88c807SRodney W. Grimes
2444b88c807SRodney W. Grimes /*
2454b88c807SRodney W. Grimes * gid not in the table, add it to the front of the chain
2464b88c807SRodney W. Grimes */
2474b88c807SRodney W. Grimes if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
2484b88c807SRodney W. Grimes pt->gid = gid;
2494b88c807SRodney W. Grimes pt->fow = grptb[indx];
2504b88c807SRodney W. Grimes grptb[indx] = pt;
2514b88c807SRodney W. Grimes return(0);
2524b88c807SRodney W. Grimes }
253778766feSKris Kennaway paxwarn(1, "Group selection table out of memory");
2544b88c807SRodney W. Grimes return(-1);
2554b88c807SRodney W. Grimes }
2564b88c807SRodney W. Grimes
2574b88c807SRodney W. Grimes /*
2584b88c807SRodney W. Grimes * grp_match()
2594b88c807SRodney W. Grimes * check if this files gid matches a selected gid.
2604b88c807SRodney W. Grimes * Return:
2614b88c807SRodney W. Grimes * 0 if this archive member should be processed, 1 if it should be skipped
2624b88c807SRodney W. Grimes */
2634b88c807SRodney W. Grimes
2644b88c807SRodney W. Grimes static int
grp_match(ARCHD * arcn)265f789b261SWarner Losh grp_match(ARCHD *arcn)
2664b88c807SRodney W. Grimes {
267f789b261SWarner Losh GRPT *pt;
2684b88c807SRodney W. Grimes
2694b88c807SRodney W. Grimes /*
2704b88c807SRodney W. Grimes * hash and look for it in the table
2714b88c807SRodney W. Grimes */
2724b88c807SRodney W. Grimes pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
2734b88c807SRodney W. Grimes while (pt != NULL) {
2744b88c807SRodney W. Grimes if (pt->gid == arcn->sb.st_gid)
2754b88c807SRodney W. Grimes return(0);
2764b88c807SRodney W. Grimes pt = pt->fow;
2774b88c807SRodney W. Grimes }
2784b88c807SRodney W. Grimes
2794b88c807SRodney W. Grimes /*
2804b88c807SRodney W. Grimes * not found
2814b88c807SRodney W. Grimes */
2824b88c807SRodney W. Grimes return(1);
2834b88c807SRodney W. Grimes }
2844b88c807SRodney W. Grimes
2854b88c807SRodney W. Grimes /*
2864b88c807SRodney W. Grimes * Time range selection routines
2874b88c807SRodney W. Grimes *
2884b88c807SRodney W. Grimes * Routines to handle user selection of files based on the modification and/or
2894b88c807SRodney W. Grimes * inode change time falling within a specified time range (the non-standard
2904b88c807SRodney W. Grimes * -T flag). The user may specify any number of different file time ranges.
2914b88c807SRodney W. Grimes * Time ranges are checked one at a time until a match is found (if at all).
2924b88c807SRodney W. Grimes * If the file has a mtime (and/or ctime) which lies within one of the time
2939d5abbddSJens Schweikhardt * ranges, the file is selected. Time ranges may have a lower and/or an upper
2944b88c807SRodney W. Grimes * value. These ranges are inclusive. When no time ranges are supplied to pax
2954b88c807SRodney W. Grimes * with the -T option, all members in the archive will be selected by the time
2964b88c807SRodney W. Grimes * range routines. When only a lower range is supplied, only files with a
2979d5abbddSJens Schweikhardt * mtime (and/or ctime) equal to or younger are selected. When only an upper
2984b88c807SRodney W. Grimes * range is supplied, only files with a mtime (and/or ctime) equal to or older
2994b88c807SRodney W. Grimes * are selected. When the lower time range is equal to the upper time range,
3004b88c807SRodney W. Grimes * only files with a mtime (or ctime) of exactly that time are selected.
3014b88c807SRodney W. Grimes */
3024b88c807SRodney W. Grimes
3034b88c807SRodney W. Grimes /*
3044b88c807SRodney W. Grimes * trng_add()
3054b88c807SRodney W. Grimes * add a time range match to the time range list.
3064b88c807SRodney W. Grimes * This is a non-standard pax option. Lower and upper ranges are in the
307*d05e43bcSDag-Erling Smørgrav * format: [[[[[cc]yy]mm]dd]HH]MM[.SS] and are comma separated.
3084b88c807SRodney W. Grimes * Time ranges are based on current time, so 1234 would specify a time of
3094b88c807SRodney W. Grimes * 12:34 today.
3104b88c807SRodney W. Grimes * Return:
3114b88c807SRodney W. Grimes * 0 if the time range was added to the list, -1 otherwise
3124b88c807SRodney W. Grimes */
3134b88c807SRodney W. Grimes
3144b88c807SRodney W. Grimes int
trng_add(char * str)315f789b261SWarner Losh trng_add(char *str)
3164b88c807SRodney W. Grimes {
317f789b261SWarner Losh TIME_RNG *pt;
318f789b261SWarner Losh char *up_pt = NULL;
319f789b261SWarner Losh char *stpt;
320f789b261SWarner Losh char *flgpt;
321f789b261SWarner Losh int dot = 0;
3224b88c807SRodney W. Grimes
3234b88c807SRodney W. Grimes /*
3244b88c807SRodney W. Grimes * throw out the badly formed time ranges
3254b88c807SRodney W. Grimes */
3264b88c807SRodney W. Grimes if ((str == NULL) || (*str == '\0')) {
327778766feSKris Kennaway paxwarn(1, "Empty time range string");
3284b88c807SRodney W. Grimes return(-1);
3294b88c807SRodney W. Grimes }
3304b88c807SRodney W. Grimes
3314b88c807SRodney W. Grimes /*
3324b88c807SRodney W. Grimes * locate optional flags suffix /{cm}.
3334b88c807SRodney W. Grimes */
334778766feSKris Kennaway if ((flgpt = strrchr(str, '/')) != NULL)
3354b88c807SRodney W. Grimes *flgpt++ = '\0';
3364b88c807SRodney W. Grimes
3374b88c807SRodney W. Grimes for (stpt = str; *stpt != '\0'; ++stpt) {
3384b88c807SRodney W. Grimes if ((*stpt >= '0') && (*stpt <= '9'))
3394b88c807SRodney W. Grimes continue;
3404b88c807SRodney W. Grimes if ((*stpt == ',') && (up_pt == NULL)) {
3414b88c807SRodney W. Grimes *stpt = '\0';
3424b88c807SRodney W. Grimes up_pt = stpt + 1;
3434b88c807SRodney W. Grimes dot = 0;
3444b88c807SRodney W. Grimes continue;
3454b88c807SRodney W. Grimes }
3464b88c807SRodney W. Grimes
3474b88c807SRodney W. Grimes /*
3484b88c807SRodney W. Grimes * allow only one dot per range (secs)
3494b88c807SRodney W. Grimes */
3504b88c807SRodney W. Grimes if ((*stpt == '.') && (!dot)) {
3514b88c807SRodney W. Grimes ++dot;
3524b88c807SRodney W. Grimes continue;
3534b88c807SRodney W. Grimes }
354778766feSKris Kennaway paxwarn(1, "Improperly specified time range: %s", str);
3554b88c807SRodney W. Grimes goto out;
3564b88c807SRodney W. Grimes }
3574b88c807SRodney W. Grimes
3584b88c807SRodney W. Grimes /*
3594b88c807SRodney W. Grimes * allocate space for the time range and store the limits
3604b88c807SRodney W. Grimes */
3614b88c807SRodney W. Grimes if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
362778766feSKris Kennaway paxwarn(1, "Unable to allocate memory for time range");
3634b88c807SRodney W. Grimes return(-1);
3644b88c807SRodney W. Grimes }
3654b88c807SRodney W. Grimes
3664b88c807SRodney W. Grimes /*
36701c99176SUlrich Spörlein * by default we only will check file mtime, but the user can specify
3684b88c807SRodney W. Grimes * mtime, ctime (inode change time) or both.
3694b88c807SRodney W. Grimes */
3704b88c807SRodney W. Grimes if ((flgpt == NULL) || (*flgpt == '\0'))
3714b88c807SRodney W. Grimes pt->flgs = CMPMTME;
3724b88c807SRodney W. Grimes else {
3734b88c807SRodney W. Grimes pt->flgs = 0;
3744b88c807SRodney W. Grimes while (*flgpt != '\0') {
3754b88c807SRodney W. Grimes switch(*flgpt) {
3764b88c807SRodney W. Grimes case 'M':
3774b88c807SRodney W. Grimes case 'm':
3784b88c807SRodney W. Grimes pt->flgs |= CMPMTME;
3794b88c807SRodney W. Grimes break;
3804b88c807SRodney W. Grimes case 'C':
3814b88c807SRodney W. Grimes case 'c':
3824b88c807SRodney W. Grimes pt->flgs |= CMPCTME;
3834b88c807SRodney W. Grimes break;
3844b88c807SRodney W. Grimes default:
385778766feSKris Kennaway paxwarn(1, "Bad option %c with time range %s",
3864b88c807SRodney W. Grimes *flgpt, str);
387f3ef893aSXin LI free(pt);
3884b88c807SRodney W. Grimes goto out;
3894b88c807SRodney W. Grimes }
3904b88c807SRodney W. Grimes ++flgpt;
3914b88c807SRodney W. Grimes }
3924b88c807SRodney W. Grimes }
3934b88c807SRodney W. Grimes
3944b88c807SRodney W. Grimes /*
3954b88c807SRodney W. Grimes * start off with the current time
3964b88c807SRodney W. Grimes */
397778766feSKris Kennaway pt->low_time = pt->high_time = time(NULL);
3984b88c807SRodney W. Grimes if (*str != '\0') {
3994b88c807SRodney W. Grimes /*
4004b88c807SRodney W. Grimes * add lower limit
4014b88c807SRodney W. Grimes */
4024b88c807SRodney W. Grimes if (str_sec(str, &(pt->low_time)) < 0) {
403778766feSKris Kennaway paxwarn(1, "Illegal lower time range %s", str);
404367787a3SBrian Somers free(pt);
4054b88c807SRodney W. Grimes goto out;
4064b88c807SRodney W. Grimes }
4074b88c807SRodney W. Grimes pt->flgs |= HASLOW;
4084b88c807SRodney W. Grimes }
4094b88c807SRodney W. Grimes
4104b88c807SRodney W. Grimes if ((up_pt != NULL) && (*up_pt != '\0')) {
4114b88c807SRodney W. Grimes /*
4124b88c807SRodney W. Grimes * add upper limit
4134b88c807SRodney W. Grimes */
4144b88c807SRodney W. Grimes if (str_sec(up_pt, &(pt->high_time)) < 0) {
415778766feSKris Kennaway paxwarn(1, "Illegal upper time range %s", up_pt);
416367787a3SBrian Somers free(pt);
4174b88c807SRodney W. Grimes goto out;
4184b88c807SRodney W. Grimes }
4194b88c807SRodney W. Grimes pt->flgs |= HASHIGH;
4204b88c807SRodney W. Grimes
4214b88c807SRodney W. Grimes /*
4224b88c807SRodney W. Grimes * check that the upper and lower do not overlap
4234b88c807SRodney W. Grimes */
4244b88c807SRodney W. Grimes if (pt->flgs & HASLOW) {
4254b88c807SRodney W. Grimes if (pt->low_time > pt->high_time) {
426778766feSKris Kennaway paxwarn(1, "Upper %s and lower %s time overlap",
4274b88c807SRodney W. Grimes up_pt, str);
428367787a3SBrian Somers free(pt);
4294b88c807SRodney W. Grimes return(-1);
4304b88c807SRodney W. Grimes }
4314b88c807SRodney W. Grimes }
4324b88c807SRodney W. Grimes }
4334b88c807SRodney W. Grimes
4344b88c807SRodney W. Grimes pt->fow = NULL;
4354b88c807SRodney W. Grimes if (trhead == NULL) {
4364b88c807SRodney W. Grimes trtail = trhead = pt;
4374b88c807SRodney W. Grimes return(0);
4384b88c807SRodney W. Grimes }
4394b88c807SRodney W. Grimes trtail->fow = pt;
4404b88c807SRodney W. Grimes trtail = pt;
4414b88c807SRodney W. Grimes return(0);
4424b88c807SRodney W. Grimes
4434b88c807SRodney W. Grimes out:
444*d05e43bcSDag-Erling Smørgrav paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]");
4454b88c807SRodney W. Grimes return(-1);
4464b88c807SRodney W. Grimes }
4474b88c807SRodney W. Grimes
4484b88c807SRodney W. Grimes /*
4494b88c807SRodney W. Grimes * trng_match()
4504b88c807SRodney W. Grimes * check if this files mtime/ctime falls within any supplied time range.
4514b88c807SRodney W. Grimes * Return:
4524b88c807SRodney W. Grimes * 0 if this archive member should be processed, 1 if it should be skipped
4534b88c807SRodney W. Grimes */
4544b88c807SRodney W. Grimes
4554b88c807SRodney W. Grimes static int
trng_match(ARCHD * arcn)456f789b261SWarner Losh trng_match(ARCHD *arcn)
4574b88c807SRodney W. Grimes {
458f789b261SWarner Losh TIME_RNG *pt;
4594b88c807SRodney W. Grimes
4604b88c807SRodney W. Grimes /*
4614b88c807SRodney W. Grimes * have to search down the list one at a time looking for a match.
4624b88c807SRodney W. Grimes * remember time range limits are inclusive.
4634b88c807SRodney W. Grimes */
4644b88c807SRodney W. Grimes pt = trhead;
4654b88c807SRodney W. Grimes while (pt != NULL) {
4664b88c807SRodney W. Grimes switch(pt->flgs & CMPBOTH) {
4674b88c807SRodney W. Grimes case CMPBOTH:
4684b88c807SRodney W. Grimes /*
4694b88c807SRodney W. Grimes * user wants both mtime and ctime checked for this
4704b88c807SRodney W. Grimes * time range
4714b88c807SRodney W. Grimes */
4724b88c807SRodney W. Grimes if (((pt->flgs & HASLOW) &&
4734b88c807SRodney W. Grimes (arcn->sb.st_mtime < pt->low_time) &&
4744b88c807SRodney W. Grimes (arcn->sb.st_ctime < pt->low_time)) ||
4754b88c807SRodney W. Grimes ((pt->flgs & HASHIGH) &&
4764b88c807SRodney W. Grimes (arcn->sb.st_mtime > pt->high_time) &&
4774b88c807SRodney W. Grimes (arcn->sb.st_ctime > pt->high_time))) {
4784b88c807SRodney W. Grimes pt = pt->fow;
4794b88c807SRodney W. Grimes continue;
4804b88c807SRodney W. Grimes }
4814b88c807SRodney W. Grimes break;
4824b88c807SRodney W. Grimes case CMPCTME:
4834b88c807SRodney W. Grimes /*
4844b88c807SRodney W. Grimes * user wants only ctime checked for this time range
4854b88c807SRodney W. Grimes */
4864b88c807SRodney W. Grimes if (((pt->flgs & HASLOW) &&
4874b88c807SRodney W. Grimes (arcn->sb.st_ctime < pt->low_time)) ||
4884b88c807SRodney W. Grimes ((pt->flgs & HASHIGH) &&
4894b88c807SRodney W. Grimes (arcn->sb.st_ctime > pt->high_time))) {
4904b88c807SRodney W. Grimes pt = pt->fow;
4914b88c807SRodney W. Grimes continue;
4924b88c807SRodney W. Grimes }
4934b88c807SRodney W. Grimes break;
4944b88c807SRodney W. Grimes case CMPMTME:
4954b88c807SRodney W. Grimes default:
4964b88c807SRodney W. Grimes /*
4974b88c807SRodney W. Grimes * user wants only mtime checked for this time range
4984b88c807SRodney W. Grimes */
4994b88c807SRodney W. Grimes if (((pt->flgs & HASLOW) &&
5004b88c807SRodney W. Grimes (arcn->sb.st_mtime < pt->low_time)) ||
5014b88c807SRodney W. Grimes ((pt->flgs & HASHIGH) &&
5024b88c807SRodney W. Grimes (arcn->sb.st_mtime > pt->high_time))) {
5034b88c807SRodney W. Grimes pt = pt->fow;
5044b88c807SRodney W. Grimes continue;
5054b88c807SRodney W. Grimes }
5064b88c807SRodney W. Grimes break;
5074b88c807SRodney W. Grimes }
5084b88c807SRodney W. Grimes break;
5094b88c807SRodney W. Grimes }
5104b88c807SRodney W. Grimes
5114b88c807SRodney W. Grimes if (pt == NULL)
5124b88c807SRodney W. Grimes return(1);
5134b88c807SRodney W. Grimes return(0);
5144b88c807SRodney W. Grimes }
5154b88c807SRodney W. Grimes
5164b88c807SRodney W. Grimes /*
5174b88c807SRodney W. Grimes * str_sec()
518*d05e43bcSDag-Erling Smørgrav * Convert a time string in the format of [[[[[cc]yy]mm]dd]HH]MM[.SS] to
519*d05e43bcSDag-Erling Smørgrav * seconds UTC. Tval already has current time loaded into it at entry.
5204b88c807SRodney W. Grimes * Return:
5214b88c807SRodney W. Grimes * 0 if converted ok, -1 otherwise
5224b88c807SRodney W. Grimes */
5234b88c807SRodney W. Grimes
5244b88c807SRodney W. Grimes static int
str_sec(const char * p,time_t * tval)525*d05e43bcSDag-Erling Smørgrav str_sec(const char *p, time_t *tval)
5264b88c807SRodney W. Grimes {
527f789b261SWarner Losh struct tm *lt;
528*d05e43bcSDag-Erling Smørgrav const char *dot, *t;
529*d05e43bcSDag-Erling Smørgrav size_t len;
530*d05e43bcSDag-Erling Smørgrav int bigyear;
531*d05e43bcSDag-Erling Smørgrav int yearset;
532*d05e43bcSDag-Erling Smørgrav
533*d05e43bcSDag-Erling Smørgrav yearset = 0;
534*d05e43bcSDag-Erling Smørgrav len = strlen(p);
535*d05e43bcSDag-Erling Smørgrav
536*d05e43bcSDag-Erling Smørgrav for (t = p, dot = NULL; *t; ++t) {
537*d05e43bcSDag-Erling Smørgrav if (isdigit((unsigned char)*t))
538*d05e43bcSDag-Erling Smørgrav continue;
539*d05e43bcSDag-Erling Smørgrav if (*t == '.' && dot == NULL) {
540*d05e43bcSDag-Erling Smørgrav dot = t;
541*d05e43bcSDag-Erling Smørgrav continue;
542*d05e43bcSDag-Erling Smørgrav }
543*d05e43bcSDag-Erling Smørgrav return(-1);
544*d05e43bcSDag-Erling Smørgrav }
5454b88c807SRodney W. Grimes
5464b88c807SRodney W. Grimes lt = localtime(tval);
547*d05e43bcSDag-Erling Smørgrav
548*d05e43bcSDag-Erling Smørgrav if (dot != NULL) { /* .SS */
549*d05e43bcSDag-Erling Smørgrav if (strlen(++dot) != 2)
5504b88c807SRodney W. Grimes return(-1);
551*d05e43bcSDag-Erling Smørgrav lt->tm_sec = ATOI2(dot);
552*d05e43bcSDag-Erling Smørgrav if (lt->tm_sec > 61)
5534b88c807SRodney W. Grimes return(-1);
554*d05e43bcSDag-Erling Smørgrav len -= 3;
5554b88c807SRodney W. Grimes } else
5564b88c807SRodney W. Grimes lt->tm_sec = 0;
5574b88c807SRodney W. Grimes
558*d05e43bcSDag-Erling Smørgrav switch (len) {
559*d05e43bcSDag-Erling Smørgrav case 12: /* cc */
560*d05e43bcSDag-Erling Smørgrav bigyear = ATOI2(p);
561*d05e43bcSDag-Erling Smørgrav lt->tm_year = (bigyear * 100) - 1900;
562*d05e43bcSDag-Erling Smørgrav yearset = 1;
5634b88c807SRodney W. Grimes /* FALLTHROUGH */
564*d05e43bcSDag-Erling Smørgrav case 10: /* yy */
565*d05e43bcSDag-Erling Smørgrav if (yearset) {
566*d05e43bcSDag-Erling Smørgrav lt->tm_year += ATOI2(p);
567*d05e43bcSDag-Erling Smørgrav } else {
568*d05e43bcSDag-Erling Smørgrav lt->tm_year = ATOI2(p);
569*d05e43bcSDag-Erling Smørgrav if (lt->tm_year < 69) /* hack for 2000 ;-} */
570*d05e43bcSDag-Erling Smørgrav lt->tm_year += (2000 - 1900);
571*d05e43bcSDag-Erling Smørgrav }
572*d05e43bcSDag-Erling Smørgrav /* FALLTHROUGH */
573*d05e43bcSDag-Erling Smørgrav case 8: /* mm */
574*d05e43bcSDag-Erling Smørgrav lt->tm_mon = ATOI2(p);
575*d05e43bcSDag-Erling Smørgrav if ((lt->tm_mon > 12) || !lt->tm_mon)
5764b88c807SRodney W. Grimes return(-1);
577*d05e43bcSDag-Erling Smørgrav --lt->tm_mon; /* time struct is 0 - 11 */
5784b88c807SRodney W. Grimes /* FALLTHROUGH */
579*d05e43bcSDag-Erling Smørgrav case 6: /* dd */
580*d05e43bcSDag-Erling Smørgrav lt->tm_mday = ATOI2(p);
581*d05e43bcSDag-Erling Smørgrav if ((lt->tm_mday > 31) || !lt->tm_mday)
5824b88c807SRodney W. Grimes return(-1);
5834b88c807SRodney W. Grimes /* FALLTHROUGH */
584*d05e43bcSDag-Erling Smørgrav case 4: /* HH */
585*d05e43bcSDag-Erling Smørgrav lt->tm_hour = ATOI2(p);
586*d05e43bcSDag-Erling Smørgrav if (lt->tm_hour > 23)
5874b88c807SRodney W. Grimes return(-1);
5884b88c807SRodney W. Grimes /* FALLTHROUGH */
589*d05e43bcSDag-Erling Smørgrav case 2: /* MM */
590*d05e43bcSDag-Erling Smørgrav lt->tm_min = ATOI2(p);
591*d05e43bcSDag-Erling Smørgrav if (lt->tm_min > 59)
5924b88c807SRodney W. Grimes return(-1);
5934b88c807SRodney W. Grimes break;
5944b88c807SRodney W. Grimes default:
5954b88c807SRodney W. Grimes return(-1);
5964b88c807SRodney W. Grimes }
597*d05e43bcSDag-Erling Smørgrav
598*d05e43bcSDag-Erling Smørgrav /* convert broken-down time to UTC clock time seconds */
5994b88c807SRodney W. Grimes if ((*tval = mktime(lt)) == -1)
6004b88c807SRodney W. Grimes return(-1);
6014b88c807SRodney W. Grimes return(0);
6024b88c807SRodney W. Grimes }
603