#include #include #include #include #include #include #include #include #include #include "dirent.h" /* Separator Character */ #define SEPARATOR_CHAR "\\" char *programName; void listFiles( const char *directory ); void listFilesRec( const char *directory, int depth ) { DIR *dp; struct dirent *slot; struct stat buffer; char *timeRepresentation; char fullName[ MAX_PATH ]; int i; if( ( dp = opendir( directory ) ) == NULL ) { fprintf( stderr, "%s: Error opening %s\n", programName, directory ); return; } for( slot = readdir( dp ); slot != NULL; slot = readdir( dp ) ) { if( strcmp( slot->d_name, "." ) == 0 ) continue; if( strcmp( slot->d_name, ".." ) == 0 ) continue; sprintf( fullName, "%s%s%s", directory, SEPARATOR_CHAR, slot->d_name ); if( stat( fullName, &buffer ) != 0 ) { fprintf( stderr, "%s: Can't stat %s\n", programName, slot->d_name ); continue; } if( ( buffer.st_mode & _S_IFMT ) == _S_IFDIR ) printf( "(DIR)" ); else printf( " " ); timeRepresentation = ctime( &buffer.st_mtime ); timeRepresentation[ 24 ] = '\0'; for( i = 0; i < depth; i++ ) printf( " " ); printf( "%s", timeRepresentation ); printf( " %8ld", buffer.st_size ); printf( " %s", fullName ); printf( "\n" ); if( ( buffer.st_mode & _S_IFMT ) == _S_IFDIR ) listFilesRec( fullName, depth + 1 ); } closedir( dp ); } void listFiles( const char *directory ) { listFilesRec( directory, 0 ); } int main( int argc, char * argv[ ] ) { int i; programName = argv[ 0 ]; if( argc == 1 ) listFiles( "." ); else for( i = 1; i < argc; i++ ) listFiles( argv[ i ] ); return 0; }