/* ECP: FILEname=fig13_9.c */ /* 1*/ long /* 2*/ du( const char *DirName ) /* 3*/ { /* 4*/ DIR *DirPtr; /* Pointer To Directory Stream */ /* 5*/ struct dirent *Slot; /* Slot in Directory */ /* 6*/ struct stat StatBuf; /* 7*/ long Blocks = 0L; /* 8*/ char FileName[ MAXPATHLEN ]; /* 9*/ if( ( DirPtr = opendir( DirName ) ) == NULL ) /*10*/ { /*11*/ perror( DirName ); /*12*/ return 0L; /*13*/ } /*14*/ while( ( Slot = readdir( DirPtr ) ) != NULL ) /*15*/ { /*16*/ if( strcmp( Slot->d_name, ".." ) == 0 ) /*17*/ continue; /* Skip Parent */ /*18*/ sprintf( FileName, "%s/%s", DirName, Slot->d_name ); /*19*/ if( lstat( FileName, &StatBuf ) != 0 ) /*20*/ { /*21*/ perror( FileName ); /*22*/ continue; /*23*/ } /*24*/ if ( S_ISDIR( StatBuf.st_mode ) ) /* Directories */ /*25*/ { /*26*/ if( strcmp( Slot->d_name, "." ) == 0 ) /*27*/ Blocks += StatBuf.st_blocks; /*28*/ else /*29*/ Blocks += du( FileName ); /*30*/ continue; /*31*/ } /*32*/ /* Print The Answer in 1k Blocks, Not 512 Byte Blocks */ /*33*/ if( aflag ) /*34*/ printf( "%-8ld%s\n", StatBuf.st_blocks/2, FileName ); /*35*/ Blocks += StatBuf.st_blocks; /*36*/ } /*37*/ if( Blocks ) /*38*/ printf( "%-8ld%s\n", Blocks/2, DirName ); /*39*/ closedir( DirPtr ); /*40*/ return Blocks; /*41*/ }