/*
 *	reset the file timestamps which are later than the current time to
 *	now.
 *
 *	Bear in mind that this takes some real time to run, so allow files to
 *	be a day ahead (they will sort themselves out within 24 hours).
 */

#include <stdio.h>
#include <sys/types.h>	/* for stat.h */
#include <sys/stat.h>	/* for struct stbuf */
#include <sys/time.h>

int downdate();
time_t now, tomorrow;	/* time of day now and in 24 hours time */

main(argc, argv)
char **argv;
{
	int i;

	if (argc < 2) {
		fputs("Usage: downdate <dir> ...\n", stderr);
		exit(1);
	}
	now = time(0);
	tomorrow = now + 24*60*60;
	for (i=1; i<argc; i++) descend(argv[i], downdate);
	exit(0);
}

time_t	tvp[2];

int
downdate(name, fullname, stbuf)
char *name, *fullname;
struct stat *stbuf;
{
	int changed = 0;	/* need we change the times on the file? */

	if (stbuf == NULL) return(0);

	tvp[0] = stbuf->st_atime;
	tvp[1] = stbuf->st_mtime;

	if (tvp[0] > tomorrow || tvp[0] < 0) {
		tvp[0] = now;
		changed = 1;
	}
	if (tvp[1] > tomorrow || tvp[1] < 0) {
		tvp[1] = now;
		changed = 1;
	}

	if (changed) {
		if (utime(name, tvp) != 0) {
			perror(fullname);
			changed = 0;
		} else {
			puts(fullname);
		}
	}
	return(changed);
}
