home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sources / wanted / 3759 < prev    next >
Encoding:
Text File  |  1992-07-27  |  3.9 KB  |  123 lines

  1. Xref: sparky comp.sources.wanted:3759 news.software.readers:1560 comp.lang.perl:4979 alt.sources:1770
  2. Path: sparky!uunet!dtix!darwin.sura.net!convex!convex!tchrist
  3. From: tchrist@convex.COM (Tom Christiansen)
  4. Newsgroups: comp.sources.wanted,news.software.readers,comp.lang.perl,alt.sources
  5. Subject: Re: Wanted: /bin/sh script for mantaining KILL files.
  6. Message-ID: <1992Jul27.154340.8482@news.eng.convex.com>
  7. Date: 27 Jul 92 15:43:40 GMT
  8. References: <1992Jul26.085354.2710@wybbs.mi.org>
  9. Sender: usenet@news.eng.convex.com (news access account)
  10. Reply-To: tchrist@convex.COM (Tom Christiansen)
  11. Followup-To: comp.sources.wanted,news.software.readers,comp.lang.perl,alt.sources.d
  12. Organization: CONVEX Realtime Development, Colorado Springs, CO
  13. Lines: 103
  14. Originator: tchrist@pixel.convex.com
  15. Nntp-Posting-Host: pixel.convex.com
  16. X-Disclaimer: This message was written by a user at CONVEX Computer
  17.               Corp. The opinions expressed are those of the user and
  18.               not necessarily those of CONVEX.
  19.  
  20. Archive-name: killkill
  21. Submitter: Tom Christiansen <tchrist@convex.com>
  22.  
  23. From the keyboard of an unknown person @wybbs.mi.org:
  24. :I would like to get my hands on a script to do
  25. :some or all of the following.
  26. :1> Look for all kill files over X-bytes in length
  27. :1a> trim the file down until it's under X-bytes, by removing
  28. :    the first line(s) of kill commands (leaving the
  29. :    THRU line intact.
  30. :2> Remove KILL files that are older than X-days since 
  31. :    last access.
  32. :3> Mail a report to the user on which files were cleaned
  33. :    up, and what was taken out, or which files were
  34. :    deleted.
  35. :4> Can be used either system-wide using cron, or
  36. :    on an individual basis, like in .profile
  37. :Ok, folks... that's what I would like. I would do
  38. :it myself, but I'm still not fluent in /bin/sh scripts
  39. :to get it to work. (Plus, looking at the script or
  40. :source if U care to make it compilable would be very
  41. :useful to show me how to incorporate routines into
  42. :other code as well.)
  43. :I am using SCO-Xenix SysV-386.
  44.  
  45. Here's a Perl solution.  I'll leave the running from cron
  46. or sending mail as an exercise for the reader.
  47.  
  48. --tom
  49.  
  50. #!/usr/bin/perl
  51. #
  52. # killkill -- maintain killfiles by size and age
  53. # tchrist@convex.com    Mon Jul 27 10:35:44 CDT 1992
  54. #
  55. # arguments are considered user names, defaulting
  56. # to the current user if not given.
  57. #
  58. # for all killfiles in a user's newsdirectory that are older than
  59. # $MAXDAYS, unlink them, or if bigger than $MAXSIZE, save the THRU line
  60. # and discard lines (oldest first) until size is under $MAXSIZE.  
  61. #
  62. # a killfile is defined by the pattern $KILLPAT; a newsdir is a
  63. # directory residing in ~$user/$NEWSDIR.
  64. #
  65. # print out each killfile found, and what if anything is done to it.
  66. # if $DEBUG is true, don't really touch any files.
  67.  
  68. require 'find.pl';
  69.  
  70. $MAXSIZE = 250;
  71. $MAXDAYS = 30;
  72. $KILLPAT = '^KILL$';
  73. $NEWSDIR = 'News';
  74. $DEBUG = 0;
  75.  
  76. if (@ARGV == 0) { # no args means default to current user
  77.     @ARGV = (       $ENV{USER}
  78.          || $ENV{LOGNAME}
  79.          || (getpwuid($>))[0]
  80.          || die "Cannot determine user for id $>"
  81.         );
  82.  
  83. print "$0: maxsize is $MAXSIZE, maxdays is $MAXDAYS, debug is $DEBUG\n";
  84.  
  85. while ($user = shift) {
  86.     $home = (getpwnam($user))[7];
  87.     $newsdir = "$home/$NEWSDIR";
  88.     next unless -d $newsdir;
  89.     &find($newsdir);
  90.  
  91. sub wanted {
  92.     /$KILLPAT/o || return;
  93.     $size = -s $_;
  94.     $age  = -M _;  
  95.     printf "%-50s  age = %5.1f   size = %d\n", $name, $age, $size;
  96.     if ($age > $MAXDAYS) {
  97.     print "\tunlinking $name\n";
  98.     unlink unless $DEBUG;
  99.     } elsif ($size > $MAXSIZE) {
  100.     print "\ttrimming $name\n";
  101.     unless ($DEBUG) {
  102.         ($thru, @lines) = `cat $name`;
  103.         shift @lines until length("$thru @lines") < $MAXSIZE;
  104.         open (KF, ">$name") || die "can't write $name: $!";
  105.         print KF $thru, @lines;
  106.         close KF || die "can't close $name: $!"
  107.     }
  108.     } 
  109. __END__
  110.  
  111.  
  112. -- 
  113.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  114.  
  115.  
  116.     /earth is 98% full ... please delete anyone you can.
  117.