Comparing modified times of files

Chip Salzenberg chip at tct.uucp
Thu Mar 28 09:11:22 AEST 1991


Compile and install this program.  End of problem.  Thanks, Henry.

/*
 * From Henry Spencer
 *
 * > 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?
 *
 * newer file1 file2
 *
 * exit with 0 status if file1 exists and file2 does not, or if file1's last
 * modified time is at least as recent as file2's.
 */

#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);
}
-- 
Chip Salzenberg at Teltronics/TCT     <chip at tct.uucp>, <uunet!pdn!tct!chip>
   "All this is conjecture of course, since I *only* post in the nude.
    Nothing comes between me and my t.b.  Nothing."   -- Bill Coderre



More information about the Comp.unix.shell mailing list