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