diff options
| author | Mosder <xmosder12@gmail.com> | 2026-07-27 18:15:19 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-07-27 18:15:19 +0200 |
| commit | 0b58af7bd1c0c060c55dadb33603570d0c044e14 (patch) | |
| tree | 45ea7b11f9988161eb56905bfd02aaa0e7d20e6b | |
| parent | 69b8bf48614da9501763e232cec752650fe7cf55 (diff) | |
refactor: changed hardcoded PROGRAM_NAME into argv[0] and moved global install destination into /usr/local/bin from /usr/bin
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | analyzer/commands.c | 16 | ||||
| -rw-r--r-- | analyzer/commands.h | 4 | ||||
| -rw-r--r-- | main.c | 2 |
5 files changed, 13 insertions, 13 deletions
@@ -2,7 +2,7 @@ CC=gcc CFLAGS=-Wall -Wextra -iquote ./analyzer -O2 -flto -s global: compile - sudo mv branal /usr/bin + sudo mv branal /usr/local/bin local: compile mv branal ${HOME}/.local/bin @@ -51,7 +51,7 @@ echo "#define INTERFACE \"<interface>\"" > analyzer/headers/interface.h **4. Compile the code**: Finally, compile the code with `make`. -To install it into `/usr/bin`: +To install it into `/usr/local/bin`: ``` make ``` diff --git a/analyzer/commands.c b/analyzer/commands.c index 676c78d..706a8f2 100644 --- a/analyzer/commands.c +++ b/analyzer/commands.c @@ -7,16 +7,15 @@ #include "capture.h" #include "utils/memory.h" -#define PROGRAM_NAME "branal" #define INDENT " " #define MAX_COMMAND_LENGTH 100 // calculate the longest command length to align explenations -int longest_command_length() { +int longest_command_length(const char *prog_name) { int longest_len = 0; for (size_t i = 0; i < commands_count; i++) { Command cmd = commands[i]; - int len = strlen(PROGRAM_NAME); + int len = strlen(prog_name); len += strlen(" ( | ) "); len += strlen(cmd.short_name) + strlen(cmd.name); len += strlen(cmd.args_str) == 0 ? 0 : strlen(cmd.args_str) + 1; @@ -29,19 +28,18 @@ int longest_command_length() { // help command handler int print_help(int argc, char *argv[]) { (void)argc; - (void)argv; - int len = longest_command_length(); + int len = longest_command_length(argv[0]); printf("A bare-bones analyzer for the game Broken Ranks\n"); printf("\nUsage:\n"); - printf(INDENT "%-*s- same as below\n", len, PROGRAM_NAME); + printf(INDENT "%-*s- same as below\n", len, argv[0]); for (size_t i = 0; i < commands_count; i++) { Command cmd = commands[i]; char buffer[MAX_COMMAND_LENGTH]; - sprintf(buffer, PROGRAM_NAME " (%s | %s) ", cmd.short_name, cmd.name); + sprintf(buffer, "%s (%s | %s) ", argv[0], cmd.short_name, cmd.name); if (strlen(cmd.args_str)) strcat(buffer, cmd.args_str); @@ -98,7 +96,7 @@ int is_command(Command command, char *str) { return !strcmp(command.short_name, str) || !strcmp(command.name, str); } -void command_not_recognized() { +void command_not_recognized(const char *prog_name) { printf("Command not recognized\n"); - printf("Use \"%s (%s | %s)\" for help\n", PROGRAM_NAME, help_command.short_name, help_command.name); + printf("Use \"%s (%s | %s)\" for help\n", prog_name, help_command.short_name, help_command.name); } diff --git a/analyzer/commands.h b/analyzer/commands.h index f53a140..378af36 100644 --- a/analyzer/commands.h +++ b/analyzer/commands.h @@ -27,4 +27,6 @@ extern const size_t commands_count; extern int is_command(Command command, char *str); // print information about help when program didn't recognize command -extern void command_not_recognized(); +// params: +// - prog_name - name of the program (argv[0]) +extern void command_not_recognized(const char *prog_name); @@ -13,6 +13,6 @@ int main(int argc, char *argv[]) { return commands[i].handler(argc, argv); } - command_not_recognized(); + command_not_recognized(argv[0]); return 1; } |
