added make target debug

This commit is contained in:
root committed 2026-06-28 22:34:22 +02:00
1 parent 2c17c15472
commit f2e8055172
3 files changed
+95 -58

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
set tui border-kind ascii
tui new-layout good {-horizontal src 2} 3 cmd 2
layout good
list main
+8
View File
@@ -30,6 +30,8 @@ WCC := -Wconversion \
-Wshadow \ -Wshadow \
-Wpointer-arith -Wpointer-arith
DEBUG_FLAGS := -O0 -g3
C_DEF := C_DEF :=
C_SRC := main.c \ C_SRC := main.c \
@@ -39,6 +41,7 @@ C_SRC := main.c \
C_FLAGS := -std=c11 \ C_FLAGS := -std=c11 \
$(WG) \ $(WG) \
$(WCC) \ $(WCC) \
$(DEBUG_FLAGS) \
$(addprefix -I, $(INC_DIR)) \ $(addprefix -I, $(INC_DIR)) \
$(addprefix -D, $(C_DEF)) $(addprefix -D, $(C_DEF))
@@ -50,6 +53,8 @@ LD_FLAGS := $(addprefix -l, $(LIB)) \
OBJ := $(addprefix $(OBJ_DIR)/, $(C_SRC:.c=.o)) OBJ := $(addprefix $(OBJ_DIR)/, $(C_SRC:.c=.o))
BIN := $(BIN_DIR)/$(NAME) BIN := $(BIN_DIR)/$(NAME)
GDBINIT := ./.gdbinit
# the flags file is required for `clangd` (lsp client) # the flags file is required for `clangd` (lsp client)
FLAGS_FILE := ./compile_flags.txt FLAGS_FILE := ./compile_flags.txt
@@ -114,3 +119,6 @@ test: $(BIN)
-A \ -A \
~/repositories/test-projects/zig/ ~/repositories/test-projects/zig/
debug: $(BIN)
$(GDB) --command=$(GDBINIT) $<
+83 -58
View File
@@ -1,3 +1,4 @@
#include <asm-generic/errno-base.h>
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
// external dependencies // external dependencies
@@ -21,7 +22,6 @@
typedef struct directory_entry { typedef struct directory_entry {
char name[ NAME_MAX ]; char name[ NAME_MAX ];
mode_t mode; mode_t mode;
struct directory_entry* next;
} directory_entry_t; } directory_entry_t;
// constant declarations // constant declarations
@@ -30,12 +30,14 @@ char permission_characters[3] = { 'x', 'w', 'r' };
// function prototypes // function prototypes
int permissions_to_string( mode_t permissions, char* string ); int permissions_to_string( mode_t permissions, char* string );
int mask_permissions( mode_t file_mode ); int mask_permissions( mode_t file_mode );
size_t count_entries( DIR* directory, arguments_t* args ); int count_entries( DIR* directory, arguments_t* args );
directory_entry_t* get_entries( int get_entries(
DIR* directory, DIR* directory,
size_t num_entries, directory_entry_t** entry_table,
int num_entries,
arguments_t* args arguments_t* args
); );
int compare_entries( const void* operand_a, const void* operand_b );
// program logic // program logic
int main( int argc, char** argv ) int main( int argc, char** argv )
@@ -43,6 +45,7 @@ int main( int argc, char** argv )
arguments_t args; arguments_t args;
int permissions; int permissions;
char permissions_string[10]; char permissions_string[10];
int ret;
initialise_args( &args ); initialise_args( &args );
parse_args( argc, argv, &args ); parse_args( argc, argv, &args );
@@ -59,28 +62,39 @@ int main( int argc, char** argv )
return -1; return -1;
} }
size_t num_entries = count_entries( directory, &args ); int num_entries = count_entries( directory, &args );
if ( !num_entries ) return 0; if ( !num_entries ) return 0;
directory_entry_t* entry_table = directory_entry_t** entry_table =
get_entries( directory, num_entries, &args ); ( directory_entry_t** )calloc(
if ( !entry_table ) ( 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" ); log_error( "failed to retrieve directory entries" );
return -EIO; return ret;
} }
qsort(
( void * )entry_table,
( size_t )num_entries,
sizeof( *entry_table ),
( __compar_fn_t )compare_entries
);
// TODO: implement size aware output. // TODO: implement size aware output.
// //
// struct winsize w; // struct winsize w;
// ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
directory_entry_t* current_entry = entry_table; for ( int n = 0; n < num_entries; n++ )
while ( NULL != current_entry )
{ {
char* file_type; char* file_type;
switch ( current_entry->mode & S_IFMT ) switch ( entry_table[ n ]->mode & S_IFMT )
{ {
case S_IFBLK: case S_IFBLK:
file_type = "b"; file_type = "b";
@@ -110,65 +124,60 @@ int main( int argc, char** argv )
log_info( log_info(
"%s %s", "%s %s",
file_type, file_type,
current_entry->name entry_table[ n ]->name
); );
break; break;
case char_permissions: case char_permissions:
permissions_to_string( permissions_to_string(
current_entry->mode, entry_table[ n ]->mode,
permissions_string permissions_string
); );
log_info( log_info(
"%s %s %s", "%s %s %s",
file_type, file_type,
permissions_string, permissions_string,
current_entry->name entry_table[ n ]->name
); );
break; break;
case octal_permissions: case octal_permissions:
permissions = mask_permissions( current_entry->mode ); permissions = mask_permissions( entry_table[ n ]->mode );
log_info( log_info(
"%s %o %s", "%s %o %s",
file_type, file_type,
permissions, permissions,
current_entry->name entry_table[ n ]->name
); );
break; break;
} }
current_entry = current_entry->next;
} }
// free all allocated elements/entries // free all allocated elements/entries
directory_entry_t* next_entry; for ( int n = 0; n < num_entries; n++ )
while ( entry_table && num_entries )
{ {
next_entry = entry_table->next; free( entry_table[ n ] );
free( entry_table );
entry_table = next_entry;
num_entries--;
} }
free( entry_table );
closedir( directory ); closedir( directory );
return 0; return 0;
} }
size_t count_entries( DIR* directory, arguments_t* args ) int count_entries( DIR* directory, arguments_t* args )
{ {
if ( !directory ) return 0; if ( !directory ) return 0;
if ( !args ) return 0; if ( !args ) return 0;
size_t entry_count = 0; int entry_count = 0;
struct dirent* entry; struct dirent* entry;
rewinddir( directory ); rewinddir( directory );
while ( NULL != ( entry = readdir( directory ) ) ) while ( NULL != ( entry = readdir( directory ) ) )
{ {
if ( !args->show_all && ( !strncmp( entry->d_name, ".", 1) if ( !args->show_all && ( !strncmp( entry->d_name, ".", 2)
|| !strncmp( entry->d_name, "..", 2 ) ) ) || !strncmp( entry->d_name, "..", 3 ) ) )
{ {
continue; continue;
} }
@@ -186,31 +195,33 @@ size_t count_entries( DIR* directory, arguments_t* args )
return entry_count; return entry_count;
} }
directory_entry_t* get_entries( int get_entries(
DIR* directory, DIR* directory,
size_t num_entries, directory_entry_t** entry_table,
int num_entries,
arguments_t* args arguments_t* args
) )
{ {
if ( !directory ) return NULL; if ( !directory ) return -EINVAL;
if ( !num_entries ) return NULL; if ( !num_entries ) return -EINVAL;
if ( !args ) return NULL; if ( !args ) return -EINVAL;
/* int entry_count = 0;
* make sure to initialise the pointers to NULL, as random values directory_entry_t* new_entry;
* 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;
struct dirent* entry; struct dirent* entry;
char full_path[PATH_MAX]; char full_path[PATH_MAX];
struct stat file_stats = { 0 }; struct stat file_stats = { 0 };
if ( !entry_table )
{
log_error( "failed to allocate memory for the entry table" );
return -ENOMEM;
}
rewinddir( directory ); 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. * it is important to compare test string length + 1.
@@ -228,9 +239,8 @@ directory_entry_t* get_entries(
continue; continue;
} }
new_element = ( directory_entry_t* )malloc( sizeof(directory_entry_t) ); new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) );
if ( !new_element ) return NULL; if ( !new_entry ) return -ENOMEM;
if ( !entry_table ) entry_table = new_element;
snprintf( snprintf(
full_path, full_path,
@@ -240,19 +250,17 @@ directory_entry_t* get_entries(
entry->d_name 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 ); strncpy( new_entry->name, entry->d_name, NAME_MAX );
new_element->mode = file_stats.st_mode; new_entry->mode = file_stats.st_mode;
new_element->next = NULL;
if ( NULL != last_element ) last_element->next = new_element; entry_table[entry_count] = new_entry;
last_element = new_element;
num_entries--; entry_count++;
} }
return entry_table; return ( int )entry_count;
} }
int mask_permissions( mode_t file_mode ) int mask_permissions( mode_t file_mode )
@@ -282,3 +290,20 @@ int permissions_to_string( mode_t file_mode, char* string )
return 0; 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 );
}