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