diff --git a/Makefile b/Makefile index fb6268d..b9a9d32 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,8 @@ C_SRC := main.c \ arguments.c \ stringing.c \ sorting.c \ - format.c + format.c \ + display.c C_FLAGS := -std=c11 \ $(WG) \ diff --git a/inc/arguments.h b/inc/arguments.h index 4604734..30f5697 100644 --- a/inc/arguments.h +++ b/inc/arguments.h @@ -34,6 +34,7 @@ typedef struct { bool long_view; bool reverse_order; bool tree_view; + bool show_types; permission_mode_t permission_mode; sorting_mode_t sorting_mode; char* path; diff --git a/inc/config.h b/inc/config.h index 62e21bd..1e56927 100644 --- a/inc/config.h +++ b/inc/config.h @@ -1,9 +1,3 @@ -/* ------------------------------------------------------------------ - * -- project: SCAN - * -- description: abstraction for terminal control sequences - * ------------------------------------------------------------------ - */ - #ifndef __CONFIG_H #define __CONFIG_H @@ -12,69 +6,65 @@ /* -- colour and style configuration */ // additional information // #define PERM_STYLE_NUM ( ( int )1 ) -// const style_t permission_style[PERM_STYLE_NUM] = { style_none }; +// const style_t permission_style[ PERM_STYLE_NUM ] = { style_none }; // const uint32_t permission_colour = 0xEEEEEE; // // #define DATE_STYLE_NUM ( ( int )1 ) -// const style_t date_style[DATE_STYLE_NUM] = { style_none }; +// const style_t date_style[ DATE_STYLE_NUM ] = { style_none }; // const uint32_t date_colour = 0xEEEEEE; // // #define TYPE_STYLE_NUM ( ( int )1 ) -// const style_t type_style[TYPE_STYLE_NUM] = { style_none }; +// const style_t type_style[ TYPE_STYLE_NUM ] = { style_none }; // const uint32_t type_colour = 0xEEEEEE; // // #define SIZE_STYLE_NUM ( ( int )1 ) -// const style_t size_style[SIZE_STYLE_NUM] = { style_none }; +// const style_t size_style[ SIZE_STYLE_NUM ] = { style_none }; // const uint32_t size_colour = 0xEEEEEE; // files by type #define BLK_STYLE_NUM ( ( int )1 ) -const style_t block_style[BLK_STYLE_NUM] = { style_none }; +const style_t block_style[ BLK_STYLE_NUM ] = { style_none }; const uint32_t block_colour = 0xFFCC13; #define CHAR_STYLE_NUM ( ( int )1 ) -const style_t character_style[CHAR_STYLE_NUM] = { style_none }; +const style_t character_style[ CHAR_STYLE_NUM ] = { style_none }; const uint32_t character_colour = 0xEEEEEE; #define DIR_STYLE_NUM ( ( int )1 ) -const style_t directory_style[DIR_STYLE_NUM] = { style_bold }; +const style_t directory_style[ DIR_STYLE_NUM ] = { style_bold }; const uint32_t directory_colour = 0x6CA05C; #define FIFO_STYLE_NUM ( ( int )1 ) -const style_t fifo_style[FIFO_STYLE_NUM] = { style_none }; +const style_t fifo_style[ FIFO_STYLE_NUM ] = { style_none }; const uint32_t fifo_colour = 0xEEEEEE; #define LINK_STYLE_NUM ( ( int )1 ) -const style_t link_style[LINK_STYLE_NUM] = { style_none }; +const style_t link_style[ LINK_STYLE_NUM ] = { style_none }; const uint32_t link_colour = 0x19D1DE; #define BLNK_STYLE_NUM ( ( int )1 ) -const style_t brokenlink_style[BLNK_STYLE_NUM] = { style_none }; +const style_t brokenlink_style[ BLNK_STYLE_NUM ] = { style_none }; const uint32_t brokenlink_colour = 0xC00008; #define SOCK_STYLE_NUM ( ( int )1 ) -const style_t socket_style[SOCK_STYLE_NUM] = { style_none }; +const style_t socket_style[ SOCK_STYLE_NUM ] = { style_none }; const uint32_t socket_colour = 0xEEEEEE; #define EXEC_STYLE_NUM ( ( int )1 ) -const style_t exec_style[EXEC_STYLE_NUM] = { style_none }; +const style_t exec_style[ EXEC_STYLE_NUM ] = { style_none }; const uint32_t exec_colour = 0xEEEEEE; -// // files by extension -// #define MAKE_STYLE_NUM ( ( int )1 ) -// const style_t makefile_style[MAKE_STYLE_NUM] = { style_none }; -// const uint32_t makefile_colour = 0xEAE977; -// -// #define SRC_STYLE_NUM ( ( int )1 ) -// const style_t source_style[SRC_STYLE_NUM] = { style_none }; -// const uint32_t source_colour = 0x6FADD8; -// -// #define MEDIA_STYLE_NUM ( ( int )1 ) -// const style_t media_style[MEDIA_STYLE_NUM] = { style_none }; -// const uint32_t media_colour = 0x6FADD8; -// -// #define AR_STYLE_NUM ( ( int )1 ) -// const style_t archive_style[AR_STYLE_NUM] = { style_none }; -// const uint32_t archive_colour = 0x6FADD8; +// files by extension +#define MAKE_STYLE_NUM ( ( int )1 ) +const style_t makefile_style[ MAKE_STYLE_NUM ] = { style_none }; +const uint32_t makefile_colour = 0xEAE977; + +#define SRC_STYLE_NUM ( ( int )1 ) +const style_t source_style[ SRC_STYLE_NUM ] = { style_none }; +const uint32_t source_colour = 0x6FADD8; + +#define AR_STYLE_NUM ( ( int )1 ) +const style_t archive_style[ AR_STYLE_NUM ] = { style_none }; +const uint32_t archive_colour = 0x6FADD8; #endif diff --git a/inc/display.h b/inc/display.h new file mode 100644 index 0000000..1bacb19 --- /dev/null +++ b/inc/display.h @@ -0,0 +1,12 @@ +#ifndef __DISPLAY_H +#define __DISPLAY_H + +#include "type_definitions.h" + +void display_link_target(const directory_entry_t* const entry); +void set_type_style( const directory_entry_t* const entry ); +void reset_type_style( const directory_entry_t* const entry ); +void set_file_style( const directory_entry_t* const entry ); +void reset_file_style( const directory_entry_t* const entry ); + +#endif diff --git a/inc/type_definitions.h b/inc/type_definitions.h index 70e94bd..7ae1dfd 100644 --- a/inc/type_definitions.h +++ b/inc/type_definitions.h @@ -22,6 +22,7 @@ typedef enum { typedef struct { char name[ NAME_MAX ]; + char* extension; struct stat stats; entry_type_t type; } directory_entry_t; diff --git a/src/arguments.c b/src/arguments.c index 8b81b0c..7cf12fe 100644 --- a/src/arguments.c +++ b/src/arguments.c @@ -20,6 +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_NO_COLOUR ( ( int )0x101 ) // function prototypes @@ -69,7 +70,8 @@ static struct argp_option options[] = .group = 0 }, { - .name = "no-color", + // TODO: implement this functionality + .name = "no-colour", .key = KEY_NO_COLOUR, .arg = NULL, .flags = 0, @@ -132,6 +134,14 @@ static struct argp_option options[] = .doc = "use tree listing format", .group = 0 }, + { + .name = "types", + .key = KEY_SHOW_TYPES, + .arg = NULL, + .flags = 0, + .doc = "display file type (block, character, directory, ...)", + .group = 0 + }, { 0 } // this is required to properly end the options array .. }; @@ -160,8 +170,9 @@ void initialise_args( arguments_t* arguments ) arguments->long_view = false; arguments->reverse_order = false; arguments->tree_view = false; - arguments->permission_mode = no_permissions; - arguments->sorting_mode = sort_by_name; + arguments->show_types = false; + arguments->permission_mode = octal_permissions; + arguments->sorting_mode = sort_by_extension; arguments->path = "."; } @@ -225,6 +236,9 @@ 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; + break; case KEY_REVERSE_ORDER: arguments->reverse_order = true; break; diff --git a/src/display.c b/src/display.c new file mode 100644 index 0000000..7b4684f --- /dev/null +++ b/src/display.c @@ -0,0 +1,213 @@ +#include +#define _XOPEN_SOURCE 700 + +#include +#include + +#include "display.h" +#include "type_definitions.h" +#include "log.h" +#include "format.h" +#include "config.h" +#include "stringing.h" + +// function signatures +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 ); + +// module logic +void display_link_target(const directory_entry_t* const entry) +{ + char link[ PATH_MAX ]; + ssize_t length = 0; + struct stat link_stat; + int link_invalid; + + length = readlink( entry->name, &link[ 0 ], PATH_MAX ); + + if ( 0 > length ) + { + log_error( "failed to read link '%s'", entry->name ); + } + + link[ length ] = '\0'; + link_invalid = stat( link, &link_stat ); + + fprintf( stdout, " --> "); + + if ( link_invalid ) + { + format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); + format_set_fg_colour( brokenlink_colour ); + } + else + { + format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); + format_set_fg_colour( link_colour ); + } + + fprintf( stdout, "%s", link ); + + if ( link_invalid ) + { + format_reset_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); + format_clear_fg_colour(); + } + else + { + format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); + format_clear_fg_colour(); + } +} + +void set_type_style( const directory_entry_t* const entry ) +{ + switch ( entry->type ) + { + case type_block: + format_set_style( &block_style[ 0 ], BLK_STYLE_NUM ); + format_set_fg_colour( block_colour ); + break; + + case type_character: + format_set_style( &character_style[ 0 ], CHAR_STYLE_NUM ); + format_set_fg_colour( character_colour ); + break; + + case type_directory: + format_set_style( &directory_style[ 0 ], DIR_STYLE_NUM ); + format_set_fg_colour( directory_colour ); + break; + + case type_fifo: + format_set_style( &fifo_style[ 0 ], FIFO_STYLE_NUM ); + format_set_fg_colour( fifo_colour ); + break; + + case type_link: + format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); + format_set_fg_colour( link_colour ); + break; + + case type_regular: + set_file_style( entry ); + break; + + case type_socket: + format_set_style( &socket_style[ 0 ], SOCK_STYLE_NUM ); + format_set_fg_colour( socket_colour ); + break; + } +} + +void reset_type_style( const directory_entry_t* const entry ) +{ + switch ( entry->type ) + { + case type_block: + format_reset_style( &block_style[ 0 ], BLK_STYLE_NUM ); + format_clear_fg_colour(); + break; + + case type_character: + format_reset_style( &character_style[ 0 ], CHAR_STYLE_NUM ); + format_clear_fg_colour(); + break; + + case type_directory: + format_reset_style( &directory_style[ 0 ], DIR_STYLE_NUM ); + format_clear_fg_colour(); + break; + + case type_fifo: + format_reset_style( &fifo_style[ 0 ], FIFO_STYLE_NUM ); + format_clear_fg_colour(); + break; + + case type_link: + format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); + format_clear_fg_colour(); + break; + + case type_regular: + reset_file_style( entry ); + break; + + case type_socket: + format_reset_style( &socket_style[ 0 ], SOCK_STYLE_NUM ); + format_clear_fg_colour(); + break; + } +} + +void set_file_style( const directory_entry_t* const entry ) +{ + if ( !entry ) return; + + // makefiles and readmes + if ( compare_makefile( entry ) ) + { + format_set_style( &makefile_style[ 0 ], MAKE_STYLE_NUM ); + format_set_fg_colour( makefile_colour ); + } + else if ( compare_archives( entry ) ) + { + format_set_style( &archive_style[ 0 ], AR_STYLE_NUM ); + format_set_fg_colour( archive_colour ); + } + else if ( compare_sources( entry ) ) + { + format_set_style( &source_style[ 0 ], SRC_STYLE_NUM ); + format_set_fg_colour( source_colour ); + } +} + +void reset_file_style( const directory_entry_t* const entry ) +{ + if ( !entry ) return; + + // makefiles and readmes + if ( compare_makefile( entry ) ) + { + format_reset_style( &makefile_style[ 0 ], MAKE_STYLE_NUM ); + format_clear_fg_colour(); + } + else if ( compare_archives( entry ) ) + { + format_reset_style( &archive_style[ 0 ], AR_STYLE_NUM ); + format_clear_fg_colour(); + } + else if ( compare_sources( entry ) ) + { + format_reset_style( &source_style[ 0 ], SRC_STYLE_NUM ); + format_clear_fg_colour(); + } +} + +bool compare_makefile( const directory_entry_t* const entry ) +{ + return 0 == compare_strings( entry->name, "makefile", NAME_MAX, false ) + || 0 == compare_strings( entry->name, "readme", 6, false ) + || 0 == compare_strings( entry->extension, "mk", 3, false ); +} + +bool compare_archives( const directory_entry_t* const entry ) +{ + return 0 == compare_strings( entry->extension, "zip", 4, true ) + || 0 == compare_strings( entry->extension, "tar", 4, true ) + || 0 == compare_strings( entry->extension, "gz", 3, true ) + || 0 == compare_strings( entry->extension, "bzip2", 6, true ) + || 0 == compare_strings( entry->extension, "xz", 3, true ); +} + +bool compare_sources( const directory_entry_t* const entry ) +{ + return 0 == compare_strings( entry->extension, "c", 2, true ) + || 0 == compare_strings( entry->extension, "cpp", 4, true ) + || 0 == compare_strings( entry->extension, "h", 2, true ) + || 0 == compare_strings( entry->extension, "rs", 3, true ) + || 0 == compare_strings( entry->extension, "zig", 4, true ) + || 0 == compare_strings( entry->extension, "odin", 5, true ) + || 0 == compare_strings( entry->extension, "py", 3, true ); +} diff --git a/src/main.c b/src/main.c index b2c9073..1c94ad2 100644 --- a/src/main.c +++ b/src/main.c @@ -18,8 +18,7 @@ #include "arguments.h" #include "type_definitions.h" #include "sorting.h" -#include "format.h" -#include "config.h" +#include "display.h" // macro definitions #define SUCCESS 0 @@ -55,13 +54,11 @@ static int display_directory_contents( size_t recursion_level, size_t is_last ); -void display_link_target(const directory_entry_t* const entry); -void set_style( const directory_entry_t* const entry ); -void reset_style( const directory_entry_t* const entry ); // program logic int main( int argc, char** argv ) { + int retval = SUCCESS; arguments_t args; initialise_args( &args ); @@ -75,10 +72,12 @@ int main( int argc, char** argv ) if ( !directory ) { if ( ENOENT == errno ) + { log_error( "no directory `%s`", args.path ); - else - log_error( "failed to open directory `%s`", args.path ); + return -ENOENT; + } + log_error( "failed to open directory `%s`", args.path ); return -EIO; } @@ -95,10 +94,15 @@ int main( int argc, char** argv ) 0 ); } + else + { + log_error( "failed to enter directory '%s'", args.path ); + retval = -EIO; + } closedir( directory ); - return SUCCESS; + return retval; } int count_entries( DIR* directory, const arguments_t* const args ) @@ -141,7 +145,6 @@ int get_entries( struct dirent* entry; struct stat file_stats = { 0 }; - rewinddir( directory ); while ( NULL != ( entry = readdir( directory ) ) @@ -155,6 +158,17 @@ int get_entries( if ( lstat( entry->d_name, &file_stats ) ) return -EIO; strncpy( new_entry->name, entry->d_name, NAME_MAX ); + + new_entry->extension = strrchr( new_entry->name, '.' ); + if ( new_entry->extension == new_entry->name ) + { + new_entry->extension = NULL; + } + else if ( new_entry->extension ) + { + new_entry->extension += 1; + } + new_entry->stats = file_stats; switch ( file_stats.st_mode & S_IFMT ) { @@ -388,9 +402,9 @@ int display_directory_contents( ); } - set_style( entry ); + set_type_style( entry ); fprintf( stdout, "%s", entry->name ); - reset_style( entry ); + reset_type_style( entry ); if ( type_link == entry->type ) { @@ -485,127 +499,3 @@ void size_to_string( ); } } - -void display_link_target(const directory_entry_t* const entry) -{ - char link[ PATH_MAX ]; - ssize_t length = 0; - struct stat link_stat; - int link_invalid; - - length = readlink( entry->name, &link[ 0 ], PATH_MAX ); - - if ( 0 > length ) - { - log_error( "failed to read link '%s'", entry->name ); - } - - link[ length ] = '\0'; - link_invalid = stat( link, &link_stat ); - - fprintf( stdout, " --> "); - - if ( link_invalid ) - { - format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); - format_set_fg_colour( brokenlink_colour ); - } - else - { - format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_set_fg_colour( link_colour ); - } - - fprintf( stdout, "%s", link ); - - if ( link_invalid ) - { - format_reset_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); - format_clear_fg_colour(); - } - else - { - format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_clear_fg_colour(); - } -} - -void set_style( const directory_entry_t* const entry ) -{ - switch ( entry->type ) - { - case type_block: - format_set_style( &block_style[ 0 ], BLK_STYLE_NUM ); - format_set_fg_colour( block_colour ); - break; - - case type_character: - format_set_style( &character_style[ 0 ], CHAR_STYLE_NUM ); - format_set_fg_colour( character_colour ); - break; - - case type_directory: - format_set_style( &directory_style[ 0 ], DIR_STYLE_NUM ); - format_set_fg_colour( directory_colour ); - break; - - case type_fifo: - format_set_style( &fifo_style[ 0 ], FIFO_STYLE_NUM ); - format_set_fg_colour( fifo_colour ); - break; - - case type_link: - format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_set_fg_colour( link_colour ); - break; - - case type_regular: - // TODO: handle files by extension - break; - - case type_socket: - format_set_style( &socket_style[ 0 ], SOCK_STYLE_NUM ); - format_set_fg_colour( socket_colour ); - break; - } -} - -void reset_style( const directory_entry_t* const entry ) -{ - switch ( entry->type ) - { - case type_block: - format_reset_style( &block_style[ 0 ], BLK_STYLE_NUM ); - format_clear_fg_colour(); - break; - - case type_character: - format_reset_style( &character_style[ 0 ], CHAR_STYLE_NUM ); - format_clear_fg_colour(); - break; - - case type_directory: - format_reset_style( &directory_style[ 0 ], DIR_STYLE_NUM ); - format_clear_fg_colour(); - break; - - case type_fifo: - format_reset_style( &fifo_style[ 0 ], FIFO_STYLE_NUM ); - format_clear_fg_colour(); - break; - - case type_link: - format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_clear_fg_colour(); - break; - - case type_regular: - // TODO: handle files by extension - break; - - case type_socket: - format_reset_style( &socket_style[ 0 ], SOCK_STYLE_NUM ); - format_clear_fg_colour(); - break; - } -} diff --git a/src/sorting.c b/src/sorting.c index 3867b9a..182ba8d 100644 --- a/src/sorting.c +++ b/src/sorting.c @@ -182,17 +182,11 @@ int compare_extension( directory_entry_t* a = *( directory_entry_t** )operand_a; directory_entry_t* b = *( directory_entry_t** )operand_b; - char* extension_a = strrchr( a->name, '.' ); - char* extension_b = strrchr( b->name, '.' ); + if ( !a->extension && !b->extension ) return 0; + if ( !a->extension && b->extension ) return -1; + if ( a->extension && !b->extension ) return 1; - 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 ); + return compare_strings( a->extension, b->extension, NAME_MAX, false ); } int compare_timestamp( diff --git a/src/stringing.c b/src/stringing.c index eac6301..084d7bd 100644 --- a/src/stringing.c +++ b/src/stringing.c @@ -49,6 +49,10 @@ int string_to_upper( char* const string ) return 0; } +// compares strings and returns `a - b` +// -1 if a < b (lower ascii value) +// 0 if a = b +// 1 if a > b (higher ascii value) int compare_strings( const char* const a, const char* const b,