fixed argp code, implemented --all

This commit is contained in:
root committed 2026-06-23 23:41:45 +02:00
1 parent 62542734b5
commit c87dd241a0
3 files changed
+99 -58

No files matched your search

+1
View File
@@ -19,6 +19,7 @@ typedef struct {
bool tree_view;
} arguments_t;
void initialise_args( arguments_t* arguments );
error_t parse_args( int argc, char** argv, arguments_t* arguments );
#endif
+91 -44
View File
@@ -1,67 +1,73 @@
#include <argp.h>
#include <stdbool.h>
#include "arguments.h"
#include "log.h"
// function prototypes
static error_t arg_parser( int key, char* arg, struct argp_state* state );
// constant definitions
const char* argp_program_version = "scan version 0.1";
// global variable definitions
const char* argp_program_version = "0.1";
static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view.";
static char argument_description[] = "ARGUMENTS:";
static char argument_description[] = "[path]";
static struct argp_option options[] =
{
{
"all",
'a',
NULL,
OPTION_ARG_OPTIONAL,
"show all items (including hidden ones)",
0
.name = "all",
.key = 'a',
.arg = NULL,
.flags = 0,
.doc = "show all items (including hidden ones)",
.group = 0
},
{
"almost-all",
'A',
NULL,
OPTION_ARG_OPTIONAL,
"same --all, excluding . and ..",
0
.name = "almost-all",
.key = 'A',
.arg = NULL,
.flags = 0,
.doc = "same as `all`, but hiding `.` and `..`",
.group = 0
},
{
"group-directories",
'g',
NULL,
OPTION_ARG_OPTIONAL,
"group directories at the top",
0
.name = "group-directories",
.key = 'g',
.arg = NULL,
.flags = 0,
.doc = "group directories at the top",
.group = 0
},
{
"human-readable",
'h',
NULL,
OPTION_ARG_OPTIONAL,
"show sizes in human readable format",
0
.name = "human-readable",
.key = 'h',
.arg = NULL,
.flags = 0,
.doc = "show sizes in human readable format",
.group = 0
},
{
"long",
'l',
NULL,
OPTION_ARG_OPTIONAL,
"use long listing format",
0
.name = "long",
.key = 'l',
.arg = NULL,
.flags = 0,
.doc = "use long listing format",
.group = 0
},
{
"tree",
'T',
NULL,
OPTION_ARG_OPTIONAL,
"use tree listing format",
0
}
.name = "tree",
.key = 'T',
.arg = NULL,
.flags = 0,
.doc = "use tree listing format",
.group = 0
},
{ 0 } // this is required to properly end the options array ..
};
static struct argp parser = {
static struct argp parser =
{
options,
arg_parser,
argument_description,
@@ -73,17 +79,58 @@ static struct argp parser = {
// module logic
void initialise_args( arguments_t* arguments )
{
arguments->show_all = false;
arguments->show_almost_all = false;
arguments->group_directories = false;
arguments->human_readable = false;
arguments->long_view = false;
arguments->tree_view = false;
}
error_t parse_args( int argc, char** argv, arguments_t* arguments )
{
// TODO: revisit ARG_INDEX for handling unknown arguments
return argp_parse(&parser, argc, argv, 0, NULL, arguments);
int arg_index = 0;
if ( 0 != argp_parse(&parser, argc, argv, 0, &arg_index, arguments ) )
{
log_error( "error! arg_index = %d;", arg_index );
}
return 0;
}
static error_t arg_parser( int key, char* arg, struct argp_state* state )
{
( void )key;
( void )arg;
( void )state;
arguments_t* arguments = ( arguments_t* )( state->input );
if ( !state ) return -EINVAL;
switch ( key )
{
case 'a':
arguments->show_all = true;
break;
case 'A':
arguments->show_almost_all = true;
break;
case 'g':
arguments->group_directories = true;
break;
case 'h':
arguments->human_readable = true;
break;
case 'l':
arguments->long_view = true;
break;
case 't':
arguments->tree_view = true;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
+7 -14
View File
@@ -16,22 +16,11 @@ int main( int argc, char** argv )
{
arguments_t args;
initialise_args( &args );
parse_args( argc, argv, &args );
if ( 2 < argc ) return -1;
char argument_directory[ PATH_MAX ];
if ( 2 == argc )
{
strcpy( argument_directory, argv[1] );
}
else
{
argument_directory[0] = '.';
argument_directory[1] = '\0';
}
char argument_directory[ PATH_MAX ] = ".";
DIR* directory = opendir( argument_directory );
struct dirent* entry;
@@ -47,6 +36,10 @@ int main( int argc, char** argv )
while ( NULL != ( entry = readdir( directory ) ) )
{
if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) )
{
continue;
}
log_info( "%s", entry->d_name );
}