From 9166ccdd84daf25bf3ab9ffcd3ebd34502536dee Mon Sep 17 00:00:00 2001 From: zegonix Date: Sun, 26 Jul 2026 22:26:28 +0200 Subject: [PATCH] fixed tree view --- src/main.c | 94 +++++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 54 deletions(-) diff --git a/src/main.c b/src/main.c index 5c697e8..680c5db 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,6 @@ static int permissions_to_string( mode_t permissions, char* string ); static int mask_permissions( mode_t file_mode ); static int count_entries( DIR* directory, const arguments_t* const args ); static int get_entries( - char* const path, DIR* directory, directory_entry_t** entry_table, int num_entries, @@ -42,10 +41,9 @@ static int get_entries( static int is_entry_visible( const arguments_t* const args, char* entry_name ); static int display_directory_contents( const arguments_t* const args, - char* const path, DIR* directory, size_t recursion_level, - bool is_last + size_t is_last ); // program logic @@ -68,14 +66,21 @@ int main( int argc, char** argv ) return -EIO; } - // TODO: handle return code - display_directory_contents( - &args, - args.path, - directory, - 0, - false - ); + if ( args.tree_view ) + log_info( "%s", args.path ); + + if ( !chdir( args.path ) ) + { + // TODO: handle return code + display_directory_contents( + &args, + directory, + 0, + 0 + ); + } + + closedir( directory ); return SUCCESS; } @@ -102,8 +107,8 @@ int count_entries( DIR* directory, const arguments_t* const args ) return entry_count; } +/* get entries of **current** directory */ int get_entries( - char* const path, DIR* directory, directory_entry_t** entry_table, int num_entries, @@ -118,7 +123,6 @@ int get_entries( int entry_count = 0; directory_entry_t* new_entry; struct dirent* entry; - char full_path[PATH_MAX]; struct stat file_stats = { 0 }; @@ -132,15 +136,7 @@ int get_entries( new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) ); if ( !new_entry ) return -ENOMEM; - snprintf( - full_path, - PATH_MAX, - "%s/%s", - path, - entry->d_name - ); - - if ( lstat( full_path, &file_stats ) ) return -EIO; + if ( lstat( entry->d_name, &file_stats ) ) return -EIO; strncpy( new_entry->name, entry->d_name, NAME_MAX ); new_entry->stats = file_stats; @@ -234,14 +230,14 @@ int is_entry_visible( const arguments_t* const args, char* entry_name ) int display_directory_contents( const arguments_t* const args, - char* const path, DIR* directory, size_t recursion_level, - bool is_last + size_t is_last ) { if ( !args ) return -EINVAL; if ( !directory ) return -EINVAL; + if ( recursion_level > args->depth ) return -EINVAL; char file_type; int permissions; @@ -260,7 +256,7 @@ int display_directory_contents( ); if ( !entry_table ) return -ENOMEM; - ret = get_entries( path, directory, entry_table, num_entries, args ); + ret = get_entries( directory, entry_table, num_entries, args ); if ( 0 > ret ) { log_error( @@ -392,12 +388,18 @@ int display_directory_contents( char tree[ NAME_MAX ] = ""; for ( size_t k = 0; k < recursion_level; k++ ) { - tree[ k ] = ' '; - tree[ k + 1 ] = ' '; - tree[ k + 2 ] = ' '; - tree[ k + 3 ] = ' '; + if ( !( is_last & 1 << k ) ) + { + tree[ 4 * k ] = '|'; + } + else + { + tree[ 4 * k ] = ' '; + } + tree[ 4 * k + 1 ] = ' '; + tree[ 4 * k + 2 ] = ' '; + tree[ 4 * k + 3 ] = ' '; } - if ( 0 < recursion_level && !is_last ) tree[ 0 ] = '|'; if ( num_entries != n + 1 ) { tree[ 4 * recursion_level ] = '+'; @@ -408,17 +410,16 @@ int display_directory_contents( } tree[ 4 * recursion_level + 1 ] = '-'; tree[ 4 * recursion_level + 2 ] = '-'; + tree[ 4 * recursion_level + 3 ] = ' '; log_info( - "%s %s", + "%s%s", tree, entry->name ); - if ( args->tree_view - && recursion_level < args->depth - && entry->type == type_directory - ) + if ( recursion_level < args->depth + && entry->type == type_directory ) { DIR* subdirectory = opendir(entry->name); if ( !subdirectory ) @@ -427,30 +428,16 @@ int display_directory_contents( } else { - char* entry_path = ( char* )calloc( - PATH_MAX, - sizeof(char) - ); - if ( !entry_path ) continue; - - snprintf( - entry_path, - PATH_MAX, - "%s/%s", - path, - entry->name - ); - + chdir( entry->name ); // TODO: handle return code display_directory_contents( args, - entry_path, subdirectory, recursion_level + 1, - num_entries == n + 1 + is_last | (size_t)( num_entries == n + 1 ) << recursion_level ); - - free( entry_path ); + closedir( subdirectory ); + chdir( ".." ); } } } @@ -462,7 +449,6 @@ int display_directory_contents( free( entry_table[ n ] ); } free( entry_table ); - closedir( directory ); return SUCCESS; }