From: olson@anchor.esd.sgi.com (Dave Olson)
Subject: Re: setting file access time
Organization:  Silicon Graphics, Inc.  Mountain View, CA
Date: Fri, 11 Sep 92 22:40:36 GMT

In <32519@adm.brl.mil> beyer@bflsgu.fl.bs.dlr.de (R. Beyer) writes:

| I am looking for a command which can be used on the command line or in
| a script to set the file access time like, for instance, utime(2).
| 

Here you go; it does quite a bit more, actually.  I don't remember
just why all of it was in one program, but...

/*	get time date stamp from a coff binary and print it in
	human-readable form (like odump -fv).
	Checks to see if f_magic looks like it could be a COFF
	header.  Given all the different magic #'s, this is
	rather difficult.  The best (easiest) I can see to do is to
	look and see if f_magic&0500 == 0500.  Could look at opthdr
	too, but this is  just a quick and dirty tool.  Word order
	is also a problem that the coff format doesn't really
	address...  So for now, I will just check for 'native' order.
	Otherwise have to worry about word and byte order in the
	f_timdat also.
	Dave Olson, 5/88

	Added -s option to set st_mtime from this field. 2/89
*/
#include "sys/types.h"
#include "filehdr.h"
#include "stdio.h"
#include "fcntl.h"
#include <unistd.h>

char *prog;
int errcnt;
char *ctime();
extern int errno;
void error(), showtime();

main(cnt, args)
int cnt;
char **args;
{
	int c, setit = 0, verbose = 0, numeric = 0;
	extern int optind, opterr;
	extern char *optarg;

	opterr = 0;	/* do our own errors */
	prog = *args;

	while((c=getopt(cnt, args, "svn")) != -1) {
		switch(c) {
		case '?':
			usage();
			break;
		case 's':
			setit = 1;
			break;
		case 'v':
			verbose++;
			break;
		case 'n':	/* output the stamp in numeric form */
			numeric++;
			break;
		}
	}

	if(cnt == optind)
		showtime(0, "stdin", 0, 1);
	else for( ; optind < cnt; optind++) {
		int fd = open(args[optind], O_RDONLY);
		if(fd == -1)
			error(0, "Couldn't open %s", args[optind]);
		else {
			showtime(fd, args[optind], setit, verbose, numeric);
			close(fd);
		}
	}
	exit(errcnt);
}

usage()
{
	fprintf(stderr, "Usage: %s [-v] [-s] [-n] [file] ...\n", prog);
	exit(1);
}


void
error(exval, fmt, a1, a2, a3)
int exval;
char *fmt;
{
	char buf[1024];

	strcpy(buf, prog);
	strcat(buf, ": ");

	sprintf(buf+strlen(buf), fmt, a1, a2, a3);

	if(errno)
		perror(buf);
	else
		fprintf(stderr, "%s\n", buf);
	if(exval)
		exit(exval);
	errcnt++;
}


void
showtime(fd, name, set, verbose, numeric)
int fd, set;
{
	struct filehdr fhdr;
	struct utimbuf ut;

	if(read(fd, &fhdr, sizeof(fhdr)) != sizeof(fhdr)) {
		error(0, "Couldn't read coff filehdr of %s", name);
		return;
	}
	if((fhdr.f_magic & 0500) != 0500) {
		if(verbose)
			errno=0, error(0, "%s doesn't appear to be an coff binary", name);
		return;
	}
	if(set) {
		ut.actime = fhdr.f_timdat;
		ut.modtime = fhdr.f_timdat;
		if(utime(name, &ut) == -1) {
			error(0, "couldn't set times on %s", name);
			return;
		}
	}
	if(!set || verbose > 1) {
		if(numeric)	/* for comparing compile times in scripts */
			printf("%s: %u\n", name, fhdr.f_timdat);
		else
			printf("%s: %.24s\n", name, ctime(&fhdr.f_timdat));
	}
}
--
Let no one tell me that silence gives consent,  |   Dave Olson
because whoever is silent dissents.             |   Silicon Graphics, Inc.
    Maria Isabel Barreno                        |   olson@sgi.com
Newsgroups: comp.sys.sgi