home *** CD-ROM | disk | FTP | other *** search
- Fax : : (61 2) 805-7433 | This one goes up to 11.....
-
- /*
- * du.c: Calculate disk usage similar to UNIX version
- *
- * Compiles under TC. May need mods for MSC.
- *
- * PLEASE : If you make any improvements, send a copy to me so I can make
- * changes available to everyone.....
- *
- * Author: Mark Allsop (Internet address: mallsop@suna.mqcc.mq.oz.au)
- *
- * See #define VERSION and __DATE__ in compile for current version and date
- *
- * This software is placed into the public domain. The author grants the right
- * to anyone to alter this program, provided that any upgrades or modifications
- * are mailed back to him.
- * This program may not be sold for any profit. The source code may, however ,
- * be used as part of any other package PROVIDED appropriate credit is given in
- * the program source code AND documentation and that no fee is ever charged
- * for just the code in this program. This entire comment block MUST appear
- * in the code at the place of usage of the code in this module.
- * For further information, contact the author!
- * This disclaimer is intended to stop people ripping off my software, and
- * using what was free and making money from it. I call such people *scum*.
- * I encourage you to release other utilities into the public domain so that
- * life can be made easier for all of us.
- */
-
- /*
- * This currently changes the directories to find files. This is slow, but I
- * havn't had the time to do it properly yet. You should just use strings
- * and the path and cat/uncat them.
- */
-
- /*
- * Options:-
- * -a: generate an entry for every file
- * -s: only display grand total for each of the file names
- * -r: display error info about unreadable directories, etc.
- * -k: display sizes in Kb
- * -m: display sizes in Mb
- */
-
- #include <conio.h>
- #include <stdio.h>
- #include <string.h>
- #include <dos.h>
- #include <dir.h>
-
- #define VERSION "1.0"
- #define MAXPATHLEN 100
- #define conv_size(size) ((size_type == KB) ? (size) / 1024 : (size_type==MB) \
- ? (size) / 1048576 : (size))
-
- int c_break(void);
- void sw_flags (int *ac, char **av, char *letters, int *flags);
- unsigned long find_usage(char *path, int quiet);
- void du_help(char *fname);
-
- enum Size_type {BYTES, KB, MB} size_type;
-
- int all, disp_errs;
- int curr_disk, diff_drv;
- char cwd_org[MAXPATHLEN], cwd_org2[MAXPATHLEN];
-
- main(int ac, char **av)
- {
- /* all & disp_errs are global to save stack space during recusrion */
- int i, j, flags[5], quiet;
- char dir[MAXPATHLEN];
-
- if (av[1][0] == '?') {
- du_help(av[0]);
- return(0);
- }
-
- ctrlbrk(c_break); /* Catch ctrl-c's so you don't get left elsewhere */
-
- sw_flags(&ac, av, "arskm", flags);
- all = flags[0];
- disp_errs = flags[1];
- quiet = flags[2];
- size_type = (flags[4]) ? MB : ((flags[3]) ? KB : BYTES);
-
- printf("Sizes given are in %s\n", (size_type == KB) ? "Kb"
- : (size_type == MB) ? "Mb" : "bytes");
- getcwd(cwd_org, MAXPATHLEN); /* Save cwd so we can return to it */
- curr_disk = getdisk();
- if (ac == 1) {
- strcpy(dir, ".");
- printf("%12ld %s\n", conv_size(find_usage(dir, quiet)), dir);
- } else {
- for (i=1; i<ac; i++) {
- strcpy(dir, av[i]);
- if (dir[1] == ':' && strlen(dir) >= 2) {
- setdisk(toupper(dir[0])-'A');
- getcwd(cwd_org2, MAXPATHLEN); /* Save cwd so we can return to it */
- diff_drv=1; /* Diff drive - need to restore 2wice *
-
- j=2;
- while(dir[j] != '\0'){
- dir[j-2] = dir[j];
- j++;
- }
- dir[j-2] = dir[j];
- if (dir[0] == '\0') /* No name! Can't have that now, can we! */
- strcpy(dir, ".");
- }
- else
- diff_drv=0; /* Same drive as before */
- if (chdir(dir) == 0) /* Directory exists */
- printf("%12ld %s\n", conv_size(find_usage(dir, quiet)), dir);
- else
- printf("%s: No such file or directory\n", dir);
- setdisk(curr_disk);
- chdir(cwd_org); /* Return to original directory */
- }
- if (diff_drv) {
- chdir(cwd_org2); /* Reset for each command */
- diff_drv=0;
- }
- }
- if (diff_drv)
- chdir(cwd_org2); /* Reset for each command */
- setdisk(curr_disk);
- chdir(cwd_org); /* Return to original directory */
- return(0);
- }
-
- unsigned long find_usage(char *path, int quiet)
- {
- char dpath[MAXPATHLEN];
- struct ffblk fblk;
- unsigned long size = 0, tsiz;
-
- if (findfirst("*.*", &fblk, 0xff) == 0) {
- do {
- if (!strcmp(fblk.ff_name, ".") || !strcmp(fblk.ff_name, ".."))
- continue;
- switch (fblk.ff_attrib) {
- case FA_DIREC:
- size += (unsigned long)fblk.ff_fsize;
- if (chdir(fblk.ff_name) == 0) {
- sprintf(dpath, "%s\\%s", path, fblk.ff_name);
- tsiz = find_usage(dpath, quiet);
- if (!quiet)
- printf("%12ld %s\n", conv_size(tsiz), dpath);
- size += tsiz;
- chdir("..");
- }
- else if (disp_errs)
- printf("%s\\%s: Permission Denied\n", path, fblk.ff_name);
- break;
- case FA_LABEL:
- case FA_RDONLY:
- case FA_HIDDEN:
- case FA_SYSTEM:
- case FA_ARCH:
- default:
- size += (unsigned long)fblk.ff_fsize;
- if (all)
- printf("%12ld %s\\%s\n", conv_size(fblk.ff_fsize), path,
- fblk.ff_name);
- break;
- }
- } while (findnext(&fblk) == 0);
- }
- return(size); /* Default: can't have had any size! */
- }
-
- void du_help(char *fname)
- {
- printf("%s [-s][-a][-r][-k][-m] [list of names]: disk usage\n", fname);
- printf(" -s: Only display grand total for each specified name\n");
- printf(" -a: Generate an entry for each file\n");
- printf(" -r: Display errors for directories that can't be opened\n");
- printf(" -k: Display sizes in Kb\n");
- printf(" -m: Display sizes in Mb\n");
- printf("list of names: list of file/directory names for usage of.\n");
- printf(
- " Author: Mark Allsop, Internet address: mallsop@suna.mqcc.mq.oz.au.\n");
- printf(" Date: %s. Version: %s. Public domain program.\n",
- __DATE__, VERSION);
- }
-
- int c_break(void)
- {
- printf("User interrupt!\n");
- if (diff_drv)
- chdir(cwd_org2); /* Reset for each command */
- chdir(cwd_org); /* Return to original directory */
- setdisk(curr_disk);
- exit(1); /* They want out! */
- }
-
-
- /*
- * swflag.c: Parse flag type switches.
- *
- * sw_flag (&ac, av, "letters", flags)
- *
- * For each letter, if it is found in a switch which contains
- * only valid letters from the sequence "letters" then the corresponding
- * flag is set true, else set false. The flags count the number of occurrences
-
- * of the switch letter. Thus, the program can use repetition for
- * emphasis.
- * e.g. letter "v" for verbosity; -vv would mean extra verbose.
- *
- * HISTORY
- * 19-Sep-86 Leonard Hamey (lgh) at Carnegie-Mellon University
- * Fixed bug which allowed a lone hyphen to be matched as a flags
- * argument with no flags.
- *
- * 9-Aug-86 Leonard Hamey (lgh) at Carnegie-Mellon University
- * Created.
- */
-
- void sw_flags(ac, av, letters, flags)
- int *ac;
- register char **av;
- register char *letters;
- register int *flags;
- {
- register char *p;
- register int i, j;
- if (letters[0] == '-')
- letters++;
- for (i = 0; letters[i] != '\0'; i++)
- flags[i] = 0;
- for (i = *ac; --i >= 1; )
- {
- p = av[i];
- if (*p == '-' && p[1] != '\0')
- {
- for (p++; *p != '\0'; p++)
- if (strchr (letters, *p) == 0)
- break;
- if (*p != '\0')
- continue;
- for (p = av[i]+1; *p != '\0'; p++)
- { /*
- * Count occurrences of the flag
- */
- flags[strchr(letters,*p) - letters]++;
- }
- /*
- * Discard argument.
- */
- (*ac)--;
- for (j = i; j < *ac; j++)
- av[j] = av[j+1];
- av[j] = NULL;
- }
- }
- }
-
-