From 0455ebaa40552bd80100dbc7e120e9374e9b3814 Mon Sep 17 00:00:00 2001 From: zegonix Date: Wed, 24 Jun 2026 23:27:51 +0200 Subject: [PATCH] added positional argument, started experimenting with lstat() --- inc/arguments.h | 2 ++ src/arguments.c | 27 ++++++++++++++++++++++++++- src/main.c | 25 ++++++++----------------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/inc/arguments.h b/inc/arguments.h index e07ca77..88289ba 100644 --- a/inc/arguments.h +++ b/inc/arguments.h @@ -13,10 +13,12 @@ typedef struct { bool show_all; bool show_almost_all; + bool no_colour; bool group_directories; bool human_readable; bool long_view; bool tree_view; + char* path; } arguments_t; void initialise_args( arguments_t* arguments ); diff --git a/src/arguments.c b/src/arguments.c index a5342ab..a64b57f 100644 --- a/src/arguments.c +++ b/src/arguments.c @@ -3,6 +3,9 @@ #include "arguments.h" #include "log.h" +// keys for arguments with no short version +#define KEY_NO_COLOUR ( ( int )0x100 ) + // function prototypes static error_t arg_parser( int key, char* arg, struct argp_state* state ); @@ -31,6 +34,14 @@ static struct argp_option options[] = .doc = "same as `all`, but hiding `.` and `..`", .group = 0 }, + { + .name = "no-color", + .key = KEY_NO_COLOUR, + .arg = NULL, + .flags = 0, + .doc = "do not colour the output (for scripting)", + .group = 0 + }, { .name = "group-directories", .key = 'g', @@ -83,10 +94,12 @@ void initialise_args( arguments_t* arguments ) { arguments->show_all = false; arguments->show_almost_all = false; + arguments->no_colour = false; arguments->group_directories = false; arguments->human_readable = false; arguments->long_view = false; arguments->tree_view = false; + arguments->path = "."; } error_t parse_args( int argc, char** argv, arguments_t* arguments ) @@ -103,7 +116,6 @@ error_t parse_args( int argc, char** argv, arguments_t* arguments ) static error_t arg_parser( int key, char* arg, struct argp_state* state ) { - ( void )arg; arguments_t* arguments = ( arguments_t* )( state->input ); if ( !state ) return -EINVAL; @@ -116,6 +128,9 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state ) case 'A': arguments->show_almost_all = true; break; + case KEY_NO_COLOUR: + arguments->no_colour = true; + break; case 'g': arguments->group_directories = true; break; @@ -128,6 +143,16 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state ) case 't': arguments->tree_view = true; break; + case ARGP_KEY_ARG: + if ( 0 == state->arg_num ) + { + arguments->path = arg; + } + else + { + return ARGP_ERR_UNKNOWN; + } + break; default: return ARGP_ERR_UNKNOWN; } diff --git a/src/main.c b/src/main.c index 0c315b9..2da62b6 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include #include #include +#include // internal dependencies #include "log.h" @@ -20,42 +21,32 @@ int main( int argc, char** argv ) parse_args( argc, argv, &args ); - char argument_directory[ PATH_MAX ] = "."; - DIR* directory = opendir( argument_directory ); + DIR* directory = opendir( args.path ); struct dirent* entry; if ( !directory ) { if ( ENOENT == errno ) - log_error( "no directory `%s`", argument_directory ); + log_error( "no directory `%s`", args.path ); else - log_error( "failed to open directory `%s`", argument_directory ); + log_error( "failed to open directory `%s`", args.path ); return -1; } while ( NULL != ( entry = readdir( directory ) ) ) { + char full_path[ PATH_MAX ]; // TODO: assemble full path + struct stat status = { 0 }; if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) ) { continue; } - log_info( "%s", entry->d_name ); + // if ( lstat( ) ) // TODO: get file stats + log_info( "%s: %c", entry->d_name, entry->d_type ); } closedir( directory ); - // char current_directory[ PATH_MAX ]; - // - // if ( NULL != getcwd( ¤t_directory[0], PATH_MAX ) ) - // { - // log_info( "current directory: %s", current_directory ); - // } - // else - // { - // log_error( "failed to get the current directory" ); - // return -1; - // } - return 0; }