Home | History | Annotate | Line # | Download | only in pax
sel_subs.c revision 1.6
      1  1.6      tls /*	$NetBSD: sel_subs.c,v 1.6 1997/01/11 02:06:43 tls Exp $	*/
      2  1.5      cgd 
      3  1.1      jtc /*-
      4  1.1      jtc  * Copyright (c) 1992 Keith Muller.
      5  1.1      jtc  * Copyright (c) 1992, 1993
      6  1.1      jtc  *	The Regents of the University of California.  All rights reserved.
      7  1.1      jtc  *
      8  1.1      jtc  * This code is derived from software contributed to Berkeley by
      9  1.1      jtc  * Keith Muller of the University of California, San Diego.
     10  1.1      jtc  *
     11  1.1      jtc  * Redistribution and use in source and binary forms, with or without
     12  1.1      jtc  * modification, are permitted provided that the following conditions
     13  1.1      jtc  * are met:
     14  1.1      jtc  * 1. Redistributions of source code must retain the above copyright
     15  1.1      jtc  *    notice, this list of conditions and the following disclaimer.
     16  1.1      jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1      jtc  *    notice, this list of conditions and the following disclaimer in the
     18  1.1      jtc  *    documentation and/or other materials provided with the distribution.
     19  1.1      jtc  * 3. All advertising materials mentioning features or use of this software
     20  1.1      jtc  *    must display the following acknowledgement:
     21  1.1      jtc  *	This product includes software developed by the University of
     22  1.1      jtc  *	California, Berkeley and its contributors.
     23  1.1      jtc  * 4. Neither the name of the University nor the names of its contributors
     24  1.1      jtc  *    may be used to endorse or promote products derived from this software
     25  1.1      jtc  *    without specific prior written permission.
     26  1.1      jtc  *
     27  1.1      jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1      jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1      jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1      jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1      jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1      jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1      jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1      jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1      jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1      jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1      jtc  * SUCH DAMAGE.
     38  1.1      jtc  */
     39  1.1      jtc 
     40  1.1      jtc #ifndef lint
     41  1.5      cgd #if 0
     42  1.5      cgd static char sccsid[] = "@(#)sel_subs.c	8.1 (Berkeley) 5/31/93";
     43  1.5      cgd #else
     44  1.6      tls static char rcsid[] = "$NetBSD: sel_subs.c,v 1.6 1997/01/11 02:06:43 tls Exp $";
     45  1.5      cgd #endif
     46  1.1      jtc #endif /* not lint */
     47  1.1      jtc 
     48  1.1      jtc #include <sys/types.h>
     49  1.1      jtc #include <sys/time.h>
     50  1.1      jtc #include <sys/stat.h>
     51  1.1      jtc #include <sys/param.h>
     52  1.1      jtc #include <pwd.h>
     53  1.1      jtc #include <grp.h>
     54  1.1      jtc #include <stdio.h>
     55  1.1      jtc #include <ctype.h>
     56  1.1      jtc #include <string.h>
     57  1.1      jtc #include <strings.h>
     58  1.1      jtc #include <unistd.h>
     59  1.1      jtc #include <stdlib.h>
     60  1.1      jtc #include "pax.h"
     61  1.1      jtc #include "sel_subs.h"
     62  1.1      jtc #include "extern.h"
     63  1.1      jtc 
     64  1.6      tls static int str_sec __P((char *, time_t *));
     65  1.6      tls static int usr_match __P((ARCHD *));
     66  1.6      tls static int grp_match __P((ARCHD *));
     67  1.6      tls static int trng_match __P((ARCHD *));
     68  1.1      jtc 
     69  1.1      jtc static TIME_RNG *trhead = NULL;		/* time range list head */
     70  1.1      jtc static TIME_RNG *trtail = NULL;		/* time range list tail */
     71  1.1      jtc static USRT **usrtb = NULL;		/* user selection table */
     72  1.1      jtc static GRPT **grptb = NULL;		/* group selection table */
     73  1.1      jtc 
     74  1.1      jtc /*
     75  1.1      jtc  * Routines for selection of archive members
     76  1.1      jtc  */
     77  1.1      jtc 
     78  1.1      jtc /*
     79  1.1      jtc  * sel_chk()
     80  1.1      jtc  *	check if this file matches a specfied uid, gid or time range
     81  1.1      jtc  * Return:
     82  1.1      jtc  *	0 if this archive member should be processed, 1 if it should be skipped
     83  1.1      jtc  */
     84  1.1      jtc 
     85  1.1      jtc #if __STDC__
     86  1.1      jtc int
     87  1.6      tls sel_chk(ARCHD *arcn)
     88  1.1      jtc #else
     89  1.1      jtc int
     90  1.1      jtc sel_chk(arcn)
     91  1.6      tls 	ARCHD *arcn;
     92  1.1      jtc #endif
     93  1.1      jtc {
     94  1.1      jtc 	if (((usrtb != NULL) && usr_match(arcn)) ||
     95  1.1      jtc 	    ((grptb != NULL) && grp_match(arcn)) ||
     96  1.1      jtc 	    ((trhead != NULL) && trng_match(arcn)))
     97  1.1      jtc 		return(1);
     98  1.1      jtc 	return(0);
     99  1.1      jtc }
    100  1.1      jtc 
    101  1.1      jtc /*
    102  1.1      jtc  * User/group selection routines
    103  1.1      jtc  *
    104  1.1      jtc  * Routines to handle user selection of files based on the file uid/gid. To
    105  1.1      jtc  * add an entry, the user supplies either then name or the uid/gid starting with
    106  1.1      jtc  * a # on the command line. A \# will eascape the #.
    107  1.1      jtc  */
    108  1.1      jtc 
    109  1.1      jtc /*
    110  1.1      jtc  * usr_add()
    111  1.1      jtc  *	add a user match to the user match hash table
    112  1.1      jtc  * Return:
    113  1.1      jtc  *	0 if added ok, -1 otherwise;
    114  1.1      jtc  */
    115  1.1      jtc 
    116  1.1      jtc #if __STDC__
    117  1.1      jtc int
    118  1.6      tls usr_add(char *str)
    119  1.1      jtc #else
    120  1.1      jtc int
    121  1.1      jtc usr_add(str)
    122  1.6      tls 	char *str;
    123  1.1      jtc #endif
    124  1.1      jtc {
    125  1.6      tls 	u_int indx;
    126  1.6      tls 	USRT *pt;
    127  1.6      tls 	struct passwd *pw;
    128  1.6      tls 	uid_t uid;
    129  1.1      jtc 
    130  1.1      jtc 	/*
    131  1.1      jtc 	 * create the table if it doesn't exist
    132  1.1      jtc 	 */
    133  1.1      jtc 	if ((str == NULL) || (*str == '\0'))
    134  1.1      jtc 		return(-1);
    135  1.1      jtc 	if ((usrtb == NULL) &&
    136  1.1      jtc  	    ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
    137  1.1      jtc                 warn(1, "Unable to allocate memory for user selection table");
    138  1.1      jtc                 return(-1);
    139  1.1      jtc 	}
    140  1.1      jtc 
    141  1.1      jtc 	/*
    142  1.1      jtc 	 * figure out user spec
    143  1.1      jtc 	 */
    144  1.1      jtc 	if (str[0] != '#') {
    145  1.1      jtc 		/*
    146  1.1      jtc 		 * it is a user name, \# escapes # as first char in user name
    147  1.1      jtc 		 */
    148  1.1      jtc 		if ((str[0] == '\\') && (str[1] == '#'))
    149  1.1      jtc 			++str;
    150  1.1      jtc 		if ((pw = getpwnam(str)) == NULL) {
    151  1.1      jtc                 	warn(1, "Unable to find uid for user: %s", str);
    152  1.1      jtc                 	return(-1);
    153  1.1      jtc 		}
    154  1.1      jtc 		uid = (uid_t)pw->pw_uid;
    155  1.1      jtc         } else
    156  1.1      jtc #		ifdef NET2_STAT
    157  1.1      jtc 		uid = (uid_t)atoi(str+1);
    158  1.1      jtc #		else
    159  1.1      jtc 		uid = (uid_t)strtoul(str+1, (char **)NULL, 10);
    160  1.1      jtc #		endif
    161  1.1      jtc 	endpwent();
    162  1.1      jtc 
    163  1.1      jtc 	/*
    164  1.1      jtc 	 * hash it and go down the hash chain (if any) looking for it
    165  1.1      jtc 	 */
    166  1.1      jtc 	indx = ((unsigned)uid) % USR_TB_SZ;
    167  1.1      jtc 	if ((pt = usrtb[indx]) != NULL) {
    168  1.1      jtc                 while (pt != NULL) {
    169  1.1      jtc                         if (pt->uid == uid)
    170  1.1      jtc 				return(0);
    171  1.1      jtc                         pt = pt->fow;
    172  1.1      jtc                 }
    173  1.1      jtc 	}
    174  1.1      jtc 
    175  1.1      jtc 	/*
    176  1.1      jtc 	 * uid is not yet in the table, add it to the front of the chain
    177  1.1      jtc 	 */
    178  1.1      jtc 	if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
    179  1.1      jtc 		pt->uid = uid;
    180  1.1      jtc 		pt->fow = usrtb[indx];
    181  1.1      jtc 		usrtb[indx] = pt;
    182  1.1      jtc 		return(0);
    183  1.1      jtc 	}
    184  1.1      jtc         warn(1, "User selection table out of memory");
    185  1.1      jtc         return(-1);
    186  1.1      jtc }
    187  1.1      jtc 
    188  1.1      jtc /*
    189  1.1      jtc  * usr_match()
    190  1.1      jtc  *	check if this files uid matches a selected uid.
    191  1.1      jtc  * Return:
    192  1.1      jtc  *	0 if this archive member should be processed, 1 if it should be skipped
    193  1.1      jtc  */
    194  1.1      jtc 
    195  1.1      jtc #if __STDC__
    196  1.1      jtc static int
    197  1.6      tls usr_match(ARCHD *arcn)
    198  1.1      jtc #else
    199  1.1      jtc static int
    200  1.1      jtc usr_match(arcn)
    201  1.6      tls 	ARCHD *arcn;
    202  1.1      jtc #endif
    203  1.1      jtc {
    204  1.6      tls 	USRT *pt;
    205  1.1      jtc 
    206  1.1      jtc 	/*
    207  1.1      jtc 	 * hash and look for it in the table
    208  1.1      jtc 	 */
    209  1.1      jtc 	pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
    210  1.1      jtc 	while (pt != NULL) {
    211  1.1      jtc 		if (pt->uid == arcn->sb.st_uid)
    212  1.1      jtc 			return(0);
    213  1.1      jtc 		pt = pt->fow;
    214  1.1      jtc 	}
    215  1.1      jtc 
    216  1.1      jtc 	/*
    217  1.1      jtc 	 * not found
    218  1.1      jtc 	 */
    219  1.1      jtc 	return(1);
    220  1.1      jtc }
    221  1.1      jtc 
    222  1.1      jtc /*
    223  1.1      jtc  * grp_add()
    224  1.1      jtc  *	add a group match to the group match hash table
    225  1.1      jtc  * Return:
    226  1.1      jtc  *	0 if added ok, -1 otherwise;
    227  1.1      jtc  */
    228  1.1      jtc 
    229  1.1      jtc #if __STDC__
    230  1.1      jtc int
    231  1.6      tls grp_add(char *str)
    232  1.1      jtc #else
    233  1.1      jtc int
    234  1.1      jtc grp_add(str)
    235  1.6      tls 	char *str;
    236  1.1      jtc #endif
    237  1.1      jtc {
    238  1.6      tls 	u_int indx;
    239  1.6      tls 	GRPT *pt;
    240  1.6      tls 	struct group *gr;
    241  1.6      tls 	gid_t gid;
    242  1.1      jtc 
    243  1.1      jtc 	/*
    244  1.1      jtc 	 * create the table if it doesn't exist
    245  1.1      jtc 	 */
    246  1.1      jtc 	if ((str == NULL) || (*str == '\0'))
    247  1.1      jtc 		return(-1);
    248  1.1      jtc 	if ((grptb == NULL) &&
    249  1.1      jtc  	    ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
    250  1.1      jtc                 warn(1, "Unable to allocate memory fo group selection table");
    251  1.1      jtc                 return(-1);
    252  1.1      jtc 	}
    253  1.1      jtc 
    254  1.1      jtc 	/*
    255  1.1      jtc 	 * figure out user spec
    256  1.1      jtc 	 */
    257  1.1      jtc 	if (str[0] != '#') {
    258  1.1      jtc 		/*
    259  1.1      jtc 		 * it is a group name, \# escapes # as first char in group name
    260  1.1      jtc 		 */
    261  1.1      jtc 		if ((str[0] == '\\') && (str[1] == '#'))
    262  1.1      jtc 			++str;
    263  1.1      jtc 		if ((gr = getgrnam(str)) == NULL) {
    264  1.1      jtc                 	warn(1,"Cannot determine gid for group name: %s", str);
    265  1.1      jtc                 	return(-1);
    266  1.1      jtc 		}
    267  1.1      jtc 		gid = (gid_t)gr->gr_gid;
    268  1.1      jtc         } else
    269  1.1      jtc #		ifdef NET2_STAT
    270  1.1      jtc 		gid = (gid_t)atoi(str+1);
    271  1.1      jtc #		else
    272  1.1      jtc 		gid = (gid_t)strtoul(str+1, (char **)NULL, 10);
    273  1.1      jtc #		endif
    274  1.1      jtc 	endgrent();
    275  1.1      jtc 
    276  1.1      jtc 	/*
    277  1.1      jtc 	 * hash it and go down the hash chain (if any) looking for it
    278  1.1      jtc 	 */
    279  1.1      jtc 	indx = ((unsigned)gid) % GRP_TB_SZ;
    280  1.1      jtc 	if ((pt = grptb[indx]) != NULL) {
    281  1.1      jtc                 while (pt != NULL) {
    282  1.1      jtc                         if (pt->gid == gid)
    283  1.1      jtc 				return(0);
    284  1.1      jtc                         pt = pt->fow;
    285  1.1      jtc                 }
    286  1.1      jtc 	}
    287  1.1      jtc 
    288  1.1      jtc 	/*
    289  1.1      jtc 	 * gid not in the table, add it to the front of the chain
    290  1.1      jtc 	 */
    291  1.1      jtc 	if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
    292  1.1      jtc 		pt->gid = gid;
    293  1.1      jtc 		pt->fow = grptb[indx];
    294  1.1      jtc 		grptb[indx] = pt;
    295  1.1      jtc 		return(0);
    296  1.1      jtc 	}
    297  1.1      jtc         warn(1, "Group selection table out of memory");
    298  1.1      jtc         return(-1);
    299  1.1      jtc }
    300  1.1      jtc 
    301  1.1      jtc /*
    302  1.1      jtc  * grp_match()
    303  1.1      jtc  *	check if this files gid matches a selected gid.
    304  1.1      jtc  * Return:
    305  1.1      jtc  *	0 if this archive member should be processed, 1 if it should be skipped
    306  1.1      jtc  */
    307  1.1      jtc 
    308  1.1      jtc #if __STDC__
    309  1.1      jtc static int
    310  1.6      tls grp_match(ARCHD *arcn)
    311  1.1      jtc #else
    312  1.1      jtc static int
    313  1.1      jtc grp_match(arcn)
    314  1.6      tls 	ARCHD *arcn;
    315  1.1      jtc #endif
    316  1.1      jtc {
    317  1.6      tls 	GRPT *pt;
    318  1.1      jtc 
    319  1.1      jtc 	/*
    320  1.1      jtc 	 * hash and look for it in the table
    321  1.1      jtc 	 */
    322  1.1      jtc 	pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
    323  1.1      jtc 	while (pt != NULL) {
    324  1.1      jtc 		if (pt->gid == arcn->sb.st_gid)
    325  1.1      jtc 			return(0);
    326  1.1      jtc 		pt = pt->fow;
    327  1.1      jtc 	}
    328  1.1      jtc 
    329  1.1      jtc 	/*
    330  1.1      jtc 	 * not found
    331  1.1      jtc 	 */
    332  1.1      jtc 	return(1);
    333  1.1      jtc }
    334  1.1      jtc 
    335  1.1      jtc /*
    336  1.1      jtc  * Time range selection routines
    337  1.1      jtc  *
    338  1.1      jtc  * Routines to handle user selection of files based on the modification and/or
    339  1.1      jtc  * inode change time falling within a specified time range (the non-standard
    340  1.1      jtc  * -T flag). The user may specify any number of different file time ranges.
    341  1.1      jtc  * Time ranges are checked one at a time until a match is found (if at all).
    342  1.1      jtc  * If the file has a mtime (and/or ctime) which lies within one of the time
    343  1.1      jtc  * ranges, the file is selected. Time ranges may have a lower and/or a upper
    344  1.1      jtc  * value. These ranges are inclusive. When no time ranges are supplied to pax
    345  1.1      jtc  * with the -T option, all members in the archive will be selected by the time
    346  1.1      jtc  * range routines. When only a lower range is supplied, only files with a
    347  1.1      jtc  * mtime (and/or ctime) equal to or younger are selected. When only a upper
    348  1.1      jtc  * range is supplied, only files with a mtime (and/or ctime) equal to or older
    349  1.1      jtc  * are selected. When the lower time range is equal to the upper time range,
    350  1.1      jtc  * only files with a mtime (or ctime) of exactly that time are selected.
    351  1.1      jtc  */
    352  1.1      jtc 
    353  1.1      jtc /*
    354  1.1      jtc  * trng_add()
    355  1.1      jtc  *	add a time range match to the time range list.
    356  1.1      jtc  *	This is a non-standard pax option. Lower and upper ranges are in the
    357  1.1      jtc  *	format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
    358  1.1      jtc  *	Time ranges are based on current time, so 1234 would specify a time of
    359  1.1      jtc  *	12:34 today.
    360  1.1      jtc  * Return:
    361  1.1      jtc  *	0 if the time range was added to the list, -1 otherwise
    362  1.1      jtc  */
    363  1.1      jtc 
    364  1.1      jtc #if __STDC__
    365  1.1      jtc int
    366  1.6      tls trng_add(char *str)
    367  1.1      jtc #else
    368  1.1      jtc int
    369  1.1      jtc trng_add(str)
    370  1.6      tls 	char *str;
    371  1.1      jtc #endif
    372  1.1      jtc {
    373  1.6      tls 	TIME_RNG *pt;
    374  1.6      tls 	char *up_pt = NULL;
    375  1.6      tls 	char *stpt;
    376  1.6      tls 	char *flgpt;
    377  1.6      tls 	int dot = 0;
    378  1.1      jtc 
    379  1.1      jtc 	/*
    380  1.1      jtc 	 * throw out the badly formed time ranges
    381  1.1      jtc 	 */
    382  1.1      jtc 	if ((str == NULL) || (*str == '\0')) {
    383  1.1      jtc 		warn(1, "Empty time range string");
    384  1.1      jtc 		return(-1);
    385  1.1      jtc 	}
    386  1.1      jtc 
    387  1.1      jtc 	/*
    388  1.1      jtc 	 * locate optional flags suffix /{cm}.
    389  1.1      jtc 	 */
    390  1.4  mycroft 	if ((flgpt = strrchr(str, '/')) != NULL)
    391  1.1      jtc 		*flgpt++ = '\0';
    392  1.1      jtc 
    393  1.1      jtc 	for (stpt = str; *stpt != '\0'; ++stpt) {
    394  1.1      jtc 		if ((*stpt >= '0') && (*stpt <= '9'))
    395  1.1      jtc 			continue;
    396  1.1      jtc 		if ((*stpt == ',') && (up_pt == NULL)) {
    397  1.1      jtc 			*stpt = '\0';
    398  1.1      jtc 			up_pt = stpt + 1;
    399  1.1      jtc 			dot = 0;
    400  1.1      jtc 			continue;
    401  1.1      jtc 		}
    402  1.1      jtc 
    403  1.1      jtc 		/*
    404  1.1      jtc 		 * allow only one dot per range (secs)
    405  1.1      jtc 		 */
    406  1.1      jtc 		if ((*stpt == '.') && (!dot)) {
    407  1.1      jtc 			++dot;
    408  1.1      jtc 			continue;
    409  1.1      jtc 		}
    410  1.1      jtc 		warn(1, "Improperly specified time range: %s", str);
    411  1.1      jtc 		goto out;
    412  1.1      jtc 	}
    413  1.1      jtc 
    414  1.1      jtc 	/*
    415  1.1      jtc 	 * allocate space for the time range and store the limits
    416  1.1      jtc 	 */
    417  1.1      jtc 	if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
    418  1.1      jtc 		warn(1, "Unable to allocate memory for time range");
    419  1.1      jtc 		return(-1);
    420  1.1      jtc 	}
    421  1.1      jtc 
    422  1.1      jtc 	/*
    423  1.1      jtc 	 * by default we only will check file mtime, but usee can specify
    424  1.1      jtc 	 * mtime, ctime (inode change time) or both.
    425  1.1      jtc 	 */
    426  1.1      jtc 	if ((flgpt == NULL) || (*flgpt == '\0'))
    427  1.1      jtc 		pt->flgs = CMPMTME;
    428  1.1      jtc 	else {
    429  1.1      jtc 		pt->flgs = 0;
    430  1.1      jtc 		while (*flgpt != '\0') {
    431  1.1      jtc 			switch(*flgpt) {
    432  1.1      jtc 			case 'M':
    433  1.1      jtc 			case 'm':
    434  1.1      jtc 				pt->flgs |= CMPMTME;
    435  1.1      jtc 				break;
    436  1.1      jtc 			case 'C':
    437  1.1      jtc 			case 'c':
    438  1.1      jtc 				pt->flgs |= CMPCTME;
    439  1.1      jtc 				break;
    440  1.1      jtc 			default:
    441  1.1      jtc 				warn(1, "Bad option %c with time range %s",
    442  1.1      jtc 				    *flgpt, str);
    443  1.1      jtc 				goto out;
    444  1.1      jtc 			}
    445  1.1      jtc 			++flgpt;
    446  1.1      jtc 		}
    447  1.1      jtc 	}
    448  1.1      jtc 
    449  1.1      jtc 	/*
    450  1.1      jtc 	 * start off with the current time
    451  1.1      jtc 	 */
    452  1.1      jtc 	pt->low_time = pt->high_time = time((time_t *)NULL);
    453  1.1      jtc 	if (*str != '\0') {
    454  1.1      jtc 		/*
    455  1.1      jtc 		 * add lower limit
    456  1.1      jtc 		 */
    457  1.1      jtc 		if (str_sec(str, &(pt->low_time)) < 0) {
    458  1.1      jtc 			warn(1, "Illegal lower time range %s", str);
    459  1.1      jtc 			(void)free((char *)pt);
    460  1.1      jtc 			goto out;
    461  1.1      jtc 		}
    462  1.1      jtc 		pt->flgs |= HASLOW;
    463  1.1      jtc 	}
    464  1.1      jtc 
    465  1.1      jtc 	if ((up_pt != NULL) && (*up_pt != '\0')) {
    466  1.1      jtc 		/*
    467  1.1      jtc 		 * add upper limit
    468  1.1      jtc 		 */
    469  1.1      jtc 		if (str_sec(up_pt, &(pt->high_time)) < 0) {
    470  1.1      jtc 			warn(1, "Illegal upper time range %s", up_pt);
    471  1.1      jtc 			(void)free((char *)pt);
    472  1.1      jtc 			goto out;
    473  1.1      jtc 		}
    474  1.1      jtc 		pt->flgs |= HASHIGH;
    475  1.1      jtc 
    476  1.1      jtc 		/*
    477  1.1      jtc 		 * check that the upper and lower do not overlap
    478  1.1      jtc 		 */
    479  1.1      jtc 		if (pt->flgs & HASLOW) {
    480  1.1      jtc 			if (pt->low_time > pt->high_time) {
    481  1.1      jtc 				warn(1, "Upper %s and lower %s time overlap",
    482  1.1      jtc 					up_pt, str);
    483  1.1      jtc 				(void)free((char *)pt);
    484  1.1      jtc 				return(-1);
    485  1.1      jtc 			}
    486  1.1      jtc 		}
    487  1.1      jtc 	}
    488  1.1      jtc 
    489  1.1      jtc 	pt->fow = NULL;
    490  1.1      jtc 	if (trhead == NULL) {
    491  1.1      jtc 		trtail = trhead = pt;
    492  1.1      jtc 		return(0);
    493  1.1      jtc 	}
    494  1.1      jtc 	trtail->fow = pt;
    495  1.1      jtc 	trtail = pt;
    496  1.1      jtc 	return(0);
    497  1.1      jtc 
    498  1.1      jtc     out:
    499  1.1      jtc 	warn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
    500  1.1      jtc 	return(-1);
    501  1.1      jtc }
    502  1.1      jtc 
    503  1.1      jtc /*
    504  1.1      jtc  * trng_match()
    505  1.1      jtc  *	check if this files mtime/ctime falls within any supplied time range.
    506  1.1      jtc  * Return:
    507  1.1      jtc  *	0 if this archive member should be processed, 1 if it should be skipped
    508  1.1      jtc  */
    509  1.1      jtc 
    510  1.1      jtc #if __STDC__
    511  1.1      jtc static int
    512  1.6      tls trng_match(ARCHD *arcn)
    513  1.1      jtc #else
    514  1.1      jtc static int
    515  1.1      jtc trng_match(arcn)
    516  1.6      tls 	ARCHD *arcn;
    517  1.1      jtc #endif
    518  1.1      jtc {
    519  1.6      tls 	TIME_RNG *pt;
    520  1.1      jtc 
    521  1.1      jtc 	/*
    522  1.1      jtc 	 * have to search down the list one at a time looking for a match.
    523  1.1      jtc 	 * remember time range limits are inclusive.
    524  1.1      jtc 	 */
    525  1.1      jtc 	pt = trhead;
    526  1.1      jtc 	while (pt != NULL) {
    527  1.1      jtc 		switch(pt->flgs & CMPBOTH) {
    528  1.1      jtc 		case CMPBOTH:
    529  1.1      jtc 			/*
    530  1.1      jtc 			 * user wants both mtime and ctime checked for this
    531  1.1      jtc 			 * time range
    532  1.1      jtc 			 */
    533  1.1      jtc 			if (((pt->flgs & HASLOW) &&
    534  1.1      jtc 			    (arcn->sb.st_mtime < pt->low_time) &&
    535  1.1      jtc 			    (arcn->sb.st_ctime < pt->low_time)) ||
    536  1.1      jtc 			    ((pt->flgs & HASHIGH) &&
    537  1.1      jtc 			    (arcn->sb.st_mtime > pt->high_time) &&
    538  1.1      jtc 			    (arcn->sb.st_ctime > pt->high_time))) {
    539  1.1      jtc 				pt = pt->fow;
    540  1.1      jtc 				continue;
    541  1.1      jtc 			}
    542  1.1      jtc 			break;
    543  1.1      jtc 		case CMPCTME:
    544  1.1      jtc 			/*
    545  1.1      jtc 			 * user wants only ctime checked for this time range
    546  1.1      jtc 			 */
    547  1.1      jtc 			if (((pt->flgs & HASLOW) &&
    548  1.1      jtc 			    (arcn->sb.st_ctime < pt->low_time)) ||
    549  1.1      jtc 			    ((pt->flgs & HASHIGH) &&
    550  1.1      jtc 			    (arcn->sb.st_ctime > pt->high_time))) {
    551  1.1      jtc 				pt = pt->fow;
    552  1.1      jtc 				continue;
    553  1.1      jtc 			}
    554  1.1      jtc 			break;
    555  1.1      jtc 		case CMPMTME:
    556  1.1      jtc 		default:
    557  1.1      jtc 			/*
    558  1.1      jtc 			 * user wants only mtime checked for this time range
    559  1.1      jtc 			 */
    560  1.1      jtc 			if (((pt->flgs & HASLOW) &&
    561  1.1      jtc 			    (arcn->sb.st_mtime < pt->low_time)) ||
    562  1.1      jtc 			    ((pt->flgs & HASHIGH) &&
    563  1.1      jtc 			    (arcn->sb.st_mtime > pt->high_time))) {
    564  1.1      jtc 				pt = pt->fow;
    565  1.1      jtc 				continue;
    566  1.1      jtc 			}
    567  1.1      jtc 			break;
    568  1.1      jtc 		}
    569  1.1      jtc 		break;
    570  1.1      jtc 	}
    571  1.1      jtc 
    572  1.1      jtc 	if (pt == NULL)
    573  1.1      jtc 		return(1);
    574  1.1      jtc 	return(0);
    575  1.1      jtc }
    576  1.1      jtc 
    577  1.1      jtc /*
    578  1.1      jtc  * str_sec()
    579  1.1      jtc  *	Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
    580  1.1      jtc  *	seconds. Tval already has current time loaded into it at entry.
    581  1.1      jtc  * Return:
    582  1.1      jtc  *	0 if converted ok, -1 otherwise
    583  1.1      jtc  */
    584  1.1      jtc 
    585  1.1      jtc #if __STDC__
    586  1.1      jtc static int
    587  1.6      tls str_sec(char *str, time_t *tval)
    588  1.1      jtc #else
    589  1.1      jtc static int
    590  1.1      jtc str_sec(str, tval)
    591  1.6      tls 	char *str;
    592  1.1      jtc 	time_t *tval;
    593  1.1      jtc #endif
    594  1.1      jtc {
    595  1.6      tls 	struct tm *lt;
    596  1.6      tls 	char *dot = NULL;
    597  1.1      jtc 
    598  1.1      jtc 	lt = localtime(tval);
    599  1.4  mycroft 	if ((dot = strchr(str, '.')) != NULL) {
    600  1.1      jtc 		/*
    601  1.1      jtc 		 * seconds (.ss)
    602  1.1      jtc 		 */
    603  1.1      jtc 		*dot++ = '\0';
    604  1.1      jtc 		if (strlen(dot) != 2)
    605  1.1      jtc 			return(-1);
    606  1.1      jtc 		if ((lt->tm_sec = ATOI2(dot)) > 61)
    607  1.1      jtc 			return(-1);
    608  1.1      jtc 	} else
    609  1.1      jtc 		lt->tm_sec = 0;
    610  1.1      jtc 
    611  1.1      jtc 	switch (strlen(str)) {
    612  1.1      jtc 	case 10:
    613  1.1      jtc 		/*
    614  1.1      jtc 		 * year (yy)
    615  1.1      jtc 		 * watch out for year 2000
    616  1.1      jtc 		 */
    617  1.1      jtc 		if ((lt->tm_year = ATOI2(str)) < 69)
    618  1.1      jtc 			lt->tm_year += 100;
    619  1.1      jtc 		str += 2;
    620  1.1      jtc 		/* FALLTHROUGH */
    621  1.1      jtc 	case 8:
    622  1.1      jtc 		/*
    623  1.1      jtc 		 * month (mm)
    624  1.1      jtc 		 * watch out months are from 0 - 11 internally
    625  1.1      jtc 		 */
    626  1.1      jtc 		if ((lt->tm_mon = ATOI2(str)) > 12)
    627  1.1      jtc 			return(-1);
    628  1.1      jtc 		--lt->tm_mon;
    629  1.1      jtc 		str += 2;
    630  1.1      jtc 		/* FALLTHROUGH */
    631  1.1      jtc 	case 6:
    632  1.1      jtc 		/*
    633  1.1      jtc 		 * day (dd)
    634  1.1      jtc 		 */
    635  1.1      jtc 		if ((lt->tm_mday = ATOI2(str)) > 31)
    636  1.1      jtc 			return(-1);
    637  1.1      jtc 		str += 2;
    638  1.1      jtc 		/* FALLTHROUGH */
    639  1.1      jtc 	case 4:
    640  1.1      jtc 		/*
    641  1.1      jtc 		 * hour (hh)
    642  1.1      jtc 		 */
    643  1.1      jtc 		if ((lt->tm_hour = ATOI2(str)) > 23)
    644  1.1      jtc 			return(-1);
    645  1.1      jtc 		str += 2;
    646  1.1      jtc 		/* FALLTHROUGH */
    647  1.1      jtc 	case 2:
    648  1.1      jtc 		/*
    649  1.1      jtc 		 * minute (mm)
    650  1.1      jtc 		 */
    651  1.1      jtc 		if ((lt->tm_min = ATOI2(str)) > 59)
    652  1.1      jtc 			return(-1);
    653  1.1      jtc 		break;
    654  1.1      jtc 	default:
    655  1.1      jtc 		return(-1);
    656  1.1      jtc 	}
    657  1.1      jtc 	/*
    658  1.1      jtc 	 * convert broken-down time to GMT clock time seconds
    659  1.1      jtc 	 */
    660  1.1      jtc 	if ((*tval = mktime(lt)) == -1)
    661  1.1      jtc 		return(-1);
    662  1.1      jtc 	return(0);
    663  1.1      jtc }
    664