File Modification Dates

Jonathan I. Kamens jik at athena.mit.edu
Wed May 10 22:27:04 AEST 1989


In article <207 at psgdc> rg at psgdc (Dick Gill) writes:
>                                    The new problem which, he
>said hopefully, someone has already tackled, is how to retrieve
>the file's modification date in a consistent format whether the
>file was modified in the past year or not. (If I could also get
>the modification time, life would be even better --- but then
>lets not get greedy!)
>
>I'll be happy to get a C program, shellscript or anything else
>that I can call from a Bourne shell script.
>
>Thanks

It's much more fun if someone *hasn't* tackled it before, because then
I get to write a new program to do it!

And guess what, it even comes with a man page!

Example output:

% modtime modtime
Wed May 10 08:04:58 1989
% modtime modtime.c modtime.1
modtime.c: Wed May 10 08:04:10 1989
modtime.1: Wed May 10 08:23:16 1989

Jonathan Kamens			              USnail:
MIT Project Athena				410 Memorial Drive, No. 223F
jik at Athena.MIT.EDU				Cambridge, MA 02139-4318
Office: 617-253-4261			      Home: 617-225-8218

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  modtime.1 modtime.c
# Wrapped by jik at pit-manager on Wed May 10 08:24:55 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'modtime.1' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'modtime.1'\"
else
echo shar: Extracting \"'modtime.1'\" \(1198 characters\)
sed "s/^X//" >'modtime.1' <<'END_OF_FILE'
X.\" modtime.1
X.\"
X.\" man page for modtime utility.
X.\"
X.\" Copyright (c) 1989 by Jonathan Kamens (jik at Athena.MIT.EDU).  Do
X.\" whatever you want with this as long as my copyright stays on it
X.\" and you don't try to pass it off as your own.
X.TH MODTIME 1 "May 10, 1989"
X.SH NAME
Xmodtime - find out the modification time, access time, or change time
Xof a file
X.SH SYNOPIS
X.B delete
X[ 
X.B \-m | \-a | \-c 
X] filename ...
X.SH DESCRIPTION
XBy default,
X.I modtime
Xoutputs to standard out the modification times of the files passed to
Xit on the command line, in a consistent format similar to that of
X.IR date (1).
XIf more than one file is specified, each time is preceded by the name
Xof the file to which it refers.
X.PP
XThe \-a
Xand \-c
Xflags can be used to select access time and change time rather than
Xmodification time for the specified files.  The -m
Xflag is a no-op since modification time is the default.
X.PP
XA file whose name starts with `\-' can be specified by preceding
Xthe filename with two dashes (\-\|\-); all subsequent command-line
Xarguments will
Xnot be interpreted as command-line options.
X.SH SEE ALSO
Xls(1), stat(2)
X.SH AUTHOR
XJonathan Kamens, MIT Project Athena (jik at Athena.MIT.EDU).
END_OF_FILE
if test 1198 -ne `wc -c <'modtime.1'`; then
    echo shar: \"'modtime.1'\" unpacked with wrong size!
fi
# end of 'modtime.1'
fi
if test -f 'modtime.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'modtime.c'\"
else
echo shar: Extracting \"'modtime.c'\" \(2342 characters\)
sed "s/^X//" >'modtime.c' <<'END_OF_FILE'
X/*
X * modtime.c
X * 
X * BSD-based program to output the mod-time of the file(s) whose
X * name(s) is/are given to it on the command line in the same format
X * as date(1) (but without the current year getting lost).
X *
X * This is much more complicated than it probably needs to be, but the
X * extra stuff makes it infinitely more useful.  If it doesn't compile
X * on your machine, then follow the philosophy that I follow when
X * dealing with Berkeley code -- delete lines until it works!
X * 
X * Copyright (c) by Jonathan Kamens (jik at Athena.MIT.EDU).  Do whatever
X * you want with this as long as my copyright stays on it and you
X * don't try to pass it off as your own. 
X */
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <sys/param.h>
X#include <strings.h>
X#include <stdio.h>
X#include <errno.h>
X
Xchar *whoami;
Xextern char *malloc();
Xextern int getopt();
Xextern char *optarg;
Xextern int optind;
X
Xmain (argc, argv)
Xint argc;
Xchar *argv[];
X{
X     struct stat buf;
X     int fnflag = 0, errflag = 0;
X     int aflag = 0, mflag = 1, cflag = 0; /* display mtime by default */
X     char *errbuf;
X     time_t time;
X     int option;
X     
X     whoami = (whoami = rindex(argv[0], '/')) ? whoami + 1 : argv[0];
X     errbuf = malloc(MAXPATHLEN + strlen(whoami) + 1);
X     if (! errbuf) {
X	  perror(whoami);
X	  exit(1);
X     }
X
X     while ((option = getopt(argc, argv, "amc")) != EOF)
X     switch (option) {
X     case 'a':
X	  aflag = 1;
X	  mflag = cflag = 0;
X	  break;
X     case 'c':
X	  cflag = 1;
X	  aflag = mflag = 0;
X	  break;
X     case 'm':
X	  mflag = 1;
X	  aflag = cflag = 0;
X	  break;
X     default:
X	  usage();
X	  exit(1);
X     }
X
X     argv += optind - 1;
X     argc -= optind - 1;
X     
X     if (argc < 2) {
X	  usage();
X	  exit(1);
X     }
X
X     if (argc > 2)
X	  fnflag++;
X
X     for (argc--, argv++; argc; argc--, argv++) {
X	  if (stat(*argv, &buf)) {
X	       errflag++;
X	       sprintf(errbuf, "%s: %s", whoami, *argv);
X	       perror(errbuf);
X	       continue;
X	  }
X	  
X	  if (fnflag)
X	       fprintf(stdout, "%s: ", *argv);
X
X	  if (mflag)
X	       time = buf.st_mtime;
X	  else if (aflag)
X	       time = buf.st_atime;
X	  else
X	       time = buf.st_ctime;
X	  
X	  fprintf(stdout, "%s", ctime(&time)); /* ctime provides newline */
X     }
X
X     return(errflag);
X}
X
Xusage()
X{
X     fprintf(stderr, "usage: %s [-m | -a | -c] filename ...\n", whoami);
X}
END_OF_FILE
if test 2342 -ne `wc -c <'modtime.c'`; then
    echo shar: \"'modtime.c'\" unpacked with wrong size!
fi
# end of 'modtime.c'
fi
echo shar: End of shell archive.
exit 0



More information about the Comp.unix.questions mailing list