fixed -a
This commit is contained in:
1 parent
a29805cad2
commit
4b1c8c0de4
5 files changed
+148
-45
No files matched your search
@@ -16,6 +16,13 @@ typedef enum {
|
|||||||
octal_permissions = 2
|
octal_permissions = 2
|
||||||
} permission_mode_t;
|
} 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 {
|
typedef struct {
|
||||||
bool show_all;
|
bool show_all;
|
||||||
bool show_almost_all;
|
bool show_almost_all;
|
||||||
@@ -24,8 +31,10 @@ typedef struct {
|
|||||||
bool group_directories;
|
bool group_directories;
|
||||||
bool human_readable;
|
bool human_readable;
|
||||||
bool long_view;
|
bool long_view;
|
||||||
|
bool reverse_order;
|
||||||
bool tree_view;
|
bool tree_view;
|
||||||
permission_mode_t permission_mode;
|
permission_mode_t permission_mode;
|
||||||
|
sorting_mode_t sorting_mode;
|
||||||
char* path;
|
char* path;
|
||||||
} arguments_t;
|
} arguments_t;
|
||||||
|
|
||||||
|
|||||||
+6
-5
@@ -1,13 +1,14 @@
|
|||||||
#ifndef __SORTING_H
|
#ifndef __SORTING_H
|
||||||
#define __SORTING_H
|
#define __SORTING_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include "type_definitions.h"
|
#include "type_definitions.h"
|
||||||
|
#include "arguments.h"
|
||||||
|
|
||||||
int compare_entries( const void* operand_a, const void* operand_b );
|
int sort_entries(
|
||||||
int compare_entries_casesensitive(
|
arguments_t* args,
|
||||||
const void* operand_a,
|
directory_entry_t** const entry_table,
|
||||||
const void* operand_b
|
int num_entries
|
||||||
);
|
);
|
||||||
int group_directories( directory_entry_t** entry_table, int num_entries );
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -13,6 +13,8 @@
|
|||||||
#define KEY_HUMAN_READABLE ( ( int )'h' )
|
#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_SORT_MODE ( ( int )'s' )
|
||||||
#define KEY_TREE_VIEW ( ( int )'t' )
|
#define KEY_TREE_VIEW ( ( int )'t' )
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
@@ -24,6 +26,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 sort_description[] = "property to sort by: name, ext(ension), time, size";
|
||||||
|
|
||||||
static struct argp_option options[] =
|
static struct argp_option options[] =
|
||||||
{
|
{
|
||||||
@@ -91,6 +94,22 @@ static struct argp_option options[] =
|
|||||||
.doc = "use tree listing format",
|
.doc = "use tree listing format",
|
||||||
.group = 0
|
.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",
|
.name = "tree",
|
||||||
.key = KEY_TREE_VIEW,
|
.key = KEY_TREE_VIEW,
|
||||||
@@ -124,8 +143,10 @@ void initialise_args( arguments_t* arguments )
|
|||||||
arguments->group_directories = false;
|
arguments->group_directories = false;
|
||||||
arguments->human_readable = false;
|
arguments->human_readable = false;
|
||||||
arguments->long_view = false;
|
arguments->long_view = false;
|
||||||
|
arguments->reverse_order = false;
|
||||||
arguments->tree_view = false;
|
arguments->tree_view = false;
|
||||||
arguments->permission_mode = no_permissions;
|
arguments->permission_mode = no_permissions;
|
||||||
|
arguments->sorting_mode = sort_by_name;
|
||||||
arguments->path = ".";
|
arguments->path = ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,6 +194,31 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
|||||||
case KEY_TREE_VIEW:
|
case KEY_TREE_VIEW:
|
||||||
arguments->tree_view = true;
|
arguments->tree_view = true;
|
||||||
break;
|
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:
|
case KEY_PERMISSIONS:
|
||||||
if ( !arg )
|
if ( !arg )
|
||||||
{
|
{
|
||||||
|
|||||||
+32
-35
@@ -1,4 +1,6 @@
|
|||||||
|
#ifndef _XOPEN_SOURCE
|
||||||
#define _XOPEN_SOURCE 700
|
#define _XOPEN_SOURCE 700
|
||||||
|
#endif
|
||||||
|
|
||||||
// external dependencies
|
// external dependencies
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -30,6 +32,7 @@ int get_entries(
|
|||||||
int num_entries,
|
int num_entries,
|
||||||
arguments_t* args
|
arguments_t* args
|
||||||
);
|
);
|
||||||
|
int is_entry_visible( arguments_t* args, char* entry_name );
|
||||||
|
|
||||||
// program logic
|
// program logic
|
||||||
int main( int argc, char** argv )
|
int main( int argc, char** argv )
|
||||||
@@ -74,16 +77,7 @@ int main( int argc, char** argv )
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// __compar_fn_t comparator = args.case_sensitive_sorting
|
ret = sort_entries(&args, entry_table, num_entries);
|
||||||
// ? compare_entries_casesensitive : compare_entries;
|
|
||||||
// qsort(
|
|
||||||
// ( void * )entry_table,
|
|
||||||
// ( size_t )num_entries,
|
|
||||||
// sizeof( *entry_table ),
|
|
||||||
// comparator
|
|
||||||
// );
|
|
||||||
|
|
||||||
ret = group_directories(entry_table, num_entries);
|
|
||||||
if ( 0 != ret )
|
if ( 0 != ret )
|
||||||
{
|
{
|
||||||
log_error( "grouping directories failed with error [%d]", 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 ) ) )
|
while ( NULL != ( entry = readdir( directory ) ) )
|
||||||
{
|
{
|
||||||
if ( !args->show_all && ( !strncmp( entry->d_name, ".", 2)
|
if ( is_entry_visible( args, entry->d_name ) ) continue;
|
||||||
|| !strncmp( entry->d_name, "..", 3 ) ) )
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !args->show_almost_all && '.' == entry->d_name[0] )
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
entry_count++;
|
entry_count++;
|
||||||
}
|
}
|
||||||
@@ -223,21 +208,7 @@ int get_entries(
|
|||||||
while ( NULL != ( entry = readdir( directory ) )
|
while ( NULL != ( entry = readdir( directory ) )
|
||||||
&& entry_count < num_entries )
|
&& entry_count < num_entries )
|
||||||
{
|
{
|
||||||
/*
|
if ( is_entry_visible( args, entry->d_name ) ) continue;
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) );
|
new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) );
|
||||||
if ( !new_entry ) return -ENOMEM;
|
if ( !new_entry ) return -ENOMEM;
|
||||||
@@ -291,3 +262,29 @@ int permissions_to_string( mode_t file_mode, char* string )
|
|||||||
return 0;
|
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
@@ -1,17 +1,63 @@
|
|||||||
|
#ifndef _XOPEN_SOURCE
|
||||||
#define _XOPEN_SOURCE 700
|
#define _XOPEN_SOURCE 700
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include "sorting.h"
|
#include "sorting.h"
|
||||||
#include "type_definitions.h"
|
#include "type_definitions.h"
|
||||||
|
#include "arguments.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "stringing.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
|
* 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 )
|
if ( !operand_a || !operand_b )
|
||||||
{
|
{
|
||||||
@@ -28,7 +74,7 @@ int compare_entries( const void* operand_a, const void* operand_b )
|
|||||||
/*
|
/*
|
||||||
* compare entries case sensitive
|
* compare entries case sensitive
|
||||||
*/
|
*/
|
||||||
int compare_entries_casesensitive(
|
int comp_name_case_sensitive(
|
||||||
const void* operand_a,
|
const void* operand_a,
|
||||||
const void* operand_b
|
const void* operand_b
|
||||||
)
|
)
|
||||||
@@ -45,7 +91,11 @@ int compare_entries_casesensitive(
|
|||||||
return compare_strings( a->name, b->name, NAME_MAX, true );
|
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 ( !entry_table ) return -EINVAL;
|
||||||
if ( 0 >= num_entries ) 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,
|
( void * )directory_table,
|
||||||
( size_t )directory_counter,
|
( size_t )directory_counter,
|
||||||
sizeof( *directory_table ),
|
sizeof( *directory_table ),
|
||||||
compare_entries
|
comp_name_case_insensitive
|
||||||
);
|
);
|
||||||
qsort(
|
qsort(
|
||||||
( void * )non_dir_table,
|
( void * )non_dir_table,
|
||||||
( size_t )non_dir_counter,
|
( size_t )non_dir_counter,
|
||||||
sizeof( *non_dir_table ),
|
sizeof( *non_dir_table ),
|
||||||
compare_entries
|
comp_name_case_insensitive
|
||||||
);
|
);
|
||||||
|
|
||||||
for ( int n = 0; n < directory_counter; n++ )
|
for ( int n = 0; n < directory_counter; n++ )
|
||||||
|
|||||||
Reference in new issue
Block a user