xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/doc/mdate-sh (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
14f645668Schristos#!/bin/sh
24f645668Schristos# Get modification time of a file or directory and pretty-print it.
34f645668Schristos
44f645668Schristosscriptversion=2016-01-11.22; # UTC
54f645668Schristos
6*cb63e24eSchristos# Copyright (C) 1995-2024 Free Software Foundation, Inc.
74f645668Schristos# written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, June 1995
84f645668Schristos#
94f645668Schristos# This program is free software; you can redistribute it and/or modify
104f645668Schristos# it under the terms of the GNU General Public License as published by
114f645668Schristos# the Free Software Foundation; either version 2, or (at your option)
124f645668Schristos# any later version.
134f645668Schristos#
144f645668Schristos# This program is distributed in the hope that it will be useful,
154f645668Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
164f645668Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
174f645668Schristos# GNU General Public License for more details.
184f645668Schristos#
194f645668Schristos# You should have received a copy of the GNU General Public License
204f645668Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
214f645668Schristos
224f645668Schristos# As a special exception to the GNU General Public License, if you
234f645668Schristos# distribute this file as part of a program that contains a
244f645668Schristos# configuration script generated by Autoconf, you may include it under
254f645668Schristos# the same distribution terms that you use for the rest of that program.
264f645668Schristos
274f645668Schristos# This file is maintained in Automake, please report
284f645668Schristos# bugs to <bug-automake@gnu.org> or send patches to
294f645668Schristos# <automake-patches@gnu.org>.
304f645668Schristos
314f645668Schristosif test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
324f645668Schristos  emulate sh
334f645668Schristos  NULLCMD=:
344f645668Schristos  # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
354f645668Schristos  # is contrary to our usage.  Disable this feature.
364f645668Schristos  alias -g '${1+"$@"}'='"$@"'
374f645668Schristos  setopt NO_GLOB_SUBST
384f645668Schristosfi
394f645668Schristos
404f645668Schristoscase $1 in
414f645668Schristos  '')
424f645668Schristos     echo "$0: No file.  Try '$0 --help' for more information." 1>&2
434f645668Schristos     exit 1;
444f645668Schristos     ;;
454f645668Schristos  -h | --h*)
464f645668Schristos    cat <<\EOF
474f645668SchristosUsage: mdate-sh [--help] [--version] FILE
484f645668Schristos
494f645668SchristosPretty-print the modification day of FILE, in the format:
504f645668Schristos1 January 1970
514f645668Schristos
524f645668SchristosReport bugs to <bug-automake@gnu.org>.
534f645668SchristosEOF
544f645668Schristos    exit $?
554f645668Schristos    ;;
564f645668Schristos  -v | --v*)
574f645668Schristos    echo "mdate-sh $scriptversion"
584f645668Schristos    exit $?
594f645668Schristos    ;;
604f645668Schristosesac
614f645668Schristos
624f645668Schristoserror ()
634f645668Schristos{
644f645668Schristos  echo "$0: $1" >&2
654f645668Schristos  exit 1
664f645668Schristos}
674f645668Schristos
684f645668Schristos
694f645668Schristos# Prevent date giving response in another language.
704f645668SchristosLANG=C
714f645668Schristosexport LANG
724f645668SchristosLC_ALL=C
734f645668Schristosexport LC_ALL
744f645668SchristosLC_TIME=C
754f645668Schristosexport LC_TIME
764f645668Schristos
774f645668Schristos# GNU ls changes its time format in response to the TIME_STYLE
784f645668Schristos# variable.  Since we cannot assume 'unset' works, revert this
794f645668Schristos# variable to its documented default.
804f645668Schristosif test "${TIME_STYLE+set}" = set; then
814f645668Schristos  TIME_STYLE=posix-long-iso
824f645668Schristos  export TIME_STYLE
834f645668Schristosfi
844f645668Schristos
854f645668Schristossave_arg1=$1
864f645668Schristos
874f645668Schristos# Find out how to get the extended ls output of a file or directory.
884f645668Schristosif ls -L /dev/null 1>/dev/null 2>&1; then
894f645668Schristos  ls_command='ls -L -l -d'
904f645668Schristoselse
914f645668Schristos  ls_command='ls -l -d'
924f645668Schristosfi
934f645668Schristos# Avoid user/group names that might have spaces, when possible.
944f645668Schristosif ls -n /dev/null 1>/dev/null 2>&1; then
954f645668Schristos  ls_command="$ls_command -n"
964f645668Schristosfi
974f645668Schristos
984f645668Schristos# A 'ls -l' line looks as follows on OS/2.
994f645668Schristos#  drwxrwx---        0 Aug 11  2001 foo
1004f645668Schristos# This differs from Unix, which adds ownership information.
1014f645668Schristos#  drwxrwx---   2 root  root      4096 Aug 11  2001 foo
1024f645668Schristos#
1034f645668Schristos# To find the date, we split the line on spaces and iterate on words
1044f645668Schristos# until we find a month.  This cannot work with files whose owner is a
1054f645668Schristos# user named "Jan", or "Feb", etc.  However, it's unlikely that '/'
1064f645668Schristos# will be owned by a user whose name is a month.  So we first look at
1074f645668Schristos# the extended ls output of the root directory to decide how many
1084f645668Schristos# words should be skipped to get the date.
1094f645668Schristos
1104f645668Schristos# On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
1114f645668Schristosset x`$ls_command /`
1124f645668Schristos
1134f645668Schristos# Find which argument is the month.
1144f645668Schristosmonth=
1154f645668Schristoscommand=
1164f645668Schristosuntil test $month
1174f645668Schristosdo
1184f645668Schristos  test $# -gt 0 || error "failed parsing '$ls_command /' output"
1194f645668Schristos  shift
1204f645668Schristos  # Add another shift to the command.
1214f645668Schristos  command="$command shift;"
1224f645668Schristos  case $1 in
1234f645668Schristos    Jan) month=January; nummonth=1;;
1244f645668Schristos    Feb) month=February; nummonth=2;;
1254f645668Schristos    Mar) month=March; nummonth=3;;
1264f645668Schristos    Apr) month=April; nummonth=4;;
1274f645668Schristos    May) month=May; nummonth=5;;
1284f645668Schristos    Jun) month=June; nummonth=6;;
1294f645668Schristos    Jul) month=July; nummonth=7;;
1304f645668Schristos    Aug) month=August; nummonth=8;;
1314f645668Schristos    Sep) month=September; nummonth=9;;
1324f645668Schristos    Oct) month=October; nummonth=10;;
1334f645668Schristos    Nov) month=November; nummonth=11;;
1344f645668Schristos    Dec) month=December; nummonth=12;;
1354f645668Schristos  esac
1364f645668Schristosdone
1374f645668Schristos
1384f645668Schristostest -n "$month" || error "failed parsing '$ls_command /' output"
1394f645668Schristos
1404f645668Schristos# Get the extended ls output of the file or directory.
1414f645668Schristosset dummy x`eval "$ls_command \"\\\$save_arg1\""`
1424f645668Schristos
1434f645668Schristos# Remove all preceding arguments
1444f645668Schristoseval $command
1454f645668Schristos
1464f645668Schristos# Because of the dummy argument above, month is in $2.
1474f645668Schristos#
1484f645668Schristos# On a POSIX system, we should have
1494f645668Schristos#
1504f645668Schristos# $# = 5
1514f645668Schristos# $1 = file size
1524f645668Schristos# $2 = month
1534f645668Schristos# $3 = day
1544f645668Schristos# $4 = year or time
1554f645668Schristos# $5 = filename
1564f645668Schristos#
1574f645668Schristos# On Darwin 7.7.0 and 7.6.0, we have
1584f645668Schristos#
1594f645668Schristos# $# = 4
1604f645668Schristos# $1 = day
1614f645668Schristos# $2 = month
1624f645668Schristos# $3 = year or time
1634f645668Schristos# $4 = filename
1644f645668Schristos
1654f645668Schristos# Get the month.
1664f645668Schristoscase $2 in
1674f645668Schristos  Jan) month=January; nummonth=1;;
1684f645668Schristos  Feb) month=February; nummonth=2;;
1694f645668Schristos  Mar) month=March; nummonth=3;;
1704f645668Schristos  Apr) month=April; nummonth=4;;
1714f645668Schristos  May) month=May; nummonth=5;;
1724f645668Schristos  Jun) month=June; nummonth=6;;
1734f645668Schristos  Jul) month=July; nummonth=7;;
1744f645668Schristos  Aug) month=August; nummonth=8;;
1754f645668Schristos  Sep) month=September; nummonth=9;;
1764f645668Schristos  Oct) month=October; nummonth=10;;
1774f645668Schristos  Nov) month=November; nummonth=11;;
1784f645668Schristos  Dec) month=December; nummonth=12;;
1794f645668Schristosesac
1804f645668Schristos
1814f645668Schristoscase $3 in
1824f645668Schristos  ???*) day=$1;;
1834f645668Schristos  *) day=$3; shift;;
1844f645668Schristosesac
1854f645668Schristos
1864f645668Schristos# Here we have to deal with the problem that the ls output gives either
1874f645668Schristos# the time of day or the year.
1884f645668Schristoscase $3 in
1894f645668Schristos  *:*) set `date`; eval year=\$$#
1904f645668Schristos       case $2 in
1914f645668Schristos	 Jan) nummonthtod=1;;
1924f645668Schristos	 Feb) nummonthtod=2;;
1934f645668Schristos	 Mar) nummonthtod=3;;
1944f645668Schristos	 Apr) nummonthtod=4;;
1954f645668Schristos	 May) nummonthtod=5;;
1964f645668Schristos	 Jun) nummonthtod=6;;
1974f645668Schristos	 Jul) nummonthtod=7;;
1984f645668Schristos	 Aug) nummonthtod=8;;
1994f645668Schristos	 Sep) nummonthtod=9;;
2004f645668Schristos	 Oct) nummonthtod=10;;
2014f645668Schristos	 Nov) nummonthtod=11;;
2024f645668Schristos	 Dec) nummonthtod=12;;
2034f645668Schristos       esac
2044f645668Schristos       # For the first six month of the year the time notation can also
2054f645668Schristos       # be used for files modified in the last year.
2064f645668Schristos       if (expr $nummonth \> $nummonthtod) > /dev/null;
2074f645668Schristos       then
2084f645668Schristos	 year=`expr $year - 1`
2094f645668Schristos       fi;;
2104f645668Schristos  *) year=$3;;
2114f645668Schristosesac
2124f645668Schristos
2134f645668Schristos# The result.
2144f645668Schristosecho $day $month $year
2154f645668Schristos
2164f645668Schristos# Local Variables:
2174f645668Schristos# mode: shell-script
2184f645668Schristos# sh-indentation: 2
2194f645668Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
2204f645668Schristos# time-stamp-start: "scriptversion="
2214f645668Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
2224f645668Schristos# time-stamp-time-zone: "UTC0"
2234f645668Schristos# time-stamp-end: "; # UTC"
2244f645668Schristos# End:
225