mdate-sh 5.5 KB

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