implemented sorting by extension
This commit is contained in:
1 parent
cdfa9fec4e
commit
b81dcc10ac
4 files changed
+132
-76
No files matched your search
@@ -29,7 +29,6 @@ typedef struct {
|
|||||||
bool case_sensitive_sorting;
|
bool case_sensitive_sorting;
|
||||||
bool no_colour;
|
bool no_colour;
|
||||||
bool group_directories;
|
bool group_directories;
|
||||||
bool human_readable;
|
|
||||||
bool long_view;
|
bool long_view;
|
||||||
bool reverse_order;
|
bool reverse_order;
|
||||||
bool tree_view;
|
bool tree_view;
|
||||||
|
|||||||
+6
-17
@@ -10,7 +10,6 @@
|
|||||||
#define KEY_CASE_SENSITIVE ( ( int )'c' )
|
#define KEY_CASE_SENSITIVE ( ( int )'c' )
|
||||||
#define KEY_NO_COLOUR ( ( int )0x101 )
|
#define KEY_NO_COLOUR ( ( int )0x101 )
|
||||||
#define KEY_GROUP_DIRECTORIES ( ( int )'g' )
|
#define KEY_GROUP_DIRECTORIES ( ( int )'g' )
|
||||||
#define KEY_HUMAN_READABLE ( ( int )'h' )
|
|
||||||
#define KEY_LONG_VIEW ( ( int )'l' )
|
#define KEY_LONG_VIEW ( ( int )'l' )
|
||||||
#define KEY_PERMISSIONS ( ( int )'p' )
|
#define KEY_PERMISSIONS ( ( int )'p' )
|
||||||
#define KEY_REVERSE_ORDER ( ( int )'r' )
|
#define KEY_REVERSE_ORDER ( ( int )'r' )
|
||||||
@@ -26,6 +25,7 @@ const char* argp_program_version = "scan version 0.1";
|
|||||||
// global variable definitions
|
// global variable definitions
|
||||||
static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view.";
|
static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view.";
|
||||||
static char argument_description[] = "[path]";
|
static char argument_description[] = "[path]";
|
||||||
|
static char permissions_description[] = "display format for permissions: none, char(acters), octal";
|
||||||
static char sort_description[] = "property to sort by: name, ext(ension), time, size";
|
static char sort_description[] = "property to sort by: name, ext(ension), time, size";
|
||||||
|
|
||||||
static struct argp_option options[] =
|
static struct argp_option options[] =
|
||||||
@@ -70,14 +70,6 @@ static struct argp_option options[] =
|
|||||||
.doc = "group directories at the top",
|
.doc = "group directories at the top",
|
||||||
.group = 0
|
.group = 0
|
||||||
},
|
},
|
||||||
{
|
|
||||||
.name = "human-readable",
|
|
||||||
.key = KEY_HUMAN_READABLE,
|
|
||||||
.arg = NULL,
|
|
||||||
.flags = 0,
|
|
||||||
.doc = "show sizes in human readable format",
|
|
||||||
.group = 0
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
.name = "long",
|
.name = "long",
|
||||||
.key = KEY_LONG_VIEW,
|
.key = KEY_LONG_VIEW,
|
||||||
@@ -91,7 +83,7 @@ static struct argp_option options[] =
|
|||||||
.key = KEY_PERMISSIONS,
|
.key = KEY_PERMISSIONS,
|
||||||
.arg = "mode",
|
.arg = "mode",
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.doc = "use tree listing format",
|
.doc = permissions_description,
|
||||||
.group = 0
|
.group = 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -104,9 +96,9 @@ static struct argp_option options[] =
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "sort",
|
.name = "sort",
|
||||||
.key = KEY_PERMISSIONS,
|
.key = KEY_SORT_MODE,
|
||||||
.arg = "property",
|
.arg = "property",
|
||||||
.flags = OPTION_ARG_OPTIONAL,
|
.flags = 0,
|
||||||
.doc = sort_description,
|
.doc = sort_description,
|
||||||
.group = 0
|
.group = 0
|
||||||
},
|
},
|
||||||
@@ -141,7 +133,6 @@ void initialise_args( arguments_t* arguments )
|
|||||||
arguments->case_sensitive_sorting = false;
|
arguments->case_sensitive_sorting = false;
|
||||||
arguments->no_colour = false;
|
arguments->no_colour = false;
|
||||||
arguments->group_directories = false;
|
arguments->group_directories = false;
|
||||||
arguments->human_readable = false;
|
|
||||||
arguments->long_view = false;
|
arguments->long_view = false;
|
||||||
arguments->reverse_order = false;
|
arguments->reverse_order = false;
|
||||||
arguments->tree_view = false;
|
arguments->tree_view = false;
|
||||||
@@ -185,9 +176,6 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
|||||||
case KEY_GROUP_DIRECTORIES:
|
case KEY_GROUP_DIRECTORIES:
|
||||||
arguments->group_directories = true;
|
arguments->group_directories = true;
|
||||||
break;
|
break;
|
||||||
case KEY_HUMAN_READABLE:
|
|
||||||
arguments->human_readable = true;
|
|
||||||
break;
|
|
||||||
case KEY_LONG_VIEW:
|
case KEY_LONG_VIEW:
|
||||||
arguments->long_view = true;
|
arguments->long_view = true;
|
||||||
break;
|
break;
|
||||||
@@ -228,7 +216,8 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
|||||||
{
|
{
|
||||||
arguments->permission_mode = no_permissions;
|
arguments->permission_mode = no_permissions;
|
||||||
}
|
}
|
||||||
else if ( !strncmp( "char", arg, 5 ) )
|
else if ( !strncmp( "char", arg, 5 )
|
||||||
|
|| !strncmp( "characters", arg, 11 ) )
|
||||||
{
|
{
|
||||||
arguments->permission_mode = char_permissions;
|
arguments->permission_mode = char_permissions;
|
||||||
}
|
}
|
||||||
|
|||||||
+85
-32
@@ -19,8 +19,13 @@
|
|||||||
#include "type_definitions.h"
|
#include "type_definitions.h"
|
||||||
#include "sorting.h"
|
#include "sorting.h"
|
||||||
|
|
||||||
|
// macro definitions
|
||||||
|
#define LENGTH_PERMISSIONS ( ( size_t )11 )
|
||||||
|
#define LENGTH_SIZE ( ( size_t )9 )
|
||||||
|
|
||||||
// constant declarations
|
// constant declarations
|
||||||
char permission_characters[3] = { 'x', 'w', 'r' };
|
char permission_characters[] = { 'x', 'w', 'r' };
|
||||||
|
char size_characters[] = { 'B', 'k', 'M', 'G', 'T', 'P' };
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
int permissions_to_string( mode_t permissions, char* string );
|
int permissions_to_string( mode_t permissions, char* string );
|
||||||
@@ -38,8 +43,10 @@ int is_entry_visible( arguments_t* args, char* entry_name );
|
|||||||
int main( int argc, char** argv )
|
int main( int argc, char** argv )
|
||||||
{
|
{
|
||||||
arguments_t args;
|
arguments_t args;
|
||||||
|
char file_type;
|
||||||
int permissions;
|
int permissions;
|
||||||
char permissions_string[10];
|
char file_permissions[LENGTH_PERMISSIONS];
|
||||||
|
char file_size[LENGTH_SIZE];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
// TODO: free memory before exiting on error!
|
// TODO: free memory before exiting on error!
|
||||||
@@ -86,65 +93,111 @@ int main( int argc, char** argv )
|
|||||||
|
|
||||||
for ( int n = 0; n < num_entries; n++ )
|
for ( int n = 0; n < num_entries; n++ )
|
||||||
{
|
{
|
||||||
char* file_type;
|
directory_entry_t* entry = entry_table[ n ];
|
||||||
switch ( entry_table[ n ]->stats.st_mode & S_IFMT )
|
|
||||||
|
switch ( entry->stats.st_mode & S_IFMT )
|
||||||
{
|
{
|
||||||
case S_IFBLK:
|
case S_IFBLK:
|
||||||
file_type = "b";
|
file_type = 'b';
|
||||||
break;
|
break;
|
||||||
case S_IFCHR:
|
case S_IFCHR:
|
||||||
file_type = "c";
|
file_type = 'c';
|
||||||
break;
|
break;
|
||||||
case S_IFDIR:
|
case S_IFDIR:
|
||||||
file_type = "d";
|
file_type = 'd';
|
||||||
break;
|
break;
|
||||||
case S_IFIFO:
|
case S_IFIFO:
|
||||||
file_type = "f";
|
file_type = 'f';
|
||||||
break;
|
break;
|
||||||
case S_IFLNK:
|
case S_IFLNK:
|
||||||
file_type = "l";
|
file_type = 'l';
|
||||||
break;
|
break;
|
||||||
case S_IFREG:
|
case S_IFREG:
|
||||||
file_type = " ";
|
file_type = ' ';
|
||||||
break;
|
break;
|
||||||
case S_IFSOCK:
|
case S_IFSOCK:
|
||||||
file_type = "s";
|
file_type = 's';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
switch ( args.permission_mode )
|
switch ( args.permission_mode )
|
||||||
{
|
{
|
||||||
case no_permissions:
|
case no_permissions:
|
||||||
log_info(
|
file_permissions[0] = '\0';
|
||||||
"%s %s",
|
|
||||||
file_type,
|
|
||||||
entry_table[ n ]->name
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case char_permissions:
|
case char_permissions:
|
||||||
|
file_permissions[0] = ' ';
|
||||||
permissions_to_string(
|
permissions_to_string(
|
||||||
entry_table[ n ]->stats.st_mode,
|
entry->stats.st_mode,
|
||||||
permissions_string
|
&file_permissions[1]
|
||||||
);
|
|
||||||
log_info(
|
|
||||||
"%s %s %s",
|
|
||||||
file_type,
|
|
||||||
permissions_string,
|
|
||||||
entry_table[ n ]->name
|
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case octal_permissions:
|
case octal_permissions:
|
||||||
permissions =
|
permissions = mask_permissions(
|
||||||
mask_permissions( entry_table[ n ]->stats.st_mode );
|
entry->stats.st_mode
|
||||||
log_info(
|
);
|
||||||
"%s %o %s",
|
snprintf(
|
||||||
file_type,
|
&file_permissions[ 0 ],
|
||||||
permissions,
|
10,
|
||||||
entry_table[ n ]->name
|
" %3o",
|
||||||
|
permissions
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char size_quantifier = '\0';
|
||||||
|
double size_format = ( float )entry->stats.st_size;
|
||||||
|
if ( entry->stats.st_mode & S_IFREG
|
||||||
|
|| entry->stats.st_mode & S_IFCHR )
|
||||||
|
{
|
||||||
|
for ( int k = 0; k < ( int )sizeof( size_characters ); k++ )
|
||||||
|
{
|
||||||
|
if ( 1000.0f > size_format )
|
||||||
|
{
|
||||||
|
size_quantifier = size_characters[ k ];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
size_format /= 1000.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 10.0 > size_format )
|
||||||
|
{
|
||||||
|
snprintf(
|
||||||
|
&file_size[ 0 ],
|
||||||
|
LENGTH_SIZE,
|
||||||
|
" %1.1f%c",
|
||||||
|
size_format,
|
||||||
|
size_quantifier
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(
|
||||||
|
&file_size[ 0 ],
|
||||||
|
LENGTH_SIZE,
|
||||||
|
" %3.0f%c",
|
||||||
|
size_format,
|
||||||
|
size_quantifier
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(
|
||||||
|
&file_size[ 0 ],
|
||||||
|
LENGTH_SIZE,
|
||||||
|
" -"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
log_info(
|
||||||
|
"%c%s%s %s",
|
||||||
|
file_type,
|
||||||
|
file_permissions,
|
||||||
|
file_size,
|
||||||
|
entry->name
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// free all allocated elements/entries
|
// free all allocated elements/entries
|
||||||
@@ -224,7 +277,7 @@ int get_entries(
|
|||||||
if ( lstat( full_path, &file_stats ) ) return -EIO;
|
if ( lstat( full_path, &file_stats ) ) return -EIO;
|
||||||
|
|
||||||
strncpy( new_entry->name, entry->d_name, NAME_MAX );
|
strncpy( new_entry->name, entry->d_name, NAME_MAX );
|
||||||
new_entry->stats.st_mode = file_stats.st_mode;
|
new_entry->stats = file_stats;
|
||||||
|
|
||||||
entry_table[entry_count] = new_entry;
|
entry_table[entry_count] = new_entry;
|
||||||
|
|
||||||
|
|||||||
+41
-26
@@ -57,10 +57,28 @@ int sort_entries(
|
|||||||
|
|
||||||
__compar_fn_t compare_function;
|
__compar_fn_t compare_function;
|
||||||
|
|
||||||
|
if ( true == args->case_sensitive_sorting )
|
||||||
|
{
|
||||||
|
compare_function = compare_string_case_sensitive;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
compare_function = compare_string_case_insensitive;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( args->sorting_mode != sort_by_name )
|
||||||
|
{
|
||||||
|
qsort(
|
||||||
|
( void* )entry_table,
|
||||||
|
( size_t )num_entries,
|
||||||
|
sizeof( *entry_table ),
|
||||||
|
compare_function
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
switch ( args->sorting_mode )
|
switch ( args->sorting_mode )
|
||||||
{
|
{
|
||||||
case sort_by_name:
|
case sort_by_name:
|
||||||
compare_function = compare_string_case_insensitive;
|
|
||||||
break;
|
break;
|
||||||
case sort_by_extension:
|
case sort_by_extension:
|
||||||
compare_function = compare_extension;
|
compare_function = compare_extension;
|
||||||
@@ -162,30 +180,19 @@ int compare_extension(
|
|||||||
exit( -EINVAL );
|
exit( -EINVAL );
|
||||||
}
|
}
|
||||||
|
|
||||||
// directory_entry_t* a = *( directory_entry_t** )operand_a;
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
||||||
// directory_entry_t* b = *( directory_entry_t** )operand_b;
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
||||||
// char* extension;
|
char* extension_a = strrchr( a->name, '.' );
|
||||||
//
|
char* extension_b = strrchr( b->name, '.' );
|
||||||
// size_t len_a = strnlen(a->name, NAME_MAX);
|
|
||||||
// size_t len_b = strnlen(b->name, NAME_MAX);
|
|
||||||
//
|
|
||||||
// char* entry_a = ( char* )calloc(
|
|
||||||
// len_b,
|
|
||||||
// sizeof( char )
|
|
||||||
// );
|
|
||||||
// char* entry_b = ( char* )calloc(
|
|
||||||
// len_b,
|
|
||||||
// sizeof( char )
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// extension = strrchr( a->name, '.' );
|
|
||||||
//
|
|
||||||
// strncpy( &entry_a[ 0 ], )
|
|
||||||
//
|
|
||||||
// free( entry_a );
|
|
||||||
// free( entry_b );
|
|
||||||
|
|
||||||
return 0;
|
if ( extension_a == a->name ) extension_a = NULL;
|
||||||
|
if ( extension_b == b->name ) extension_b = NULL;
|
||||||
|
|
||||||
|
if ( !extension_a && !extension_b ) return 0;
|
||||||
|
if ( !extension_a && extension_b ) return -1;
|
||||||
|
if ( extension_a && !extension_b ) return 1;
|
||||||
|
|
||||||
|
return compare_strings( extension_a, extension_b, NAME_MAX, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare_timestamp(
|
int compare_timestamp(
|
||||||
@@ -198,7 +205,11 @@ int compare_timestamp(
|
|||||||
log_error( "invalid parameter to compare function" );
|
log_error( "invalid parameter to compare function" );
|
||||||
exit( -EINVAL );
|
exit( -EINVAL );
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
||||||
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
||||||
|
|
||||||
|
return ( int )( b->stats.st_mtim.tv_sec - a->stats.st_mtim.tv_sec );
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare_size(
|
int compare_size(
|
||||||
@@ -211,5 +222,9 @@ int compare_size(
|
|||||||
log_error( "invalid parameter to compare function" );
|
log_error( "invalid parameter to compare function" );
|
||||||
exit( -EINVAL );
|
exit( -EINVAL );
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
||||||
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
||||||
|
|
||||||
|
return ( int )( b->stats.st_size - a->stats.st_size );
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user