file times and shell commands

Moderator, John Quarterman std-unix at ut-sally.UUCP
Sun Nov 2 10:17:15 AEST 1986


From: pyramid!utzoo!henry (Henry Spencer)
Date: Fri, 31 Oct 86 21:25:31 CST

> There doesn't appear to be any decent way to compare the last modified
> times of files from the shell...

Before everybody starts inventing their own names for this, it should be
noted that V8 already has a program for this, newer(1).  It takes two
filenames as arguments, and exits with status 0 if and only if either
(a) the first exists and the second does not, or (b) both exist and the
first's modification time is at least as recent as the second's.  Other-
wise it exits with non-zero status.  (The preceding two sentences are
essentially the whole of the manual page for it.)

Relatively few people have V8, but in the absence of any other precedent
for what this facility should like look, it seems reasonable to follow
V8's lead.

Here is an independent rewrite, done from the manual page and not the
code, by me, hereby placed in the public domain:

/*
 * newer - is first file newer than second?
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

main(argc, argv)
int argc;
char *argv[];
{
	struct stat file1;
	struct stat file2;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s file1 file2\n", argv[0]);
		exit(2);
	}

	if (stat(argv[1], &file1) < 0)
		exit(1);
	if (stat(argv[2], &file2) < 0)
		exit(0);
	if (file1.st_mtime >= file2.st_mtime)
		exit(0);
	exit(1);
}

				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,decvax,pyramid}!utzoo!henry

Volume-Number: Volume 8, Number 16



More information about the Mod.std.unix mailing list