1#!/bin/sh
2# Get modification time of a file or directory and pretty-print it.
3
4scriptversion=2018-03-07.03; # UTC
5
6# Copyright (C) 1995-2018 Free Software Foundation, Inc.
7# written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, June 1995
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2, or (at your option)
12# any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22# As a special exception to the GNU General Public License, if you
23# distribute this file as part of a program that contains a
24# configuration script generated by Autoconf, you may include it under
25# the same distribution terms that you use for the rest of that program.
26
27# This file is maintained in Automake, please report
28# bugs to <bug-automake@gnu.org> or send patches to
29# <automake-patches@gnu.org>.
30
31if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
32  emulate sh
33  NULLCMD=:
34  # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
35  # is contrary to our usage.  Disable this feature.
36  alias -g '${1+"$@"}'='"$@"'
37  setopt NO_GLOB_SUBST
38fi
39
40case $1 in
41  '')
42     echo "$0: No file.  Try '$0 --help' for more information." 1>&2
43     exit 1;
44     ;;
45  -h | --h*)
46    cat <<\EOF
47Usage: mdate-sh [--help] [--version] FILE
48
49Pretty-print the modification day of FILE, in the format:
501 January 1970
51
52Report bugs to <bug-automake@gnu.org>.
53EOF
54    exit $?
55    ;;
56  -v | --v*)
57    echo "mdate-sh $scriptversion"
58    exit $?
59    ;;
60esac
61
62error ()
63{
64  echo "$0: $1" >&2
65  exit 1
66}
67
68
69# Prevent date giving response in another language.
70LANG=C
71export LANG
72LC_ALL=C
73export LC_ALL
74LC_TIME=C
75export LC_TIME
76
77# Use UTC to get reproducible result.
78TZ=UTC0
79export TZ
80
81# GNU ls changes its time format in response to the TIME_STYLE
82# variable.  Since we cannot assume 'unset' works, revert this
83# variable to its documented default.
84if test "${TIME_STYLE+set}" = set; then
85  TIME_STYLE=posix-long-iso
86  export TIME_STYLE
87fi
88
89save_arg1=$1
90
91# Find out how to get the extended ls output of a file or directory.
92if ls -L /dev/null 1>/dev/null 2>&1; then
93  ls_command='ls -L -l -d'
94else
95  ls_command='ls -l -d'
96fi
97# Avoid user/group names that might have spaces, when possible.
98if ls -n /dev/null 1>/dev/null 2>&1; then
99  ls_command="$ls_command -n"
100fi
101
102# A 'ls -l' line looks as follows on OS/2.
103#  drwxrwx---        0 Aug 11  2001 foo
104# This differs from Unix, which adds ownership information.
105#  drwxrwx---   2 root  root      4096 Aug 11  2001 foo
106#
107# To find the date, we split the line on spaces and iterate on words
108# until we find a month.  This cannot work with files whose owner is a
109# user named "Jan", or "Feb", etc.  However, it's unlikely that '/'
110# will be owned by a user whose name is a month.  So we first look at
111# the extended ls output of the root directory to decide how many
112# words should be skipped to get the date.
113
114# On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
115set x`$ls_command /`
116
117# Find which argument is the month.
118month=
119command=
120until test $month
121do
122  test $# -gt 0 || error "failed parsing '$ls_command /' output"
123  shift
124  # Add another shift to the command.
125  command="$command shift;"
126  case $1 in
127    Jan) month=January; nummonth=1;;
128    Feb) month=February; nummonth=2;;
129    Mar) month=March; nummonth=3;;
130    Apr) month=April; nummonth=4;;
131    May) month=May; nummonth=5;;
132    Jun) month=June; nummonth=6;;
133    Jul) month=July; nummonth=7;;
134    Aug) month=August; nummonth=8;;
135    Sep) month=September; nummonth=9;;
136    Oct) month=October; nummonth=10;;
137    Nov) month=November; nummonth=11;;
138    Dec) month=December; nummonth=12;;
139  esac
140done
141
142test -n "$month" || error "failed parsing '$ls_command /' output"
143
144# Get the extended ls output of the file or directory.
145set dummy x`eval "$ls_command \"\\\$save_arg1\""`
146
147# Remove all preceding arguments
148eval $command
149
150# Because of the dummy argument above, month is in $2.
151#
152# On a POSIX system, we should have
153#
154# $# = 5
155# $1 = file size
156# $2 = month
157# $3 = day
158# $4 = year or time
159# $5 = filename
160#
161# On Darwin 7.7.0 and 7.6.0, we have
162#
163# $# = 4
164# $1 = day
165# $2 = month
166# $3 = year or time
167# $4 = filename
168
169# Get the month.
170case $2 in
171  Jan) month=January; nummonth=1;;
172  Feb) month=February; nummonth=2;;
173  Mar) month=March; nummonth=3;;
174  Apr) month=April; nummonth=4;;
175  May) month=May; nummonth=5;;
176  Jun) month=June; nummonth=6;;
177  Jul) month=July; nummonth=7;;
178  Aug) month=August; nummonth=8;;
179  Sep) month=September; nummonth=9;;
180  Oct) month=October; nummonth=10;;
181  Nov) month=November; nummonth=11;;
182  Dec) month=December; nummonth=12;;
183esac
184
185case $3 in
186  ???*) day=$1;;
187  *) day=$3; shift;;
188esac
189
190# Here we have to deal with the problem that the ls output gives either
191# the time of day or the year.
192case $3 in
193  *:*) set `date`; eval year=\$$#
194       case $2 in
195	 Jan) nummonthtod=1;;
196	 Feb) nummonthtod=2;;
197	 Mar) nummonthtod=3;;
198	 Apr) nummonthtod=4;;
199	 May) nummonthtod=5;;
200	 Jun) nummonthtod=6;;
201	 Jul) nummonthtod=7;;
202	 Aug) nummonthtod=8;;
203	 Sep) nummonthtod=9;;
204	 Oct) nummonthtod=10;;
205	 Nov) nummonthtod=11;;
206	 Dec) nummonthtod=12;;
207       esac
208       # For the first six month of the year the time notation can also
209       # be used for files modified in the last year.
210       if (expr $nummonth \> $nummonthtod) > /dev/null;
211       then
212	 year=`expr $year - 1`
213       fi;;
214  *) year=$3;;
215esac
216
217# The result.
218echo $day $month $year
219
220# Local Variables:
221# mode: shell-script
222# sh-indentation: 2
223# eval: (add-hook 'before-save-hook 'time-stamp)
224# time-stamp-start: "scriptversion="
225# time-stamp-format: "%:y-%02m-%02d.%02H"
226# time-stamp-time-zone: "UTC0"
227# time-stamp-end: "; # UTC"
228# End:
229