*BSD News Article 26105


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!bunyip.cc.uq.oz.au!harbinger.cc.monash.edu.au!yeshua.marcam.com!news.kei.com!sol.ctr.columbia.edu!emory!swrinde!news.dell.com!natinst.com!hrd769.brooks.af.mil!cynjut.ogisd.ess.harris.com!cynjut.ogisd.ess.harris.com!not-for-mail
From: burgess@cynjut.ogisd.ess.harris.com (Dave Burgess)
Newsgroups: comp.os.386bsd.misc
Subject: Re: Date Comparison function for test(1).
Date: 16 Jan 1994 17:13:56 -0600
Organization: Creative Imagineering
Lines: 376
Message-ID: <2hchnk$9qh@cynjut.ogisd.ess.harris.com>
References: <2hc9qf$aq@cynjut.ogisd.ess.harris.com>
NNTP-Posting-Host: cynjut.ogisd.ess.harris.com

In article <2hc9qf$aq@cynjut.ogisd.ess.harris.com>,
Dave Burgess <burgess@cynjut.ogisd.ess.harris.com> wrote:
>I have made a few changes to the program test to allow date checking for
>two files in a test(1) in the shell.  I have modified lots of the source
>code in the /usr/src/bin/test directory.  In addition to the normal
>changes to the test.c files, I have also modified the man page to
>include the three new options.  These options are:
>
>	file1 -nt file2 	# File1 is newer than file2
>	file1 -ot file2		# File1 is older than file2
>	file1 -sa file2		# File1 is EXACTLY the same age as file2
>
>The way that I added this functionality is not intended to be pretty.  I
>was most interested in keeping the impact of the changes small.  There
>are probably a dozen ways to implment these changes and make them work
>"better".  All I know for sure is that these seem to work just fine for
>me.
>

I guess that it is customary to send out the patch with the
announcement.

- - - - - - - - - - - - - - - - Cut Here - - - - - - - - - - - - - - - -

*** ../test/Makefile	Thu Dec 16 23:49:10 1993
--- Makefile	Sun Jan  9 19:47:52 1994
***************
*** 8,14 ****
  MLINKS=	test.1 '[.1'
  
  # use this rule to if you update binary_ops, or unary_ops
! make_op: 
! 	sh ${.CURDIR}/mkops
! 
  .include <bsd.prog.mk>
--- 8,20 ----
  MLINKS=	test.1 '[.1'
  
  # use this rule to if you update binary_ops, or unary_ops
! #make_op: 
! #	sh ${.CURDIR}/mkops
! #
! #  IF YOU UPDATE binary_ops or unary_ops and have an obj directory link,
! #  you MUST run mkops yourself.  If you don't, the resulting output
! #  files will end up in obj.  While that isn't a problem (sine they get
! #  correctly compiled after that), this program will not run, since you
! #  don't have copies of *_ops in obj.
! #
  .include <bsd.prog.mk>
*** ../test/TEST.csh	Thu Dec 16 23:49:11 1993
--- TEST.csh	Sat Jan 15 16:24:49 1994
***************
*** 1,6 ****
  #	@(#)TEST.csh	5.1 (Berkeley) 6/8/92
  
! alias t './test \!*; echo $status'
  #alias t 'test \!*; echo $status'
  
  echo 't -b /dev/ttyp2'
--- 1,6 ----
  #	@(#)TEST.csh	5.1 (Berkeley) 6/8/92
  
! alias t './obj/test \!*; echo $status'
  #alias t 'test \!*; echo $status'
  
  echo 't -b /dev/ttyp2'
***************
*** 135,137 ****
--- 135,167 ----
  t 700 -le 1000 -a -n "1" -a "20" = "20"
  echo 't ! \( 700 -le 1000 -a -n "1" -a "20" = "20" \)'
  t ! \( 700 -le 1000 -a -n "1" -a "20" = "20" \)
+ 
+ echo 't /etc/passwd -nt ./obj/test'
+ t /etc/passwd -nt ./obj/test
+ echo 't ! /etc/passwd -nt ./obj/test'
+ t ! /etc/passwd -nt ./obj/test
+ 
+ echo 't /etc/passwd -ot ./obj/test'
+ t /etc/passwd -ot ./obj/test
+ echo 't ! /etc/passwd -ot ./obj/test'
+ t ! /etc/passwd -ot ./obj/test
+ 
+ echo 't /etc/passwd -sa ./obj/test'
+ t /etc/passwd -sa ./obj/test
+ echo 't ! /etc/passwd -sa ./obj/test'
+ t ! /etc/passwd -sa ./obj/test
+ 
+ echo 't /etc/passwd -sa /etc/passwd'
+ t /etc/passwd -sa /etc/passwd
+ echo 't ! /etc/passwd -sa /etc/passwd'
+ t ! /etc/passwd -sa /etc/passwd
+ 
+ echo 't /etc -nt /etc/passwd  # Check Dir age vs. file'
+ t /etc -nt /etc/passwd
+ echo 't ! /etc -nt /etc/passwd'
+ t ! /etc -nt /etc/passwd
+ 
+ echo 't obj -nt /etc/passwd  # Check SymLink age vs. file'
+ t obj -nt /etc/passwd
+ echo 't ! obj -nt /etc/passwd'
+ t ! obj -nt /etc/passwd
*** ../test/binary_op	Thu Dec 16 23:49:11 1993
--- binary_op	Sun Jan  9 19:47:52 1994
***************
*** 53,56 ****
  LT	 -lt	4    OP_INT
  LE	 -le	4    OP_INT
  GE	 -ge	4    OP_INT
! 
--- 53,58 ----
  LT	 -lt	4    OP_INT
  LE	 -le	4    OP_INT
  GE	 -ge	4    OP_INT
! NT	 -nt	4    OP_FILE
! OT	 -ot    4    OP_FILE
! SA	 -sa    4    OP_FILE
*** ../test/mkops	Thu Dec 16 23:49:12 1993
--- mkops	Sun Jan  9 19:47:52 1994
***************
*** 48,53 ****
--- 48,54 ----
  
  extern char *const unary_op[];
  extern char *const binary_op[];
+ extern char *const andor_op[];
  extern const char op_priority[];
  extern const char op_argflag[];'
  
***************
*** 69,74 ****
--- 70,80 ----
  echo '      NULL
  };
  
+ char *const andor_op[] = {'
+ awk '/^OR/	{printf "      \"%s\",\n", $2}' binary_op
+ awk '/^AND/	{printf "      \"%s\",\n", $2}' binary_op
+ echo '      NULL
+ };
  const char op_priority[] = {'
  awk '/^[^#]/	{printf "      %s,\n", $3}' unary_op binary_op
  echo '};
*** ../test/operators.c	Thu Dec 16 23:49:12 1993
--- operators.c	Sun Jan  9 19:47:53 1994
***************
*** 2,11 ****
   * Operators used in the test command.
   */
  
- #ifndef lint
- static char rcsid[] = "$Id: operators.c,v 1.3 1993/09/23 22:51:17 mycroft Exp $";
- #endif /* not lint */
- 
  #include <stdio.h>
  #include "operators.h"
  
--- 2,7 ----
***************
*** 43,59 ****
        "-lt",
        "-le",
        "-ge",
        NULL
  };
  
  char *const andor_op[] = {
! 	"-o",
! 	"|",
! 	"-a",
! 	"&",
! 	NULL
  };
- 
  const char op_priority[] = {
        3,
        12,
--- 39,57 ----
        "-lt",
        "-le",
        "-ge",
+       "-nt",
+       "-ot",
+       "-sa",
        NULL
  };
  
  char *const andor_op[] = {
!       "-o",
!       "|",
!       "-a",
!       "&",
!       NULL
  };
  const char op_priority[] = {
        3,
        12,
***************
*** 84,89 ****
--- 82,90 ----
        4,
        4,
        4,
+       4,
+       4,
+       4,
  };
  
  const char op_argflag[] = {
***************
*** 116,119 ****
--- 117,123 ----
        OP_INT,
        OP_INT,
        OP_INT,
+       OP_FILE,
+       OP_FILE,
+       OP_FILE,
  };
*** ../test/operators.h	Thu Dec 16 23:49:13 1993
--- operators.h	Sun Jan  9 19:47:53 1994
***************
*** 1,5 ****
- /*	$Id: operators.h,v 1.3 1993/09/23 22:51:18 mycroft Exp $ */
- 
  #define NOT 0
  #define ISBLOCK 1
  #define ISCHAR 2
--- 1,3 ----
***************
*** 29,34 ****
--- 27,35 ----
  #define LT 26
  #define LE 27
  #define GE 28
+ #define NT 29
+ #define OT 30
+ #define SA 31
  
  #define FIRST_BINARY_OP 17
  
*** ../test/test.1	Thu Dec 16 23:49:13 1993
--- test.1	Sat Jan 15 16:16:42 1994
***************
*** 192,197 ****
--- 192,213 ----
  is algebraically less
  than or equal to the integer
  .Ar \&n\&2 .
+ .\" New stuff
+ .It Ar \&file\&1 Fl \&nt Ar \&file\&2
+ True if file 
+ .Ar \&file\&1
+ is newer than
+ .Ar \&file\&2 .
+ .It Ar \&file\&1 Fl \&ot Ar \&file\&2
+ True if the integer
+ .Ar \&file\&1
+ is older than
+ .Ar \&file\&2 .
+ .It Ar \&file\&1 Fl \&sa Ar \&file\&2
+ True if the integer
+ .Ar \&file\&1
+ is the \&same \&age as
+ .Ar \&file\&2 .
  .El
  .Pp
  These primaries can be combined with the following operators:
*** ../test/test.c	Thu Dec 16 23:49:14 1993
--- test.c	Sat Jan 15 16:02:21 1994
***************
*** 419,424 ****
--- 419,425 ----
  		sp->type = BOOLEAN;
  		break;
  	case EQ:
+ 	case SA:
  		if (sp->u.num == (sp + 1)->u.num)
  			goto true;
  		goto false;
***************
*** 427,436 ****
--- 428,439 ----
  			goto true;
  		goto false;
  	case GT:
+ 	case NT:
  		if (sp->u.num > (sp + 1)->u.num)
  			goto true;
  		goto false;
  	case LT:
+ 	case OT:
  		if (sp->u.num < (sp + 1)->u.num)
  			goto true;
  		goto false;
***************
*** 442,448 ****
  		if (sp->u.num >= (sp + 1)->u.num)
  			goto true;
  		goto false;
- 
  	}
  }
  
--- 445,450 ----
***************
*** 491,496 ****
--- 493,499 ----
  posix_binary_op(argv)
  	char  **argv;
  {
+ 	struct filestat fs;
  	struct value v[2];
  	int op, c;
  	char *opname;
***************
*** 501,510 ****
  	op += FIRST_BINARY_OP;
  	c = op_argflag[op];
  
! 	if (c == OP_INT) {
  		v[0].u.num = chk_atol(argv[0]);
  		v[1].u.num = chk_atol(argv[2]);
! 	} else {
  		v[0].u.string = argv[0];
  		v[1].u.string = argv[2];
  	}
--- 504,546 ----
  	op += FIRST_BINARY_OP;
  	c = op_argflag[op];
  
! 	opname = argv[0];
! 	v[0].u.string = opname;
! 	if (c == OP_FILE) {
! 		fs.name = opname;
! 		fs.rcode = stat(opname, &fs.stat);
! 		if (fs.rcode < 0) {
! 			err("file not found: %s\n",opname);
! 			v[0].u.num = 0L;
! 		} else {
! 			v[0].u.num = fs.stat.st_ctime;
! #ifdef CHECKING_DATES
! 			fprintf(stderr,"ctime = %s\n",ctime(&fs.stat.st_ctime)); 
! #endif
! 		}
! 	}
! 	opname = argv[2];
! 	v[1].u.string = opname;
! 	if (c == OP_FILE) {
! 		fs.name = opname;
! 		fs.rcode = stat(opname, &fs.stat);
! 		if (fs.rcode < 0) {
! 			err("file not found: %s\n",opname);
! 			v[1].u.num = 0L;
! 		} else {
! 			v[1].u.num = fs.stat.st_ctime;
! #ifdef CHECKING_DATES
! 			fprintf(stderr,"ctime = %s\n",ctime(&fs.stat.st_ctime)); 
! #endif
! 		}
! 	}
! 
! 	if (c == OP_FILE) {	/* Checking file dates */
! 		c = OP_INT;
! 	} else if (c == OP_INT) {
  		v[0].u.num = chk_atol(argv[0]);
  		v[1].u.num = chk_atol(argv[2]);
! 	    } else {
  		v[0].u.string = argv[0];
  		v[1].u.string = argv[2];
  	}
-- 
------
TSgt Dave Burgess               | Dave Burgess
NCOIC, USSTRATCOM/J6444         | *BSD FAQ Maintainer
Offutt AFB, NE                  | Burgess@hrd769.brooks.af.mil