From 5888e8c839f0aff24d63b5581ed902da47ba36c3 Mon Sep 17 00:00:00 2001 From: midipix Date: Sun, 22 Nov 2015 01:03:06 -0500 Subject: driver integration 4/9: argument vector parser: derive program name as needed. --- src/internal/argv/argv.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/internal/argv') diff --git a/src/internal/argv/argv.h b/src/internal/argv/argv.h index 4749769..0cb3c40 100644 --- a/src/internal/argv/argv.h +++ b/src/internal/argv/argv.h @@ -82,6 +82,8 @@ struct argv_ctx { const char * program; }; +static const char * argv_program_name(const char *); + static void argv_usage( FILE *, const char * header, @@ -377,6 +379,21 @@ static void argv_scan( } } +static const char * argv_program_name(const char * program_path) +{ + const char * ch; + + if (program_path) { + if ((ch = strrchr(program_path,'/'))) + return *(++ch) ? ch : 0; + + if ((ch = strrchr(program_path,'\\'))) + return *(++ch) ? ch : 0; + } + + return program_path; +} + static void argv_show_error(struct argv_ctx * ctx) { fprintf(stderr,"%s: error: ",ctx->program); @@ -538,8 +555,12 @@ static struct argv_meta * argv_get( argv_scan(argv,options,&ctx,0); if (ctx.errcode != ARGV_ERROR_OK) { + if (!ctx.program) + ctx.program = argv_program_name(argv[0]); + if (ctx.flags & ARGV_VERBOSITY_ERRORS) argv_show_error(&ctx); + return 0; } -- cgit v1.2.3