diff options
author | ru <ru@FreeBSD.org> | 2001-09-14 12:47:13 +0000 |
---|---|---|
committer | ru <ru@FreeBSD.org> | 2001-09-14 12:47:13 +0000 |
commit | 66b2cd14ab2bc1afb15b9049320d89fe9846e317 (patch) | |
tree | cd5ff906e7fac50993fc36ab98c35020c5e92c95 /usr.bin/find/function.c | |
parent | a0f990cffc8a6c119a7ddaa6dd974ff436bce275 (diff) | |
download | FreeBSD-src-66b2cd14ab2bc1afb15b9049320d89fe9846e317.zip FreeBSD-src-66b2cd14ab2bc1afb15b9049320d89fe9846e317.tar.gz |
Bloat find(1) even more, and introduce the concept
of time units to be used with -[acm]time primaries.
Based on patch from Nils M Holm <nmh@t3x.org>.
PR: bin/29165, bin/30309
Diffstat (limited to 'usr.bin/find/function.c')
-rw-r--r-- | usr.bin/find/function.c | 116 |
1 files changed, 104 insertions, 12 deletions
diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index a729ca7..b477f20 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -67,7 +67,7 @@ static const char rcsid[] = time_t get_date __P((char *date, struct timeb *now)); -#define COMPARE(a, b) { \ +#define COMPARE(a, b) do { \ switch (plan->flags & F_ELG_MASK) { \ case F_EQUAL: \ return (a == b); \ @@ -78,7 +78,7 @@ time_t get_date __P((char *date, struct timeb *now)); default: \ abort(); \ } \ -} +} while(0) static PLAN * palloc(option) @@ -138,6 +138,82 @@ find_parsenum(plan, option, vp, endch) } /* + * find_parsetime -- + * Parse a string of the form [+-]([0-9]+[smhdw]?)+ and return the value. + */ +static long long +find_parsetime(plan, option, vp) + PLAN *plan; + char *option, *vp; +{ + long long secs, value; + char *str, *unit; /* Pointer to character ending conversion. */ + + /* Determine comparison from leading + or -. */ + str = vp; + switch (*str) { + case '+': + ++str; + plan->flags |= F_GREATER; + break; + case '-': + ++str; + plan->flags |= F_LESSTHAN; + break; + default: + plan->flags |= F_EQUAL; + break; + } + + value = strtoq(str, &unit, 10); + if (value == 0 && unit == str) { + errx(1, "%s: %s: illegal time value", option, vp); + /* NOTREACHED */ + } + if (*unit == '\0') + return value; + + /* Units syntax. */ + secs = 0; + for (;;) { + switch(*unit) { + case 's': /* seconds */ + secs += value; + break; + case 'm': /* minutes */ + secs += value * 60; + break; + case 'h': /* hours */ + secs += value * 3600; + break; + case 'd': /* days */ + secs += value * 86400; + break; + case 'w': /* weeks */ + secs += value * 604800; + break; + default: + errx(1, "%s: %s: bad unit '%c'", option, vp, *unit); + /* NOTREACHED */ + } + str = unit + 1; + if (*str == '\0') /* EOS */ + break; + value = strtoq(str, &unit, 10); + if (value == 0 && unit == str) { + errx(1, "%s: %s: illegal time value", option, vp); + /* NOTREACHED */ + } + if (*unit == '\0') { + errx(1, "%s: %s: missing trailing unit", option, vp); + /* NOTREACHED */ + } + } + plan->flags |= F_EXACTTIME; + return secs; +} + +/* * nextarg -- * Check that another argument still exists, return a pointer to it, * and increment the argument vector pointer. @@ -226,16 +302,31 @@ f_Xtime(plan, entry) FTSENT *entry; { extern time_t now; + int exact_time; + + exact_time = plan->flags & F_EXACTTIME; if (plan->flags & F_TIME_C) { - COMPARE((now - entry->fts_statp->st_ctime + - 86400 - 1) / 86400, plan->t_data); + if (exact_time) + COMPARE(now - entry->fts_statp->st_ctime, + plan->t_data); + else + COMPARE((now - entry->fts_statp->st_ctime + + 86400 - 1) / 86400, plan->t_data); } else if (plan->flags & F_TIME_A) { - COMPARE((now - entry->fts_statp->st_atime + - 86400 - 1) / 86400, plan->t_data); + if (exact_time) + COMPARE(now - entry->fts_statp->st_atime, + plan->t_data); + else + COMPARE((now - entry->fts_statp->st_atime + + 86400 - 1) / 86400, plan->t_data); } else { - COMPARE((now - entry->fts_statp->st_mtime + - 86400 - 1) / 86400, plan->t_data); + if (exact_time) + COMPARE(now - entry->fts_statp->st_mtime, + plan->t_data); + else + COMPARE((now - entry->fts_statp->st_mtime + + 86400 - 1) / 86400, plan->t_data); } } @@ -244,15 +335,16 @@ c_Xtime(option, argvp) OPTION *option; char ***argvp; { - char *ndays; + char *value; PLAN *new; - ndays = nextarg(option, argvp); + value = nextarg(option, argvp); ftsoptions &= ~FTS_NOSTAT; new = palloc(option); - new->t_data = find_parsenum(new, option->name, ndays, NULL); - TIME_CORRECT(new); + new->t_data = find_parsetime(new, option->name, value); + if (!(new->flags & F_EXACTTIME)) + TIME_CORRECT(new); return new; } |