diff --git a/inc/arguments.h b/inc/arguments.h index e55088c..dcf8b5f 100644 --- a/inc/arguments.h +++ b/inc/arguments.h @@ -16,6 +16,13 @@ typedef enum { octal_permissions = 2 } permission_mode_t; +typedef enum { + sort_by_name = 0, + sort_by_extension = 1, + sort_by_time = 2, + sort_by_size = 3 +} sorting_mode_t; + typedef struct { bool show_all; bool show_almost_all; @@ -24,8 +31,10 @@ typedef struct { bool group_directories; bool human_readable; bool long_view; + bool reverse_order; bool tree_view; permission_mode_t permission_mode; + sorting_mode_t sorting_mode; char* path; } arguments_t; diff --git a/inc/sorting.h b/inc/sorting.h index b0f9582..3f28dcc 100644 --- a/inc/sorting.h +++ b/inc/sorting.h @@ -1,13 +1,14 @@ #ifndef __SORTING_H #define __SORTING_H +#include #include "type_definitions.h" +#include "arguments.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 sort_entries( + arguments_t* args, + directory_entry_t** const entry_table, + int num_entries ); -int group_directories( directory_entry_t** entry_table, int num_entries ); #endif diff --git a/src/arguments.c b/src/arguments.c index bcda93a..cc89d06 100644 --- a/src/arguments.c +++ b/src/arguments.c @@ -13,6 +13,8 @@ #define KEY_HUMAN_READABLE ( ( int )'h' ) #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' ) // function prototypes @@ -24,6 +26,7 @@ const char* argp_program_version = "scan version 0.1"; // global variable definitions static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view."; static char argument_description[] = "[path]"; +static char sort_description[] = "property to sort by: name, ext(ension), time, size"; static struct argp_option options[] = { @@ -91,6 +94,22 @@ static struct argp_option options[] = .doc = "use tree listing format", .group = 0 }, + { + .name = "reverse", + .key = KEY_REVERSE_ORDER, + .arg = NULL, + .flags = 0, + .doc = "reverse sorting order", + .group = 0 + }, + { + .name = "sort", + .key = KEY_PERMISSIONS, + .arg = "property", + .flags = OPTION_ARG_OPTIONAL, + .doc = sort_description, + .group = 0 + }, { .name = "tree", .key = KEY_TREE_VIEW, @@ -124,8 +143,10 @@ void initialise_args( arguments_t* arguments ) arguments->group_directories = false; arguments->human_readable = false; arguments->long_view = false; + arguments->reverse_order = false; arguments->tree_view = false; arguments->permission_mode = no_permissions; + arguments->sorting_mode = sort_by_name; arguments->path = "."; } @@ -173,6 +194,31 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state ) case KEY_TREE_VIEW: arguments->tree_view = true; break; + case KEY_REVERSE_ORDER: + arguments->reverse_order = true; + break; + case KEY_SORT_MODE: + if ( arg ) + { + if ( !strncmp( "name", arg, 5 ) ) + { + arguments->sorting_mode = sort_by_name; + } + else if ( !strncmp( "ext", arg, 4 ) + || !strncmp( "extension", arg, 10 ) ) + { + arguments->sorting_mode = sort_by_extension; + } + else if ( !strncmp( "time", arg, 5 ) ) + { + arguments->sorting_mode = sort_by_time; + } + else if ( !strncmp( "size", arg, 5 ) ) + { + arguments->sorting_mode = sort_by_size; + } + } + break; case KEY_PERMISSIONS: if ( !arg ) { diff --git a/src/main.c b/src/main.c index 0e437a5..b44b41f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,6 @@ +#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 700 +#endif // external dependencies #include @@ -30,6 +32,7 @@ int get_entries( int num_entries, arguments_t* args ); +int is_entry_visible( arguments_t* args, char* entry_name ); // program logic int main( int argc, char** argv ) @@ -74,16 +77,7 @@ int main( int argc, char** argv ) return ret; } - // __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 - // ); - - ret = group_directories(entry_table, num_entries); + ret = sort_entries(&args, entry_table, num_entries); if ( 0 != ret ) { log_error( "grouping directories failed with error [%d]", ret ); @@ -176,16 +170,7 @@ int count_entries( DIR* directory, arguments_t* args ) while ( NULL != ( entry = readdir( directory ) ) ) { - if ( !args->show_all && ( !strncmp( entry->d_name, ".", 2) - || !strncmp( entry->d_name, "..", 3 ) ) ) - { - continue; - } - - if ( !args->show_almost_all && '.' == entry->d_name[0] ) - { - continue; - } + if ( is_entry_visible( args, entry->d_name ) ) continue; entry_count++; } @@ -223,21 +208,7 @@ int get_entries( while ( NULL != ( entry = readdir( directory ) ) && entry_count < num_entries ) { - /* - * 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->d_name, ".", 2) - || !strncmp( entry->d_name, "..", 3 ) ) ) - { - continue; - } - - if ( !args->show_almost_all && ( '.' == entry->d_name[0] ) ) - { - continue; - } + if ( is_entry_visible( args, entry->d_name ) ) continue; new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) ); if ( !new_entry ) return -ENOMEM; @@ -291,3 +262,29 @@ int permissions_to_string( mode_t file_mode, char* string ) 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; +} diff --git a/src/sorting.c b/src/sorting.c index 7d0e837..ff54367 100644 --- a/src/sorting.c +++ b/src/sorting.c @@ -1,17 +1,63 @@ +#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 700 +#endif +#include #include #include #include #include "sorting.h" #include "type_definitions.h" +#include "arguments.h" #include "log.h" #include "stringing.h" +/* function prototypes */ +int comp_name_case_insensitive( + const void* operand_a, + const void* operand_b +); +int comp_name_case_sensitive( + const void* operand_a, + const void* operand_b +); +int sort_directories_first( + directory_entry_t** const entry_table, + int num_entries +); + +/* sort entries taking flags into account */ +int sort_entries( + arguments_t* args, + directory_entry_t **const entry_table, + int num_entries +) +{ + if ( !args ) return -EINVAL; + if ( !entry_table ) return -EINVAL; + if ( 0 > num_entries ) return -EINVAL; + + if ( true == args->group_directories ) + { + sort_directories_first( entry_table, num_entries ); + } + else + { + qsort( + ( void * )entry_table, + ( size_t )num_entries, + sizeof( *entry_table ), + comp_name_case_insensitive + ); + } + + return 0; +} + /* * compare entries case insensitive */ -int compare_entries( const void* operand_a, const void* operand_b ) +int comp_name_case_insensitive( const void* operand_a, const void* operand_b ) { if ( !operand_a || !operand_b ) { @@ -28,7 +74,7 @@ int compare_entries( const void* operand_a, const void* operand_b ) /* * compare entries case sensitive */ -int compare_entries_casesensitive( +int comp_name_case_sensitive( const void* operand_a, const void* operand_b ) @@ -45,7 +91,11 @@ int compare_entries_casesensitive( return compare_strings( a->name, b->name, NAME_MAX, true ); } -int group_directories( directory_entry_t** entry_table, int num_entries ) +/* group directories first */ +int sort_directories_first( + directory_entry_t** const entry_table, + int num_entries +) { if ( !entry_table ) return -EINVAL; if ( 0 >= num_entries ) return -EINVAL; @@ -90,13 +140,13 @@ int group_directories( directory_entry_t** entry_table, int num_entries ) ( void * )directory_table, ( size_t )directory_counter, sizeof( *directory_table ), - compare_entries + comp_name_case_insensitive ); qsort( ( void * )non_dir_table, ( size_t )non_dir_counter, sizeof( *non_dir_table ), - compare_entries + comp_name_case_insensitive ); for ( int n = 0; n < directory_counter; n++ )