[comp.unix.questions] File Modification Dates

jik@athena.mit.edu (Jonathan I. Kamens) (05/10/89)

In article <207@psgdc> rg@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@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@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@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@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@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

tale@pawl.rpi.edu (David C Lawrence) (05/11/89)

In article <207@psgdc>, rg@psgdc (Dick Gill) writes:
[wants code to get readable modtime from filename]
Dick> I'll be happy to get a C program, shellscript or anything else
Dick> that I can call from a Bourne shell script.

In addition to Jonathan's modtime programme and Randal's perl script,
there is also "sls", available in the comp.unix.sources archives.  I
like sls a lot for scripts because I can choose my format string very
easily rather than pull apart the lines generated by a standard ls -l.
Examples:

Script started on Wed May 10 22:07:52 1989
(blank lines added for clarity in reading)

imagine:tale (1) ls -dl emacs
drwxr-xr-x  2 tale          512 May  4 14:28 emacs

imagine:tale (2) sls -dl emacs
drwxr-xr-x  2 tale           512 May 04 1989 14:28 emacs

imagine:tale (3) sls -dp %a emacs
May 09 1989 19:54

imagine:tale (4) sls -dp '%n: %c"%h %d"' {emacs,bin,src}
bin: Apr 27
emacs: May 04
src: May 10

imagine:tale (5) exit

script done on Wed May 10 22:09:04 1989

That was just extremely simple stuff, but that's what is great about
sls ... it's fast and it can do complex formatting or simple
formatting, all depending on your needs.

Dave
--
      tale@rpitsmts.bitnet, tale%mts@itsgw.rpi.edu, tale@pawl.rpi.edu