Home | History | Annotate | Line # | Download | only in build-aux
      1  1.1  christos #!/bin/sh
      2  1.1  christos # Get modification time of a file or directory and pretty-print it.
      3  1.1  christos 
      4  1.1  christos scriptversion=2005-06-29.22
      5  1.1  christos 
      6  1.1  christos # Copyright (C) 1995, 1996, 1997, 2003, 2004, 2005 Free Software
      7  1.1  christos # Foundation, Inc.
      8  1.1  christos # written by Ulrich Drepper <drepper (at] gnu.ai.mit.edu>, June 1995
      9  1.1  christos #
     10  1.1  christos # This program is free software; you can redistribute it and/or modify
     11  1.1  christos # it under the terms of the GNU General Public License as published by
     12  1.1  christos # the Free Software Foundation; either version 2, or (at your option)
     13  1.1  christos # any later version.
     14  1.1  christos #
     15  1.1  christos # This program is distributed in the hope that it will be useful,
     16  1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  1.1  christos # GNU General Public License for more details.
     19  1.1  christos #
     20  1.1  christos # You should have received a copy of the GNU General Public License
     21  1.1  christos # along with this program; if not, write to the Free Software Foundation,
     22  1.1  christos # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
     23  1.1  christos 
     24  1.1  christos # As a special exception to the GNU General Public License, if you
     25  1.1  christos # distribute this file as part of a program that contains a
     26  1.1  christos # configuration script generated by Autoconf, you may include it under
     27  1.1  christos # the same distribution terms that you use for the rest of that program.
     28  1.1  christos 
     29  1.1  christos # This file is maintained in Automake, please report
     30  1.1  christos # bugs to <bug-automake (at] gnu.org> or send patches to
     31  1.1  christos # <automake-patches (at] gnu.org>.
     32  1.1  christos 
     33  1.1  christos case $1 in
     34  1.1  christos   '')
     35  1.1  christos      echo "$0: No file.  Try \`$0 --help' for more information." 1>&2
     36  1.1  christos      exit 1;
     37  1.1  christos      ;;
     38  1.1  christos   -h | --h*)
     39  1.1  christos     cat <<\EOF
     40  1.1  christos Usage: mdate-sh [--help] [--version] FILE
     41  1.1  christos 
     42  1.1  christos Pretty-print the modification time of FILE.
     43  1.1  christos 
     44  1.1  christos Report bugs to <bug-automake (at] gnu.org>.
     45  1.1  christos EOF
     46  1.1  christos     exit $?
     47  1.1  christos     ;;
     48  1.1  christos   -v | --v*)
     49  1.1  christos     echo "mdate-sh $scriptversion"
     50  1.1  christos     exit $?
     51  1.1  christos     ;;
     52  1.1  christos esac
     53  1.1  christos 
     54  1.1  christos # Prevent date giving response in another language.
     55  1.1  christos LANG=C
     56  1.1  christos export LANG
     57  1.1  christos LC_ALL=C
     58  1.1  christos export LC_ALL
     59  1.1  christos LC_TIME=C
     60  1.1  christos export LC_TIME
     61  1.1  christos 
     62  1.1  christos # GNU ls changes its time format in response to the TIME_STYLE
     63  1.1  christos # variable.  Since we cannot assume `unset' works, revert this
     64  1.1  christos # variable to its documented default.
     65  1.1  christos if test "${TIME_STYLE+set}" = set; then
     66  1.1  christos   TIME_STYLE=posix-long-iso
     67  1.1  christos   export TIME_STYLE
     68  1.1  christos fi
     69  1.1  christos 
     70  1.1  christos save_arg1=$1
     71  1.1  christos 
     72  1.1  christos # Find out how to get the extended ls output of a file or directory.
     73  1.1  christos if ls -L /dev/null 1>/dev/null 2>&1; then
     74  1.1  christos   ls_command='ls -L -l -d'
     75  1.1  christos else
     76  1.1  christos   ls_command='ls -l -d'
     77  1.1  christos fi
     78  1.1  christos 
     79  1.1  christos # A `ls -l' line looks as follows on OS/2.
     80  1.1  christos #  drwxrwx---        0 Aug 11  2001 foo
     81  1.1  christos # This differs from Unix, which adds ownership information.
     82  1.1  christos #  drwxrwx---   2 root  root      4096 Aug 11  2001 foo
     83  1.1  christos #
     84  1.1  christos # To find the date, we split the line on spaces and iterate on words
     85  1.1  christos # until we find a month.  This cannot work with files whose owner is a
     86  1.1  christos # user named `Jan', or `Feb', etc.  However, it's unlikely that `/'
     87  1.1  christos # will be owned by a user whose name is a month.  So we first look at
     88  1.1  christos # the extended ls output of the root directory to decide how many
     89  1.1  christos # words should be skipped to get the date.
     90  1.1  christos 
     91  1.1  christos # On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
     92  1.1  christos set x`ls -l -d /`
     93  1.1  christos 
     94  1.1  christos # Find which argument is the month.
     95  1.1  christos month=
     96  1.1  christos command=
     97  1.1  christos until test $month
     98  1.1  christos do
     99  1.1  christos   shift
    100  1.1  christos   # Add another shift to the command.
    101  1.1  christos   command="$command shift;"
    102  1.1  christos   case $1 in
    103  1.1  christos     Jan) month=January; nummonth=1;;
    104  1.1  christos     Feb) month=February; nummonth=2;;
    105  1.1  christos     Mar) month=March; nummonth=3;;
    106  1.1  christos     Apr) month=April; nummonth=4;;
    107  1.1  christos     May) month=May; nummonth=5;;
    108  1.1  christos     Jun) month=June; nummonth=6;;
    109  1.1  christos     Jul) month=July; nummonth=7;;
    110  1.1  christos     Aug) month=August; nummonth=8;;
    111  1.1  christos     Sep) month=September; nummonth=9;;
    112  1.1  christos     Oct) month=October; nummonth=10;;
    113  1.1  christos     Nov) month=November; nummonth=11;;
    114  1.1  christos     Dec) month=December; nummonth=12;;
    115  1.1  christos   esac
    116  1.1  christos done
    117  1.1  christos 
    118  1.1  christos # Get the extended ls output of the file or directory.
    119  1.1  christos set dummy x`eval "$ls_command \"\$save_arg1\""`
    120  1.1  christos 
    121  1.1  christos # Remove all preceding arguments
    122  1.1  christos eval $command
    123  1.1  christos 
    124  1.1  christos # Because of the dummy argument above, month is in $2.
    125  1.1  christos #
    126  1.1  christos # On a POSIX system, we should have
    127  1.1  christos #
    128  1.1  christos # $# = 5
    129  1.1  christos # $1 = file size
    130  1.1  christos # $2 = month
    131  1.1  christos # $3 = day
    132  1.1  christos # $4 = year or time
    133  1.1  christos # $5 = filename
    134  1.1  christos #
    135  1.1  christos # On Darwin 7.7.0 and 7.6.0, we have
    136  1.1  christos #
    137  1.1  christos # $# = 4
    138  1.1  christos # $1 = day
    139  1.1  christos # $2 = month
    140  1.1  christos # $3 = year or time
    141  1.1  christos # $4 = filename
    142  1.1  christos 
    143  1.1  christos # Get the month.
    144  1.1  christos case $2 in
    145  1.1  christos   Jan) month=January; nummonth=1;;
    146  1.1  christos   Feb) month=February; nummonth=2;;
    147  1.1  christos   Mar) month=March; nummonth=3;;
    148  1.1  christos   Apr) month=April; nummonth=4;;
    149  1.1  christos   May) month=May; nummonth=5;;
    150  1.1  christos   Jun) month=June; nummonth=6;;
    151  1.1  christos   Jul) month=July; nummonth=7;;
    152  1.1  christos   Aug) month=August; nummonth=8;;
    153  1.1  christos   Sep) month=September; nummonth=9;;
    154  1.1  christos   Oct) month=October; nummonth=10;;
    155  1.1  christos   Nov) month=November; nummonth=11;;
    156  1.1  christos   Dec) month=December; nummonth=12;;
    157  1.1  christos esac
    158  1.1  christos 
    159  1.1  christos case $3 in
    160  1.1  christos   ???*) day=$1;;
    161  1.1  christos   *) day=$3; shift;;
    162  1.1  christos esac
    163  1.1  christos 
    164  1.1  christos # Here we have to deal with the problem that the ls output gives either
    165  1.1  christos # the time of day or the year.
    166  1.1  christos case $3 in
    167  1.1  christos   *:*) set `date`; eval year=\$$#
    168  1.1  christos        case $2 in
    169  1.1  christos 	 Jan) nummonthtod=1;;
    170  1.1  christos 	 Feb) nummonthtod=2;;
    171  1.1  christos 	 Mar) nummonthtod=3;;
    172  1.1  christos 	 Apr) nummonthtod=4;;
    173  1.1  christos 	 May) nummonthtod=5;;
    174  1.1  christos 	 Jun) nummonthtod=6;;
    175  1.1  christos 	 Jul) nummonthtod=7;;
    176  1.1  christos 	 Aug) nummonthtod=8;;
    177  1.1  christos 	 Sep) nummonthtod=9;;
    178  1.1  christos 	 Oct) nummonthtod=10;;
    179  1.1  christos 	 Nov) nummonthtod=11;;
    180  1.1  christos 	 Dec) nummonthtod=12;;
    181  1.1  christos        esac
    182  1.1  christos        # For the first six month of the year the time notation can also
    183  1.1  christos        # be used for files modified in the last year.
    184  1.1  christos        if (expr $nummonth \> $nummonthtod) > /dev/null;
    185  1.1  christos        then
    186  1.1  christos 	 year=`expr $year - 1`
    187  1.1  christos        fi;;
    188  1.1  christos   *) year=$3;;
    189  1.1  christos esac
    190  1.1  christos 
    191  1.1  christos # The result.
    192  1.1  christos echo $day $month $year
    193  1.1  christos 
    194  1.1  christos # Local Variables:
    195  1.1  christos # mode: shell-script
    196  1.1  christos # sh-indentation: 2
    197  1.1  christos # eval: (add-hook 'write-file-hooks 'time-stamp)
    198  1.1  christos # time-stamp-start: "scriptversion="
    199  1.1  christos # time-stamp-format: "%:y-%02m-%02d.%02H"
    200  1.1  christos # time-stamp-end: "$"
    201  1.1  christos # End:
    202