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