home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / 32dudf12.zip / src / du.c < prev   
C/C++ Source or Header  |  1994-01-01  |  4KB  |  163 lines

  1. /**************************************************
  2. * DU (*nix like) for OS/2 v2.x & DOS
  3. * Copyright (c) 1993,1994 Timo Kokkonen.
  4. *
  5. * compiler: Borland C++ for OS/2 or DOS
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <dos.h>
  12.  
  13.  
  14. #ifdef __OS2__
  15.  #define VERSION_BITS "32"
  16.  #define FILEMASK "*"
  17. #else
  18.  #define VERSION_BITS "16"
  19.  #define FILEMASK "*.*"
  20. #endif
  21.  
  22. #define RELEASE_DATE "01-Jan-94"
  23. #define VERSION_NO   "1.11"
  24.  
  25.  
  26. /******************** GLOBAL VARIABLES *********************/
  27. int level=0;
  28. int sublevels=1;
  29. unsigned long cluster_size;
  30. unsigned long true_sum=0;
  31. unsigned long file_counter=0;
  32.  
  33. /***********************************************************/
  34.  
  35. void print_syntax(void)
  36. {
  37.   printf("\nSyntax: DU [directory] [/n]\n\n"
  38.      "  directory = pathname of base-directory\n"
  39.      "  n         = how many sub-directory levels to display,\n"
  40.      "              default = 1 (0 = all)\n\n");
  41.  
  42. }
  43.  
  44.  
  45. char *SplitDir(char *pathname)
  46. {
  47.   static char dirri[_MAX_PATH];
  48.   char drive[_MAX_DRIVE],
  49.        dir[_MAX_DIR],
  50.        file[_MAX_FNAME],
  51.        ext[_MAX_EXT];
  52.  
  53.   _splitpath(pathname,drive,dir,file,ext);
  54.   strcpy(dirri,drive);
  55.   strcat(dirri,dir);
  56.   return dirri;
  57. }
  58.  
  59.  
  60. int getDiskSize(unsigned char drive,unsigned long *size,
  61.                     unsigned long *free,
  62.                     unsigned long *clustersize)
  63. {
  64.   struct diskfree_t       data;
  65.  
  66.   if(_dos_getdiskfree(drive,&data)==0) {
  67.     *size=(unsigned long)data.total_clusters*data.bytes_per_sector*
  68.       data.sectors_per_cluster;
  69.     *free=(unsigned long)data.avail_clusters*data.bytes_per_sector*
  70.       data.sectors_per_cluster;
  71.     *clustersize=(unsigned long)data.bytes_per_sector*
  72.          data.sectors_per_cluster;
  73.     return 1;
  74.   }
  75.   *size=*free=*clustersize=0;
  76.   return 0;
  77. }
  78.  
  79.  
  80. unsigned long calc_dir(char *pathname)
  81. {
  82.   struct find_t  direntry;
  83.   char apu[_MAX_PATH];
  84.   int done;
  85.   unsigned long sum=0;
  86.  
  87.   level++;
  88.   strcpy(apu,pathname);
  89.   strcat(apu,FILEMASK);
  90.   done=_dos_findfirst(apu,_A_NORMAL+_A_RDONLY+_A_HIDDEN+_A_SYSTEM+
  91.               _A_SUBDIR+_A_ARCH,&direntry);
  92.   while (!done) {
  93.     sum+=direntry.size;
  94.     if (direntry.size % cluster_size == 0) true_sum+=direntry.size;
  95.      else true_sum+=cluster_size+direntry.size-(direntry.size%cluster_size);
  96.     if (!(direntry.attrib&_A_SUBDIR)) file_counter++;
  97.     if ((direntry.attrib  & _A_SUBDIR)&& (strcmp(direntry.name,".."))&&
  98.     (strcmp(direntry.name,"."))) {
  99.       strcpy(apu,pathname);
  100.       strcat(apu,direntry.name);
  101.       strcat(apu,"\\");
  102.       sum+=calc_dir(apu);
  103.     }
  104.     done=_dos_findnext(&direntry);
  105.   }
  106.  if ((level<=sublevels+1)||(sublevels<=0)) printf("%7luk %s\n",sum/1024,pathname);
  107.  level--;
  108.  return sum;
  109. }
  110.  
  111.  
  112. /*************** MAIN PROGRAM ******************************/
  113. int main(int arg_count, char **args)
  114. {
  115.   char startpath[_MAX_PATH], *apu;
  116.   char *defaultdir = ".";
  117.   unsigned long disk_size,disk_free, summa;
  118.   double slack;
  119.  
  120.   printf("\nDU/" VERSION_BITS "bit v" VERSION_NO
  121.      "     Copyright (c) Timo Kokkonen, OH6LXV               "
  122.      RELEASE_DATE "\n\n");
  123.  
  124.   strcpy(startpath,".");
  125.   if (arg_count>1) {
  126.     if (!strcmp(args[1],"/?")) {
  127.       print_syntax();
  128.       exit(0);
  129.     }
  130.     if (args[arg_count-1][0]=='/')
  131.      if (sscanf(&args[arg_count-1][1],"%d",&sublevels)!=1) {
  132.        printf("\nInvalid parameter '%s'.\n",args[arg_count-1]);
  133.        print_syntax();
  134.        exit(1);
  135.      }
  136.   }
  137.   if ((arg_count>=2)&&(args[1][0]!='/'))  apu=args[1];
  138.     else apu=defaultdir;
  139.  
  140.   if (!_fullpath(startpath,apu,_MAX_PATH)) {
  141.     printf("Cannot convert '%s' to absolute path.\n",apu);
  142.     exit(1);
  143.   }
  144.   if (startpath[strlen(startpath)-1]!='\\') strcat(startpath,"\\");
  145.   strcpy(startpath,SplitDir(startpath));
  146.  
  147.   if (!getDiskSize(startpath[0]-64,&disk_size,&disk_free,&cluster_size)) {
  148.     printf("Cannot read drive %c: \n",startpath[0]);
  149.     exit(1);
  150.   }
  151.   summa=calc_dir(startpath);
  152.   if (true_sum!=0) slack=(1-(double)summa/true_sum)*100.0;
  153.     else slack=0;
  154.  
  155.   // printf(" %lu  %lu  %lu \n",disk_size-disk_free,summa,true_sum);
  156.   printf("\n %lu bytes in %lu file(s), %1.1lf%% slack (%lu bytes).\n",
  157.                             true_sum,
  158.                             file_counter,
  159.                             slack,
  160.                             true_sum-summa);
  161.   return 0;
  162. }
  163.