structure and group directories
This commit is contained in:
1 parent
f2e8055172
commit
a29805cad2
9 files changed
+293
-49
No files matched your search
@@ -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) \
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef __STRING_FUNCTIONS_H
|
||||
#define __STRING_FUNCTIONS_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
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
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef __TYPE_DEFINITIONS_H
|
||||
#define __TYPE_DEFINITIONS_H
|
||||
|
||||
#include <linux/limits.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
// type definitions
|
||||
typedef struct directory_entry {
|
||||
char name[ NAME_MAX ];
|
||||
struct stat stats;
|
||||
} directory_entry_t;
|
||||
|
||||
#endif
|
||||
+17
-4
@@ -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;
|
||||
}
|
||||
|
||||
+28
-44
@@ -1,4 +1,3 @@
|
||||
#include <asm-generic/errno-base.h>
|
||||
#define _XOPEN_SOURCE 700
|
||||
|
||||
// external dependencies
|
||||
@@ -7,9 +6,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <argp.h>
|
||||
#include <errno.h>
|
||||
#include <linux/limits.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
@@ -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;
|
||||
@@ -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 );
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
#define _XOPEN_SOURCE 700
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#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;
|
||||
}
|
||||
Reference in new issue
Block a user