Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Sep 2001 10:10:01 -0700 (PDT)
From:      Ruslan Ermilov <ru@FreeBSD.ORG>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/30309: New FIND(1) option
Message-ID:  <200109061710.f86HA1q35219@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: Ruslan Ermilov <ru@FreeBSD.ORG>
To: Nils M Holm <nmh@t3x.org>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/30309: New FIND(1) option
Date: Thu, 6 Sep 2001 20:05:16 +0300

 On Thu, Sep 06, 2001 at 03:17:48PM +0200, Nils M Holm wrote:
 > > 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.
 > 
 Uh oh, this already applies to 4.2-RELEASE sources, which have
 since been heavily modified in -CURRENT and -STABLE.
 
 How about the following patch instead (without the manpage bit)?
 
 Index: find.h
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/find/find.h,v
 retrieving revision 1.12
 diff -u -p -r1.12 find.h
 --- find.h	2001/09/04 16:09:01	1.12
 +++ find.h	2001/09/06 17:04:29
 @@ -69,6 +69,7 @@ typedef	struct _plandata *creat_f(struct
  #define	F_MTTYPE	0x00001000
  #define	F_MTUNKNOWN	0x00002000
  #define	F_IGNCASE	0x00010000	/* iname ipath iregex */
 +#define	F_EXACTTIME	F_IGNCASE	/* -[acm]time units syntax */
  
  /* node definition */
  typedef struct _plandata {
 Index: function.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/find/function.c,v
 retrieving revision 1.33
 diff -u -p -r1.33 function.c
 --- function.c	2001/09/04 16:09:01	1.33
 +++ function.c	2001/09/06 17:04:31
 @@ -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 
  	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;
  }
  
 
 Cheers,
 -- 
 Ruslan Ermilov		Oracle Developer/DBA,
 ru@sunbay.com		Sunbay Software AG,
 ru@FreeBSD.org		FreeBSD committer,
 +380.652.512.251	Simferopol, Ukraine
 
 http://www.FreeBSD.org	The Power To Serve
 http://www.oracle.com	Enabling The Information Age

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?200109061710.f86HA1q35219>