home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / unix / du_wc.arc / DU.C next >
Encoding:
C/C++ Source or Header  |  1989-04-10  |  3.4 KB  |  107 lines

  1. /************************************************************************
  2.  *   DU.C -- a *NIX-like utility program by James W. Birdsall
  3.  *      3/13/89
  4.  *      vers 1.3
  5.  *
  6.  *   usage: du [-s] [dir1] [dir2] [dir3...]
  7.  *
  8.  *   DU is almost exactly like the *NIX utility of the same name. It totals
  9.  *   the sizes of the files in each subdirectory and prints out same, with
  10.  *   the total for higher-level directories including all subdirectories
  11.  *   therein.
  12.  *      With no arguments, DU starts from the current directory, otherwise
  13.  *   it examines a series of directories specified on the command line.
  14.  *   A drive spec (e.g. a:, c:, etc.) is a valid argument as well.
  15.  *      The -s option displays the total for files in the listed directory
  16.  *   ONLY as well as the total which includes the subdirectories.
  17.  */
  18.  
  19.  
  20. #include <dir.h>
  21. #include <dos.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. int subtotal_flag = 0;
  26.  
  27. unsigned long dump(char *);
  28.  
  29. main(int argc, char *argv[])
  30. {
  31.    int i;
  32.  
  33.    if (argc == 1)
  34.       dump("*");
  35.      else
  36.       if (argc == 2 && argv[1][0] == '-' && (argv[1][1] == 's' || argv[1][1] == 'S')) {
  37.          subtotal_flag = 1;
  38.          dump("*");
  39.          }
  40.         else {
  41.          for(i = 1; i < argc; i++)
  42.             if (argv[i][0] == '-' && (argv[i][1] == 's' || argv[i][1] == 'S'))
  43.                subtotal_flag = 1;
  44.          for(i = 1; i < argc; i++)
  45.             if (argv[i][0] != '-') {
  46.                dump(argv[i]);
  47.                putch('\n');
  48.                }
  49.          }
  50.    exit(0);
  51. }
  52.  
  53.  
  54. unsigned long dump(char *direc)
  55. {
  56.    struct ffblk temp;
  57.    char path[MAXPATH], *temp2, temp3[MAXPATH];
  58.    unsigned long tempsum = 0;
  59.    unsigned long subtotal = 0;
  60.  
  61.    strcpy(path, direc);
  62. /* if the final character of the supplied path is not a backslash, append */
  63. /* a backslash and an asterisk, otherwise just append an asterisk */
  64.    if (direc[strlen(direc)-1] != '\\' && direc[strlen(direc)-1] != '/') {
  65.       if (direc[0] != '*')
  66.          strcat(path,"\\*");
  67.       }
  68.      else
  69.       strcat(path,"*");
  70. /* find a subdirectory */
  71.    if (!(findfirst(path, &temp, FA_DIREC)) && (temp.ff_attrib & FA_DIREC)) {
  72.       for(temp2 = path; *temp2 != '*'; temp2++) ;
  73.       *temp2 = '\0';
  74. /* add the name to the path and DUMP it */
  75. /* making sure that it isn't directory . or .. , both of which lead to */
  76. /* infinite recursion :-) */
  77. /* add the sizes of the subdirectories while we're at it */
  78.       if (temp.ff_name[0] != '.')
  79.          tempsum += dump(strcat(strcpy(temp3, path), temp.ff_name));
  80. /* find more subdirectories, subject to same limitations as above */
  81.       while (!(findnext(&temp)) && (temp.ff_attrib & FA_DIREC))
  82.          if (temp.ff_name[0] != '.')
  83.             tempsum += dump(strcat(strcpy(temp3, path), temp.ff_name));
  84.       }
  85. /* now all that's left are files, so add up the sizes */
  86.    strcat(path, "*.*");
  87. /* the actual adding */
  88.    if (!(findfirst(path, &temp, 0))) {
  89.       subtotal += temp.ff_fsize;
  90.       while(!(findnext(&temp)))
  91.          subtotal += temp.ff_fsize;
  92.       }
  93.    tempsum += subtotal;
  94. /* fix the path to be presentable (more or less) */
  95.    for (temp2 = path; *temp2 != '*'; temp2++) ;
  96.    if (temp2 == path)
  97.       strcpy(path, ".");
  98.      else
  99.       *(--temp2) = '\0';
  100. /* printout */
  101.    if (subtotal_flag)
  102.       printf("%-30s (%8ld) %8ld\n", path, subtotal, tempsum);
  103.      else
  104.       printf("%-30s %8ld\n",path, tempsum);
  105.    return (tempsum);
  106. }
  107.