mdate-sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/sh
  2. # Get modification time of a file or directory and pretty-print it.
  3. # Copyright (C) 1995, 1996, 1997, 2003 Free Software Foundation, Inc.
  4. # written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, June 1995
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software Foundation,
  18. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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. # Prevent date giving response in another language.
  24. LANG=C
  25. export LANG
  26. LC_ALL=C
  27. export LC_ALL
  28. LC_TIME=C
  29. export LC_TIME
  30. save_arg1="$1"
  31. # Find out how to get the extended ls output of a file or directory.
  32. if ls -L /dev/null 1>/dev/null 2>&1; then
  33. ls_command='ls -L -l -d'
  34. else
  35. ls_command='ls -l -d'
  36. fi
  37. # A `ls -l' line looks as follows on OS/2.
  38. # drwxrwx--- 0 Aug 11 2001 foo
  39. # This differs from Unix, which adds ownership information.
  40. # drwxrwx--- 2 root root 4096 Aug 11 2001 foo
  41. #
  42. # To find the date, we split the line on spaces and iterate on words
  43. # until we find a month. This cannot work with files whose owner is a
  44. # user named `Jan', or `Feb', etc. However, it's unlikely that `/'
  45. # will be owned by a user whose name is a month. So we first look at
  46. # the extended ls output of the root directory to decide how many
  47. # words should be skipped to get the date.
  48. # On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
  49. set - x`$ls_command /`
  50. # Find which argument is the month.
  51. month=
  52. command=
  53. until test $month
  54. do
  55. shift
  56. # Add another shift to the command.
  57. command="$command shift;"
  58. case $1 in
  59. Jan) month=January; nummonth=1;;
  60. Feb) month=February; nummonth=2;;
  61. Mar) month=March; nummonth=3;;
  62. Apr) month=April; nummonth=4;;
  63. May) month=May; nummonth=5;;
  64. Jun) month=June; nummonth=6;;
  65. Jul) month=July; nummonth=7;;
  66. Aug) month=August; nummonth=8;;
  67. Sep) month=September; nummonth=9;;
  68. Oct) month=October; nummonth=10;;
  69. Nov) month=November; nummonth=11;;
  70. Dec) month=December; nummonth=12;;
  71. esac
  72. done
  73. # Get the extended ls output of the file or directory.
  74. set - x`eval "$ls_command \"\$save_arg1\""`
  75. # Remove all preceding arguments
  76. eval $command
  77. # Get the month. Next argument is day, followed by the year or time.
  78. case $1 in
  79. Jan) month=January; nummonth=1;;
  80. Feb) month=February; nummonth=2;;
  81. Mar) month=March; nummonth=3;;
  82. Apr) month=April; nummonth=4;;
  83. May) month=May; nummonth=5;;
  84. Jun) month=June; nummonth=6;;
  85. Jul) month=July; nummonth=7;;
  86. Aug) month=August; nummonth=8;;
  87. Sep) month=September; nummonth=9;;
  88. Oct) month=October; nummonth=10;;
  89. Nov) month=November; nummonth=11;;
  90. Dec) month=December; nummonth=12;;
  91. esac
  92. day=$2
  93. # Here we have to deal with the problem that the ls output gives either
  94. # the time of day or the year.
  95. case $3 in
  96. *:*) set `date`; eval year=\$$#
  97. case $2 in
  98. Jan) nummonthtod=1;;
  99. Feb) nummonthtod=2;;
  100. Mar) nummonthtod=3;;
  101. Apr) nummonthtod=4;;
  102. May) nummonthtod=5;;
  103. Jun) nummonthtod=6;;
  104. Jul) nummonthtod=7;;
  105. Aug) nummonthtod=8;;
  106. Sep) nummonthtod=9;;
  107. Oct) nummonthtod=10;;
  108. Nov) nummonthtod=11;;
  109. Dec) nummonthtod=12;;
  110. esac
  111. # For the first six month of the year the time notation can also
  112. # be used for files modified in the last year.
  113. if (expr $nummonth \> $nummonthtod) > /dev/null;
  114. then
  115. year=`expr $year - 1`
  116. fi;;
  117. *) year=$3;;
  118. esac
  119. # The result.
  120. echo $day $month $year