home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / c_spec / execute / ls.c < prev    next >
C/C++ Source or Header  |  1986-02-20  |  5KB  |  173 lines

  1. #include <stdio.h>
  2. #include "/include/getargs.h"
  3. #include "/include/dir.h"
  4.  
  5. #define STAND_ALONE 1
  6.  
  7. /*----------------------------------------------------------------------+
  8.  *           LS.C: An MSDOS directory utility            |
  9.  *                                    |
  10.  *     (c) Copyright 1985, Allen I. Holub. All rights reserved.    |
  11.  *   This program may be reproduced for personal, non-profit use only.    |
  12.  *                                    |
  13.  *   Exit status:  0  if files are found.                |
  14.  *           1  if no files are found.                |
  15.  *           2  if internal error.                |
  16.  *                                    |
  17.  *   Note that though this version of ls doesn't do a sort by extension |
  18.  *   if you say "ls *.c *.exe" the sub groups will stay together    |
  19.  *----------------------------------------------------------------------+
  20.  */
  21.  
  22. extern    DIRECTORY  *mk_dir();
  23.  
  24. #define ISTRUE(x)  (x != 0)
  25. #define ISFALSE(x) (x == 0)
  26. #define MAXBUF       132
  27. #define MAXDIRS    256    /* Max number of directories that will be printed */
  28.  
  29. static int  Longfmt     = 0;    /* Global variables set by command line */
  30. static int  Dont_expand = 0;
  31. static int  Files_only  = 0;
  32. static int  No_graphics = 0;
  33. static int  Dirs_only   = 0;
  34. static int  List_all    = 0;
  35. static int  Num_cols    = 0;
  36. static int  Pathname    = 0;
  37.  
  38. static ARG Argtab[] =
  39. {                                    
  40.     { 'a', BOOLEAN, &List_all,   "List all files (including hidden)" },
  41.     { 'c', INTEGER, &Num_cols,   "Print output in <num> columns"     },
  42.     { 'd', BOOLEAN, &Dirs_only,  "List only directories"             },
  43.     { 'f', BOOLEAN, &Files_only, "List only files"                 },
  44.     { 'l', BOOLEAN, &Longfmt,    "Print directory in long format"    },
  45.     { 'p', BOOLEAN, &Pathname,   "Prepend pathname if one is given"     },
  46.     { 's', BOOLEAN, &No_graphics,"Suppress ANSI graphics chars"      },
  47.     { 'x', BOOLEAN, &Dont_expand,"Don't expand directory names"     }
  48. };
  49.  
  50. #define TSIZE (sizeof(Argtab)/sizeof(ARG))
  51.  
  52. /*----------------------------------------------------------------------*/
  53.  
  54. static  void  printdir(dirc, dirv, maxwidth)
  55. int    dirc   ;
  56. char    **dirv ;
  57. {
  58.     /*    Print out the directory in Numcolumns. Note that if
  59.      *    Numcols is not specified on the command line then
  60.      *    the proper number of columns will be computed here.
  61.      */
  62.  
  63.     if( !Num_cols )
  64.         Num_cols = 79 / (maxwidth+1) ;
  65.  
  66.     printf("\n");
  67.     ptext( dirc, dirv, stdout, Num_cols, 80/Num_cols,
  68.                 (dirc/Num_cols) + (dirc % Num_cols != 0) );
  69.     printf("\n");
  70. }
  71.  
  72. /*----------------------------------------------------------------------*/
  73.  
  74. static DIRECTORY  *makedir()
  75. {
  76.     register DIRECTORY  *dp;
  77.  
  78.     if( !(dp = mk_dir(MAXDIRS)) )
  79.     {
  80.         fprintf(stderr, "ls: out of memory!");
  81.         exit( 2 );
  82.     }
  83.  
  84.     dp->longf    = ISTRUE  ( Longfmt     );
  85.     dp->files    = ISFALSE ( Dirs_only    );
  86.     dp->dirs    = ISFALSE ( Files_only    );
  87.     dp->graphics    = ISFALSE ( No_graphics    );
  88.     dp->hidden    = ISTRUE  ( List_all     );
  89.     dp->path    = ISTRUE  ( Pathname    );
  90.     dp->label    = 1;
  91.     dp->sort    = 1;
  92.     dp->exp        = ISFALSE ( Dont_expand );
  93.  
  94.     return dp;
  95. }
  96.  
  97. /*----------------------------------------------------------------------*/
  98.  
  99. void    ls( argc, argv )
  100. char    **argv;
  101. {
  102.     static DIRECTORY  *dp ;
  103.     unsigned tc, spc, bps, ac;/* Total clusters, sectors/cluster,
  104.                    * bytes/sector, available clusters.
  105.                    */
  106.  
  107.     argc = getargs( argc, argv, Argtab, TSIZE );
  108.  
  109.     if( Longfmt )            /* -l is always printed in 1 column    */
  110.         Num_cols = 1;        /* even if -cN was given on the cmd */
  111.                     /* line                */
  112.  
  113.     dp = makedir();
  114.  
  115.     --argc;
  116.     ++argv;
  117.  
  118.     diskinfo( (**argv && argv[0][1]==':') ? (toupper(**argv)-'A')+1: 0,
  119.                         &spc, &bps, &ac, &tc );
  120.     if( argc <= 0 )
  121.         dir( "*.*" , dp );
  122.     else
  123.         for(; --argc >= 0 ;  dir(*argv++, dp) )
  124.             ;
  125.  
  126.     printdir( dp->nfiles + dp->ndirs, dp->dirv, dp->width);
  127.  
  128.  
  129.     if( dp->nfiles )
  130.         printf("%d file%s (%4.1f K out of %ld K)",
  131.             dp->nfiles, dp->nfiles == 1 ? "" : "s",
  132.             (double)((double)dp->nbytes/1024.0),
  133.             (long)(((long)tc * (long)spc * (long)bps)/1024L));
  134.     
  135.     if( dp->ndirs )
  136.     {
  137.         if( dp->nfiles )
  138.             printf(", ");
  139.  
  140.         printf("%d director%s",dp->ndirs, dp->ndirs==1 ? "y" :"ies");
  141.     }
  142.  
  143.     if( *dp->vol_label  &&  (dp->nfiles || dp->ndirs)  )
  144.         printf( ", Volume: %s" , dp->vol_label );
  145.  
  146.  
  147.     printf("\n");
  148.  
  149.     if( dp->maxdirs <= 0 )
  150.     {
  151.         printf("\007Maximum directory count reached,");
  152.         printf(" list may be truncated.\n");
  153.     }
  154.  
  155.     exit( !(dp->nfiles + dp->ndirs) );
  156. }
  157.  
  158. /*----------------------------------------------------------------------*/
  159.  
  160. #ifdef STAND_ALONE
  161.  
  162. main( argc, argv )
  163. char    **argv;
  164. {
  165.     ctlc();
  166.     reargv(&argc, &argv);
  167.     fprintf(stderr,"LS: Copyright (C) 1986, Allen I. Holub. ");
  168.     fprintf(stderr,"All rights reserved.\n");
  169.     ls( argc, argv );
  170. }
  171.  
  172. #endif
  173.