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