home *** CD-ROM | disk | FTP | other *** search
- /* ******************************************** */
- /* TREE.COM substitute (c) UK 1992 Rob Staveley */
- /* Public Domain property */
- /* */
- /* Compiled with Borland C++ 3.0 */
- /* BCC -mt -tDc STREE.C */
- /* */
- /* To say hello, mail : rstaveley@cix */
- /* ******************************************** */
-
- #ifndef __TINY__
- #error Use TINY model and link as COM file
- #endif
-
- #include <dir.h>
- #include <dos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- struct fileSize {
- long bytesActual, /* Files are sized by disk space occupation */
- bytesOccupation; /* as well as actual size (1 cluster = min) */
- };
-
- static int dircount = 0, /* tally of files and directories */
- filecount = 0,
- a = 0, /* ASCII only */
- f = 0; /* display files */
- static long bpc;
-
- static char *strut[] = { /* graphic characters with ASCII alternvs */
- " \xb3",
- " |"
- };
-
- static char *elbow[] = {
- " \xc3\xc4",
- " +-"
- };
-
- static char *elbeq[] = {
- " \xd4\xcd\xcd",
- " +=="
- };
-
- static char here[] = "."; /* current directory */
-
- static const char usagestr[] =
- "Designed as a replacement for DOS 5.0's TREE command, which"
- "\ngraphically displays the directory structure of a drive or path."
- "\n\nSTREE [drive:][path] [/F] [/A]"
- "\n\n\t/F Displays the names of the files in each directory."
- "\n\n\t/A Uses ASCII instead of extended IBM characters.";
-
- static struct fileSize sizedir(
- const char *pathname,
- const char *prefix,
- int nesting
- );
-
- int main(
- int argc,
- char *argv[]
- ) {
-
- char olddir[ MAXPATH ],
- prefix[ MAXPATH ],
- argv1[ MAXPATH ];
-
- int rtnval,
- thendrive,
- nowdrive;
-
- struct dfree free;
- struct fileSize byteCount = { 0, 0 };
-
- fprintf( stderr, "\nSubstitute TREE (c) UK 1992 Rob Staveley\n\n" );
-
- strcpy( argv1, here );
- thendrive = getdisk();
- thendrive++;
- if ( argc > 1 ) {
- int i = 0, j = 1;
- while ( j < argc ) {
- if (
- strlen( argv[ j ] ) > 1 && (
- ( argv[ j ][ 0 ] == '-' )
- ||
- ( argv[ j ][ 0 ] == '/' )
- )
- )
- {
- switch ( argv[ j ][ 1 ] ) {
- case 'a':; case 'A':
- a = 1; break;
- case 'f':; case 'F':
- f = 1; break;
- case '?':; case 'H':; case 'h':
- fprintf( stderr, usagestr );
- exit( 100 );
- default:
- i = j;
- }
- }
- else
- i = j;
- j++;
- }
- if ( i ) {
- strncpy( argv1, argv[ i ], MAXPATH - 1 );
- strupr( argv1 );
- if ( argv1[ 1 ] == ':' ) {
- if ( _chdrive( argv1[ 0 ] - 'A' + 1 ) ) {
- fprintf( stderr, "Unable to access drive %c\n", argv1[ 0 ] );
- exit( 98 );
- }
- if ( strlen( argv[ i ] ) > 2 )
- strncpy( argv1, &argv[ i ][ 2 ], MAXPATH - 1 );
- else
- strcpy( argv1, here );
- strupr( argv1 );
- }
- else
- thendrive = 0;
- }
- }
-
- if ( strlen( argv1 ) > 1 && argv1[ strlen( argv1 ) - 1 ] == '\\' )
- argv1[ strlen( argv1 ) - 1 ] = 0;
-
- setcbrk( 1 );
- getcwd( olddir, MAXPATH );
-
- if ( chdir( argv1 ) ) {
- fprintf( stderr, "Access to [%s] denied\n", argv1 );
- if ( thendrive )
- _chdrive( thendrive );
- exit( errno );
- }
-
-
- fprintf( stderr, "Current dir [%s]\n", olddir);
- fprintf( stderr, "Inspecting [%s]\n\n", argv1 );
-
- strcpy( prefix, olddir );
- if (
- strlen( olddir ) != 3
- && argv1[ 0 ] != '\\'
- )
- strcat( prefix, "\\" );
- strcat( prefix, argv1 );
- if ( strstr( argv1, ".." ) != NULL )
- strcpy( prefix, argv1 );
- if ( prefix[ strlen( prefix ) - 1 ] != '\\' )
- strcat( prefix, "\\" );
-
- nowdrive = getdisk();
- getdfree( nowdrive + 1, &free );
- if ( free.df_sclus == 0xFFFF ) {
- fprintf( stderr, "\nEstimating 512 bytes per cluster\n" );
- bpc = 512;
- }
- else
- bpc = free.df_bsec * free.df_sclus;
-
- byteCount = sizedir( "", strupr( prefix ), 0 );
- fprintf( stderr,
- "\nProfile:\n\tFiles :\t%d\n\tDirs :\t%d\n"
- "\tBytes :\t%ld (%ld)\n\tFree :\t%ld\n",
- filecount, dircount,
- byteCount.bytesActual, byteCount.bytesOccupation,
- bpc * free.df_avail
- );
- chdir( olddir );
- if ( thendrive )
- _chdrive( thendrive );
- return 0;
- }
-
- static long diskOcc( long actualBytes ) {
- return bpc * ( actualBytes / bpc + ( ( actualBytes % bpc ) != 0 ) );
- }
-
- static struct fileSize sizedir(
- const char *pathname, const char *prefix, int nesting
- ) {
- int done,
- lfilecount = 0, /* local tallies */
- localdirs = 0;
- static int hello = 0; /* static marker */
- struct fileSize rtnval = { 0, 0 },
- localbytes = { 0, 0 };
- struct ffblk ffblk;
- char pathspec[ MAXPATH ];
- if ( hello++ ) {
- for ( done = 0; done < nesting - 1; done++ )
- printf( strut[ a ] );
- printf( elbow[ a ] );
- }
- getcwd( pathspec, MAXPATH );
- if ( pathspec[ strlen( pathspec ) - 1 ] == '\\' )
- pathspec[ strlen( pathspec ) - 1 ] = 0;
- printf( "%s", pathspec );
- strcpy( pathspec, pathname );
- printf( "\\%s\n", pathspec );
- strcat( pathspec, "*.*" );
- done = findfirst( pathspec, &ffblk, 0x10 );
- while ( !done ) {
- if ( ffblk.ff_attrib == 0x10 ) {
- if ( ffblk.ff_name[0] != '.' ) {
- char *dirpath;
- struct fileSize ds;
- dirpath = malloc( strlen( pathname ) + strlen( ffblk.ff_name ) + 2 );
- strcpy( dirpath, pathname );
- strcat( dirpath, ffblk.ff_name );
- strcat( dirpath, "\\" );
- ds = sizedir( dirpath, prefix, nesting + 1 );
- rtnval.bytesOccupation += ds.bytesOccupation;
- rtnval.bytesActual += ds.bytesActual;
- dirpath[ strlen( dirpath ) - 1 ] = 0;
- dircount++; localdirs++;
- free( dirpath );
- }
- }
- else {
- if ( f ) {
- int done2;
- for ( done2 = -1; done2 < nesting; done2++ )
- printf( strut[ a ] );
- printf( " %s\n", ffblk.ff_name );
- }
- localbytes.bytesActual += ffblk.ff_fsize;
- localbytes.bytesOccupation += diskOcc( ffblk.ff_fsize );
- lfilecount++;
- }
- done=findnext( &ffblk );
- }
- for ( done = -1; done < nesting - 1; done++ )
- printf( strut[ a ] );
- filecount += lfilecount;
- rtnval.bytesActual += localbytes.bytesActual;
- rtnval.bytesOccupation += localbytes.bytesOccupation;
- printf(
- " %s %ld (%ld) bytes in %d files\n", elbeq[ a ],
- localbytes.bytesActual, localbytes.bytesOccupation, lfilecount
- );
- if ( localdirs ) {
- for ( done = -1; done < nesting - 1; done++ )
- printf( strut[ a ] );
- printf(
- " %ld (%ld) bytes including %d dirs\n",
- rtnval.bytesActual, rtnval.bytesOccupation, localdirs
- );
- }
- return rtnval;
- }
-
-
-