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