This commit is contained in:
root committed 2026-07-19 21:40:21 +02:00
1 parent a29805cad2
commit 4b1c8c0de4
5 files changed
+148 -45

No files matched your search

+46
View File
@@ -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 )
{
+32 -35
View File
@@ -1,4 +1,6 @@
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
// external dependencies
#include <stdlib.h>
@@ -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;
}
+55 -5
View File
@@ -1,17 +1,63 @@
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#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++ )