From 8bd7c065093d98fa2d1a4fc2eabadd0c3c500047 Mon Sep 17 00:00:00 2001 From: zegonix Date: Wed, 7 Oct 2026 22:01:57 +0200 Subject: [PATCH] show broken links with distinct style --- inc/config.h | 48 +++++++++++++++++++-------------------- src/main.c | 64 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 70 insertions(+), 42 deletions(-) diff --git a/inc/config.h b/inc/config.h index f94203a..62e21bd 100644 --- a/inc/config.h +++ b/inc/config.h @@ -28,37 +28,37 @@ // 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 uint32_t block_colour = 0xFFCC13; +#define BLK_STYLE_NUM ( ( int )1 ) +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 uint32_t character_colour = 0xEEEEEE; +#define CHAR_STYLE_NUM ( ( int )1 ) +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 uint32_t directory_colour = 0x6CA05C; +#define DIR_STYLE_NUM ( ( int )1 ) +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 uint32_t fifo_colour = 0xEEEEEE; +#define FIFO_STYLE_NUM ( ( int )1 ) +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 uint32_t link_colour = 0x19D1DE; +#define LINK_STYLE_NUM ( ( int )1 ) +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 uint32_t brokenlink_colour = 0xEEEEEE; +#define BLNK_STYLE_NUM ( ( int )1 ) +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 uint32_t socket_colour = 0xEEEEEE; +#define SOCK_STYLE_NUM ( ( int )1 ) +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 uint32_t exec_colour = 0xEEEEEE; +#define EXEC_STYLE_NUM ( ( int )1 ) +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 ) diff --git a/src/main.c b/src/main.c index b8e31db..b2c9073 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,7 @@ 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 ); @@ -393,24 +394,7 @@ int display_directory_contents( if ( type_link == entry->type ) { - char link[ PATH_MAX ]; - ssize_t length = 0; - - length = readlink( entry->name, &link[ 0 ], PATH_MAX ); - - if ( 0 > length ) - { - log_error( "failed to read link '%s'", entry->name ); - } - link[ length ] = '\0'; - - fprintf( stdout, " --> "); - - format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_set_fg_colour( link_colour ); - fprintf( stdout, "%s", link ); - format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_clear_fg_colour(); + display_link_target( entry ); } fputc( '\n', stdout ); @@ -502,6 +486,50 @@ 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 )