home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / dirutl / tools.arc / DU.C < prev    next >
Text File  |  1987-09-23  |  5KB  |  192 lines

  1. /*
  2.  *  Attribute bits:
  3.  *      d - directory
  4.  *      r - read only file
  5.  *      h - hidden file
  6.  *      s - system file
  7.  *      a - archive bit
  8.  *
  9.  *  Files within each directory are sorted alphabetically.
  10.  *  Directories are listed separate from other files.
  11.  */
  12.  
  13. #include <stdio.h>              /* Standard I/O definitions */
  14. #include <ctype.h>              /* Character type macros */
  15. #include <dos.h>                /* Msdos definitions */
  16. #include <errno.h>              /* error codes */
  17. #include <string.h>
  18. #include <malloc.h>
  19.  
  20. #define A_RONLY         0x01    /* Read only file */
  21. #define A_HIDDEN        0x02    /* Hidden file */
  22. #define A_SYSTEM        0x04    /* System file */
  23. #define A_DIRECTORY     0x10    /* Directory file */
  24. #define A_ARCHIVE       0x20    /* Archive bit */
  25.  
  26. static char ID[]="## du.c 1.0 K.van Houten 092387 ##";
  27. struct date {
  28. unsigned    d_sec:      5;      /* Time, 2 second intervals */
  29. unsigned    d_min:      6;      /* Time, minutes */
  30. unsigned    d_hour:     5;      /* Time, hours */
  31. unsigned    d_day:      5;      /* Date, day of month */
  32. unsigned    d_month:    4;      /* Date, month of year */
  33. unsigned    d_year:     7;      /* Date, year since 1980 */
  34. };
  35. struct find {
  36.     char    fnd_dosinfo[21];    /* Reserved for dos */
  37.     char    fnd_attr;           /* File attribute */
  38.     struct date fnd_date;       /* Date structure */
  39.     long    fnd_size;           /* File size */
  40.     char    fnd_name[13];       /* File name less path */
  41. };
  42. long addir();
  43. struct find f;                  /* Used to return data from msdos */
  44. union REGS r, o;                /* Contains register values for intcall() */
  45. int   tmp, i;
  46. int bsize=8*1024;               /* size of disk allocation */
  47. int kpb;                        /* Kbytes per block */
  48. int     sflg = 0;               /* summarize */
  49. long total;
  50. extern int clsize();            /* get cluster size */
  51.  
  52. main (argc, argv)
  53. int     argc;                   /* Number of command line arguments */
  54. char   *argv[];                 /* Array of pointers to arguments */
  55. {
  56.     char    buf[70];
  57.     bsize=clsize(defdrv());
  58.     kpb=bsize/1024;
  59.  
  60.     for (argc--,argv++; argc>0; argc--,argv++) {
  61.         if (**argv == '-') {
  62.             while (*++(*argv)) {
  63.                 switch (**argv) {
  64.                 case 's':
  65.                     sflg++;
  66.                     break;
  67.                 default:
  68.                     printf("Usage: du directory\n");
  69.                     exit(1);
  70.                 }
  71.             }
  72.         }
  73.         else
  74.             break;
  75.     }
  76.  
  77.     r.h.ah = 0x1a;              /* SET DISK TRANSFER ADDRESS FUNCTION */
  78.     r.x.dx = (int) & f;
  79.     tmp = intdos (&r, &o);
  80. /*  Produces error 0 ???
  81.     if (o.x.cflag) {
  82.         perror ("SETTING DTA ");
  83.         exit (1);
  84.     }
  85. */
  86.     if (argc == 0) {
  87.         total=addir(".");
  88.     }
  89.     else {
  90.         findf(*argv);
  91.         if (o.x.cflag) {
  92.             printf("du: entry not found\n");
  93.             exit(1);
  94.         }
  95.         if (!(f.fnd_attr & A_DIRECTORY)) {
  96.             printf("du: %s: not a directory\n",f.fnd_name);
  97.             exit(1);
  98.         }
  99.         total=addir(*argv);
  100.     }
  101.     if (sflg) {
  102.         if (argc) {
  103.             printf("%10ld %s\n",kpb*total,strlwr(*argv));
  104.         }
  105.         else {
  106.             printf("%10ld .\n",kpb*total);
  107.         }
  108.     }
  109. }
  110.  
  111. long
  112. addir(dir)
  113. char *dir;
  114. {
  115.     char *dirlist[100];  /* for subdirs */
  116.     char buf[100];       /* for filename */
  117.     int dircnt=0;        /* nr of subdirs */
  118.     int i;
  119.     long dtot=0;         /* total of this level */
  120.  
  121.     strcpy(buf,dir);
  122.     strcat(buf,"/*.*");
  123. /*    printf("Scanning %s\n",dir); */
  124.     findf(buf);
  125.     while (o.x.cflag == 0) {
  126.         if (f.fnd_attr & A_DIRECTORY) {
  127.             if (!(*f.fnd_name == '.')) {
  128.                 dirlist[dircnt]=malloc(sizeof(f.fnd_name)+1);
  129.                 if (dirlist[dircnt] == NULL) {
  130.                     perror("du ");
  131.                     exit(1);
  132.                 }
  133.                 strcpy(dirlist[dircnt++],f.fnd_name);
  134.             }
  135.         }
  136.         else {
  137.           dtot+=((f.fnd_size%bsize)?(f.fnd_size/bsize+1):(f.fnd_size/bsize));
  138.         }
  139.         findn();
  140.     }
  141.     /* all entrys done,
  142.      * now scan subdirs.
  143.      */
  144.     for(i=0;i<dircnt;i++) {
  145.         strcpy(buf,dir);
  146.         strcat(buf,"/");
  147.         strcat(buf,dirlist[i]);
  148. /*        printf("nesting: %s\n",buf); */
  149.         dtot += addir(buf);
  150.     }
  151.     if (sflg ==0)
  152.         printf("%10ld %s\n",kpb*dtot,strlwr(dir));
  153.     return(dtot);
  154. }
  155.  
  156. /*
  157.  * Find first entry, return info in find structure.
  158.  */
  159. findf (file)
  160. char *file;
  161. {
  162.     r.h.ah = 0x4e;              /* FINDFIRST function */
  163.     r.x.cx = A_HIDDEN | A_SYSTEM | A_DIRECTORY |
  164.         A_RONLY | A_ARCHIVE;
  165.     r.x.dx = (int) file;
  166.     tmp = intdos (&r, &o);      /* Find first */
  167. }
  168.  
  169. /*
  170.  * Find next entry
  171.  */
  172. findn ()
  173. {
  174.     r.h.ah = 0x4f;              /* Now use FINDNEXT function */
  175.     tmp = intdos (&r, &o);      /* Find next */
  176. }
  177.  
  178. int
  179. defdrv()
  180. {
  181.     int tmp;
  182.     union REGS r;
  183.  
  184.     r.h.ah = 0x19;  /* get default drive */
  185.     tmp = intdos(&r,&r);
  186.     if (r.x.cflag != 0) {
  187.         printf("Error reading def. drive: %d\n",tmp);
  188.         exit(1);
  189.     }
  190.     return(r.h.al);
  191. }
  192.