home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * DU.C -- a *NIX-like utility program by James W. Birdsall
- * 3/13/89
- * vers 1.3
- *
- * usage: du [-s] [dir1] [dir2] [dir3...]
- *
- * DU is almost exactly like the *NIX utility of the same name. It totals
- * the sizes of the files in each subdirectory and prints out same, with
- * the total for higher-level directories including all subdirectories
- * therein.
- * With no arguments, DU starts from the current directory, otherwise
- * it examines a series of directories specified on the command line.
- * A drive spec (e.g. a:, c:, etc.) is a valid argument as well.
- * The -s option displays the total for files in the listed directory
- * ONLY as well as the total which includes the subdirectories.
- */
-
-
- #include <dir.h>
- #include <dos.h>
- #include <stdio.h>
- #include <string.h>
-
- int subtotal_flag = 0;
-
- unsigned long dump(char *);
-
- main(int argc, char *argv[])
- {
- int i;
-
- if (argc == 1)
- dump("*");
- else
- if (argc == 2 && argv[1][0] == '-' && (argv[1][1] == 's' || argv[1][1] == 'S')) {
- subtotal_flag = 1;
- dump("*");
- }
- else {
- for(i = 1; i < argc; i++)
- if (argv[i][0] == '-' && (argv[i][1] == 's' || argv[i][1] == 'S'))
- subtotal_flag = 1;
- for(i = 1; i < argc; i++)
- if (argv[i][0] != '-') {
- dump(argv[i]);
- putch('\n');
- }
- }
- exit(0);
- }
-
-
- unsigned long dump(char *direc)
- {
- struct ffblk temp;
- char path[MAXPATH], *temp2, temp3[MAXPATH];
- unsigned long tempsum = 0;
- unsigned long subtotal = 0;
-
- strcpy(path, direc);
- /* if the final character of the supplied path is not a backslash, append */
- /* a backslash and an asterisk, otherwise just append an asterisk */
- if (direc[strlen(direc)-1] != '\\' && direc[strlen(direc)-1] != '/') {
- if (direc[0] != '*')
- strcat(path,"\\*");
- }
- else
- strcat(path,"*");
- /* find a subdirectory */
- if (!(findfirst(path, &temp, FA_DIREC)) && (temp.ff_attrib & FA_DIREC)) {
- for(temp2 = path; *temp2 != '*'; temp2++) ;
- *temp2 = '\0';
- /* add the name to the path and DUMP it */
- /* making sure that it isn't directory . or .. , both of which lead to */
- /* infinite recursion :-) */
- /* add the sizes of the subdirectories while we're at it */
- if (temp.ff_name[0] != '.')
- tempsum += dump(strcat(strcpy(temp3, path), temp.ff_name));
- /* find more subdirectories, subject to same limitations as above */
- while (!(findnext(&temp)) && (temp.ff_attrib & FA_DIREC))
- if (temp.ff_name[0] != '.')
- tempsum += dump(strcat(strcpy(temp3, path), temp.ff_name));
- }
- /* now all that's left are files, so add up the sizes */
- strcat(path, "*.*");
- /* the actual adding */
- if (!(findfirst(path, &temp, 0))) {
- subtotal += temp.ff_fsize;
- while(!(findnext(&temp)))
- subtotal += temp.ff_fsize;
- }
- tempsum += subtotal;
- /* fix the path to be presentable (more or less) */
- for (temp2 = path; *temp2 != '*'; temp2++) ;
- if (temp2 == path)
- strcpy(path, ".");
- else
- *(--temp2) = '\0';
- /* printout */
- if (subtotal_flag)
- printf("%-30s (%8ld) %8ld\n", path, subtotal, tempsum);
- else
- printf("%-30s %8ld\n",path, tempsum);
- return (tempsum);
- }