refactoring
This commit is contained in:
1 parent
5b1b974ac7
commit
e37f04a1cd
6 files changed
+199
-120
No files matched your search
+133
-6
@@ -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)
|
||||
@@ -67,7 +141,7 @@ void display_link_target(const directory_entry_t* const entry)
|
||||
link[ length ] = '\0';
|
||||
link_invalid = stat( link, &link_stat );
|
||||
|
||||
fprintf( stdout, " --> ");
|
||||
fprintf( stdout, "--> ");
|
||||
|
||||
if ( link_invalid )
|
||||
{
|
||||
@@ -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,
|
||||
" "
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user