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