Retaining file modification times

Alex Martelli alex at am.sublink.org
Fri May 24 07:18:30 AEST 1991


phillips at FOZZIE.NRL.NAVY.MIL (Lee Phillips) writes:
:I want to do something to a file without changing its modification
:time.  I suppose I can write a script to get the time from ls, do the
:modification, then restore the time (after converting it to numerical
:format) with /usr/5bin/touch, but I'm hoping that there is an easier
:way. (Csh solutions preferred.) 

Would you settle for a C solution without the 'sh'?-)  I think it's
easier than transforming ls's output into a form acceptable for touch.
---cut here: ti.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
extern char*malloc();

int main(argc, argv) int argc; char **argv;
{
	struct utimbuf {time_t actime, modtime;} *timar=0;
	struct stat bufi;
	int i,pid,waitrc;
	unsigned int sonrc;

	if(argc<2) {
		fprintf(stderr,"Usage: %s command [files]\n",argv[0]);
		return 1;
	}
	if(argc>2) {
		timar=(struct utimbuf*)malloc((argc-2));
		for(i=2; i<argc; i++) {
			if(-1==stat(argv[i],&bufi))
				timar[i-2].actime=0;
			else {
				timar[i-2].actime=bufi.st_atime;
				timar[i-2].modtime=bufi.st_mtime;
			}
		}
	}
	if((pid=fork())==0) {	/* in child process, exec command */
		execvp(argv[1], argv+1);
		fprintf(stderr, "%s: ", argv[0]);
		perror(argv[1]);
	}
	waitrc=wait(&sonrc);
	for(i=2; i<argc; i++)
		if(timar[i-2].actime) utime(argv[i], &timar[i-2]);
	if(timar) free(timar);
	if(sonrc&0xFF) return sonrc;
	else return sonrc>>8;
}
---end cut: ti.c

This is rather a kludge, actually.  The syntax (after 'cc -o ti ti.c')
is 'ti command -maybe -some -options file arguments...', but I don't
want ti to have to "know" about the syntax of "command" to discern which
of the following words are filenames, and which are not.  So what ti
does is try to stat() *each* of its args; then, after fork/exec/wait of
command, it tries to restore actime and modtime for each arg that it was
originally able to stat successfully - I figured this would be mostly
harmless, i.e. even if you DO have a file called '-l', doing, say,
'ti ci -l a.c b.c c.c', the fact that '-l' is NOT being modified by
the (RCS check-in-then-check-out-again,locked) target command will not
normally be a problem (the times of file '-l' will be harmlessly
restored to what they already were...).
You may want to adopt some alternative approach, such as parsing
the target command's arguments according to some option-syntax
convention.
Other problems have to do with such commands as 'ti sort -ofo fi', where
file 'fo' is going to be modified but is not easily recognizable as a
part of the target command; also 'ti sort fi >fo', since the shell is
not going to let ti know that its output is redirected to fo...

Anyway, the main way I use it is just as in the first example above -
I definitely do NOT want source files' modtimes to be updated when
they are just, conceptually, being checked-in, not 'modified'... such
updates were impeding my intended usage of make+rcs before I came up
with this kludgey but basically workable solution.
-- 
Alex Martelli - (home snailmail:) v. Barontini 27, 40138 Bologna, ITALIA
Email: (work:) martelli at cadlab.sublink.org, (home:) alex at am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; 
Fax: ++39 (51) 366964 (work only), Fidonet: 332/401.3 (home only).



More information about the Comp.unix.shell mailing list