Lines Matching defs:?

0 /*	$NetBSD: sel_subs.c,v 1.23 2009/12/21 12:44:48 christos Exp $	*/
3 /*-
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
9 * Keith Muller of the University of California, San Diego.
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
21 * without specific prior written permission.
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * SUCH DAMAGE.
37 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
42 #if 0
43 static char sccsid[] = "@(#)sel_subs.c 8.1 (Berkeley) 5/31/93";
45 __RCSID("$NetBSD: sel_subs.c,v 1.23 2009/12/21 12:44:48 christos Exp $");
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/param.h>
54 #include <pwd.h>
55 #include <grp.h>
56 #include <stdio.h>
57 #include <ctype.h>
58 #include <string.h>
59 #include <strings.h>
60 #include <time.h>
61 #include <unistd.h>
62 #include <stdlib.h>
63 #include <tzfile.h>
65 #include "pax.h"
66 #include "sel_subs.h"
67 #include "extern.h"
74 static TIME_RNG *trhead = NULL; /* time range list head */
75 static TIME_RNG *trtail = NULL; /* time range list tail */
76 static USRT **usrtb = NULL; /* user selection table */
77 static GRPT **grptb = NULL; /* group selection table */
85 * check if this file matches a specified uid, gid or time range
86 * Return:
87 * 0 if this archive member should be processed, 1 if it should be skipped
93 if (((usrtb != NULL) && usr_match(arcn)) ||
94 ((grptb != NULL) && grp_match(arcn)) ||
95 ((trhead != NULL) && trng_match(arcn)))
96 return 1;
97 return 0;
103 * Routines to handle user selection of files based on the file uid/gid. To
105 * a # on the command line. A \# will escape the #.
110 * add a user match to the user match hash table
111 * Return:
112 * 0 if added ok, -1 otherwise;
124 * create the table if it doesn't exist
126 if ((str == NULL) || (*str == '\0'))
127 return -1;
128 if ((usrtb == NULL) &&
129 ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
130 tty_warn(1,
132 return -1;
138 if (str[0] != '#') {
140 * it is a user name, \# escapes # as first char in user name
142 if ((str[0] == '\\') && (str[1] == '#'))
143 ++str;
144 if ((pw = getpwnam(str)) == NULL) {
145 tty_warn(1, "Unable to find uid for user: %s", str);
146 return -1;
148 uid = (uid_t)pw->pw_uid;
150 uid = (uid_t)strtoul(str+1, NULL, 10);
156 indx = ((unsigned)uid) % USR_TB_SZ;
157 if ((pt = usrtb[indx]) != NULL) {
158 while (pt != NULL) {
159 if (pt->uid == uid)
160 return 0;
161 pt = pt->fow;
168 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
169 pt->uid = uid;
170 pt->fow = usrtb[indx];
171 usrtb[indx] = pt;
172 return 0;
174 tty_warn(1, "User selection table out of memory");
175 return -1;
180 * check if this files uid matches a selected uid.
181 * Return:
182 * 0 if this archive member should be processed, 1 if it should be skipped
193 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
194 while (pt != NULL) {
195 if (pt->uid == arcn->sb.st_uid)
196 return 0;
197 pt = pt->fow;
203 return 1;
208 * add a group match to the group match hash table
209 * Return:
210 * 0 if added ok, -1 otherwise;
222 * create the table if it doesn't exist
224 if ((str == NULL) || (*str == '\0'))
225 return -1;
226 if ((grptb == NULL) &&
227 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
228 tty_warn(1,
230 return -1;
236 if (str[0] != '#') {
238 * it is a group name, \# escapes # as first char in group name
240 if ((str[0] == '\\') && (str[1] == '#'))
241 ++str;
242 if ((gr = getgrnam(str)) == NULL) {
243 tty_warn(1,
244 "Cannot determine gid for group name: %s", str);
245 return -1;
247 gid = (gid_t)gr->gr_gid;
249 gid = (gid_t)strtoul(str+1, NULL, 10);
255 indx = ((unsigned)gid) % GRP_TB_SZ;
256 if ((pt = grptb[indx]) != NULL) {
257 while (pt != NULL) {
258 if (pt->gid == gid)
259 return 0;
260 pt = pt->fow;
267 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
268 pt->gid = gid;
269 pt->fow = grptb[indx];
270 grptb[indx] = pt;
271 return 0;
273 tty_warn(1, "Group selection table out of memory");
274 return -1;
279 * check if this files gid matches a selected gid.
280 * Return:
281 * 0 if this archive member should be processed, 1 if it should be skipped
292 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
293 while (pt != NULL) {
294 if (pt->gid == arcn->sb.st_gid)
295 return 0;
296 pt = pt->fow;
302 return 1;
309 * inode change time falling within a specified time range (the non-standard
310 * -T flag). The user may specify any number of different file time ranges.
311 * Time ranges are checked one at a time until a match is found (if at all).
312 * If the file has a mtime (and/or ctime) which lies within one of the time
313 * ranges, the file is selected. Time ranges may have a lower and/or a upper
314 * value. These ranges are inclusive. When no time ranges are supplied to pax
315 * with the -T option, all members in the archive will be selected by the time
316 * range routines. When only a lower range is supplied, only files with a
317 * mtime (and/or ctime) equal to or younger are selected. When only a upper
318 * range is supplied, only files with a mtime (and/or ctime) equal to or older
319 * are selected. When the lower time range is equal to the upper time range,
320 * only files with a mtime (or ctime) of exactly that time are selected.
325 * add a time range match to the time range list.
326 * This is a non-standard pax option. Lower and upper ranges are in the
327 * format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
328 * Time ranges are based on current time, so 1234 would specify a time of
329 * 12:34 today.
330 * Return:
331 * 0 if the time range was added to the list, -1 otherwise
338 char *up_pt = NULL;
341 int dot = 0;
346 if ((str == NULL) || (*str == '\0')) {
347 tty_warn(1, "Empty time range string");
348 return -1;
352 * locate optional flags suffix /{cm}.
354 if ((flgpt = strrchr(str, '/')) != NULL)
355 *flgpt++ = '\0';
357 for (stpt = str; *stpt != '\0'; ++stpt) {
358 if ((*stpt >= '0') && (*stpt <= '9'))
360 if ((*stpt == ',') && (up_pt == NULL)) {
361 *stpt = '\0';
362 up_pt = stpt + 1;
363 dot = 0;
370 if ((*stpt == '.') && (!dot)) {
371 ++dot;
374 tty_warn(1, "Improperly specified time range: %s", str);
381 if ((pt = malloc(sizeof(TIME_RNG))) == NULL) {
382 tty_warn(1, "Unable to allocate memory for time range");
383 return -1;
388 * mtime, ctime (inode change time) or both.
390 if ((flgpt == NULL) || (*flgpt == '\0'))
391 pt->flgs = CMPMTME;
393 pt->flgs = 0;
394 while (*flgpt != '\0') {
396 case 'M':
397 case 'm':
398 pt->flgs |= CMPMTME;
400 case 'C':
401 case 'c':
402 pt->flgs |= CMPCTME;
404 default:
405 tty_warn(1, "Bad option %c with time range %s",
410 ++flgpt;
417 pt->low_time = pt->high_time = time(NULL);
418 if (*str != '\0') {
422 if (str_sec(str, &(pt->low_time)) < 0) {
423 tty_warn(1, "Illegal lower time range %s", str);
427 pt->flgs |= HASLOW;
430 if ((up_pt != NULL) && (*up_pt != '\0')) {
434 if (str_sec(up_pt, &(pt->high_time)) < 0) {
435 tty_warn(1, "Illegal upper time range %s", up_pt);
439 pt->flgs |= HASHIGH;
444 if (pt->flgs & HASLOW) {
445 if (pt->low_time > pt->high_time) {
446 tty_warn(1,
447 "Upper %s and lower %s time overlap",
450 return -1;
455 pt->fow = NULL;
456 if (trhead == NULL) {
457 trtail = trhead = pt;
458 return 0;
460 trtail->fow = pt;
461 trtail = pt;
462 return 0;
464 out:
465 tty_warn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
466 return -1;
471 * check if this files mtime/ctime falls within any supplied time range.
472 * Return:
473 * 0 if this archive member should be processed, 1 if it should be skipped
482 * have to search down the list one at a time looking for a match.
483 * remember time range limits are inclusive.
485 pt = trhead;
486 while (pt != NULL) {
487 switch(pt->flgs & CMPBOTH) {
488 case CMPBOTH:
493 if (((pt->flgs & HASLOW) &&
494 (arcn->sb.st_mtime < pt->low_time) &&
495 (arcn->sb.st_ctime < pt->low_time)) ||
496 ((pt->flgs & HASHIGH) &&
497 (arcn->sb.st_mtime > pt->high_time) &&
498 (arcn->sb.st_ctime > pt->high_time))) {
499 pt = pt->fow;
503 case CMPCTME:
507 if (((pt->flgs & HASLOW) &&
508 (arcn->sb.st_ctime < pt->low_time)) ||
509 ((pt->flgs & HASHIGH) &&
510 (arcn->sb.st_ctime > pt->high_time))) {
511 pt = pt->fow;
515 case CMPMTME:
516 default:
520 if (((pt->flgs & HASLOW) &&
521 (arcn->sb.st_mtime < pt->low_time)) ||
522 ((pt->flgs & HASHIGH) &&
523 (arcn->sb.st_mtime > pt->high_time))) {
524 pt = pt->fow;
532 if (pt == NULL)
533 return 1;
534 return 0;
539 * Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
540 * seconds. Tval already has current time loaded into it at entry.
541 * Return:
542 * 0 if converted ok, -1 otherwise
545 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
548 str_sec(const char *p, time_t *tval)
551 const char *dot, *t;
554 for (t = p, dot = NULL; *t; ++t) {
555 if (isdigit((unsigned char)*t))
557 if (*t == '.' && dot == NULL) {
558 dot = t;
561 return -1;
564 lt = localtime(tval);
566 if (dot != NULL) {
567 len = strlen(dot);
568 if (len != 3)
569 return -1;
570 ++dot;
571 lt->tm_sec = ATOI2(dot);
573 len = 0;
574 lt->tm_sec = 0;
577 yearset = 0;
578 switch (strlen(p) - len) {
579 case 12:
580 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
581 yearset = 1;
583 case 10:
585 lt->tm_year += ATOI2(p);
587 yearset = ATOI2(p);
589 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
591 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
594 case 8:
595 lt->tm_mon = ATOI2(p);
596 --lt->tm_mon;
598 case 6:
599 lt->tm_mday = ATOI2(p);
601 case 4:
602 lt->tm_hour = ATOI2(p);
604 case 2:
605 lt->tm_min = ATOI2(p);
607 default:
608 return -1;
612 * convert broken-down time to GMT clock time seconds
614 if ((*tval = mktime(lt)) == -1)
615 return -1;
616 return 0;