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