From 6293ef17409cd189c926fa4b6a2046354c384440 Mon Sep 17 00:00:00 2001 From: zegonix Date: Fri, 9 Oct 2026 17:00:50 +0200 Subject: [PATCH] implemented handling of multiple path arguments --- inc/arguments.h | 5 ++++- src/arguments.c | 15 +++++-------- src/main.c | 60 +++++++++++++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/inc/arguments.h b/inc/arguments.h index b98a32b..3e00d6d 100644 --- a/inc/arguments.h +++ b/inc/arguments.h @@ -10,6 +10,8 @@ #include #include +#define MAX_PATH_NUM ( ( int )256 ) + typedef enum { no_permissions = 0, char_permissions = 1, @@ -44,7 +46,8 @@ typedef struct { permission_mode_t permission_mode; sorting_mode_t sorting_mode; time_mode_t time_mode; - char* path; + char* path[MAX_PATH_NUM]; + size_t path_count; } arguments_t; void initialise_args( arguments_t* arguments ); diff --git a/src/arguments.c b/src/arguments.c index 0ab2bee..4a143da 100644 --- a/src/arguments.c +++ b/src/arguments.c @@ -172,7 +172,8 @@ void initialise_args( arguments_t* arguments ) arguments->permission_mode = octal_permissions; arguments->sorting_mode = sort_by_extension; arguments->time_mode = time_none; - arguments->path = "."; + arguments->path[ 0 ] = "."; + arguments->path_count = 1u; } @@ -309,15 +310,11 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state ) } break; case ARGP_KEY_ARG: - // TODO: handle multiple paths if ( 0 == state->arg_num ) - { - arguments->path = arg; - } - else - { - return ARGP_ERR_UNKNOWN; - } + arguments->path_count = 0u; + + arguments->path[ arguments->path_count ] = arg; + arguments->path_count++; break; default: return ARGP_ERR_UNKNOWN; diff --git a/src/main.c b/src/main.c index 189960c..77efd5e 100644 --- a/src/main.c +++ b/src/main.c @@ -60,40 +60,46 @@ int main( int argc, char** argv ) if ( args.display_help ) return SUCCESS; - DIR* directory = opendir( args.path ); - - if ( !directory ) + for ( size_t n = 0; n < args.path_count; n++ ) { - if ( ENOENT == errno ) + DIR* directory = opendir( args.path[ n ] ); + + if ( !directory ) { - log_error( "no directory `%s`", args.path ); - return -ENOENT; + if ( ENOENT == errno ) + { + log_error( "no directory `%s`", args.path[ n ] ); + return -ENOENT; + } + + log_error( "failed to open directory `%s`", args.path[ n ] ); + return -EIO; } - log_error( "failed to open directory `%s`", args.path ); - return -EIO; - } + if ( args.tree_view ) + log_info( "%s", args.path[ n ] ); - if ( args.tree_view ) - log_info( "%s", args.path ); + if ( !chdir( args.path[ n ] ) ) + { + // TODO: handle return code + handle_directory( + &args, + directory, + 0, + 0 + ); + } + else + { + log_error( "failed to enter directory '%s'", args.path[ n ] ); + retval = -EIO; + } - if ( !chdir( args.path ) ) - { - // TODO: handle return code - handle_directory( - &args, - directory, - 0, - 0 - ); - } - else - { - log_error( "failed to enter directory '%s'", args.path ); - retval = -EIO; - } + chdir( ".." ); + closedir( directory ); - closedir( directory ); + fputc( '\n', stdout ); + } return retval; }