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