From: adamr@pain.la.ca.us (Adam Ravid)
Newsgroups: comp.bbs.waffle
Subject: diskhog filter (sources)
Date: 19 Aug 92 11:30:53 GMT
Organization: Public Access Info Network  (818/776-1447)

/****
       diskhog filter, by: Adam A. Ravid (adamr@pain.la.ca.us) 1992 (waf165)

       suggested usage:
       (requires ms-dos v5 or higher)

       C:\> dir /s c:\user | diskhog | sort /r /+18 | trim 12 > c:\admin\log

       The above line will create a file "c:\admin\log" which lists the top
       10 diskhogs that have directories in the sub-directory tree c:\user.

       How it does it.
       The input and output are controlled by the built-in special operating
       system (DOS) io handlers called pipe (|) and redirection (>).
       Basically, the "dir /s c:\user" lists all the directories and sub-
       directories in and under the c:\user directory tree.  Instead of
       printing the result to the screen, the results are 'pipped' (printed)
       to the program called "diskhog".  "diskhog" massages the input and
       spits out a nice looking report, but instead of printing the results
       to the screen, they are pipped to the DOS program "sort".  Sort uses
       the /r (reverse) and /+ (offset) options to rearrange the report in
       order of worse offenders (ie: most space used).  The output is
       pipped to yet another program called "trim" that uses the arguement
       12 to specify that only the first 12 lines that "sort" spits out should
       be displayed.  Finally, trim's output is redirected from the screen to
       a file called "log" in the c:\admin directory.
****/


#include <stdio.h>
#include <time.h>
main()
{
    unsigned long atoul (char *);
    struct tm *timeptr; time_t date;

    char buf[BUFSIZ], tmpstr[64];
    char path[64];
    int i;

    struct { char name[13]; int files; unsigned long space; } user[500];
    int  num_users = 0;

    for (i=0; i<5;  i++) gets(buf);
    strcpy(path,&buf[13]);

    buf[0]='\0';
/*  printf("%s is the path.\n\n", path);  */
    strcpy(user[0].name,"total");

    while (!feof(stdin))
    {
        do {
            if (!strncmp(buf,"Directory",9)) break;
            gets(buf);
        } while (!feof(stdin));

        strcpy(tmpstr, &buf[14+strlen(path)]);
        if (!strchr(tmpstr,'\\'))
        {
            strcpy(user[++num_users].name,tmpstr);
            user[num_users].space =0L;
            user[num_users].files =0;
        }

        while ((!feof(stdin)) && (buf[0]!=' ')) gets(buf);

        strcpy(tmpstr,strtok(buf," \n"));
        user[num_users].files += (int)atoul(tmpstr) - 2;
        strtok(NULL," \n"); strcpy(tmpstr,strtok(NULL," \n"));
        user[num_users].space += atoul(tmpstr);

        user[0].files += user[num_users].files;
        user[0].space += user[num_users].space;
    }

    time(&date);
    timeptr = localtime(&date);

    printf("%s        (%02d/%02d/%02d)\n%s\n",
      "user_________________space____%_____files____%",
      timeptr->tm_mon + 1, timeptr->tm_mday, timeptr->tm_year,"");
/*    "-----------------------------------------------"); */
    for (i=0; i<num_users; i++)
       printf("%-14s%12lu  %3.0f    %6d  %3.0f\n", strlwr(user[i].name),
         user[i].space, (double)user[i].space / (double)user[0].space * 100. + 0.5.,
         user[i].files, (double)user[i].files / (double)user[0].files * 100. + 0.49999);
 
}

unsigned long atoul (char *s)
{
    unsigned long value=0L, base = 10L;
    while ((*s > 47) && (*s < 58)) value = base * value + (*s++ - 48);
    return (value);
}


/****************************************
The following is a seperate program
that I use with this one, it's called
TRIM and basicallly, it trims the output
to <arg1> number of lines.  Here it is
commented from this program:
-- cut --

#include <stdio.h>

main(int argc, char *argv[])
{
    int i=0; char buf[BUFSIZ];
    if (argc == 1)  fprintf(stderr,"usage: %s %s %s %s\n", argv[0],
                      "<# of lines>", "[< in-filespec]", "[> out-filespec]");
    else while (i++ < atoi(argv[1])) puts(gets(buf));
    putchar('\n');
}

-- cut --
******************************************/


--
adamr@pain.la.ca.us