From 6fb01cfe29d058c2728a9522080621140b3ff64d Mon Sep 17 00:00:00 2001 From: zegonix Date: Sat, 25 Jul 2026 23:57:38 +0200 Subject: [PATCH] implemented tree view --- Makefile | 6 - inc/arguments.h | 1 + inc/sorting.h | 2 +- inc/type_definitions.h | 17 +- src/arguments.c | 26 ++- src/main.c | 459 ++++++++++++++++++++++++++--------------- src/sorting.c | 2 +- 7 files changed, 336 insertions(+), 177 deletions(-) diff --git a/Makefile b/Makefile index 0f21da3..fda9399 100644 --- a/Makefile +++ b/Makefile @@ -115,12 +115,6 @@ $(FLAGS_FILE): @echo "$(C_FLAGS) $(LD_FLAGS)" | sed 's/ /\n/g' > $@ -test: $(BIN) - $(BIN) \ - --permissions=octal \ - -A \ - ~/repositories/test-projects/zig/ - debug: $(BIN) $(GDB) --command=$(GDBINIT) $< diff --git a/inc/arguments.h b/inc/arguments.h index 5ea2d19..8297314 100644 --- a/inc/arguments.h +++ b/inc/arguments.h @@ -27,6 +27,7 @@ typedef struct { bool show_all; bool show_almost_all; bool case_sensitive_sorting; + size_t depth; bool no_colour; bool group_directories; bool long_view; diff --git a/inc/sorting.h b/inc/sorting.h index 3f28dcc..5ca8222 100644 --- a/inc/sorting.h +++ b/inc/sorting.h @@ -6,7 +6,7 @@ #include "arguments.h" int sort_entries( - arguments_t* args, + const arguments_t* const args, directory_entry_t** const entry_table, int num_entries ); diff --git a/inc/type_definitions.h b/inc/type_definitions.h index 6183f13..70e94bd 100644 --- a/inc/type_definitions.h +++ b/inc/type_definitions.h @@ -1,14 +1,29 @@ #ifndef __TYPE_DEFINITIONS_H #define __TYPE_DEFINITIONS_H +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 700 +#endif + #include #include #include // type definitions -typedef struct directory_entry { +typedef enum { + type_directory, + type_character, + type_block, + type_regular, + type_fifo, + type_link, + type_socket, +} entry_type_t; + +typedef struct { char name[ NAME_MAX ]; struct stat stats; + entry_type_t type; } directory_entry_t; #endif diff --git a/src/arguments.c b/src/arguments.c index 8aca22d..bd35b7f 100644 --- a/src/arguments.c +++ b/src/arguments.c @@ -1,20 +1,25 @@ #include #include +#include #include #include "arguments.h" #include "log.h" +// constant definitions +#define MAX_TREE_DEPTH ( ( size_t )16 - 1 ) + // keys for arguments with no short version #define KEY_SHOW_ALL ( ( int )'a' ) #define KEY_SHOW_ALMOST_ALL ( ( int )'A' ) #define KEY_CASE_SENSITIVE ( ( int )'c' ) -#define KEY_NO_COLOUR ( ( int )0x101 ) +#define KEY_DEPTH ( ( int )'d' ) #define KEY_GROUP_DIRECTORIES ( ( int )'g' ) #define KEY_LONG_VIEW ( ( int )'l' ) #define KEY_PERMISSIONS ( ( int )'p' ) #define KEY_REVERSE_ORDER ( ( int )'r' ) #define KEY_SORT_MODE ( ( int )'s' ) #define KEY_TREE_VIEW ( ( int )'t' ) +#define KEY_NO_COLOUR ( ( int )0x101 ) // function prototypes static error_t arg_parser( int key, char* arg, struct argp_state* state ); @@ -54,6 +59,14 @@ static struct argp_option options[] = .doc = "sort case sensitive", .group = 0 }, + { + .name = "depth", + .key = KEY_DEPTH, + .arg = "number", + .flags = 0, + .doc = "number of directory levels to display in recursive mode", + .group = 0 + }, { .name = "no-color", .key = KEY_NO_COLOUR, @@ -131,6 +144,7 @@ void initialise_args( arguments_t* arguments ) arguments->show_all = false; arguments->show_almost_all = false; arguments->case_sensitive_sorting = false; + arguments->depth = MAX_TREE_DEPTH; arguments->no_colour = false; arguments->group_directories = false; arguments->long_view = false; @@ -170,6 +184,16 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state ) case KEY_CASE_SENSITIVE: arguments->case_sensitive_sorting = true; break; + case KEY_DEPTH: + if ( arg ) + { + errno = 0; + arguments->depth = ( size_t )strtol( arg, NULL, 10 ); + if ( errno ) return errno; + if ( MAX_TREE_DEPTH < arguments->depth ) + arguments->depth = MAX_TREE_DEPTH; + } + break; case KEY_NO_COLOUR: arguments->no_colour = true; break; diff --git a/src/main.c b/src/main.c index 9e70faa..5c697e8 100644 --- a/src/main.c +++ b/src/main.c @@ -20,6 +20,7 @@ #include "sorting.h" // macro definitions +#define SUCCESS 0 #define LENGTH_PERMISSIONS ( ( size_t )11 ) #define LENGTH_SIZE ( ( size_t )9 ) @@ -28,28 +29,29 @@ char permission_characters[] = { 'x', 'w', 'r' }; char size_characters[] = { 'B', 'k', 'M', 'G', 'T', 'P' }; // function prototypes -int permissions_to_string( mode_t permissions, char* string ); -int mask_permissions( mode_t file_mode ); -int count_entries( DIR* directory, arguments_t* args ); -int get_entries( +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, - arguments_t* args + const arguments_t* const args +); +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 ); -int is_entry_visible( arguments_t* args, char* entry_name ); // program logic int main( int argc, char** argv ) { arguments_t args; - char file_type; - int permissions; - char file_permissions[LENGTH_PERMISSIONS]; - char file_size[LENGTH_SIZE]; - int ret; - - // TODO: free memory before exiting on error! initialise_args( &args ); parse_args( argc, argv, &args ); @@ -63,12 +65,193 @@ int main( int argc, char** argv ) else log_error( "failed to open directory `%s`", args.path ); - return -1; + return -EIO; } - int num_entries = count_entries( directory, &args ); + // TODO: handle return code + display_directory_contents( + &args, + args.path, + directory, + 0, + false + ); + + return SUCCESS; +} + +int count_entries( DIR* directory, const arguments_t* const args ) +{ + if ( !directory ) return -EINVAL; + if ( !args ) return -EINVAL; + + int entry_count = 0; + struct dirent* entry; + + rewinddir( directory ); + + while ( NULL != ( entry = readdir( directory ) ) ) + { + if ( is_entry_visible( args, entry->d_name ) ) continue; + + entry_count++; + } + + rewinddir( directory ); + + return entry_count; +} + +int get_entries( + char* const path, + DIR* directory, + directory_entry_t** entry_table, + int num_entries, + const arguments_t* const args +) +{ + if ( !directory ) return -EINVAL; + if ( !entry_table ) return -EINVAL; + if ( 0 >= num_entries ) return -EINVAL; + if ( !args ) return -EINVAL; + + int entry_count = 0; + directory_entry_t* new_entry; + struct dirent* entry; + char full_path[PATH_MAX]; + struct stat file_stats = { 0 }; + + + rewinddir( directory ); + + while ( NULL != ( entry = readdir( directory ) ) + && entry_count < num_entries ) + { + if ( is_entry_visible( args, entry->d_name ) ) continue; + + 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; + + strncpy( new_entry->name, entry->d_name, NAME_MAX ); + new_entry->stats = file_stats; + switch ( file_stats.st_mode & S_IFMT ) + { + case S_IFDIR: + new_entry->type = type_directory; + break; + case S_IFCHR: + new_entry->type = type_character; + break; + case S_IFBLK: + new_entry->type = type_block; + break; + case S_IFREG: + new_entry->type = type_regular; + break; + case S_IFIFO: + new_entry->type = type_fifo; + break; + case S_IFLNK: + new_entry->type = type_link; + break; + case S_IFSOCK: + new_entry->type = type_socket; + break; + } + + entry_table[entry_count] = new_entry; + + entry_count++; + } + + return ( int )entry_count; +} + +int mask_permissions( mode_t file_mode ) +{ + return file_mode & ( S_IRWXU | S_IRWXG | S_IRWXO ); +} + +int permissions_to_string( mode_t file_mode, char* string ) +{ + if ( !string ) return -EINVAL; + + int n; + int permissions = mask_permissions( file_mode ); + + for ( n = 0; n < 9; n++ ) + { + if ( permissions & ( 1 << n ) ) + { + string[ 8 - n ] = permission_characters[ n % 3 ]; + } + else + { + string[ 8 - n ] = '-'; + } + } + string[ 9 ] = '\0'; + + return SUCCESS; +} + +/* check if entry should be displayed + * + * return 1 if entry should be ignored + * return 0 otherwise + */ +int is_entry_visible( const arguments_t* const args, char* entry_name ) +{ + /* + * it is important to compare test string length + 1. + * otherwise the check will succeed, if the name under test + * starts with the test string. + */ + if ( !args->show_all && ( !strncmp( entry_name, ".", 2) + || !strncmp( entry_name, "..", 3 ) ) ) + { + return 1; + } + + if ( !args->show_all && !args->show_almost_all + && ( '.' == entry_name[0] ) ) + { + return 1; + } + + return SUCCESS; +} + +int display_directory_contents( + const arguments_t* const args, + char* const path, + DIR* directory, + size_t recursion_level, + bool is_last +) +{ + if ( !args ) return -EINVAL; + if ( !directory ) return -EINVAL; + + char file_type; + int permissions; + char file_permissions[LENGTH_PERMISSIONS]; + char file_size[LENGTH_SIZE]; + int ret; + int num_entries = count_entries( directory, args ); + if ( 0 > num_entries ) return -EIO; - if ( 0 == num_entries ) return 0; + if ( 0 == num_entries ) return SUCCESS; directory_entry_t** entry_table = ( directory_entry_t** )calloc( @@ -77,14 +260,17 @@ int main( int argc, char** argv ) ); if ( !entry_table ) return -ENOMEM; - ret = get_entries( directory, entry_table, num_entries, &args ); + ret = get_entries( path, directory, entry_table, num_entries, args ); if ( 0 > ret ) { - log_error( "failed to retrieve directory entries" ); + log_error( + "failed to retrieve directory entries (error code = %d)", + ret + ); return ret; } - ret = sort_entries(&args, entry_table, num_entries); + ret = sort_entries( args, entry_table, num_entries ); if ( 0 != ret ) { log_error( "grouping directories failed with error [%d]", ret ); @@ -95,31 +281,31 @@ int main( int argc, char** argv ) { directory_entry_t* entry = entry_table[ n ]; - switch ( entry->stats.st_mode & S_IFMT ) + switch ( entry->type & S_IFMT ) { - case S_IFBLK: + case type_block: file_type = 'b'; break; - case S_IFCHR: + case type_character: file_type = 'c'; break; - case S_IFDIR: + case type_directory: file_type = 'd'; break; - case S_IFIFO: + case type_fifo: file_type = 'f'; break; - case S_IFLNK: + case type_link: file_type = 'l'; break; - case S_IFREG: + case type_regular: file_type = ' '; break; - case S_IFSOCK: + case type_socket: file_type = 's'; break; } - switch ( args.permission_mode ) + switch ( args->permission_mode ) { case no_permissions: file_permissions[0] = '\0'; @@ -191,13 +377,83 @@ int main( int argc, char** argv ) ); } - log_info( - "%c%s%s %s", - file_type, - file_permissions, - file_size, - entry->name - ); + if ( !args->tree_view ) + { + log_info( + "%c%s%s %s", + file_type, + file_permissions, + file_size, + entry->name + ); + } + else + { + 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 ( 0 < recursion_level && !is_last ) tree[ 0 ] = '|'; + if ( num_entries != n + 1 ) + { + tree[ 4 * recursion_level ] = '+'; + } + else + { + tree[ 4 * recursion_level ] = '`'; + } + tree[ 4 * recursion_level + 1 ] = '-'; + tree[ 4 * recursion_level + 2 ] = '-'; + + log_info( + "%s %s", + tree, + entry->name + ); + + if ( args->tree_view + && recursion_level < args->depth + && entry->type == type_directory + ) + { + DIR* subdirectory = opendir(entry->name); + if ( !subdirectory ) + { + log_error( "failed to open directory `%s`", entry->name ); + } + else + { + char* entry_path = ( char* )calloc( + PATH_MAX, + sizeof(char) + ); + if ( !entry_path ) continue; + + snprintf( + entry_path, + PATH_MAX, + "%s/%s", + path, + entry->name + ); + + // TODO: handle return code + display_directory_contents( + args, + entry_path, + subdirectory, + recursion_level + 1, + num_entries == n + 1 + ); + + free( entry_path ); + } + } + } } // free all allocated elements/entries @@ -208,136 +464,5 @@ int main( int argc, char** argv ) free( entry_table ); closedir( directory ); - return 0; -} - -int count_entries( DIR* directory, arguments_t* args ) -{ - if ( !directory ) return -EINVAL; - if ( !args ) return -EINVAL; - - int entry_count = 0; - struct dirent* entry; - - rewinddir( directory ); - - while ( NULL != ( entry = readdir( directory ) ) ) - { - if ( is_entry_visible( args, entry->d_name ) ) continue; - - entry_count++; - } - - rewinddir( directory ); - - return entry_count; -} - -int get_entries( - DIR* directory, - directory_entry_t** entry_table, - int num_entries, - arguments_t* args -) -{ - if ( !directory ) return -EINVAL; - if ( 0 >= num_entries ) return -EINVAL; - if ( !args ) return -EINVAL; - - int entry_count = 0; - directory_entry_t* new_entry; - struct dirent* entry; - char full_path[PATH_MAX]; - struct stat file_stats = { 0 }; - - if ( !entry_table ) - { - log_error( "failed to allocate memory for the entry table" ); - return -ENOMEM; - } - - rewinddir( directory ); - - while ( NULL != ( entry = readdir( directory ) ) - && entry_count < num_entries ) - { - if ( is_entry_visible( args, entry->d_name ) ) continue; - - new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) ); - if ( !new_entry ) return -ENOMEM; - - snprintf( - full_path, - PATH_MAX, - "%s/%s", - args->path, - entry->d_name - ); - - if ( lstat( full_path, &file_stats ) ) return -EIO; - - strncpy( new_entry->name, entry->d_name, NAME_MAX ); - new_entry->stats = file_stats; - - entry_table[entry_count] = new_entry; - - entry_count++; - } - - return ( int )entry_count; -} - -int mask_permissions( mode_t file_mode ) -{ - return file_mode & ( S_IRWXU | S_IRWXG | S_IRWXO ); -} - -int permissions_to_string( mode_t file_mode, char* string ) -{ - if ( !string ) return -EINVAL; - - int n; - int permissions = mask_permissions( file_mode ); - - for ( n = 0; n < 9; n++ ) - { - if ( permissions & ( 1 << n ) ) - { - string[ 8 - n ] = permission_characters[ n % 3 ]; - } - else - { - string[ 8 - n ] = '-'; - } - } - string[ 9 ] = '\0'; - - return 0; -} - -/* check if entry should be displayed - * - * return 1 if entry should be ignored - * return 0 otherwise - */ -int is_entry_visible( arguments_t* args, char* entry_name ) -{ - /* - * it is important to compare test string length + 1. - * otherwise the check will succeed, if the name under test - * starts with the test string. - */ - if ( !args->show_all && ( !strncmp( entry_name, ".", 2) - || !strncmp( entry_name, "..", 3 ) ) ) - { - return 1; - } - - if ( !args->show_all && !args->show_almost_all - && ( '.' == entry_name[0] ) ) - { - return 1; - } - - return 0; + return SUCCESS; } diff --git a/src/sorting.c b/src/sorting.c index 6cc5e43..3867b9a 100644 --- a/src/sorting.c +++ b/src/sorting.c @@ -46,7 +46,7 @@ int sort_directories_first( /* sort entries taking flags into account */ int sort_entries( - arguments_t* args, + const arguments_t* const args, directory_entry_t **const entry_table, int num_entries )