Files
scan/src/display.c
T

421 lines
10 KiB
C

#define _XOPEN_SOURCE 700
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <time.h>
#include "display.h"
#include "type_definitions.h"
#include "arguments.h"
#include "log.h"
#include "format.h"
#include "config.h"
#include "stringing.h"
// function signatures
void set_type_style( const directory_entry_t* const entry );
void set_file_style( const directory_entry_t* const entry );
void display_permissions( const char* const permissions, bool no_colour );
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(
const directory_entry_t* const entry,
char* file_size,
size_t length
);
void permissions_to_string(
size_t file_mode,
permission_mode_t permission_mode,
char* const string
);
// module logic
void display_type( entry_type_t type, bool no_colour )
{
if ( !no_colour )
{
format_set_style( type_style, TYPE_STYLE_NUM );
format_set_colour( type_colour );
}
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, bool no_colour )
{
if ( !permissions ) return;
if ( !no_colour )
{
format_set_style( permission_style, PERM_STYLE_NUM );
format_set_colour( permission_colour );
}
fprintf( stdout, "%s", permissions );
format_clear();
fputc( ' ', stdout );
}
void display_size( const directory_entry_t* const entry, bool no_colour )
{
const size_t length = 9;
char size_string[ length ];
if ( !entry ) return;
size_to_string( entry, size_string, length );
if ( !no_colour )
{
format_set_style( size_style, SIZE_STYLE_NUM );
format_set_colour( size_colour );
}
fprintf( stdout, "%s", size_string );
format_clear();
fputc( ' ', stdout );
}
void display_user( const directory_entry_t* const entry, bool no_colour )
{
struct passwd* user = getpwuid( entry->stats.st_uid );
const size_t length = 8;
char string[ length ];
if ( !entry ) return;
snprintf( string, length, "%s", user->pw_name );
if ( !no_colour )
{
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, bool no_colour )
{
const size_t length = 17;
char string[ length ];
struct tm* local = localtime( &time );
strftime( string, length, "%Y-%m-%d %H:%M", local );
if ( !no_colour )
{
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,
const arguments_t* const args
)
{
if ( !entry ) return;
if ( args->long_view && !args->tree_view )
{
const size_t PERMISSION_LENGTH = 11u;
char file_permissions[ PERMISSION_LENGTH ];
permissions_to_string(
entry->stats.st_mode,
octal_permissions,
file_permissions
);
display_type( entry->type, args->no_colour );
display_permissions( file_permissions, args->no_colour );
display_size( entry, args->no_colour );
display_user( entry, args->no_colour );
display_time( entry->stats.st_mtime, args->no_colour );
}
if ( !args->no_colour )
{
set_type_style( entry );
}
fprintf( stdout, "%s", entry->name );
format_clear();
fputc( ' ', stdout );
if ( type_link == entry->type )
{
display_link_target( entry, args->no_colour );
}
fputc( '\n', stdout );
}
void display_link_target(
const directory_entry_t* const entry,
bool no_colour
)
{
char link[ PATH_MAX ];
ssize_t length = 0;
struct stat link_stat;
int link_invalid;
if ( !entry ) return;
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 ( !no_colour )
{
if ( link_invalid )
{
format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM );
format_set_colour( brokenlink_colour );
}
else
{
format_set_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_set_colour( link_colour );
}
}
fprintf( stdout, "%s", link );
format_clear();
}
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_colour( block_colour );
break;
case type_character:
format_set_style( &character_style[ 0 ], CHAR_STYLE_NUM );
format_set_colour( character_colour );
break;
case type_directory:
format_set_style( &directory_style[ 0 ], DIR_STYLE_NUM );
format_set_colour( directory_colour );
break;
case type_fifo:
format_set_style( &fifo_style[ 0 ], FIFO_STYLE_NUM );
format_set_colour( fifo_colour );
break;
case type_link:
format_set_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_set_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_colour( socket_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_colour( makefile_colour );
}
else if ( compare_archives( entry ) )
{
format_set_style( &archive_style[ 0 ], AR_STYLE_NUM );
format_set_colour( archive_colour );
}
else if ( compare_sources( entry ) )
{
format_set_style( &source_style[ 0 ], SRC_STYLE_NUM );
format_set_colour( source_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 );
}
void size_to_string(
const directory_entry_t* const 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,
" "
);
}
}
void permissions_to_string(
size_t file_mode,
permission_mode_t permission_mode,
char* const string
)
{
const char PERMISSION_CHARS[] = { 'x', 'w', 'r' };
if ( !string ) return;
mode_t permissions = file_mode
& ( S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO );
switch ( permission_mode )
{
case no_permissions:
string[0] = '\0';
break;
case char_permissions:
string[ 0 ] = ' ';
for ( int n = 0; n < 9; n++ )
{
if ( permissions & ( 1 << n ) )
{
string[ 9 - n ] = PERMISSION_CHARS[ n % 3 ];
}
else
{
string[ 9 - n ] = '-';
}
}
string[ 10 ] = '\0';
break;
case octal_permissions:
snprintf(
&string[ 0 ],
10,
"%04o",
permissions
);
break;
}
}