From 078590846a0f2bb8515d0362c1f1d68e53164698 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Fri, 22 Nov 2024 23:10:14 +0300 Subject: [PATCH 1/4] feature: support file paths that resemble help options --- src/main.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 27c3ec2..6619004 100644 --- a/src/main.c +++ b/src/main.c @@ -186,12 +186,15 @@ static bool args_match(const char* arg, const char* args_list[]) { int main(int argc, char** argv) { puts("Polonium: a file corrupter\n"); - if (argc < 2 || args_match(argv[argc - 1], ARGS_HELP)) { + if (!(argc > 2 && !strcmp(argv[1], "--")) && + (argc < 2 || args_match(argv[argc - 1], ARGS_HELP))) { char* program_name = my_basename(argv[0]); printf( "Usage: %s [parameters] [options]\n" " OR\n" " %s <-h | -help | --help>\n" +" OR\n" +" %s -- [parameters] [options]\n" "\n" "Both parameters and options are optional.\n" "\n" @@ -230,6 +233,7 @@ int main(int argc, char** argv) { " random, set the option to `random`.\n" "\n" " -verbose : Enable verbose output\n", + program_name, program_name, program_name, UINT16_MAX, @@ -243,14 +247,23 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } + int arg_position = 2; + + /* Allow passing file paths that resemble help options by using the help + * option delimiter (`--`). */ const char* file_path = argv[1]; + if (argc > 2 && !strcmp(argv[1], "--")) { + file_path = argv[2]; + arg_position++; + } + file_type_t file_type = FILE_TYPE_AUTO; if (argc > 2) { uint8_t arg_destination = ARG_NO_DEST; bool read_off_value = false; - for (int i = 2; i < argc; ++i) { + for (int i = arg_position; i < argc; ++i) { bool last_arg = i == (argc - 1); #define arg argv[i] From 1205f4c89870e926cdeb98c12df56f4aecc678d6 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Fri, 22 Nov 2024 23:47:56 +0300 Subject: [PATCH 2/4] readability: replace macro `LOOP_ACTION_CONFIG` with inline code --- include/common.h | 4 ---- src/main.c | 15 ++++++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/common.h b/include/common.h index f15b61f..217bd74 100644 --- a/include/common.h +++ b/include/common.h @@ -106,8 +106,4 @@ typedef off_t file_offset_t; #define SET_CONFIG(x) config |= (x) #define CLEAR_CONFIG(x) config &= ~(x) -#define LOOP_ACTION_CONFIG(f, x) \ - f(x); \ - continue - #endif /* _COMMON_H */ diff --git a/src/main.c b/src/main.c index 6619004..a79a163 100644 --- a/src/main.c +++ b/src/main.c @@ -283,15 +283,20 @@ int main(int argc, char** argv) { else if (ARG_MATCH(arg, ARG_SEED)) arg_destination = ARG_DEST_SEED; else if (ARG_MATCH(arg, ARG_NOCONFIRM)) { - LOOP_ACTION_CONFIG(CLEAR_CONFIG, C_CONFIRM); + CLEAR_CONFIG(C_CONFIRM); + continue; } else if (ARG_MATCH(arg, ARG_CONTENTS)) { - LOOP_ACTION_CONFIG(SET_CONFIG, C_CONTENTS); + SET_CONFIG(C_CONTENTS); + continue; } else if (ARG_MATCH(arg, ARG_LINE_ENDINGS)) { - LOOP_ACTION_CONFIG(SET_CONFIG, C_LINE_ENDINGS); + SET_CONFIG(C_LINE_ENDINGS); + continue; } else if (ARG_MATCH(arg, ARG_PRINTABLE)) { - LOOP_ACTION_CONFIG(SET_CONFIG, C_PRINTABLE); + SET_CONFIG(C_PRINTABLE); + continue; } else if (ARG_MATCH(arg, ARG_VERBOSE)) { - LOOP_ACTION_CONFIG(SET_CONFIG, C_VERBOSE); + SET_CONFIG(C_VERBOSE); + continue; } else FATAL_ERROR_RET("Unknown command line parameter '%s'.\n" "Please invoke the program with argument %s to " From e7dc39f1f3eee1f922852175702c5babd9c29a8d Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sun, 24 Nov 2024 11:58:33 +0300 Subject: [PATCH 3/4] corrupter.c: declare function `corrupt_byte` as static --- src/corrupter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corrupter.c b/src/corrupter.c index d01aad0..1924019 100644 --- a/src/corrupter.c +++ b/src/corrupter.c @@ -44,7 +44,7 @@ static bool is_line_ending(byte c) { return c == '\n' || c == '\r'; } -enum Interoperation_Result corrupt_byte(Interoperation_Input_Vars) { +static enum Interoperation_Result corrupt_byte(Interoperation_Input_Vars) { uint8_t config = param->config; FSEEK_MACRO(file, i, SEEK_SET); From 7f67707aa89d831ec968d445f3e4e17700f2139b Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sun, 24 Nov 2024 12:06:09 +0300 Subject: [PATCH 4/4] corrupter.c: add definition for `corrupt_byte` --- src/corrupter.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/corrupter.c b/src/corrupter.c index 1924019..501dce5 100644 --- a/src/corrupter.c +++ b/src/corrupter.c @@ -29,6 +29,7 @@ const size_t UINT16_MAX_PLUS_1 = UINT16_MAX + 1; /* function definitions */ static bool get_chance(uint16_t desired_chance); static bool is_line_ending(byte c); +static enum Interoperation_Result corrupt_byte(Interoperation_Input_Vars); /* function implementations */ static bool get_chance(uint16_t desired_chance) {