[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Confession



oh, is that what ksh's test -nt is supposed to do?  i had no clue,
which is why i didn't suggest anything better.

i like Rich's ``make -f -'' solution, but something which is a little
clearer, i think is

	for (i = *.el) {
		if {newer -s $i $i^c} {
			echo $i
		}

which depends on newer(1), but you should have that anyway.  this
is the version i've been using for probably 8 years.

paul

/* newer.c -- which file is newer? */

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

#define streq(s, t)	(strcmp((s), (t)) == 0)

enum { ACCESSED, MODIFIED, CHANGED } which = MODIFIED;

#ifndef	TRUE
#define	TRUE	1
#endif
#ifndef	FALSE
#define	FALSE	0
#endif

#define	EXIT_TRUE	0
#define	EXIT_FALSE	1

extern int stat(), lstat();
int (*statfunc)() = stat;

void usage()
{
	fprintf(stderr, "usage: newer [-s] [-l] [-acm] file1 file2\n");
	exit(2);
}

time_t gettime(s)
	char *s;
{
	struct stat st;
	if (statfunc(s, &st) == -1)
		return 0;
	switch (which) {
	case ACCESSED:	return st.st_atime;
	case MODIFIED:	return st.st_mtime;
	case CHANGED:	return st.st_ctime;
	}
}

int main(argc, argv)
	int argc;
	char *argv[];
{
	int c;
	time_t t1, t2;
	int silent = FALSE;
	extern int optind;

	while ((c = getopt(argc, argv, "slacm?")) != EOF)
		switch (c) {
		case 's':	silent = TRUE;		break;
		case 'l':	statfunc = lstat;	break;
		case 'a':	which = ACCESSED;	break;
		case 'm':	which = MODIFIED;	break;
		case 'c':	which = CHANGED;	break;
		case '?':	usage();
		}
	if (optind + 2 != argc)
		usage();

	t1 = gettime(argv[optind]);
	t2 = gettime(argv[optind + 1]);

	if (silent) {
		if (t1 != 0 || t2 != 0)
			exit(t1 >= t2 ? EXIT_TRUE : EXIT_FALSE);
		fprintf(stderr, "newer: %s and %s don't exist\n",
			argv[optind], argv[optind + 1]);
		exit(2);
	}

	if (t2 == 0) {
		perror(argv[optind + 1]);
		exit(2);
	}
	if (t1 == 0) {
		perror(argv[optind]);
		exit(2);
	}

	if (t1 >= t2)
		printf("%s\n", argv[optind]);
	else
		printf("%s\n", argv[optind + 1]);
	return 0;
}