#include #include #include #include #include #include "dirent.h" #ifndef MAX_PATH #define MAX_PATH 1023 #endif void processDir_0( char *directoryName, int indent ); void print_file_info( char *fullName, char *name, struct stat *buf, int indent ) { int isDir = 0; char *timeRepresentation; int i; for( i = 0; i < indent; i++ ) printf( " " ); printf( "%d\t", buf->st_size ); if( ( buf->st_mode & S_IFMT ) == S_IFDIR ) isDir = 1; if( isDir ) printf( " (DIR)" ); else printf( " " ); timeRepresentation = ctime( &buf->st_mtime ); timeRepresentation[ 24 ] = '\0'; printf( " %s", timeRepresentation ); printf( " %s", name ); printf( "\n" ); if( isDir ) { if( strcmp( name, "." ) == 0 || strcmp( name, ".." ) == 0 ) return; processDir_0( fullName, indent + 1 ); } } void processDir_0( char *directoryName, int indent ) { DIR *dp; struct stat buf; struct dirent *slot; char fullName[ MAX_PATH ]; dp = opendir( directoryName ); if( dp == NULL ) { fprintf( stderr, "Error opening directory\n" ); return; } for( slot = readdir( dp ); slot != NULL; slot = readdir( dp ) ) { sprintf( fullName, "%s/%s", directoryName, slot->d_name ); if( stat( fullName, &buf ) == -1 ) { fprintf( stderr, "Error statting file!\n" ); continue; } else print_file_info( fullName, slot->d_name, &buf, indent ); } closedir( dp ); } void processDir( char *directoryName ) { processDir_0( directoryName, 0 ); } int main( int argc, char * argv [ ] ) { int i; if( argc == 1 ) processDir( "." ); else { for( i = 1; i < argc; i++ ) processDir( argv[ i ] ); } return 0; }