/*
 * SIZEOF.C - utility program will return the total filesize of named argument
 *
 * Usage: sizeof: c ==> count total matches and summarize
 *		  i ==> list individual files if wildcard used
 *		  q ==> quiet mode, use for counting only
 *
 */

#include <stdio.h>
#include <dir.h>

static char * Program [] = { "sizeof - s. leoce",
			     "V1 R0 SVCLVL 1 @ " };

int main( int, char * [] );	/* main function decl */

static char * pfmterr = "usage: sizeof [-ciq] file [[-iq] file ... ]\n";
static char * popterr = "sizeof: illegal option";
static char * pnofile = "sizeof: no such file";

#define	FALSE	0
#define	TRUE	1

int main(argc, argv)
int argc;
char * argv[];
{

	register long total = 0;
	auto short int LineOption = 0;
	auto short int count = FALSE;
	auto short int indiv = FALSE;
	auto short int verbose = TRUE;
	auto int final  = 0, files = 0;
	auto char * file;	/* file name begin checked */

	if( argc < 2 )
		_exit( fprintf( stderr, "%s\n", pfmterr ) + 0x04 );

	while( --argc > 0 ) {
		++argv;		/* show first arg */
		if( ( LineOption = **argv ) == '-' || LineOption == '/')
			while( LineOption = *(++(*argv)) ) {	/* switch */
				switch( LineOption ) {
				case 'c':
					count = TRUE;
					break;
				case 'i':
					indiv = TRUE;
					break;
				case 'q':
					verbose = FALSE;
					break;
				default:
					fprintf(stderr,"%s %c\n", popterr,
						 LineOption);
				}
			}
		else {  /* try to process files, or nothing */
			auto found = FALSE;

			if( ! ( file = _wild( *argv ) ) ) {
				fprintf( stderr, "%s %s\n", pnofile,
					strupr( *argv ) );
				found = FALSE;
			}
			else do {	/* hit all files */
				auto struct ffblk ffblk;

				findfirst( file, &ffblk, 0 );
				total += ffblk.ff_fsize;
				if( indiv )
					fprintf( stdout, "%-25s %10ld\n",
						strupr(file), ffblk.ff_fsize);
				final++;
				files++;
				found = TRUE;

			} while( file = _wild( NULL ) );

			if( found && verbose )
				if( ! indiv ) {
					fprintf(stdout, "%-25s %10ld",
						strupr(*argv), total);
					if( _FAttr & WILDCARDS )
						fprintf(stdout,", %d",files);
					putchar( '\n' );
				}
				else
					fprintf( stdout, "%25s %10ld, %d (%s)\n",
					"Total:",total,files,strupr(*argv));

			indiv = FALSE;
			verbose = TRUE;
			total = 0L;
			files = 0;
		}
	}

	if( count && final )
		fprintf( stdout, "\n\t%d files\n", final );
}
	