From f2e8055172dfcc0dfbbbd5e839b1852a90f36c4a Mon Sep 17 00:00:00 2001 From: zegonix Date: Sun, 28 Jun 2026 22:34:22 +0200 Subject: [PATCH] added make target `debug` --- .gdbinit | 4 ++ Makefile | 8 +++ src/main.c | 149 +++++++++++++++++++++++++++++++---------------------- 3 files changed, 99 insertions(+), 62 deletions(-) create mode 100644 .gdbinit diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..a73df5e --- /dev/null +++ b/.gdbinit @@ -0,0 +1,4 @@ +set tui border-kind ascii +tui new-layout good {-horizontal src 2} 3 cmd 2 +layout good +list main diff --git a/Makefile b/Makefile index a9670a1..58041ff 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,8 @@ WCC := -Wconversion \ -Wshadow \ -Wpointer-arith +DEBUG_FLAGS := -O0 -g3 + C_DEF := C_SRC := main.c \ @@ -39,6 +41,7 @@ C_SRC := main.c \ C_FLAGS := -std=c11 \ $(WG) \ $(WCC) \ + $(DEBUG_FLAGS) \ $(addprefix -I, $(INC_DIR)) \ $(addprefix -D, $(C_DEF)) @@ -50,6 +53,8 @@ LD_FLAGS := $(addprefix -l, $(LIB)) \ OBJ := $(addprefix $(OBJ_DIR)/, $(C_SRC:.c=.o)) BIN := $(BIN_DIR)/$(NAME) +GDBINIT := ./.gdbinit + # the flags file is required for `clangd` (lsp client) FLAGS_FILE := ./compile_flags.txt @@ -114,3 +119,6 @@ test: $(BIN) -A \ ~/repositories/test-projects/zig/ +debug: $(BIN) + $(GDB) --command=$(GDBINIT) $< + diff --git a/src/main.c b/src/main.c index 500b2b7..e48d24c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #define _XOPEN_SOURCE 700 // external dependencies @@ -21,7 +22,6 @@ typedef struct directory_entry { char name[ NAME_MAX ]; mode_t mode; - struct directory_entry* next; } directory_entry_t; // constant declarations @@ -30,12 +30,14 @@ char permission_characters[3] = { 'x', 'w', 'r' }; // function prototypes int permissions_to_string( mode_t permissions, char* string ); int mask_permissions( mode_t file_mode ); -size_t count_entries( DIR* directory, arguments_t* args ); -directory_entry_t* get_entries( - DIR* directory, - size_t num_entries, - arguments_t* args - ); +int count_entries( DIR* directory, arguments_t* args ); +int get_entries( + DIR* directory, + directory_entry_t** entry_table, + int num_entries, + arguments_t* args +); +int compare_entries( const void* operand_a, const void* operand_b ); // program logic int main( int argc, char** argv ) @@ -43,6 +45,7 @@ int main( int argc, char** argv ) arguments_t args; int permissions; char permissions_string[10]; + int ret; initialise_args( &args ); parse_args( argc, argv, &args ); @@ -59,28 +62,39 @@ int main( int argc, char** argv ) return -1; } - size_t num_entries = count_entries( directory, &args ); + int num_entries = count_entries( directory, &args ); if ( !num_entries ) return 0; - directory_entry_t* entry_table = - get_entries( directory, num_entries, &args ); - if ( !entry_table ) + directory_entry_t** entry_table = + ( directory_entry_t** )calloc( + ( size_t )num_entries, + sizeof( directory_entry_t* ) + ); + if ( !entry_table ) return -ENOMEM; + + ret = get_entries( directory, entry_table, num_entries, &args ); + if ( 0 > ret ) { - log_info( "`get_entries()` failed" ); - return -EIO; + log_error( "failed to retrieve directory entries" ); + return ret; } + qsort( + ( void * )entry_table, + ( size_t )num_entries, + sizeof( *entry_table ), + ( __compar_fn_t )compare_entries + ); + // TODO: implement size aware output. // // struct winsize w; // ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - directory_entry_t* current_entry = entry_table; - - while ( NULL != current_entry ) + for ( int n = 0; n < num_entries; n++ ) { char* file_type; - switch ( current_entry->mode & S_IFMT ) + switch ( entry_table[ n ]->mode & S_IFMT ) { case S_IFBLK: file_type = "b"; @@ -110,65 +124,60 @@ int main( int argc, char** argv ) log_info( "%s %s", file_type, - current_entry->name + entry_table[ n ]->name ); break; case char_permissions: permissions_to_string( - current_entry->mode, + entry_table[ n ]->mode, permissions_string ); log_info( "%s %s %s", file_type, permissions_string, - current_entry->name + entry_table[ n ]->name ); break; case octal_permissions: - permissions = mask_permissions( current_entry->mode ); + permissions = mask_permissions( entry_table[ n ]->mode ); log_info( "%s %o %s", file_type, permissions, - current_entry->name + entry_table[ n ]->name ); break; } - - current_entry = current_entry->next; } // free all allocated elements/entries - directory_entry_t* next_entry; - while ( entry_table && num_entries ) + for ( int n = 0; n < num_entries; n++ ) { - next_entry = entry_table->next; - free( entry_table ); - entry_table = next_entry; - num_entries--; + free( entry_table[ n ] ); } + free( entry_table ); closedir( directory ); return 0; } -size_t count_entries( DIR* directory, arguments_t* args ) +int count_entries( DIR* directory, arguments_t* args ) { if ( !directory ) return 0; if ( !args ) return 0; - size_t entry_count = 0; + int entry_count = 0; struct dirent* entry; rewinddir( directory ); while ( NULL != ( entry = readdir( directory ) ) ) { - if ( !args->show_all && ( !strncmp( entry->d_name, ".", 1) - || !strncmp( entry->d_name, "..", 2 ) ) ) + if ( !args->show_all && ( !strncmp( entry->d_name, ".", 2) + || !strncmp( entry->d_name, "..", 3 ) ) ) { continue; } @@ -186,31 +195,33 @@ size_t count_entries( DIR* directory, arguments_t* args ) return entry_count; } -directory_entry_t* get_entries( - DIR* directory, - size_t num_entries, - arguments_t* args - ) +int get_entries( + DIR* directory, + directory_entry_t** entry_table, + int num_entries, + arguments_t* args +) { - if ( !directory ) return NULL; - if ( !num_entries ) return NULL; - if ( !args ) return NULL; + if ( !directory ) return -EINVAL; + if ( !num_entries ) return -EINVAL; + if ( !args ) return -EINVAL; - /* - * make sure to initialise the pointers to NULL, as random values - * will not be caught by `if ( !pointer )` and cause a - * segmentation fault on dereferencing. - */ - directory_entry_t* entry_table = NULL; - directory_entry_t* last_element = NULL; - directory_entry_t* new_element = NULL; + 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 ) ) && num_entries ) + while ( NULL != ( entry = readdir( directory ) ) + && entry_count < num_entries ) { /* * it is important to compare test string length + 1. @@ -228,9 +239,8 @@ directory_entry_t* get_entries( continue; } - new_element = ( directory_entry_t* )malloc( sizeof(directory_entry_t) ); - if ( !new_element ) return NULL; - if ( !entry_table ) entry_table = new_element; + new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) ); + if ( !new_entry ) return -ENOMEM; snprintf( full_path, @@ -240,19 +250,17 @@ directory_entry_t* get_entries( entry->d_name ); - if ( lstat( full_path, &file_stats ) ) return NULL; + if ( lstat( full_path, &file_stats ) ) return -EIO; - strncpy( new_element->name, entry->d_name, NAME_MAX ); - new_element->mode = file_stats.st_mode; - new_element->next = NULL; + strncpy( new_entry->name, entry->d_name, NAME_MAX ); + new_entry->mode = file_stats.st_mode; - if ( NULL != last_element ) last_element->next = new_element; - last_element = new_element; + entry_table[entry_count] = new_entry; - num_entries--; + entry_count++; } - return entry_table; + return ( int )entry_count; } int mask_permissions( mode_t file_mode ) @@ -282,3 +290,20 @@ int permissions_to_string( mode_t file_mode, char* string ) return 0; } + +int compare_entries( const void* operand_a, const void* operand_b ) +{ + if ( !operand_a || !operand_b ) + { + log_error( "invalid parameter to compare function" ); + return -EINVAL; + } + + directory_entry_t* a = ( directory_entry_t* ) operand_a; + directory_entry_t* b = ( directory_entry_t* ) operand_b; + + log_info( "a = %s", a->name ); + log_info( "b = %s", b->name ); + + return strncmp( a->name, b->name, NAME_MAX ); +}