xref: /netbsd-src/external/bsd/flex/dist/build-aux/mdate-sh (revision 56bd85463476f90bb489799c99292bb30d6771c0)
130da1778Schristos#!/bin/sh
230da1778Schristos# Get modification time of a file or directory and pretty-print it.
330da1778Schristos
4*56bd8546Schristosscriptversion=2015-04-09.19; # UTC
530da1778Schristos
630da1778Schristos# Copyright (C) 1995-2014 Free Software Foundation, Inc.
730da1778Schristos# written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, June 1995
830da1778Schristos#
930da1778Schristos# This program is free software; you can redistribute it and/or modify
1030da1778Schristos# it under the terms of the GNU General Public License as published by
1130da1778Schristos# the Free Software Foundation; either version 2, or (at your option)
1230da1778Schristos# any later version.
1330da1778Schristos#
1430da1778Schristos# This program is distributed in the hope that it will be useful,
1530da1778Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
1630da1778Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1730da1778Schristos# GNU General Public License for more details.
1830da1778Schristos#
1930da1778Schristos# You should have received a copy of the GNU General Public License
2030da1778Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2130da1778Schristos
2230da1778Schristos# As a special exception to the GNU General Public License, if you
2330da1778Schristos# distribute this file as part of a program that contains a
2430da1778Schristos# configuration script generated by Autoconf, you may include it under
2530da1778Schristos# the same distribution terms that you use for the rest of that program.
2630da1778Schristos
2730da1778Schristos# This file is maintained in Automake, please report
2830da1778Schristos# bugs to <bug-automake@gnu.org> or send patches to
2930da1778Schristos# <automake-patches@gnu.org>.
3030da1778Schristos
3130da1778Schristosif test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
3230da1778Schristos  emulate sh
3330da1778Schristos  NULLCMD=:
3430da1778Schristos  # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
3530da1778Schristos  # is contrary to our usage.  Disable this feature.
3630da1778Schristos  alias -g '${1+"$@"}'='"$@"'
3730da1778Schristos  setopt NO_GLOB_SUBST
3830da1778Schristosfi
3930da1778Schristos
4030da1778Schristoscase $1 in
4130da1778Schristos  '')
4230da1778Schristos     echo "$0: No file.  Try '$0 --help' for more information." 1>&2
4330da1778Schristos     exit 1;
4430da1778Schristos     ;;
4530da1778Schristos  -h | --h*)
4630da1778Schristos    cat <<\EOF
4730da1778SchristosUsage: mdate-sh [--help] [--version] FILE
4830da1778Schristos
4930da1778SchristosPretty-print the modification day of FILE, in the format:
5030da1778Schristos1 January 1970
5130da1778Schristos
5230da1778SchristosReport bugs to <bug-automake@gnu.org>.
5330da1778SchristosEOF
5430da1778Schristos    exit $?
5530da1778Schristos    ;;
5630da1778Schristos  -v | --v*)
5730da1778Schristos    echo "mdate-sh $scriptversion"
5830da1778Schristos    exit $?
5930da1778Schristos    ;;
6030da1778Schristosesac
6130da1778Schristos
6230da1778Schristoserror ()
6330da1778Schristos{
6430da1778Schristos  echo "$0: $1" >&2
6530da1778Schristos  exit 1
6630da1778Schristos}
6730da1778Schristos
6830da1778Schristos
6930da1778Schristos# Prevent date giving response in another language.
7030da1778SchristosLANG=C
7130da1778Schristosexport LANG
7230da1778SchristosLC_ALL=C
7330da1778Schristosexport LC_ALL
7430da1778SchristosLC_TIME=C
7530da1778Schristosexport LC_TIME
7630da1778Schristos
77*56bd8546Schristos# Use UTC to get reproducible result
78*56bd8546SchristosTZ=UTC
79*56bd8546Schristosexport TZ
80*56bd8546Schristos
8130da1778Schristos# GNU ls changes its time format in response to the TIME_STYLE
8230da1778Schristos# variable.  Since we cannot assume 'unset' works, revert this
8330da1778Schristos# variable to its documented default.
8430da1778Schristosif test "${TIME_STYLE+set}" = set; then
8530da1778Schristos  TIME_STYLE=posix-long-iso
8630da1778Schristos  export TIME_STYLE
8730da1778Schristosfi
8830da1778Schristos
8930da1778Schristossave_arg1=$1
9030da1778Schristos
9130da1778Schristos# Find out how to get the extended ls output of a file or directory.
9230da1778Schristosif ls -L /dev/null 1>/dev/null 2>&1; then
9330da1778Schristos  ls_command='ls -L -l -d'
9430da1778Schristoselse
9530da1778Schristos  ls_command='ls -l -d'
9630da1778Schristosfi
9730da1778Schristos# Avoid user/group names that might have spaces, when possible.
9830da1778Schristosif ls -n /dev/null 1>/dev/null 2>&1; then
9930da1778Schristos  ls_command="$ls_command -n"
10030da1778Schristosfi
10130da1778Schristos
10230da1778Schristos# A 'ls -l' line looks as follows on OS/2.
10330da1778Schristos#  drwxrwx---        0 Aug 11  2001 foo
10430da1778Schristos# This differs from Unix, which adds ownership information.
10530da1778Schristos#  drwxrwx---   2 root  root      4096 Aug 11  2001 foo
10630da1778Schristos#
10730da1778Schristos# To find the date, we split the line on spaces and iterate on words
10830da1778Schristos# until we find a month.  This cannot work with files whose owner is a
10930da1778Schristos# user named "Jan", or "Feb", etc.  However, it's unlikely that '/'
11030da1778Schristos# will be owned by a user whose name is a month.  So we first look at
11130da1778Schristos# the extended ls output of the root directory to decide how many
11230da1778Schristos# words should be skipped to get the date.
11330da1778Schristos
11430da1778Schristos# On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
11530da1778Schristosset x`$ls_command /`
11630da1778Schristos
11730da1778Schristos# Find which argument is the month.
11830da1778Schristosmonth=
11930da1778Schristoscommand=
12030da1778Schristosuntil test $month
12130da1778Schristosdo
12230da1778Schristos  test $# -gt 0 || error "failed parsing '$ls_command /' output"
12330da1778Schristos  shift
12430da1778Schristos  # Add another shift to the command.
12530da1778Schristos  command="$command shift;"
12630da1778Schristos  case $1 in
12730da1778Schristos    Jan) month=January; nummonth=1;;
12830da1778Schristos    Feb) month=February; nummonth=2;;
12930da1778Schristos    Mar) month=March; nummonth=3;;
13030da1778Schristos    Apr) month=April; nummonth=4;;
13130da1778Schristos    May) month=May; nummonth=5;;
13230da1778Schristos    Jun) month=June; nummonth=6;;
13330da1778Schristos    Jul) month=July; nummonth=7;;
13430da1778Schristos    Aug) month=August; nummonth=8;;
13530da1778Schristos    Sep) month=September; nummonth=9;;
13630da1778Schristos    Oct) month=October; nummonth=10;;
13730da1778Schristos    Nov) month=November; nummonth=11;;
13830da1778Schristos    Dec) month=December; nummonth=12;;
13930da1778Schristos  esac
14030da1778Schristosdone
14130da1778Schristos
14230da1778Schristostest -n "$month" || error "failed parsing '$ls_command /' output"
14330da1778Schristos
14430da1778Schristos# Get the extended ls output of the file or directory.
14530da1778Schristosset dummy x`eval "$ls_command \"\\\$save_arg1\""`
14630da1778Schristos
14730da1778Schristos# Remove all preceding arguments
14830da1778Schristoseval $command
14930da1778Schristos
15030da1778Schristos# Because of the dummy argument above, month is in $2.
15130da1778Schristos#
15230da1778Schristos# On a POSIX system, we should have
15330da1778Schristos#
15430da1778Schristos# $# = 5
15530da1778Schristos# $1 = file size
15630da1778Schristos# $2 = month
15730da1778Schristos# $3 = day
15830da1778Schristos# $4 = year or time
15930da1778Schristos# $5 = filename
16030da1778Schristos#
16130da1778Schristos# On Darwin 7.7.0 and 7.6.0, we have
16230da1778Schristos#
16330da1778Schristos# $# = 4
16430da1778Schristos# $1 = day
16530da1778Schristos# $2 = month
16630da1778Schristos# $3 = year or time
16730da1778Schristos# $4 = filename
16830da1778Schristos
16930da1778Schristos# Get the month.
17030da1778Schristoscase $2 in
17130da1778Schristos  Jan) month=January; nummonth=1;;
17230da1778Schristos  Feb) month=February; nummonth=2;;
17330da1778Schristos  Mar) month=March; nummonth=3;;
17430da1778Schristos  Apr) month=April; nummonth=4;;
17530da1778Schristos  May) month=May; nummonth=5;;
17630da1778Schristos  Jun) month=June; nummonth=6;;
17730da1778Schristos  Jul) month=July; nummonth=7;;
17830da1778Schristos  Aug) month=August; nummonth=8;;
17930da1778Schristos  Sep) month=September; nummonth=9;;
18030da1778Schristos  Oct) month=October; nummonth=10;;
18130da1778Schristos  Nov) month=November; nummonth=11;;
18230da1778Schristos  Dec) month=December; nummonth=12;;
18330da1778Schristosesac
18430da1778Schristos
18530da1778Schristoscase $3 in
18630da1778Schristos  ???*) day=$1;;
18730da1778Schristos  *) day=$3; shift;;
18830da1778Schristosesac
18930da1778Schristos
19030da1778Schristos# Here we have to deal with the problem that the ls output gives either
19130da1778Schristos# the time of day or the year.
19230da1778Schristoscase $3 in
19330da1778Schristos  *:*) set `date`; eval year=\$$#
19430da1778Schristos       case $2 in
19530da1778Schristos	 Jan) nummonthtod=1;;
19630da1778Schristos	 Feb) nummonthtod=2;;
19730da1778Schristos	 Mar) nummonthtod=3;;
19830da1778Schristos	 Apr) nummonthtod=4;;
19930da1778Schristos	 May) nummonthtod=5;;
20030da1778Schristos	 Jun) nummonthtod=6;;
20130da1778Schristos	 Jul) nummonthtod=7;;
20230da1778Schristos	 Aug) nummonthtod=8;;
20330da1778Schristos	 Sep) nummonthtod=9;;
20430da1778Schristos	 Oct) nummonthtod=10;;
20530da1778Schristos	 Nov) nummonthtod=11;;
20630da1778Schristos	 Dec) nummonthtod=12;;
20730da1778Schristos       esac
20830da1778Schristos       # For the first six month of the year the time notation can also
20930da1778Schristos       # be used for files modified in the last year.
21030da1778Schristos       if (expr $nummonth \> $nummonthtod) > /dev/null;
21130da1778Schristos       then
21230da1778Schristos	 year=`expr $year - 1`
21330da1778Schristos       fi;;
21430da1778Schristos  *) year=$3;;
21530da1778Schristosesac
21630da1778Schristos
21730da1778Schristos# The result.
21830da1778Schristosecho $day $month $year
21930da1778Schristos
22030da1778Schristos# Local Variables:
22130da1778Schristos# mode: shell-script
22230da1778Schristos# sh-indentation: 2
22330da1778Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
22430da1778Schristos# time-stamp-start: "scriptversion="
22530da1778Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
22630da1778Schristos# time-stamp-time-zone: "UTC"
22730da1778Schristos# time-stamp-end: "; # UTC"
22830da1778Schristos# End:
229