home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / perl / 6974 < prev    next >
Encoding:
Internet Message Format  |  1992-11-14  |  2.4 KB

  1. Path: sparky!uunet!airgun!starr@wg2.waii.com
  2. From: rfs@se28.wg2.waii.com (Robert Starr)
  3. Newsgroups: comp.lang.perl
  4. Subject: an ls -C Perl function
  5. Keywords: perl
  6. Message-ID: <1992Nov13.141515@se28.wg2.waii.com>
  7. Date: 13 Nov 92 20:15:15 GMT
  8. Sender: news@airgun.wg.waii.com
  9. Reply-To: starr@wg2.waii.com
  10. Organization: Western Geophysical
  11. Lines: 81
  12. Nntp-Posting-Host: se28.wg2.waii.com
  13.  
  14. Here is a function to take a list of file names, and sort it as an
  15. ls -C does (sorted down columns).  I find myself spewing out file names
  16. often, and missed having them not be ordered as ls -C does.  If there
  17. was a Unix filter to do this, I couldn't find it.  But it's nice to
  18. have a Perl ditty to do the same thing anyway.
  19.  
  20. Note the completely non-portable setting of TIOCGWINSZ to get the
  21. window size.  The problem seems to be that proper .ph files cannot be
  22. created on the Sparc, since the computation of TIOCGWINSZ involves
  23. using the size of a data structure.  Any suggestions?
  24.  
  25. #--------%<----------------
  26. #!/bin/perl
  27. # $Id: lsC.pl,v 1.3 1992/11/13 19:59:25 rfs Exp $
  28. #
  29. # $Log: lsC.pl,v $
  30. #Revision 1.3  1992/11/13  19:59:25  rfs
  31. #Minor cleanup.
  32. #
  33. #Revision 1.2  1992/11/11  23:17:34  rfs
  34. #Added int() to $nrows computation.
  35. #
  36. #Revision 1.1  1992/11/11  23:02:05  rfs
  37. #Initial revision
  38. #
  39.  
  40. # Usage: @out = &LsC(@filenames);
  41. # Returns an array of filenames, sorted as ls -C would do.  The returned
  42. # array of lines aren't newline terminated.
  43. #
  44. # NOTE: very non-portable setting of $TIOCGWINSZ.  This was for Sparc.
  45. #
  46. # Author: RF Starr (starr@wg2.waii.com)
  47.  
  48. sub LsC {
  49.     package LsC;
  50.     local(@f) = @_;
  51.     local($maxlen, $_);
  52.     local(@out) = ();
  53.  
  54.     $nc = &main'_GetNumColumns() unless (defined($nc));
  55.     @files = sort(@f);
  56.     # find widest file name
  57.     $maxlen = 0;
  58.     $cnt = 0;
  59.     foreach (@files) {
  60.         $len = length($_);
  61.         $maxlen = $len if ($len > $maxlen);
  62.         $cnt++;
  63.     }
  64.     $nperline = $nc/($maxlen+1);
  65.     $nperline = int($nperline);
  66.     $nrows = int($cnt/$nperline);
  67.     $nrows++ if ($cnt % $nperline);
  68.     $fmt = "%-$maxlen.$maxlen" . "s ";
  69.     for ($i=0; $i < $nrows; $i++) {
  70.         $out[$i] = '';
  71.     }
  72.     $j = 0;
  73.     for ($i=0; $i < $cnt; $i++) {
  74.         $out[$j] .= sprintf($fmt, $files[$i]);    
  75.         $j++;
  76.         $j = 0 if ($j >= $nrows);
  77.     }
  78.     return @out;
  79. }
  80.  
  81. sub _GetNumColumns {
  82.     package _GetNumColumns;
  83.  
  84.     $nc = 80;
  85.     # You may need to change this constant
  86.     $TIOCGWINSZ = 0x40087468;
  87.     $wt = "S S S S";
  88.     $ws = pack($wt, 0, 0, 0, 0);
  89.     ioctl(STDIN, $TIOCGWINSZ, $ws);
  90.     ($nrow, $ncol) = unpack($wt, $ws);
  91.     $nc = $ncol if ($ncol);
  92.     return $nc;
  93. }
  94. 1;
  95.