display link target

This commit is contained in:
root committed 2026-10-07 17:13:06 +02:00
1 parent bb0072a6f6
commit 9c65543633
3 files changed
+198 -47

No files matched your search

+12 -16
View File
@@ -15,6 +15,7 @@ void format_clear( void )
void format_set_style( const style_t* const styles, uint8_t number )
{
if ( 0 == number ) return;
if ( NUMBER_STYLES < number ) return;
if ( style_none == styles[ 0 ] ) return;
@@ -26,20 +27,16 @@ void format_set_style( const style_t* const styles, uint8_t number )
if ( style_none <= style )
continue;
if ( n >= number - 1 )
{
fprintf( stdout, "%d", STYLE_SET[ style ] );
}
else
{
fprintf( stdout, "%d;", STYLE_SET[ style ] );
}
fprintf( stdout, "%d", STYLE_SET[ style ] );
if ( n < number - 1 )
fputc( ';', stdout );
}
fprintf( stdout, SEQUENCE_SUFFIX );
}
void format_reset_style( const style_t* const styles, uint8_t number )
{
if ( 0 == number ) return;
if ( NUMBER_STYLES < number ) return;
if ( style_none == styles[ 0 ] ) return;
@@ -47,14 +44,13 @@ void format_reset_style( const style_t* const styles, uint8_t number )
for ( int n = 0; n < number; n++ )
{
style_t style = styles[ n ];
if ( n >= number - 1 )
{
fprintf( stdout, "%d", STYLE_RESET[ style ] );
}
else
{
fprintf( stdout, "%d;", STYLE_RESET[ style ] );
}
if ( NUMBER_STYLES <= style )
continue;
fprintf( stdout, "%d", STYLE_RESET[ style ] );
if ( n < number - 1 )
fputc( ';', stdout );
}
fprintf( stdout, SEQUENCE_SUFFIX );
}
+126 -30
View File
@@ -55,6 +55,8 @@ static int display_directory_contents(
size_t recursion_level,
size_t is_last
);
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 )
@@ -335,10 +337,10 @@ int display_directory_contents(
}
( void )permissions_to_string(
args,
entry->stats.st_mode,
file_permissions
);
args,
entry->stats.st_mode,
file_permissions
);
size_to_string( entry, &file_size[ 0 ], LENGTH_SIZE );
if ( args->tree_view )
@@ -374,50 +376,64 @@ int display_directory_contents(
fprintf( stdout, "%s", tree );
}
if ( entry->type == type_directory )
if ( !args->tree_view )
{
format_set_style( directory_style, DIR_STYLE_NUM );
format_set_fg_colour( directory_colour );
fprintf(
stdout,
"%c%s%s ",
file_type,
file_permissions,
file_size
);
}
set_style( entry );
fprintf( stdout, "%s", entry->name );
if ( entry->type == type_directory )
reset_style( entry );
if ( type_link == entry->type )
{
format_reset_style( directory_style, DIR_STYLE_NUM );
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();
}
fprintf( stdout, "\n" );
// if ( !args->tree_view )
// {
// log_info(
// "%c%s%s %s",
// file_type,
// file_permissions,
// file_size,
// entry->name
// );
// }
// else
// {
fputc( '\n', stdout );
if ( recursion_level < args->depth
&& entry->type == type_directory )
if ( args->tree_view
&& recursion_level < args->depth
&& entry->type == type_directory )
{
DIR* subdirectory = opendir(entry->name);
if ( !subdirectory )
{
log_error( "failed to open directory `%s`", entry->name );
log_error( "failed to open directory '%s'", entry->name );
}
else
{
chdir( entry->name );
// TODO: handle return code
display_directory_contents(
args,
subdirectory,
recursion_level + 1,
is_last | (size_t)( num_entries == n + 1 ) << recursion_level
);
args,
subdirectory,
recursion_level + 1,
is_last | (size_t)( num_entries == n + 1 ) << recursion_level
);
closedir( subdirectory );
chdir( ".." );
}
@@ -485,3 +501,83 @@ void size_to_string(
);
}
}
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;
}
}