refactoring
This commit is contained in:
1 parent
5b1b974ac7
commit
e37f04a1cd
6 files changed
+198
-119
No files matched your search
+8
-1
@@ -23,6 +23,13 @@ typedef enum {
|
||||
sort_by_size = 3
|
||||
} sorting_mode_t;
|
||||
|
||||
typedef enum {
|
||||
time_none = 0,
|
||||
time_access = 1,
|
||||
time_modified = 2,
|
||||
time_status_change = 3
|
||||
} time_mode_t;
|
||||
|
||||
typedef struct {
|
||||
bool show_all;
|
||||
bool show_almost_all;
|
||||
@@ -34,9 +41,9 @@ typedef struct {
|
||||
bool long_view;
|
||||
bool reverse_order;
|
||||
bool tree_view;
|
||||
bool show_types;
|
||||
permission_mode_t permission_mode;
|
||||
sorting_mode_t sorting_mode;
|
||||
time_mode_t time_mode;
|
||||
char* path;
|
||||
} arguments_t;
|
||||
|
||||
|
||||
+5
-1
@@ -11,7 +11,11 @@ const uint32_t permission_colour = 0xD0D0D0;
|
||||
|
||||
#define DATE_STYLE_NUM ( ( int )1 )
|
||||
const style_t date_style[ DATE_STYLE_NUM ] = { style_none };
|
||||
const uint32_t date_colour = 0xEEEEEE;
|
||||
const uint32_t date_colour = 0xBBEEEE;
|
||||
|
||||
#define USER_STYLE_NUM ( ( int )1 )
|
||||
const style_t user_style[ USER_STYLE_NUM ] = { style_none };
|
||||
const uint32_t user_colour = 0xEEBBEE;
|
||||
|
||||
#define TYPE_STYLE_NUM ( ( int )1 )
|
||||
const style_t type_style[ TYPE_STYLE_NUM ] = { style_none };
|
||||
|
||||
+4
-2
@@ -3,9 +3,11 @@
|
||||
|
||||
#include "type_definitions.h"
|
||||
|
||||
void display_type( char type );
|
||||
void display_type( entry_type_t type );
|
||||
void display_permissions( const char* const permissions );
|
||||
void display_size( const char* const size );
|
||||
void display_size( directory_entry_t* entry );
|
||||
void display_user( directory_entry_t* entry );
|
||||
void display_time( time_t time );
|
||||
void display_entry( const directory_entry_t* const entry );
|
||||
void display_link_target( const directory_entry_t* const entry );
|
||||
|
||||
|
||||
+31
-9
@@ -20,7 +20,7 @@
|
||||
#define KEY_REVERSE_ORDER ( ( int )'r' )
|
||||
#define KEY_SORT_MODE ( ( int )'s' )
|
||||
#define KEY_TREE_VIEW ( ( int )'t' )
|
||||
#define KEY_SHOW_TYPES ( ( int )'T' )
|
||||
#define KEY_TIME_MODE ( ( int )'T' )
|
||||
#define KEY_NO_COLOUR ( ( int )0x101 )
|
||||
|
||||
// function prototypes
|
||||
@@ -70,7 +70,6 @@ static struct argp_option options[] =
|
||||
.group = 0
|
||||
},
|
||||
{
|
||||
// TODO: implement this functionality
|
||||
.name = "no-colour",
|
||||
.key = KEY_NO_COLOUR,
|
||||
.arg = NULL,
|
||||
@@ -135,11 +134,11 @@ static struct argp_option options[] =
|
||||
.group = 0
|
||||
},
|
||||
{
|
||||
.name = "types",
|
||||
.key = KEY_SHOW_TYPES,
|
||||
.arg = NULL,
|
||||
.name = "time",
|
||||
.key = KEY_TIME_MODE,
|
||||
.arg = "mode",
|
||||
.flags = 0,
|
||||
.doc = "display file type (block, character, directory, ...)",
|
||||
.doc = "time to display for directory entries",
|
||||
.group = 0
|
||||
},
|
||||
{ 0 } // this is required to properly end the options array ..
|
||||
@@ -170,9 +169,9 @@ void initialise_args( arguments_t* arguments )
|
||||
arguments->long_view = false;
|
||||
arguments->reverse_order = false;
|
||||
arguments->tree_view = false;
|
||||
arguments->show_types = false;
|
||||
arguments->permission_mode = octal_permissions;
|
||||
arguments->sorting_mode = sort_by_extension;
|
||||
arguments->time_mode = time_none;
|
||||
arguments->path = ".";
|
||||
}
|
||||
|
||||
@@ -236,8 +235,30 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
||||
case KEY_TREE_VIEW:
|
||||
arguments->tree_view = true;
|
||||
break;
|
||||
case KEY_SHOW_TYPES:
|
||||
arguments->show_types = true;
|
||||
case KEY_TIME_MODE:
|
||||
if ( arg )
|
||||
{
|
||||
// sorted according to expected frequency of use
|
||||
if ( !strncmp( "mod", arg, 4 )
|
||||
|| !strncmp( "modified", arg, 9 ) )
|
||||
{
|
||||
arguments->time_mode = time_modified;
|
||||
}
|
||||
else if ( !strncmp( "acc", arg, 4 )
|
||||
|| !strncmp( "access", arg, 7 ) )
|
||||
{
|
||||
arguments->time_mode = time_access;
|
||||
}
|
||||
else if ( !strncmp( "stat", arg, 5 )
|
||||
|| !strncmp( "status", arg, 7 ) )
|
||||
{
|
||||
arguments->time_mode = time_status_change;
|
||||
}
|
||||
else if ( !strncmp( "none", arg, 4 ) )
|
||||
{
|
||||
arguments->time_mode = time_none;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case KEY_REVERSE_ORDER:
|
||||
arguments->reverse_order = true;
|
||||
@@ -288,6 +309,7 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
||||
}
|
||||
break;
|
||||
case ARGP_KEY_ARG:
|
||||
// TODO: handle multiple paths
|
||||
if ( 0 == state->arg_num )
|
||||
{
|
||||
arguments->path = arg;
|
||||
|
||||
+132
-5
@@ -1,8 +1,9 @@
|
||||
#include <linux/limits.h>
|
||||
#define _XOPEN_SOURCE 700
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <pwd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "display.h"
|
||||
#include "type_definitions.h"
|
||||
@@ -17,14 +18,44 @@ void set_file_style( const directory_entry_t* const entry );
|
||||
bool compare_makefile( const directory_entry_t* const entry );
|
||||
bool compare_archives( const directory_entry_t* const entry );
|
||||
bool compare_sources( const directory_entry_t* const entry );
|
||||
static void size_to_string(
|
||||
directory_entry_t* entry,
|
||||
char* file_size,
|
||||
size_t length
|
||||
);
|
||||
|
||||
// module logic
|
||||
void display_type( char type )
|
||||
void display_type( entry_type_t type )
|
||||
{
|
||||
format_set_style( type_style, TYPE_STYLE_NUM );
|
||||
format_set_colour( type_colour );
|
||||
fprintf( stdout, "%c", type );
|
||||
switch ( type )
|
||||
{
|
||||
case type_block:
|
||||
fputc( 'b', stdout );
|
||||
break;
|
||||
case type_character:
|
||||
fputc( 'c', stdout );
|
||||
break;
|
||||
case type_directory:
|
||||
fputc( 'd', stdout );
|
||||
break;
|
||||
case type_fifo:
|
||||
fputc( 'f', stdout );
|
||||
break;
|
||||
case type_link:
|
||||
fputc( 'l', stdout );
|
||||
break;
|
||||
case type_regular:
|
||||
fputc( ' ', stdout );
|
||||
break;
|
||||
case type_socket:
|
||||
fputc( 's', stdout );
|
||||
break;
|
||||
}
|
||||
format_clear();
|
||||
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_permissions( const char* const permissions )
|
||||
@@ -33,14 +64,55 @@ void display_permissions( const char* const permissions )
|
||||
format_set_colour( permission_colour );
|
||||
fprintf( stdout, "%s", permissions );
|
||||
format_clear();
|
||||
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_size( const char* const size )
|
||||
void display_size( directory_entry_t *entry )
|
||||
{
|
||||
const size_t length = 9;
|
||||
char size_string[ length ];
|
||||
|
||||
size_to_string( entry, size_string, length );
|
||||
|
||||
format_set_style( size_style, SIZE_STYLE_NUM );
|
||||
format_set_colour( size_colour );
|
||||
fprintf( stdout, "%s", size );
|
||||
fprintf( stdout, "%s", size_string );
|
||||
format_clear();
|
||||
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_user( directory_entry_t* entry )
|
||||
{
|
||||
struct passwd* user = getpwuid( entry->stats.st_uid );
|
||||
const size_t length = 8;
|
||||
char string[ length ];
|
||||
|
||||
snprintf( string, length, "%s", user->pw_name );
|
||||
|
||||
format_set_style( user_style, USER_STYLE_NUM );
|
||||
format_set_colour( user_colour );
|
||||
fprintf( stdout, "%-7s", string );
|
||||
format_clear();
|
||||
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_time( time_t time )
|
||||
{
|
||||
const size_t length = 17;
|
||||
char string[ length ];
|
||||
struct tm* local = localtime( &time );
|
||||
|
||||
strftime( string, length, "%Y-%m-%d %H:%M", local );
|
||||
|
||||
format_set_style( date_style, DATE_STYLE_NUM );
|
||||
format_set_colour( date_colour );
|
||||
fprintf( stdout, "%s", string );
|
||||
format_clear();
|
||||
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_entry( const directory_entry_t* const entry )
|
||||
@@ -48,6 +120,8 @@ void display_entry( const directory_entry_t* const entry )
|
||||
set_type_style( entry );
|
||||
fprintf( stdout, "%s", entry->name );
|
||||
format_clear();
|
||||
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_link_target(const directory_entry_t* const entry)
|
||||
@@ -182,3 +256,56 @@ bool compare_sources( const directory_entry_t* const entry )
|
||||
|| 0 == compare_strings( entry->extension, "odin", 5, true )
|
||||
|| 0 == compare_strings( entry->extension, "py", 3, true );
|
||||
}
|
||||
|
||||
void size_to_string(
|
||||
directory_entry_t* entry,
|
||||
char* file_size,
|
||||
size_t length
|
||||
)
|
||||
{
|
||||
const char SIZE_CHARS[ 6 ] = { 'B', 'k', 'M', 'G', 'T', 'P' };
|
||||
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_CHARS ); k++ )
|
||||
{
|
||||
if ( 1000.0f > size_format )
|
||||
{
|
||||
size_quantifier = SIZE_CHARS[ k ];
|
||||
break;
|
||||
}
|
||||
size_format /= 1000.0f;
|
||||
}
|
||||
|
||||
if ( 10.0 > size_format )
|
||||
{
|
||||
snprintf(
|
||||
&file_size[ 0 ],
|
||||
length,
|
||||
"%1.1f%c",
|
||||
size_format,
|
||||
size_quantifier
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(
|
||||
&file_size[ 0 ],
|
||||
length,
|
||||
"%3.0f%c",
|
||||
size_format,
|
||||
size_quantifier
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(
|
||||
&file_size[ 0 ],
|
||||
length,
|
||||
" "
|
||||
);
|
||||
}
|
||||
}
|
||||
+18
-101
@@ -23,22 +23,15 @@
|
||||
// macro definitions
|
||||
#define SUCCESS 0
|
||||
#define LENGTH_PERMISSIONS ( ( size_t )11 )
|
||||
#define LENGTH_SIZE ( ( size_t )9 )
|
||||
|
||||
// constant declarations
|
||||
static char PERMISSION_CHARS[] = { 'x', 'w', 'r' };
|
||||
static char SIZE_CHARS[] = { 'B', 'k', 'M', 'G', 'T', 'P' };
|
||||
|
||||
// function prototypes
|
||||
static int permissions_to_string(
|
||||
const arguments_t* const args,
|
||||
mode_t permissions,
|
||||
char* string
|
||||
);
|
||||
static void size_to_string(
|
||||
directory_entry_t* entry,
|
||||
char* file_size,
|
||||
size_t length
|
||||
size_t file_mode,
|
||||
permission_mode_t permission_mode,
|
||||
char* const string
|
||||
);
|
||||
static int count_entries( DIR* directory, const arguments_t* const args );
|
||||
static int get_entries(
|
||||
@@ -205,17 +198,17 @@ int get_entries(
|
||||
}
|
||||
|
||||
int permissions_to_string(
|
||||
const arguments_t* const args,
|
||||
mode_t file_mode,
|
||||
char* string
|
||||
size_t file_mode,
|
||||
permission_mode_t permission_mode,
|
||||
char* const string
|
||||
)
|
||||
{
|
||||
if ( !string ) return -EINVAL;
|
||||
if ( !string ) return -EINVAL;
|
||||
|
||||
mode_t permissions = file_mode & ( S_IRWXU | S_IRWXG | S_IRWXO );
|
||||
mode_t permissions = file_mode
|
||||
& ( S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO );
|
||||
|
||||
switch ( args->permission_mode )
|
||||
switch ( permission_mode )
|
||||
{
|
||||
case no_permissions:
|
||||
string[0] = '\0';
|
||||
@@ -242,7 +235,7 @@ int permissions_to_string(
|
||||
snprintf(
|
||||
&string[ 0 ],
|
||||
10,
|
||||
" %3o",
|
||||
"%04o",
|
||||
permissions
|
||||
);
|
||||
break;
|
||||
@@ -290,9 +283,7 @@ int handle_directory(
|
||||
if ( !directory ) return -EINVAL;
|
||||
if ( recursion_level > args->depth ) return -EINVAL;
|
||||
|
||||
char file_type;
|
||||
char file_permissions[LENGTH_PERMISSIONS];
|
||||
char file_size[LENGTH_SIZE];
|
||||
int ret;
|
||||
int num_entries = count_entries( directory, args );
|
||||
|
||||
@@ -327,37 +318,11 @@ int handle_directory(
|
||||
{
|
||||
directory_entry_t* entry = entry_table[ n ];
|
||||
|
||||
switch ( entry->type )
|
||||
{
|
||||
case type_block:
|
||||
file_type = 'b';
|
||||
break;
|
||||
case type_character:
|
||||
file_type = 'c';
|
||||
break;
|
||||
case type_directory:
|
||||
file_type = 'd';
|
||||
break;
|
||||
case type_fifo:
|
||||
file_type = 'f';
|
||||
break;
|
||||
case type_link:
|
||||
file_type = 'l';
|
||||
break;
|
||||
case type_regular:
|
||||
file_type = ' ';
|
||||
break;
|
||||
case type_socket:
|
||||
file_type = 's';
|
||||
break;
|
||||
}
|
||||
|
||||
( void )permissions_to_string(
|
||||
args,
|
||||
entry->stats.st_mode,
|
||||
args->permission_mode,
|
||||
file_permissions
|
||||
);
|
||||
size_to_string( entry, &file_size[ 0 ], LENGTH_SIZE );
|
||||
|
||||
if ( args->tree_view )
|
||||
{
|
||||
@@ -392,15 +357,18 @@ int handle_directory(
|
||||
fprintf( stdout, "%s", tree );
|
||||
}
|
||||
|
||||
if ( !args->tree_view )
|
||||
// TODO: implement 'no-color' functionality
|
||||
if ( args->long_view && !args->tree_view )
|
||||
{
|
||||
display_type( file_type );
|
||||
display_type( entry->type );
|
||||
display_permissions( file_permissions );
|
||||
display_size( file_size );
|
||||
fputc( ' ', stdout );
|
||||
display_size( entry );
|
||||
display_user( entry );
|
||||
display_time( entry->stats.st_mtime );
|
||||
}
|
||||
|
||||
display_entry( entry );
|
||||
|
||||
if ( type_link == entry->type )
|
||||
{
|
||||
display_link_target( entry );
|
||||
@@ -443,54 +411,3 @@ int handle_directory(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void size_to_string(
|
||||
directory_entry_t* entry,
|
||||
char* file_size,
|
||||
size_t length
|
||||
)
|
||||
{
|
||||
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_CHARS ); k++ )
|
||||
{
|
||||
if ( 1000.0f > size_format )
|
||||
{
|
||||
size_quantifier = SIZE_CHARS[ k ];
|
||||
break;
|
||||
}
|
||||
size_format /= 1000.0f;
|
||||
}
|
||||
|
||||
if ( 10.0 > size_format )
|
||||
{
|
||||
snprintf(
|
||||
&file_size[ 0 ],
|
||||
length,
|
||||
" %1.1f%c",
|
||||
size_format,
|
||||
size_quantifier
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(
|
||||
&file_size[ 0 ],
|
||||
length,
|
||||
" %3.0f%c",
|
||||
size_format,
|
||||
size_quantifier
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(
|
||||
&file_size[ 0 ],
|
||||
length,
|
||||
" "
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user