cannam@167: #!/bin/sh cannam@167: # Get modification time of a file or directory and pretty-print it. cannam@167: cannam@167: scriptversion=2015-04-09.19; # UTC cannam@167: cannam@167: # Copyright (C) 1995-2014 Free Software Foundation, Inc. cannam@167: # written by Ulrich Drepper , June 1995 cannam@167: # cannam@167: # This program is free software; you can redistribute it and/or modify cannam@167: # it under the terms of the GNU General Public License as published by cannam@167: # the Free Software Foundation; either version 2, or (at your option) cannam@167: # any later version. cannam@167: # cannam@167: # This program is distributed in the hope that it will be useful, cannam@167: # but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@167: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@167: # GNU General Public License for more details. cannam@167: # cannam@167: # You should have received a copy of the GNU General Public License cannam@167: # along with this program. If not, see . cannam@167: cannam@167: # As a special exception to the GNU General Public License, if you cannam@167: # distribute this file as part of a program that contains a cannam@167: # configuration script generated by Autoconf, you may include it under cannam@167: # the same distribution terms that you use for the rest of that program. cannam@167: cannam@167: # This file is maintained in Automake, please report cannam@167: # bugs to or send patches to cannam@167: # . cannam@167: cannam@167: if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then cannam@167: emulate sh cannam@167: NULLCMD=: cannam@167: # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which cannam@167: # is contrary to our usage. Disable this feature. cannam@167: alias -g '${1+"$@"}'='"$@"' cannam@167: setopt NO_GLOB_SUBST cannam@167: fi cannam@167: cannam@167: case $1 in cannam@167: '') cannam@167: echo "$0: No file. Try '$0 --help' for more information." 1>&2 cannam@167: exit 1; cannam@167: ;; cannam@167: -h | --h*) cannam@167: cat <<\EOF cannam@167: Usage: mdate-sh [--help] [--version] FILE cannam@167: cannam@167: Pretty-print the modification day of FILE, in the format: cannam@167: 1 January 1970 cannam@167: cannam@167: Report bugs to . cannam@167: EOF cannam@167: exit $? cannam@167: ;; cannam@167: -v | --v*) cannam@167: echo "mdate-sh $scriptversion" cannam@167: exit $? cannam@167: ;; cannam@167: esac cannam@167: cannam@167: error () cannam@167: { cannam@167: echo "$0: $1" >&2 cannam@167: exit 1 cannam@167: } cannam@167: cannam@167: cannam@167: # Prevent date giving response in another language. cannam@167: LANG=C cannam@167: export LANG cannam@167: LC_ALL=C cannam@167: export LC_ALL cannam@167: LC_TIME=C cannam@167: export LC_TIME cannam@167: cannam@167: # Use UTC to get reproducible result cannam@167: TZ=UTC cannam@167: export TZ cannam@167: cannam@167: # GNU ls changes its time format in response to the TIME_STYLE cannam@167: # variable. Since we cannot assume 'unset' works, revert this cannam@167: # variable to its documented default. cannam@167: if test "${TIME_STYLE+set}" = set; then cannam@167: TIME_STYLE=posix-long-iso cannam@167: export TIME_STYLE cannam@167: fi cannam@167: cannam@167: save_arg1=$1 cannam@167: cannam@167: # Find out how to get the extended ls output of a file or directory. cannam@167: if ls -L /dev/null 1>/dev/null 2>&1; then cannam@167: ls_command='ls -L -l -d' cannam@167: else cannam@167: ls_command='ls -l -d' cannam@167: fi cannam@167: # Avoid user/group names that might have spaces, when possible. cannam@167: if ls -n /dev/null 1>/dev/null 2>&1; then cannam@167: ls_command="$ls_command -n" cannam@167: fi cannam@167: cannam@167: # A 'ls -l' line looks as follows on OS/2. cannam@167: # drwxrwx--- 0 Aug 11 2001 foo cannam@167: # This differs from Unix, which adds ownership information. cannam@167: # drwxrwx--- 2 root root 4096 Aug 11 2001 foo cannam@167: # cannam@167: # To find the date, we split the line on spaces and iterate on words cannam@167: # until we find a month. This cannot work with files whose owner is a cannam@167: # user named "Jan", or "Feb", etc. However, it's unlikely that '/' cannam@167: # will be owned by a user whose name is a month. So we first look at cannam@167: # the extended ls output of the root directory to decide how many cannam@167: # words should be skipped to get the date. cannam@167: cannam@167: # On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below. cannam@167: set x`$ls_command /` cannam@167: cannam@167: # Find which argument is the month. cannam@167: month= cannam@167: command= cannam@167: until test $month cannam@167: do cannam@167: test $# -gt 0 || error "failed parsing '$ls_command /' output" cannam@167: shift cannam@167: # Add another shift to the command. cannam@167: command="$command shift;" cannam@167: case $1 in cannam@167: Jan) month=January; nummonth=1;; cannam@167: Feb) month=February; nummonth=2;; cannam@167: Mar) month=March; nummonth=3;; cannam@167: Apr) month=April; nummonth=4;; cannam@167: May) month=May; nummonth=5;; cannam@167: Jun) month=June; nummonth=6;; cannam@167: Jul) month=July; nummonth=7;; cannam@167: Aug) month=August; nummonth=8;; cannam@167: Sep) month=September; nummonth=9;; cannam@167: Oct) month=October; nummonth=10;; cannam@167: Nov) month=November; nummonth=11;; cannam@167: Dec) month=December; nummonth=12;; cannam@167: esac cannam@167: done cannam@167: cannam@167: test -n "$month" || error "failed parsing '$ls_command /' output" cannam@167: cannam@167: # Get the extended ls output of the file or directory. cannam@167: set dummy x`eval "$ls_command \"\\\$save_arg1\""` cannam@167: cannam@167: # Remove all preceding arguments cannam@167: eval $command cannam@167: cannam@167: # Because of the dummy argument above, month is in $2. cannam@167: # cannam@167: # On a POSIX system, we should have cannam@167: # cannam@167: # $# = 5 cannam@167: # $1 = file size cannam@167: # $2 = month cannam@167: # $3 = day cannam@167: # $4 = year or time cannam@167: # $5 = filename cannam@167: # cannam@167: # On Darwin 7.7.0 and 7.6.0, we have cannam@167: # cannam@167: # $# = 4 cannam@167: # $1 = day cannam@167: # $2 = month cannam@167: # $3 = year or time cannam@167: # $4 = filename cannam@167: cannam@167: # Get the month. cannam@167: case $2 in cannam@167: Jan) month=January; nummonth=1;; cannam@167: Feb) month=February; nummonth=2;; cannam@167: Mar) month=March; nummonth=3;; cannam@167: Apr) month=April; nummonth=4;; cannam@167: May) month=May; nummonth=5;; cannam@167: Jun) month=June; nummonth=6;; cannam@167: Jul) month=July; nummonth=7;; cannam@167: Aug) month=August; nummonth=8;; cannam@167: Sep) month=September; nummonth=9;; cannam@167: Oct) month=October; nummonth=10;; cannam@167: Nov) month=November; nummonth=11;; cannam@167: Dec) month=December; nummonth=12;; cannam@167: esac cannam@167: cannam@167: case $3 in cannam@167: ???*) day=$1;; cannam@167: *) day=$3; shift;; cannam@167: esac cannam@167: cannam@167: # Here we have to deal with the problem that the ls output gives either cannam@167: # the time of day or the year. cannam@167: case $3 in cannam@167: *:*) set `date`; eval year=\$$# cannam@167: case $2 in cannam@167: Jan) nummonthtod=1;; cannam@167: Feb) nummonthtod=2;; cannam@167: Mar) nummonthtod=3;; cannam@167: Apr) nummonthtod=4;; cannam@167: May) nummonthtod=5;; cannam@167: Jun) nummonthtod=6;; cannam@167: Jul) nummonthtod=7;; cannam@167: Aug) nummonthtod=8;; cannam@167: Sep) nummonthtod=9;; cannam@167: Oct) nummonthtod=10;; cannam@167: Nov) nummonthtod=11;; cannam@167: Dec) nummonthtod=12;; cannam@167: esac cannam@167: # For the first six month of the year the time notation can also cannam@167: # be used for files modified in the last year. cannam@167: if (expr $nummonth \> $nummonthtod) > /dev/null; cannam@167: then cannam@167: year=`expr $year - 1` cannam@167: fi;; cannam@167: *) year=$3;; cannam@167: esac cannam@167: cannam@167: # The result. cannam@167: echo $day $month $year cannam@167: cannam@167: # Local Variables: cannam@167: # mode: shell-script cannam@167: # sh-indentation: 2 cannam@167: # eval: (add-hook 'write-file-hooks 'time-stamp) cannam@167: # time-stamp-start: "scriptversion=" cannam@167: # time-stamp-format: "%:y-%02m-%02d.%02H" cannam@167: # time-stamp-time-zone: "UTC" cannam@167: # time-stamp-end: "; # UTC" cannam@167: # End: