home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / when.icn < prev    next >
Text File  |  2000-07-29  |  9KB  |  301 lines

  1. ############################################################################
  2. #
  3. #    File:     when.icn
  4. #
  5. #    Subject:  Program to show file age
  6. #
  7. #    Author:   Chris Tenaglia
  8. #
  9. #    Date:     August 14, 1996
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This one was developed for UNIX (namely ULTRIX 4.3 rev 44). Maybe
  18. #  it will work on some other UNIX too. I'd like to know. This program
  19. #  is called 'when'. It's like a date based ls command. Some have told
  20. #  me 'find' can do the same things, but I find find a bit arcane?
  21. #  
  22. #  So 'when' is what I use. Here are some samples:
  23. #  
  24. #       when before 4/12/92      # files before a date
  25. #       when before 300          # files older than an age
  26. #       when after 3/25          # or younger than a date this year
  27. #       when before 2/1/94 and after 10/31/93     # even a range
  28. #  
  29. #  More options and clauses are supported. Look at the code for clues.
  30. #  This one only works in the current directory. It also has an interesting
  31. #  property. Maybe this is just ULTRIX, maybe not, I'd like to know anyway...
  32. #  The interpreted version works fine, but the compiled version has a
  33. #  numeric overflow. That'll make for some fun debugging. I wrote it for
  34. #  myself as a tool to locate old files for archiving or deleting. Study and
  35. #  enjoy!
  36. #
  37. ############################################################################
  38. #
  39. #  Requires:  UNIX
  40. #
  41. ############################################################################
  42.  
  43. global base,      # 1970 calculation baseline number
  44.        today,     # displacement from 12:00:01am today
  45.        now,       # upto the second mark for right now
  46.        method,    # ascending or descending order
  47.        output,    # long (ls -al) or brief (ls -1) style
  48.        command,   # optional command to do on each file
  49.        files      # list with files, sizes, and ages
  50.  
  51. procedure main(param)
  52.   local i, option, j
  53.   calc_today()
  54.   files   := directory()
  55.   method  := "none"
  56.   output  := "long"
  57.   command := ""
  58.   if *param = 0 then show_age()
  59.   every i := 1 to *param do
  60.     {
  61.     (option := param[i]) | break
  62.     case option of
  63.       {
  64.       "to"     |
  65.       "before" |
  66.       "until"   : {
  67.                   files := before(files,param[i+1])
  68.                   i    +:= 1
  69.                   }
  70.       "from"  |
  71.       "since" |
  72.       "after"   : {
  73.                   files := since(files,param[i+1])
  74.                   i    +:= 1
  75.                   }
  76.       "asc"     : method:="ascending"
  77.       "des"     : method:="descending"
  78.       "long"    : output:="long"
  79.       "brief"   : output:="brief"
  80.       "do"      : {
  81.                   every j := i+1 to *param do
  82.                     command ||:= param[j] || " "
  83.                   }
  84.       default   : 5    # stop("Unrecognized option :",option)
  85.       }
  86.     }
  87.   show_age()
  88.   end
  89.  
  90. #
  91. # just show another ls with days old numbers & optionally sorts
  92. #
  93. procedure show_age()
  94.   local line, age, ks, file, text, results, i
  95.   case method of
  96.     {
  97.     "none" : {  
  98.              every line := !files do
  99.                {
  100.                age := (today - parse(line,' ')[1]) / 86400
  101.                ks  := parse(line,' ')[2] / 1024
  102.                file:= line[23:0]
  103.                (command == "") |
  104.                  {
  105.                  write(command,line[37:0])
  106.                  system(command || line[37:0])
  107.                  next
  108.                  }
  109.                if output == "brief" then text := line[37:0]
  110.                                     else text:= right(age,6) || " days " || right(ks,6) || " kb | " || file
  111.                write(text)
  112.                }
  113.              }
  114.     "descending" : {
  115.                    results := sort(files)
  116.                    every line := !results do
  117.                      {
  118.                      age := (today - parse(line,' ')[1]) / 86400
  119.                      ks  := parse(line,' ')[2] / 1024
  120.                      file:= line[23:0]
  121.                      (command == "") |
  122.                        {
  123.                        write(command,line[37:0])
  124.                        system(command || line[37:0])
  125.                        next
  126.                        }
  127.                      if output == "brief" then text := line[37:0]
  128.                                           else text:= right(age,6) || " days " || right(ks,6) || " kb | " || file
  129.                      write(text)
  130.                      }
  131.                    }
  132.     "ascending" : {
  133.                    results := sort(files)
  134.                    every i := *results to 1 by -1 do
  135.                      {
  136.                      line:= results[i]
  137.                      age := (today - parse(line,' ')[1]) / 86400
  138.                      ks  := parse(line,' ')[2] / 1024
  139.                      file:= line[23:0]
  140.                      (command == "") |
  141.                        {
  142.                        write(command,line[37:0])
  143.                        system(command || line[37:0])
  144.                        next
  145.                        }
  146.                      if output == "brief" then text := line[37:0]
  147.                                           else text:= right(age,6) || " days " || right(ks,6) || " kb | " || file
  148.                      write(text)
  149.                      }
  150.                    }
  151.     default : 5
  152.     }
  153.   end
  154.  
  155. #
  156. # remove elements later than a date
  157. #
  158. procedure before(lst,days)
  159.   local i, mo, da, yr, tmp, dd, age, work, file, old
  160.   static  mtab
  161.   initial mtab := [0,31,59,90,120,151,181,212,243,273,304,334]
  162.   if find("/",days) then
  163.     {
  164.     mo := parse(days,'/')[1]
  165.     da := parse(days,'/')[2]
  166.     yr := parse(days,'/')[3] | parse(&date,'/')[1]
  167.     if yr < 100 then yr +:= 1900
  168.     tmp := yr * 31557600
  169.     dd  := mtab[mo] + da
  170.     if ((yr % 4) = 0) & (mo > 2) then dd +:= 1
  171.     tmp+:= dd * 86400
  172.     age := tmp
  173.     } else {
  174.     age := now - (days * 86400)
  175.     }
  176.   work := []
  177.   every file := !lst do
  178.     {
  179.     old := parse(file,' ')[1]
  180.     if old <= age then put(work,file)
  181.     }
  182.   return copy(work)
  183.   end
  184.  
  185. #
  186. # remove elements earlier than a date
  187. #
  188. procedure since(lst,days)
  189.   local mo, da, yr, tmp, dd, age, work, file, old
  190.   static  mtab
  191.   initial mtab := [0,31,59,90,120,151,181,212,243,273,304,334]
  192.   if find("/",days) then
  193.     {
  194.     mo := parse(days,'/')[1]
  195.     da := parse(days,'/')[2]
  196.     yr := parse(days,'/')[3] | parse(&date,'/')[1]
  197.     if yr < 100 then yr +:= 1900
  198.     tmp := yr * 31557600
  199.     dd  := mtab[mo] + da
  200.     if ((yr % 4) = 0) & (mo > 2) then dd +:= 1
  201.     tmp+:= dd * 86400
  202.     age := tmp
  203.     } else {
  204.     age := now - (days * 86400)
  205.     }
  206.   work := []
  207.   every file := !lst do
  208.     {
  209.     old := parse(file,' ')[1]
  210.     if old >= age then put(work,file)
  211.     }
  212.   return copy(work)
  213.   end
  214.  
  215. #
  216. # calculate today and now figures
  217. #
  218. procedure calc_today()
  219.   local tmpy, tmpm, tmpd, here
  220.   static  mtab
  221.   initial {
  222.           base := 1970*31557600
  223.           mtab := [0,31,59,90,120,151,181,212,243,273,304,334]
  224.           }
  225.   tmpy := parse(&date,'/')[1]
  226.   tmpm := parse(&date,'/')[2]
  227.   tmpd := parse(&date,'/')[3]
  228.   here := tmpy * 31557600 +
  229.           (mtab[tmpm] + tmpd) * 86400
  230.   if ((tmpy%4) = 0) & (tmpm > 2) then here +:= 86400
  231.   today := here
  232.   now   := here +
  233.            parse(&clock,':')[1] * 3600 +
  234.            parse(&clock,':')[2] *   60 +
  235.            parse(&clock,':')[3]
  236.   end
  237.  
  238. #
  239. # convert a ls -al output into a list for sorting and printing
  240. #
  241. procedure directory()
  242.   local pipe, entries, line, size, file, day, year, sec, mark, text
  243.   static mtab
  244.   initial {
  245.           mtab := table(0)
  246.           mtab["Jan"] := 0
  247.           mtab["Feb"] := 31
  248.           mtab["Mar"] := 59
  249.           mtab["Apr"] := 90
  250.           mtab["May"] := 120
  251.           mtab["Jun"] := 151
  252.           mtab["Jul"] := 181
  253.           mtab["Aug"] := 212
  254.           mtab["Sep"] := 243
  255.           mtab["Oct"] := 273
  256.           mtab["Nov"] := 304
  257.           mtab["Dec"] := 334
  258.           }
  259.   pipe    := open("ls -al","pr")
  260.   entries := []
  261.   every line := !pipe do
  262.     {
  263.     if any('dclst',line) then next   # ignore info and dirs
  264.     size := parse(line,' ')[4]
  265.     file := line[33:0]
  266.     day  := mtab[parse(line,' ')[5]] + parse(line,' ')[6]
  267.     year := if line[40] == " " then parse(line,' ')[7] else parse(&date,'/')[1]
  268.     sec  := if line[40] == " " then 0 else hhmm(parse(line,' ')[7])
  269.     mark := year * 31557600 + day * 86400 + sec
  270.     if (now-mark) < 0 then mark -:= 31557600
  271.     text := right(mark,12) || right(size,10) || " " || file
  272.     put(entries,text)
  273.     }
  274.   close(pipe)
  275.   return entries
  276.   end
  277.  
  278. #
  279. # convert hh:mm into seconds since midnight
  280. #
  281. procedure hhmm(str)
  282.   local hh, mm
  283.   hh := str[1+:2]
  284.   mm := str[4+:2]
  285.   return hh*3600 + mm*60
  286.   end
  287.  
  288. #
  289. # parse a string into a list with respect to a delimiter
  290. #
  291. procedure parse(line,delims)
  292.   local tokens
  293.   static chars
  294.   chars  := &cset -- delims
  295.   tokens := []
  296.   line ? while tab(upto(chars)) do put(tokens,tab(many(chars)))
  297.   return tokens
  298.   end
  299.  
  300.  
  301.