From a29805cad22fdc6159c0278be136c1dee6b63a5e Mon Sep 17 00:00:00 2001 From: zegonix Date: Wed, 1 Jul 2026 20:50:09 +0200 Subject: [PATCH] structure and group directories --- Makefile | 4 +- inc/arguments.h | 1 + inc/sorting.h | 13 +++++ inc/stringing.h | 18 +++++++ inc/type_definitions.h | 14 +++++ src/arguments.c | 21 ++++++-- src/main.c | 74 +++++++++++--------------- src/sorting.c | 115 +++++++++++++++++++++++++++++++++++++++++ src/stringing.c | 84 ++++++++++++++++++++++++++++++ 9 files changed, 294 insertions(+), 50 deletions(-) create mode 100644 inc/sorting.h create mode 100644 inc/stringing.h create mode 100644 inc/type_definitions.h create mode 100644 src/sorting.c create mode 100644 src/stringing.c diff --git a/Makefile b/Makefile index 58041ff..0f21da3 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,9 @@ C_DEF := C_SRC := main.c \ log.c \ - arguments.c + arguments.c \ + stringing.c \ + sorting.c C_FLAGS := -std=c11 \ $(WG) \ diff --git a/inc/arguments.h b/inc/arguments.h index 0ab2caf..e55088c 100644 --- a/inc/arguments.h +++ b/inc/arguments.h @@ -19,6 +19,7 @@ typedef enum { typedef struct { bool show_all; bool show_almost_all; + bool case_sensitive_sorting; bool no_colour; bool group_directories; bool human_readable; diff --git a/inc/sorting.h b/inc/sorting.h new file mode 100644 index 0000000..b0f9582 --- /dev/null +++ b/inc/sorting.h @@ -0,0 +1,13 @@ +#ifndef __SORTING_H +#define __SORTING_H + +#include "type_definitions.h" + +int compare_entries( const void* operand_a, const void* operand_b ); +int compare_entries_casesensitive( + const void* operand_a, + const void* operand_b +); +int group_directories( directory_entry_t** entry_table, int num_entries ); + +#endif diff --git a/inc/stringing.h b/inc/stringing.h new file mode 100644 index 0000000..6e82f28 --- /dev/null +++ b/inc/stringing.h @@ -0,0 +1,18 @@ +#ifndef __STRING_FUNCTIONS_H +#define __STRING_FUNCTIONS_H + +#include +#include + +char to_lower_case( char character ); +char to_upper_case( char character ); +int string_to_lower( char* const string ); +int string_to_upper( char* const string ); +int compare_strings( + const char* const a, + const char* const b, + size_t length, + bool case_sensitive +); + +#endif diff --git a/inc/type_definitions.h b/inc/type_definitions.h new file mode 100644 index 0000000..6183f13 --- /dev/null +++ b/inc/type_definitions.h @@ -0,0 +1,14 @@ +#ifndef __TYPE_DEFINITIONS_H +#define __TYPE_DEFINITIONS_H + +#include +#include +#include + +// type definitions +typedef struct directory_entry { + char name[ NAME_MAX ]; + struct stat stats; +} directory_entry_t; + +#endif diff --git a/src/arguments.c b/src/arguments.c index 0cc4467..bcda93a 100644 --- a/src/arguments.c +++ b/src/arguments.c @@ -7,7 +7,8 @@ // keys for arguments with no short version #define KEY_SHOW_ALL ( ( int )'a' ) #define KEY_SHOW_ALMOST_ALL ( ( int )'A' ) -#define KEY_NO_COLOUR ( ( int )0x100 ) +#define KEY_CASE_SENSITIVE ( ( int )'c' ) +#define KEY_NO_COLOUR ( ( int )0x101 ) #define KEY_GROUP_DIRECTORIES ( ( int )'g' ) #define KEY_HUMAN_READABLE ( ( int )'h' ) #define KEY_LONG_VIEW ( ( int )'l' ) @@ -42,6 +43,14 @@ static struct argp_option options[] = .doc = "same as `all`, but hiding `.` and `..`", .group = 0 }, + { + .name = "case-sensitive", + .key = KEY_CASE_SENSITIVE, + .arg = NULL, + .flags = 0, + .doc = "sort case sensitive", + .group = 0 + }, { .name = "no-color", .key = KEY_NO_COLOUR, @@ -110,6 +119,7 @@ void initialise_args( arguments_t* arguments ) { arguments->show_all = false; arguments->show_almost_all = false; + arguments->case_sensitive_sorting = false; arguments->no_colour = false; arguments->group_directories = false; arguments->human_readable = false; @@ -145,6 +155,9 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state ) case KEY_SHOW_ALMOST_ALL: arguments->show_almost_all = true; break; + case KEY_CASE_SENSITIVE: + arguments->case_sensitive_sorting = true; + break; case KEY_NO_COLOUR: arguments->no_colour = true; break; @@ -165,15 +178,15 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state ) { arguments->permission_mode = no_permissions; } - else if ( !strncmp( "none", arg, 4 ) ) + else if ( !strncmp( "none", arg, 5 ) ) { arguments->permission_mode = no_permissions; } - else if ( !strncmp( "char", arg, 4 ) ) + else if ( !strncmp( "char", arg, 5 ) ) { arguments->permission_mode = char_permissions; } - else if ( !strncmp( "octal", arg, 5 ) ) + else if ( !strncmp( "octal", arg, 6 ) ) { arguments->permission_mode = octal_permissions; } diff --git a/src/main.c b/src/main.c index e48d24c..0e437a5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,3 @@ -#include #define _XOPEN_SOURCE 700 // external dependencies @@ -7,9 +6,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -17,12 +14,8 @@ // internal dependencies #include "log.h" #include "arguments.h" - -// type definitions -typedef struct directory_entry { - char name[ NAME_MAX ]; - mode_t mode; -} directory_entry_t; +#include "type_definitions.h" +#include "sorting.h" // constant declarations char permission_characters[3] = { 'x', 'w', 'r' }; @@ -37,7 +30,6 @@ int get_entries( 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 ) @@ -47,6 +39,8 @@ int main( int argc, char** argv ) char permissions_string[10]; int ret; + // TODO: free memory before exiting on error! + initialise_args( &args ); parse_args( argc, argv, &args ); @@ -63,7 +57,8 @@ int main( int argc, char** argv ) } int num_entries = count_entries( directory, &args ); - if ( !num_entries ) return 0; + if ( 0 > num_entries ) return -EIO; + if ( 0 == num_entries ) return 0; directory_entry_t** entry_table = ( directory_entry_t** )calloc( @@ -79,22 +74,26 @@ int main( int argc, char** argv ) return ret; } - qsort( - ( void * )entry_table, - ( size_t )num_entries, - sizeof( *entry_table ), - ( __compar_fn_t )compare_entries - ); + // __compar_fn_t comparator = args.case_sensitive_sorting + // ? compare_entries_casesensitive : compare_entries; + // qsort( + // ( void * )entry_table, + // ( size_t )num_entries, + // sizeof( *entry_table ), + // comparator + // ); - // TODO: implement size aware output. - // - // struct winsize w; - // ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + ret = group_directories(entry_table, num_entries); + if ( 0 != ret ) + { + log_error( "grouping directories failed with error [%d]", ret ); + return ret; + } for ( int n = 0; n < num_entries; n++ ) { char* file_type; - switch ( entry_table[ n ]->mode & S_IFMT ) + switch ( entry_table[ n ]->stats.st_mode & S_IFMT ) { case S_IFBLK: file_type = "b"; @@ -130,7 +129,7 @@ int main( int argc, char** argv ) case char_permissions: permissions_to_string( - entry_table[ n ]->mode, + entry_table[ n ]->stats.st_mode, permissions_string ); log_info( @@ -142,7 +141,8 @@ int main( int argc, char** argv ) break; case octal_permissions: - permissions = mask_permissions( entry_table[ n ]->mode ); + permissions = + mask_permissions( entry_table[ n ]->stats.st_mode ); log_info( "%s %o %s", file_type, @@ -166,8 +166,8 @@ int main( int argc, char** argv ) int count_entries( DIR* directory, arguments_t* args ) { - if ( !directory ) return 0; - if ( !args ) return 0; + if ( !directory ) return -EINVAL; + if ( !args ) return -EINVAL; int entry_count = 0; struct dirent* entry; @@ -203,7 +203,7 @@ int get_entries( ) { if ( !directory ) return -EINVAL; - if ( !num_entries ) return -EINVAL; + if ( 0 >= num_entries ) return -EINVAL; if ( !args ) return -EINVAL; int entry_count = 0; @@ -212,7 +212,7 @@ int get_entries( char full_path[PATH_MAX]; struct stat file_stats = { 0 }; - if ( !entry_table ) + if ( !entry_table ) { log_error( "failed to allocate memory for the entry table" ); return -ENOMEM; @@ -253,7 +253,7 @@ int get_entries( if ( lstat( full_path, &file_stats ) ) return -EIO; strncpy( new_entry->name, entry->d_name, NAME_MAX ); - new_entry->mode = file_stats.st_mode; + new_entry->stats.st_mode = file_stats.st_mode; entry_table[entry_count] = new_entry; @@ -291,19 +291,3 @@ 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 ); -} diff --git a/src/sorting.c b/src/sorting.c new file mode 100644 index 0000000..7d0e837 --- /dev/null +++ b/src/sorting.c @@ -0,0 +1,115 @@ +#define _XOPEN_SOURCE 700 + +#include +#include +#include +#include "sorting.h" +#include "type_definitions.h" +#include "log.h" +#include "stringing.h" + +/* + * compare entries case insensitive + */ +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; + + return compare_strings( a->name, b->name, NAME_MAX, false ); +} + +/* + * compare entries case sensitive + */ +int compare_entries_casesensitive( + 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; + + return compare_strings( a->name, b->name, NAME_MAX, true ); +} + +int group_directories( directory_entry_t** entry_table, int num_entries ) +{ + if ( !entry_table ) return -EINVAL; + if ( 0 >= num_entries ) return -EINVAL; + + int directory_counter = 0; + int non_dir_counter = 0; + directory_entry_t** directory_table = + ( directory_entry_t** )calloc( + ( size_t )num_entries, + sizeof( directory_entry_t* ) + ); + if ( !directory_table ) return -ENOMEM; + + directory_entry_t** non_dir_table = + ( directory_entry_t** )calloc( + ( size_t )num_entries, + sizeof( directory_entry_t* ) + ); + if ( !non_dir_table ) + { + free( directory_table ); + return -ENOMEM; + } + + for ( int n = 0; n < num_entries; n++ ) + { + if ( entry_table[ n ]->stats.st_mode & S_IFDIR ) + { + directory_table[ directory_counter ] = entry_table[ n ]; + directory_counter++; + } + else + { + non_dir_table[ non_dir_counter ] = entry_table[ n ]; + non_dir_counter++; + } + } + if ( num_entries != directory_counter + non_dir_counter ) + log_error( "`group_directories()`: counting is difficult" ); + + qsort( + ( void * )directory_table, + ( size_t )directory_counter, + sizeof( *directory_table ), + compare_entries + ); + qsort( + ( void * )non_dir_table, + ( size_t )non_dir_counter, + sizeof( *non_dir_table ), + compare_entries + ); + + for ( int n = 0; n < directory_counter; n++ ) + { + entry_table[ n ] = directory_table[ n ]; + } + for ( int n = 0; n < non_dir_counter; n++ ) + { + entry_table[ directory_counter + n ] = non_dir_table[ n ]; + } + + free( directory_table ); + free( non_dir_table ); + + return 0; +} diff --git a/src/stringing.c b/src/stringing.c new file mode 100644 index 0000000..eac6301 --- /dev/null +++ b/src/stringing.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include "stringing.h" + +char to_lower_case( char character ) +{ + if ( 0x41 > character || 0x5A < character ) + return character; + + return character + ( char )0x20; +} + +char to_upper_case( char character ) +{ + if ( 0x61 > character || 0x7A < character ) + return character; + + return character - ( unsigned char )0x20; +} + +int string_to_lower( char* const string ) +{ + if ( !string ) return -EINVAL; + + int n = 0; + + while ( '\0' != string[ n ] && 0 <= n ) + { + string[ n ] = to_lower_case( string[ n ] ); + n++; + } + + return 0; +} + +int string_to_upper( char* const string ) +{ + if ( !string ) return -EINVAL; + + int n = 0; + + while ( '\0' != string[ n ] && 0 <= n ) + { + string[ n ] = to_upper_case( string[ n ] ); + n++; + } + + return 0; +} + +int compare_strings( + const char* const a, + const char* const b, + size_t length, + bool case_sensitive +) +{ + if ( !a || !b ) return -EINVAL; + + int result = 0; + + for ( size_t n = 0; n < length; n++ ) + { + if ( case_sensitive ) + { + result = ( int )a[ n ] - ( int )b[ n ]; + } + else + { + result = ( int )to_lower_case( a[ n ] ) + - ( int )to_lower_case( b[ n ] ); + } + + if ( result ) break; + + if ( '\0' == a[ n ] || '\0' == b[ n ] ) + { + break; + } + } + + return result; +}