fix: prog name behavior
This commit is contained in:
parent
75f622e610
commit
fc9ddc404f
6 changed files with 15 additions and 38 deletions
|
|
@ -27,13 +27,7 @@ $(VERSION_HEADER): FORCE
|
|||
echo "#define PING_GIT_COMMIT \"$$PING_GIT_COMMIT\"" >> $$NEW_HEADER; \
|
||||
echo "#define PING_HAS_GIT_COMMIT $$HAS_GIT" >> $$NEW_HEADER; \
|
||||
echo "" >> $$NEW_HEADER; \
|
||||
echo "struct prog_name" >> $$NEW_HEADER; \
|
||||
echo "{" >> $$NEW_HEADER; \
|
||||
echo " char *alloc;" >> $$NEW_HEADER; \
|
||||
echo " const char *name;" >> $$NEW_HEADER; \
|
||||
echo "};" >> $$NEW_HEADER; \
|
||||
echo "" >> $$NEW_HEADER; \
|
||||
echo "extern struct prog_name g_prog_name;" >> $$NEW_HEADER; \
|
||||
echo "extern char* g_prog_name;" >> $$NEW_HEADER; \
|
||||
echo "" >> $$NEW_HEADER; \
|
||||
echo "#endif" >> $$NEW_HEADER; \
|
||||
if test ! -f $(VERSION_HEADER) || ! cmp -s $$NEW_HEADER $(VERSION_HEADER); then \
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@
|
|||
#include "ping/cli.h"
|
||||
#include "version_gen.h"
|
||||
|
||||
#define CMD_NAME g_prog_name.name
|
||||
|
||||
/* Forward declarations */
|
||||
static inline void print_suggest_help(void);
|
||||
/* -------------------- */
|
||||
|
|
@ -14,7 +12,7 @@ enum cli_code
|
|||
error_unknown_opt(const char *current_opt)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: unknown option -- '%s'\n",
|
||||
CMD_NAME, current_opt);
|
||||
g_prog_name, current_opt);
|
||||
print_suggest_help();
|
||||
return CLI_ERROR;
|
||||
}
|
||||
|
|
@ -23,7 +21,7 @@ enum cli_code
|
|||
error_invalid_arg(const char *current_opt)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: option '%s' requires an argument\n",
|
||||
CMD_NAME, current_opt);
|
||||
g_prog_name, current_opt);
|
||||
print_suggest_help();
|
||||
return CLI_ERROR;
|
||||
}
|
||||
|
|
@ -32,7 +30,7 @@ enum cli_code
|
|||
error_invalid_opt(const char *current_opt)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: invalid option '%s'\n",
|
||||
CMD_NAME, current_opt);
|
||||
g_prog_name, current_opt);
|
||||
print_suggest_help();
|
||||
return CLI_ERROR;
|
||||
}
|
||||
|
|
@ -41,7 +39,7 @@ enum cli_code
|
|||
error_duplicate_opt(const char *current_opt)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: duplicate option '%s'\n",
|
||||
CMD_NAME, current_opt);
|
||||
g_prog_name, current_opt);
|
||||
print_suggest_help();
|
||||
return CLI_ERROR;
|
||||
}
|
||||
|
|
@ -50,7 +48,7 @@ enum cli_code
|
|||
error_missing_dest(void)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: usage error: Destination address required\n",
|
||||
CMD_NAME);
|
||||
g_prog_name);
|
||||
print_suggest_help();
|
||||
return CLI_ERROR;
|
||||
}
|
||||
|
|
@ -58,7 +56,7 @@ error_missing_dest(void)
|
|||
enum cli_code
|
||||
error_invalid_dest(void)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: unknown host\n", CMD_NAME);
|
||||
dprintf(STDERR_FILENO, "%s: unknown host\n", g_prog_name);
|
||||
return CLI_ERROR;
|
||||
}
|
||||
|
||||
|
|
@ -66,5 +64,5 @@ static inline void
|
|||
print_suggest_help(void)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "Try '%s --help' for more information.\n",
|
||||
CMD_NAME);
|
||||
g_prog_name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
void
|
||||
print_version(void)
|
||||
{
|
||||
printf("%s version %s\n", g_prog_name.name, PING_VERSION);
|
||||
printf("%s version %s\n", g_prog_name, PING_VERSION);
|
||||
printf("Copyright (C) 2026 lohhiicccc\n");
|
||||
printf("License GPLv3+:");
|
||||
printf("GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>\n");
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ ping_run(const struct ping_config *config)
|
|||
if (NULL == handle)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: requires CAP_NET_RAW or root privileges\n",
|
||||
g_prog_name.name);
|
||||
g_prog_name);
|
||||
return 1;
|
||||
}
|
||||
if (HAS_FLAG(config->flags, FLAG_DONT_FRAGMENT)
|
||||
|
|
|
|||
|
|
@ -22,13 +22,13 @@ do_send(struct ping_state *state, size_t payload_len)
|
|||
state->send_flag = 0;
|
||||
if (0 != ping_send_one(state, payload_len))
|
||||
dprintf(STDERR_FILENO, "%s: %s\n",
|
||||
g_prog_name.name, icmp_strerror(state->handle));
|
||||
g_prog_name, icmp_strerror(state->handle));
|
||||
if (HAS_FLAG(state->config->flags, FLAG_FLOOD))
|
||||
ping_output_flood_dot();
|
||||
if (0 != ping_scheduler_arm(state->config))
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%s: setitimer: %s\n",
|
||||
g_prog_name.name, strerror(errno));
|
||||
g_prog_name, strerror(errno));
|
||||
state->stop_flag = 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,7 @@
|
|||
#include "ping/cli.h"
|
||||
#include "version_gen.h"
|
||||
|
||||
/* Forward declarations */
|
||||
static int init_prog_name(char *name);
|
||||
struct prog_name g_prog_name = {NULL, NULL};
|
||||
/* -------------------- */
|
||||
char *g_prog_name = NULL;
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
|
|
@ -15,8 +12,8 @@ main(int argc, char **argv)
|
|||
struct ping_config config = {0};
|
||||
int ret = CLI_ERROR;
|
||||
|
||||
if (0 != init_prog_name(argv[0]))
|
||||
goto cleanup;
|
||||
g_prog_name = argv[0];
|
||||
cli_set_prog_name(g_prog_name);
|
||||
ret = cli_parse_arguments(argc, argv, &config);
|
||||
if (ret != CLI_SUCCESS)
|
||||
goto cleanup;
|
||||
|
|
@ -25,18 +22,6 @@ main(int argc, char **argv)
|
|||
|
||||
cleanup:
|
||||
cli_config_free(&config);
|
||||
free(g_prog_name.alloc);
|
||||
return (ret == CLI_ERROR) ? 2 : EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static int
|
||||
init_prog_name(char *name)
|
||||
{
|
||||
char *path_dup = strdup(name);
|
||||
if (NULL == path_dup)
|
||||
return 1;
|
||||
|
||||
g_prog_name.alloc = path_dup;
|
||||
g_prog_name.name = basename(path_dup);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue