home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5538 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  3.6 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: got a symlink checker/chaser for crontab?
  5. Message-ID: <1992Aug27.063417.5136@netlabs.com>
  6. Date: 27 Aug 92 06:34:17 GMT
  7. References: <17544@autodesk.COM>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 129
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <17544@autodesk.COM> dansmith@Autodesk.COM (Daniel Smith) writes:
  14. :     I often tie together filesystems with symlinks, so that
  15. : something like /usr/local/src will contain links which point all
  16. : over the place.  Every so often, I try to cd somewhere I haven't
  17. : been for weeks, only to find that the link has "dried up".
  18. :     Well... I basically want a script that will take
  19. : "find / -type l -print" and check the links to see if they resolve.  It's
  20. : something I'd run once a day out of crontab.  Rather than reinventing
  21. : what must be a somewhat common wheel, I thought I'd ask here first.
  22. : The script would produce a list of symlinks in need of updating/deletion,
  23. : which presumably would get piped into mail.
  24.  
  25. Well, this isn't exactly it, but you might be able to hack it into what
  26. you want.
  27.  
  28. Larry
  29.  
  30. #!/usr/bin/perl
  31. 'di';
  32. 'ig00';
  33. #
  34. # $Header: sl,v 1.1 90/08/11 13:51:00 lwall Locked $
  35. #
  36. # $Log:    sl,v $
  37. # Revision 1.1  90/08/11  13:51:00  lwall
  38. # Initial revision
  39.  
  40. die "Usage: sl [filenames]\n" unless @ARGV;
  41.  
  42. $| = 1;
  43. chop($cwd = `pwd`) || die "Can't find current directory: $!\n" if $#ARGV > 0;
  44.  
  45. print "\n";
  46. foreach $name (@ARGV) {
  47.     @indent = ();
  48.     print "$name:\n";
  49.     @path = split(m;/;, $name);
  50.     if (@path && $path[0] eq '') {
  51.     chdir '/';
  52.     shift @path;
  53.     print '/';
  54.     $indent = 1;
  55.     }
  56.     while (@path) {
  57.     $elem = shift @path;
  58.     $new = readlink($elem);
  59.     if (defined $new) {
  60.         print "$elem -> $new\n";
  61.         $new =~ s!^\./!!;
  62.         unshift(@path,split(m;/;, $new));
  63.         if (@path && $path[0] eq '') {
  64.         chdir '/';
  65.         shift @path;
  66.         print '/';
  67.         $indent = 1;
  68.         @indents = ();
  69.         }
  70.         elsif (@path && @indents && $path[0] eq '..') {
  71.         $indent = pop(@indents);
  72.         chdir '..' || die "\n\nCan't chdir to ..: $!\n";;
  73.         shift @path;
  74.         print "\t" x ($indent / 8), ' ' x ($indent % 8);
  75.         }
  76.         else {
  77.         print "\t" x ($indent / 8), ' ' x ($indent % 8);
  78.         }
  79.     }
  80.     else {
  81.         print $elem;
  82.         push(@indents,$indent);
  83.         $indent += length($elem) + 1;
  84.         if (@path) {
  85.         print '/';
  86.         chdir $elem || die "\n\nCan't chdir to $elem: $!\n";;
  87.         }
  88.     }
  89.     }
  90.     print "\n\n";
  91.     $indent = 0;
  92.     chdir $cwd || die "Can't cd back: $!\n" if $cwd ne '';
  93. }
  94. ##############################################################################
  95.  
  96.     # These next few lines are legal in both Perl and nroff.
  97.  
  98. .00;            # finish .ig
  99.  
  100. 'di            \" finish diversion--previous line must be blank
  101. .nr nl 0-1        \" fake up transition to first page again
  102. .nr % 0            \" start at page 1
  103. '; __END__ ############# From here on it's a standard manual page ############
  104. .TH SL 1 "July 30, 1990"
  105. .AT 3
  106. .SH NAME
  107. sl \- show translation of symbolic link to actual filename
  108. .SH SYNOPSIS
  109. .B sl [filenames]
  110. .SH DESCRIPTION
  111. .I Sl
  112. traverses the pathnames supplied on the command line, and for each one,
  113. tells you if it had to follow any symbolic links to find the actual filename.
  114. Example:
  115. .nf
  116.  
  117.     $ sl /usr/lib/libXw.a
  118.  
  119.     /usr/lib/libXw.a:
  120.     /usr/lib/libXw.a -> /usr/lib/X11/libXw.a
  121.     /usr/lib/X11 -> /X11/lib
  122.     /X11 -> /usr/local/X11R4
  123.     /usr/local/X11R4/lib/libXw.a
  124.  
  125. .fi
  126. .SH ENVIRONMENT
  127. No environment variables are used by
  128. .I sl.
  129. .SH FILES
  130. None.
  131. .SH AUTHOR
  132. Larry Wall
  133. .SH "SEE ALSO"
  134. ls -l
  135. .SH DIAGNOSTICS
  136. Complains if it can't chdir to the directories it's trying to traverse.
  137. .SH BUGS
  138. .ex
  139.