implemented handling of multiple path arguments
This commit is contained in:
1 parent
030ca4584a
commit
6293ef1740
3 files changed
+22
-16
No files matched your search
+4
-1
@@ -10,6 +10,8 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <argp.h>
|
#include <argp.h>
|
||||||
|
|
||||||
|
#define MAX_PATH_NUM ( ( int )256 )
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
no_permissions = 0,
|
no_permissions = 0,
|
||||||
char_permissions = 1,
|
char_permissions = 1,
|
||||||
@@ -44,7 +46,8 @@ typedef struct {
|
|||||||
permission_mode_t permission_mode;
|
permission_mode_t permission_mode;
|
||||||
sorting_mode_t sorting_mode;
|
sorting_mode_t sorting_mode;
|
||||||
time_mode_t time_mode;
|
time_mode_t time_mode;
|
||||||
char* path;
|
char* path[MAX_PATH_NUM];
|
||||||
|
size_t path_count;
|
||||||
} arguments_t;
|
} arguments_t;
|
||||||
|
|
||||||
void initialise_args( arguments_t* arguments );
|
void initialise_args( arguments_t* arguments );
|
||||||
|
|||||||
+6
-9
@@ -172,7 +172,8 @@ void initialise_args( arguments_t* arguments )
|
|||||||
arguments->permission_mode = octal_permissions;
|
arguments->permission_mode = octal_permissions;
|
||||||
arguments->sorting_mode = sort_by_extension;
|
arguments->sorting_mode = sort_by_extension;
|
||||||
arguments->time_mode = time_none;
|
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;
|
break;
|
||||||
case ARGP_KEY_ARG:
|
case ARGP_KEY_ARG:
|
||||||
// TODO: handle multiple paths
|
|
||||||
if ( 0 == state->arg_num )
|
if ( 0 == state->arg_num )
|
||||||
{
|
arguments->path_count = 0u;
|
||||||
arguments->path = arg;
|
|
||||||
}
|
arguments->path[ arguments->path_count ] = arg;
|
||||||
else
|
arguments->path_count++;
|
||||||
{
|
|
||||||
return ARGP_ERR_UNKNOWN;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return ARGP_ERR_UNKNOWN;
|
return ARGP_ERR_UNKNOWN;
|
||||||
|
|||||||
+12
-6
@@ -60,24 +60,26 @@ int main( int argc, char** argv )
|
|||||||
if ( args.display_help )
|
if ( args.display_help )
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
|
|
||||||
DIR* directory = opendir( args.path );
|
for ( size_t n = 0; n < args.path_count; n++ )
|
||||||
|
{
|
||||||
|
DIR* directory = opendir( args.path[ n ] );
|
||||||
|
|
||||||
if ( !directory )
|
if ( !directory )
|
||||||
{
|
{
|
||||||
if ( ENOENT == errno )
|
if ( ENOENT == errno )
|
||||||
{
|
{
|
||||||
log_error( "no directory `%s`", args.path );
|
log_error( "no directory `%s`", args.path[ n ] );
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
log_error( "failed to open directory `%s`", args.path );
|
log_error( "failed to open directory `%s`", args.path[ n ] );
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( args.tree_view )
|
if ( args.tree_view )
|
||||||
log_info( "%s", args.path );
|
log_info( "%s", args.path[ n ] );
|
||||||
|
|
||||||
if ( !chdir( args.path ) )
|
if ( !chdir( args.path[ n ] ) )
|
||||||
{
|
{
|
||||||
// TODO: handle return code
|
// TODO: handle return code
|
||||||
handle_directory(
|
handle_directory(
|
||||||
@@ -89,12 +91,16 @@ int main( int argc, char** argv )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
log_error( "failed to enter directory '%s'", args.path );
|
log_error( "failed to enter directory '%s'", args.path[ n ] );
|
||||||
retval = -EIO;
|
retval = -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chdir( ".." );
|
||||||
closedir( directory );
|
closedir( directory );
|
||||||
|
|
||||||
|
fputc( '\n', stdout );
|
||||||
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user