implemented listing files (path to file as argument)
This commit is contained in:
1 parent
6293ef1740
commit
d256d2b863
3 files changed
+159
-119
No files matched your search
+11
-4
@@ -3,13 +3,20 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "type_definitions.h"
|
||||
#include "arguments.h"
|
||||
|
||||
void display_type( entry_type_t type, bool no_colour );
|
||||
void display_permissions( const char* const permissions, bool no_colour );
|
||||
void display_size( directory_entry_t* entry, bool no_colour );
|
||||
void display_user( directory_entry_t* entry, bool no_colour );
|
||||
void display_size( const directory_entry_t* const entry, bool no_colour );
|
||||
void display_user( const directory_entry_t* const entry, bool no_colour );
|
||||
void display_time( time_t time, bool no_colour );
|
||||
void display_entry( const directory_entry_t* const entry, bool no_colour );
|
||||
void display_link_target( const directory_entry_t* const entry, bool no_colour );
|
||||
void display_entry(
|
||||
const directory_entry_t* const entry,
|
||||
const arguments_t* const args
|
||||
);
|
||||
void display_link_target(
|
||||
const directory_entry_t* const entry,
|
||||
bool no_colour
|
||||
);
|
||||
|
||||
#endif
|
||||
+92
-7
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "display.h"
|
||||
#include "type_definitions.h"
|
||||
#include "arguments.h"
|
||||
#include "log.h"
|
||||
#include "format.h"
|
||||
#include "config.h"
|
||||
@@ -15,14 +16,20 @@
|
||||
// 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(
|
||||
directory_entry_t* entry,
|
||||
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 )
|
||||
@@ -78,7 +85,7 @@ void display_permissions( const char* const permissions, bool no_colour )
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_size( directory_entry_t *entry, bool no_colour )
|
||||
void display_size( const directory_entry_t* const entry, bool no_colour )
|
||||
{
|
||||
const size_t length = 9;
|
||||
char size_string[ length ];
|
||||
@@ -98,7 +105,7 @@ void display_size( directory_entry_t *entry, bool no_colour )
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_user( directory_entry_t* entry, bool no_colour )
|
||||
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;
|
||||
@@ -138,11 +145,32 @@ void display_time( time_t time, bool no_colour )
|
||||
fputc( ' ', stdout );
|
||||
}
|
||||
|
||||
void display_entry( const directory_entry_t* const entry, bool no_colour )
|
||||
void display_entry(
|
||||
const directory_entry_t* const entry,
|
||||
const arguments_t* const args
|
||||
)
|
||||
{
|
||||
if ( !entry ) return;
|
||||
|
||||
if ( !no_colour )
|
||||
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 );
|
||||
}
|
||||
@@ -150,9 +178,18 @@ void display_entry( const directory_entry_t* const entry, bool no_colour )
|
||||
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 )
|
||||
void display_link_target(
|
||||
const directory_entry_t* const entry,
|
||||
bool no_colour
|
||||
)
|
||||
{
|
||||
char link[ PATH_MAX ];
|
||||
ssize_t length = 0;
|
||||
@@ -282,7 +319,7 @@ bool compare_sources( const directory_entry_t* const entry )
|
||||
}
|
||||
|
||||
void size_to_string(
|
||||
directory_entry_t* entry,
|
||||
const directory_entry_t* const entry,
|
||||
char* file_size,
|
||||
size_t length
|
||||
)
|
||||
@@ -333,3 +370,51 @@ void size_to_string(
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
+56
-108
@@ -12,6 +12,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <libgen.h>
|
||||
|
||||
// internal dependencies
|
||||
#include "log.h"
|
||||
@@ -24,15 +25,7 @@
|
||||
#define SUCCESS 0
|
||||
#define LENGTH_PERMISSIONS ( ( size_t )11 )
|
||||
|
||||
// constant declarations
|
||||
static char PERMISSION_CHARS[] = { 'x', 'w', 'r' };
|
||||
|
||||
// function prototypes
|
||||
static int permissions_to_string(
|
||||
size_t file_mode,
|
||||
permission_mode_t permission_mode,
|
||||
char* const string
|
||||
);
|
||||
static int count_entries( DIR* directory, const arguments_t* const args );
|
||||
static int get_entries(
|
||||
DIR* directory,
|
||||
@@ -47,6 +40,7 @@ static int handle_directory(
|
||||
size_t recursion_level,
|
||||
size_t is_last
|
||||
);
|
||||
entry_type_t get_entry_type( struct stat stats );
|
||||
|
||||
// program logic
|
||||
int main( int argc, char** argv )
|
||||
@@ -62,7 +56,29 @@ int main( int argc, char** argv )
|
||||
|
||||
for ( size_t n = 0; n < args.path_count; n++ )
|
||||
{
|
||||
DIR* directory = opendir( args.path[ n ] );
|
||||
struct stat status;
|
||||
DIR* directory;
|
||||
|
||||
if ( 0 != stat(args.path[ n ], &status ) )
|
||||
{
|
||||
perror( args.path[ n ] );
|
||||
}
|
||||
else if ( !S_ISDIR( status.st_mode ) )
|
||||
{
|
||||
char* name = basename( args.path[ n ] );
|
||||
directory_entry_t file =
|
||||
{
|
||||
.extension = strrchr( name, '.' ),
|
||||
.stats = status,
|
||||
.type = get_entry_type( status )
|
||||
};
|
||||
strncpy( file.name, name, NAME_MAX );
|
||||
// TODO: fix colours
|
||||
display_entry( &file, &args );
|
||||
continue;
|
||||
}
|
||||
|
||||
directory = opendir( args.path[ n ] );
|
||||
|
||||
if ( !directory )
|
||||
{
|
||||
@@ -81,7 +97,6 @@ int main( int argc, char** argv )
|
||||
|
||||
if ( !chdir( args.path[ n ] ) )
|
||||
{
|
||||
// TODO: handle return code
|
||||
handle_directory(
|
||||
&args,
|
||||
directory,
|
||||
@@ -169,87 +184,15 @@ int get_entries(
|
||||
}
|
||||
|
||||
new_entry->stats = file_stats;
|
||||
switch ( file_stats.st_mode & S_IFMT )
|
||||
{
|
||||
case S_IFDIR:
|
||||
new_entry->type = type_directory;
|
||||
break;
|
||||
case S_IFCHR:
|
||||
new_entry->type = type_character;
|
||||
break;
|
||||
case S_IFBLK:
|
||||
new_entry->type = type_block;
|
||||
break;
|
||||
case S_IFREG:
|
||||
new_entry->type = type_regular;
|
||||
break;
|
||||
case S_IFIFO:
|
||||
new_entry->type = type_fifo;
|
||||
break;
|
||||
case S_IFLNK:
|
||||
new_entry->type = type_link;
|
||||
break;
|
||||
case S_IFSOCK:
|
||||
new_entry->type = type_socket;
|
||||
break;
|
||||
}
|
||||
new_entry->type = get_entry_type( new_entry->stats );
|
||||
|
||||
entry_table[entry_count] = new_entry;
|
||||
|
||||
entry_count++;
|
||||
}
|
||||
|
||||
return ( int )entry_count;
|
||||
}
|
||||
|
||||
int permissions_to_string(
|
||||
size_t file_mode,
|
||||
permission_mode_t permission_mode,
|
||||
char* const string
|
||||
)
|
||||
{
|
||||
if ( !string ) return -EINVAL;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
/* check if entry should be displayed
|
||||
*
|
||||
* return 1 if entry should be ignored
|
||||
@@ -288,7 +231,6 @@ int handle_directory(
|
||||
if ( !directory ) return -EINVAL;
|
||||
if ( recursion_level > args->depth ) return -EINVAL;
|
||||
|
||||
char file_permissions[LENGTH_PERMISSIONS];
|
||||
int ret;
|
||||
int num_entries = count_entries( directory, args );
|
||||
|
||||
@@ -323,12 +265,6 @@ int handle_directory(
|
||||
{
|
||||
directory_entry_t* entry = entry_table[ n ];
|
||||
|
||||
( void )permissions_to_string(
|
||||
entry->stats.st_mode,
|
||||
args->permission_mode,
|
||||
file_permissions
|
||||
);
|
||||
|
||||
if ( args->tree_view )
|
||||
{
|
||||
char tree[ NAME_MAX ] = "";
|
||||
@@ -362,23 +298,7 @@ int handle_directory(
|
||||
fprintf( stdout, "%s", tree );
|
||||
}
|
||||
|
||||
if ( args->long_view && !args->tree_view )
|
||||
{
|
||||
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 );
|
||||
}
|
||||
|
||||
display_entry( entry, args->no_colour );
|
||||
|
||||
if ( type_link == entry->type )
|
||||
{
|
||||
display_link_target( entry, args->no_colour );
|
||||
}
|
||||
fputc( '\n', stdout );
|
||||
|
||||
display_entry( entry, args );
|
||||
|
||||
if ( args->tree_view
|
||||
&& recursion_level < args->depth
|
||||
@@ -392,7 +312,6 @@ int handle_directory(
|
||||
else
|
||||
{
|
||||
chdir( entry->name );
|
||||
// TODO: handle return code
|
||||
handle_directory(
|
||||
args,
|
||||
subdirectory,
|
||||
@@ -415,3 +334,32 @@ int handle_directory(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
entry_type_t get_entry_type( struct stat stats )
|
||||
{
|
||||
switch ( stats.st_mode & S_IFMT )
|
||||
{
|
||||
case S_IFDIR:
|
||||
return type_directory;
|
||||
|
||||
case S_IFCHR:
|
||||
return type_character;
|
||||
|
||||
case S_IFBLK:
|
||||
return type_block;
|
||||
|
||||
case S_IFREG:
|
||||
return type_regular;
|
||||
|
||||
case S_IFIFO:
|
||||
return type_fifo;
|
||||
|
||||
case S_IFLNK:
|
||||
return type_link;
|
||||
|
||||
case S_IFSOCK:
|
||||
return type_socket;
|
||||
|
||||
default:
|
||||
return type_regular;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user