yesterday 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/rc
  2. rfork e
  3. slashnhack=yes
  4. smon='s/Jan/01/
  5. s/Feb/02/
  6. s/Mar/03/
  7. s/Apr/04/
  8. s/May/05/
  9. s/Jun/06/
  10. s/Jul/07/
  11. s/Aug/08/
  12. s/Sep/09/
  13. s/Oct/10/
  14. s/Nov/11/
  15. s/Dec/12/'
  16. fn usage {
  17. echo 'usage: yesterday [-abcCdDN] [-[[[yy]yy]mm]dd] [-n daysago] file ...' >[1=2]
  18. exit 'usage'
  19. }
  20. fn Xcp {
  21. echo cp $1 $2
  22. cp $1 $2
  23. }
  24. fn Xdiff {
  25. echo diff $1 $2
  26. diff $1 $2
  27. }
  28. fn Xdiffn {
  29. echo diff -n $1 $2
  30. diff -n $1 $2
  31. }
  32. fn Xadiff {
  33. echo /acme/bin/adiff $1 $2
  34. /acme/bin/adiff $1 $2
  35. }
  36. fn Xcarefulcp {
  37. if(! cmp -s $1 $2) Xcp $1 $2
  38. }
  39. fn Xecho {
  40. echo $1
  41. }
  42. fn Xbind {
  43. echo bind $1 $2
  44. bind $1 $2
  45. }
  46. year=`{date|sed 's/.* //'}
  47. copy=Xecho
  48. last=()
  49. defdump=dump
  50. while(! ~ $#* 0 && ~ $1 -* && ! ~ $1 --){
  51. switch($1){
  52. case -a
  53. copy=Xadiff
  54. shift
  55. case -b
  56. copy=Xbind
  57. shift
  58. case -c
  59. copy=Xcp
  60. shift
  61. case -d
  62. copy=Xdiff
  63. shift
  64. case -D
  65. copy=Xdiffn
  66. shift
  67. case -C
  68. copy=Xcarefulcp
  69. shift
  70. case -s
  71. defdump=snap
  72. shift
  73. # The slashnhack is always right.
  74. # case -N
  75. # slashnhack=no
  76. # shift
  77. case -n*
  78. if(~ $1 -n){
  79. if(~ $#* 1)
  80. usage
  81. shift
  82. days=$1
  83. }
  84. if not
  85. days=`{echo $1 | sed 's/^-.//'}
  86. last=`{date `{hoc -e `{date -n} ^ '-'$days'*60*60*24'} | \
  87. sed -e 's%... (...) (..) ..:..:.. ... (....)%\3/\1\2%' -e 'y/ /0/' -e $smon}
  88. shift
  89. case -?
  90. mon=`{date|sed -e 's/^....(...).*/\1/' -e $smon}
  91. last=$year/$mon ^`{echo $1|sed 's/^-/0/'}
  92. shift
  93. case -??
  94. mon=`{date|sed -e 's/^....(...).*/\1/' -e $smon}
  95. last=$year/$mon ^`{echo $1|sed 's/^-//'}
  96. shift
  97. case -????
  98. last=$year/ ^ `{echo $1|sed 's/^-//'}
  99. shift
  100. case -??????
  101. last=`{echo $year|sed 's/..$//'} ^ `{echo $1|sed 's/^-(..)/\1\//'}
  102. shift
  103. case -????????
  104. last=`{echo $1|sed 's/^-(....)/\1\//'}
  105. shift
  106. case *
  107. usage
  108. }
  109. }
  110. if(! ~ $#* 0 && ~ $1 --)
  111. shift
  112. if(~ $#* 0)
  113. usage
  114. dir=`{pwd}
  115. if(! ~ $status ''){
  116. echo 'yesterday: can''t find directory' >[1=2]
  117. exit 'pwd failed'
  118. }
  119. for(i){
  120. xpath=`{cleanname -d $dir -- $i}
  121. xdump=$defdump
  122. dumppath=$xpath
  123. if(~ $slashnhack yes && ~ $xpath /n/*/*){
  124. xdump=`{echo $xpath | sed 's:/n/([^/]+)/.*:\1'$defdump':'}
  125. dumppath=`{echo $xpath | sed 's:/n/[^/]+(/.*):\1:'}
  126. }
  127. if(! test -e /n/$xdump/$year)
  128. 9fs $xdump
  129. if(~ $#last 0){
  130. xlast=`{ls -t /n/$xdump/$year|sed 1q}
  131. switch($defdump){
  132. case snap
  133. xlast=`{ls -t $xlast|sed 1q}
  134. }
  135. }
  136. if not
  137. xlast=/n/$xdump/$last
  138. if(! test -e $xlast){
  139. echo 'yesterday:' \
  140. `{echo $xlast|sed 's/.n.'$defdump'.(....).(..)(..)/\1 \2 \3/'} \
  141. 'is not a backup day for' $xdump >[1=2]
  142. exit 'bad date'
  143. }
  144. $copy $xlast^$dumppath $xpath
  145. }
  146. exit ''