/* $Id: newer.c,v 1.5 2005/03/20 07:45:41 taco Exp $
   Public domain.  */

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

/*
 *	newer x y
 *
 *	returns 0 if x exists and y does not, or if
 *	files x and y both exist and x was modified
 *	at least as recently as y, and nonzero otherwise.
 */

int
main (int argc, char **argv) {
	struct stat x, y;

	if (argc > 1 && strcmp (argv[1], "--help") == 0) {
	  fputs ("Usage: newer [OPTION]... FILE1 FILE2\n\
  Exit successfully if FILE1 exists and is at least as recent as FILE2.\n \
\n\
--help      display this help and exit\n\
--version   output version information and exit\n\n", stdout);
	  fputs ("Email bug reports to metapost@tug.org.\n", stdout);
	  exit(0);
	} else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
	  fputs ("newer 0.891\n\
This program is in the public domain.\n\
Primary author of newer: John Hobby.\n", stdout);
	  exit (0);
	}
	
	/* insist on exactly two args */
	if (argc != 3) {
	  fputs ("newer: Need exactly two arguments.\n", stderr);
	  fputs ("Try `newer --help' for more information.\n", stderr);
	  exit(1);
	}

	/* does the first file exist? */
	if (stat (argv[1], &x) < 0)
		exit(1);
	
	/* does the second file exist? */
	if (stat (argv[2], &y) < 0)
		exit(0);
	
	/* fail if the first file is older than the second */
	if (x.st_mtime < y.st_mtime)
		exit(1);
	
	/* otherwise, succeed */
	exit(0);
}
