home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LSD.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  211 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** LSD - A simple directory lister
  5. ** A public domain C demo program by Bob Stout
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include "dirport.h"
  13. #include "sniptype.h"
  14.  
  15. char *sprintfc(char *, long);
  16. char *capitalize(const char *);
  17. int   one_column(char *, char *, long, unsigned, DOSFileDate, DOSFileTime);
  18. int   five_column(char *, char *);
  19.  
  20. /*
  21. **  DOS DIR improved work-alike w/ improved formatting & attribute display
  22. **
  23. **  supports /W switch
  24. */
  25.  
  26. main(int argc, char *argv[])
  27. {
  28.       int i, files = 0, dirs = 0, argptr = 0, errflag = False_, cols, drive;
  29.       long siz_tot = 0L;
  30.       char *p, *fname, *ext, name[13], buf[67], numbuf[12];
  31.       DOSFileData ff;
  32. #ifndef __ZTC__
  33.       struct diskfree_t df;
  34. #endif
  35.       int (*display)() = one_column;
  36.  
  37.       strcpy(buf, fname = "*.*");
  38.       if(argc != 1)   for (i = 1; i < argc; ++i)
  39.       {
  40.             if ('/' == argv[i][0])
  41.             {
  42.                   if ('W' == toupper(argv[i][1]))
  43.                         display = five_column;
  44.                   else
  45.                   {
  46.                         puts("\aUsage: LSD [/W] [file]");
  47.                         errflag = True_;
  48.                   }
  49.             }
  50.             else if (!argptr)
  51.                   argptr = i;
  52.       }
  53.       if (argptr)
  54.       {
  55.             fname = argv[argptr];
  56.             strcpy(buf, fname);
  57.             if ('\\' == LAST_CHAR(buf) || ':' == LAST_CHAR(buf))
  58.                   strcat(buf, "*.*");
  59.             else
  60.             {
  61.                   if (Success_ == FIND_FIRST(buf, _A_SUBDIR, &ff))
  62.                   {
  63.                         if (ff_attr(&ff) & _A_SUBDIR && '.' != *ff_name(&ff))
  64.                         {
  65.                               if ('\\' != LAST_CHAR(buf))
  66.                                     strcat(buf, "\\");
  67.                               strcat(buf, "*.*");
  68.                         }
  69.                   }
  70.                   else  errflag = True_;
  71.             }
  72.       }
  73.       if (':' == buf[1])
  74.             drive = toupper(*buf) - '@';
  75.       else  drive = 0;
  76.       if (!errflag && !(FIND_FIRST(buf, _A_ANY, &ff))) do
  77.       {
  78.             siz_tot += ff_size(&ff);
  79.             if (ff_attr(&ff) & _A_SUBDIR)
  80.                   ++dirs;
  81.             else  ++files;
  82.             strcpy(name, ff_name(&ff));
  83.             if (NULL != (p = strchr(name, '.')) && p != name)
  84.             {
  85.                   *p  = '\0';
  86.                   ext = ++p;
  87.             }
  88.             else  ext = "";
  89.             cols = (*display)(name, ext, ff_size(&ff),
  90.                   ff_attr(&ff), ff_date(&ff), ff_time(&ff));
  91.       } while (Success_ == FIND_NEXT(&ff));
  92.       else
  93.       {
  94.             fprintf(stderr, "Cannot do directory on '%s'\n", fname);
  95.             exit(-1);
  96.       }
  97.       FIND_END(&ff);
  98.       if (cols)
  99.             fputc('\n', stdout);
  100.       sprintfc(numbuf,siz_tot);
  101.       printf("\n%3d Files totalling %s bytes\n", files, numbuf);
  102.       printf("%3d Director%s\n", dirs, (1 == dirs) ? "y" : "ies");
  103. #ifndef __ZTC__
  104.       _dos_getdiskfree(drive, &df);
  105.       sprintfc(numbuf, (long)df.avail_clusters * df.sectors_per_cluster *
  106.             df.bytes_per_sector);
  107. #else /* if ZTC */
  108.       sprintfc(numbuf, dos_getdiskfreespace(drive));
  109. #endif
  110.       printf("%s bytes free\n", numbuf);
  111.       return 0;
  112. }
  113.  
  114. /*
  115. **  The single column directory entry display function
  116. */
  117.  
  118. int one_column(char        *name,
  119.                char        *ext,
  120.                long         size,
  121.                unsigned     attribs,
  122.                DOSFileDate  date,
  123.                DOSFileTime  time)
  124. {
  125.       register int i, mask;
  126.       static char *atr = "RHSVDA", szbuf[12];
  127.  
  128.       sprintfc(szbuf, size);
  129.       printf("%-8s %-3s %12s  ", capitalize(name), capitalize(ext), szbuf);
  130.  
  131.       for (i = 0, mask = 1; i < 6; ++i, mask <<= 1)
  132.             fputc((attribs & mask) ? atr[i] : '.', stdout);
  133.  
  134.       printf("%4d-%02d-%02d%4d:%02d:%02d\n",
  135.             date.month,
  136.             date.day,
  137.             (date.year + 80) % 100,
  138.             time.hours,
  139.             time.mins,
  140.             time.tsecs * 2);
  141.  
  142.       return 0;
  143. }
  144.  
  145. /*
  146. **  The five column directory entry display function
  147. */
  148.  
  149. int five_column(char *name, char *ext)
  150. {
  151.       static int cols = 0;
  152.  
  153.       printf("%-8s %-3s%s", capitalize(name), capitalize(ext),
  154.             (5 > ++cols) ? "    " : "");
  155.       if (0 == (cols %= 5))
  156.             putchar('\n');
  157.       return (cols);
  158. }
  159.  
  160. /*
  161. **  Display a long int using commas as thousands separators
  162. */
  163.  
  164. char *sprintfc(char *string, long num)
  165. {
  166.       if (num > 999999L)
  167.       {
  168.             sprintf(string, "%d,%03d,%03d",
  169.                   (int)(num / 1000000L),
  170.                   (int)((num % 1000000L) / 1000L),
  171.                   (int)(num % 1000L));
  172.       }
  173.       else
  174.       {
  175.             if (num > 999L)
  176.             {
  177.                   sprintf(string, "%d,%03d",
  178.                         (int)(num / 1000L),
  179.                         (int)(num % 1000L));
  180.             }
  181.             else  sprintf(string, "%d", (int)num);
  182.       }
  183.       return string;
  184. }
  185.  
  186. /*
  187. **  Capitalize a name or extension in place
  188. */
  189.  
  190. char *capitalize(const char *string)
  191. {
  192.       int flag = 0;
  193.       char *ptr = (char *)string;
  194.  
  195.       do
  196.       {
  197.             if (isalpha(*ptr))
  198.             {
  199.                   if (flag)
  200.                         *ptr = (char)tolower(*ptr);
  201.                   else
  202.                   {
  203.                         flag = 1;
  204.                         *ptr = (char)toupper(*ptr);
  205.                   }
  206.             }
  207.             else  flag = 0;
  208.       } while (*++ptr);
  209.       return (char *)string;
  210. }
  211.