Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Sep 2001 06:20:02 -0700 (PDT)
From:      Nils M Holm <nmh@t3x.org>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/30309: New FIND(1) option
Message-ID:  <200109061320.f86DK2j85371@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/30309; it has been noted by GNATS.

From: Nils M Holm <nmh@t3x.org>
To: Ruslan Ermilov <ru@FreeBSD.ORG>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/30309: New FIND(1) option
Date: Thu, 6 Sep 2001 15:17:48 +0200 (CEST)

 > On Tue, Sep 04, 2001 at 02:45:11PM +0200, Nils M Holm wrote:
 > [...]
 > What do you think of just extending the -mtime functionality
 > so that it accepts time units in hours and minutes?
 
 I have extended the -mtime (and -ctime, -atime) syntax to accept units,
 as suggested. In addition to hours and minutes, it also accepts seconds,
 days, weeks, months, and years.
 
 When no units are specified, the original semantics are used.
 
 Man pages are updated (but should be proof-read).
 
 I still haven't figured out how to send proper follow-up's to
 bug reports, so I send the new diff as a reply.
 
 Nils.
 
 -----[ diff follows ]-----
 diff -u /usr/src/usr.bin/find.old/find.1 /usr/src/usr.bin/find/find.1
 --- /usr/src/usr.bin/find.old/find.1	Tue Sep  4 14:07:04 2001
 +++ /usr/src/usr.bin/find/find.1	Thu Sep  6 15:04:54 2001
 @@ -420,6 +420,40 @@
  preceded by a plus sign (``+'') or a minus sign (``\-'').
  A preceding plus sign means ``more than n'', a preceding minus sign means
  ``less than n'' and neither means ``exactly n'' .
 +.Pp
 +The
 +.Ic -atime ,
 +.Ic -ctime ,
 +and
 +.Ic -mtime
 +primaries support the following units:
 +.Pp
 +.Bl -tag -width flag -offset indent -compact
 +.It Cm s
 +seconds
 +.It Cm m
 +minutes (60s)
 +.It Cm h
 +hours (60m)
 +.It Cm D
 +days (24h)
 +.It Cm W
 +weeks (7D)
 +.It Cm M
 +months (30D)
 +.It Cm Y
 +months (365Y)
 +.El
 +.Pp
 +When a unit is specified, the primaries do not round their argument
 +values. Hence
 +.Ic -mtime Ar +1
 +is not euqal to
 +.Ic -mtime Ar +1D .
 +Units are most useful when used together with the ``+'' and ``-''
 +modifiers. For example,
 +.Ic -mtime Ar +5m30s
 +is true for all files which are at least 5 minutes and 30 seconds old.
  .Sh OPERATORS
  The primaries may be combined using the following operators.
  The operators are listed in order of decreasing precedence.
 diff -u /usr/src/usr.bin/find.old/find.h /usr/src/usr.bin/find/find.h
 --- /usr/src/usr.bin/find.old/find.h	Tue Sep  4 14:07:04 2001
 +++ /usr/src/usr.bin/find/find.h	Thu Sep  6 14:39:25 2001
 @@ -64,6 +64,7 @@
  #define	F_ANY		2			/* perm */
  	int flags;				/* private flags */
  	enum ntype type;			/* plan node type */
 +	int t_units;				/* time units flag */
  	union {
  		gid_t _g_data;			/* gid */
  		ino_t _i_data;			/* inode */
 diff -u /usr/src/usr.bin/find.old/function.c /usr/src/usr.bin/find/function.c
 --- /usr/src/usr.bin/find.old/function.c	Tue Sep  4 14:07:04 2001
 +++ /usr/src/usr.bin/find/function.c	Thu Sep  6 14:52:09 2001
 @@ -58,6 +58,7 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
 +#include <ctype.h>
  #include <unistd.h>
  
  #include "find.h"
 @@ -121,6 +122,86 @@
  }
  
  /*
 + * find_parsetime --
 + *	Parse a string of the form [+-]([0-9][hmsYMWD])* and return the value.
 + */
 +static time_t
 +find_parsetime(plan, option, vp)
 +	PLAN *plan;
 +	char *option, *vp;
 +{
 +	time_t nsec, a;
 +	char *s;
 +
 +	/* Determine comparison from leading + or -. */
 +	s = vp;
 +	switch (*s) {
 +	case '+':
 +		++s;
 +		plan->flags = F_GREATER;
 +		break;
 +	case '-':
 +		++s;
 +		plan->flags = F_LESSTHAN;
 +		break;
 +	default:
 +		plan->flags = F_EQUAL;
 +		break;
 +	}
 +
 +	if (!isdigit(*s))
 +		errx(1, "%s: %s: illegal time value", option, vp);
 +
 +	plan->t_units = 0;
 +	nsec = 0L;
 +	a = 0L;
 +	for (; *s; s++) {
 +		if (isdigit(*s)) {
 +			a = a*10L + *s-'0';
 +		}
 +		else if (*s == 's') {	/* seconds */
 +			nsec += a;
 +			a = 0L;
 +			plan->t_units = 1;
 +		}
 +		else if (*s == 'm') {	/* minutes */
 +			nsec += a*60L;
 +			a = 0L;
 +			plan->t_units = 1;
 +		}
 +		else if (*s == 'h') {	/* hours */
 +			nsec += a*3600L;
 +			a = 0L;
 +			plan->t_units = 1;
 +		}
 +		else if (*s == 'D') {	/* days */
 +			nsec += a*86400L;
 +			a = 0L;
 +			plan->t_units = 1;
 +		}
 +		else if (*s == 'W') {	/* weeks */
 +			nsec += a*604800L;
 +			a = 0L;
 +			plan->t_units = 1;
 +		}
 +		else if (*s == 'M') {	/* months (30D)*/
 +			nsec += a*2592000L;
 +			a = 0L;
 +			plan->t_units = 1;
 +		}
 +		else if (*s == 'Y') {	/* years (365D)*/
 +			nsec += a*31536000L;
 +			a = 0L;
 +			plan->t_units = 1;
 +		}
 +		else {
 +			errx(1, "%s: %s: bad unit", option, vp);
 +		}
 +	}
 +	return nsec+a;
 +}
 +
 +/*
   * The value of n for the inode times (atime, ctime, and mtime) is a range,
   * i.e. n matches from (n - 1) to n 24 hour periods.  This interacts with
   * -n, such that "-mtime -1" would be less than 0 days, which isn't what the
 @@ -175,8 +256,13 @@
  {
  	extern time_t now;
  
 -	COMPARE((now - entry->fts_statp->st_atime +
 -	    86400 - 1) / 86400, plan->t_data);
 +	if (plan->t_units) {
 +		COMPARE(now - entry->fts_statp->st_atime, plan->t_data);
 +	}
 +	else {
 +		COMPARE((now - entry->fts_statp->st_atime +
 +			86400 - 1) / 86400, plan->t_data);
 +	}
  }
  
  PLAN *
 @@ -188,7 +274,7 @@
  	ftsoptions &= ~FTS_NOSTAT;
  
  	new = palloc(N_ATIME, f_atime);
 -	new->t_data = find_parsenum(new, "-atime", arg, NULL);
 +	new->t_data = find_parsetime(new, "-atime", arg);
  	TIME_CORRECT(new, N_ATIME);
  	return (new);
  }
 @@ -238,8 +324,13 @@
  {
  	extern time_t now;
  
 -	COMPARE((now - entry->fts_statp->st_ctime +
 -	    86400 - 1) / 86400, plan->t_data);
 +	if (plan->t_units) {
 +		COMPARE(now - entry->fts_statp->st_ctime, plan->t_data);
 +	}
 +	else {
 +		COMPARE((now - entry->fts_statp->st_ctime +
 +			86400 - 1) / 86400, plan->t_data);
 +	}
  }
  
  PLAN *
 @@ -251,7 +342,7 @@
  	ftsoptions &= ~FTS_NOSTAT;
  
  	new = palloc(N_CTIME, f_ctime);
 -	new->t_data = find_parsenum(new, "-ctime", arg, NULL);
 +	new->t_data = find_parsetime(new, "-ctime", arg);
  	TIME_CORRECT(new, N_CTIME);
  	return (new);
  }
 @@ -792,8 +883,13 @@
  {
  	extern time_t now;
  
 -	COMPARE((now - entry->fts_statp->st_mtime + 86400 - 1) /
 -	    86400, plan->t_data);
 +	if (plan->t_units) {
 +		COMPARE(now - entry->fts_statp->st_mtime, plan->t_data);
 +	}
 +	else {
 +		COMPARE((now - entry->fts_statp->st_mtime +
 +			86400 - 1) / 86400, plan->t_data);
 +	}
  }
  
  PLAN *
 @@ -805,7 +901,7 @@
  	ftsoptions &= ~FTS_NOSTAT;
  
  	new = palloc(N_MTIME, f_mtime);
 -	new->t_data = find_parsenum(new, "-mtime", arg, NULL);
 +	new->t_data = find_parsetime(new, "-mtime", arg);
  	TIME_CORRECT(new, N_MTIME);
  	return (new);
  }
 --------------------------
 
 -- 
 Nils M Holm <nmh@t3x.org> -- http://www.t3x.org
 PLEASE ask before sending attachments >10K bytes.
 

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200109061320.f86DK2j85371>