mdate-sh 5.5 KB

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