Debian Codegraph @

build-aux/mdate-sh

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