mdate-sh 5.9 KB

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