Compare commits
No commits in common. "d521db90f8b6db3df6be31ba8658f7ccf65fd08d" and "75f622e61099ac81d73484ed7d39f01623f800cf" have entirely different histories.
d521db90f8
...
75f622e610
7 changed files with 39 additions and 16 deletions
|
|
@ -27,7 +27,13 @@ $(VERSION_HEADER): FORCE
|
||||||
echo "#define PING_GIT_COMMIT \"$$PING_GIT_COMMIT\"" >> $$NEW_HEADER; \
|
echo "#define PING_GIT_COMMIT \"$$PING_GIT_COMMIT\"" >> $$NEW_HEADER; \
|
||||||
echo "#define PING_HAS_GIT_COMMIT $$HAS_GIT" >> $$NEW_HEADER; \
|
echo "#define PING_HAS_GIT_COMMIT $$HAS_GIT" >> $$NEW_HEADER; \
|
||||||
echo "" >> $$NEW_HEADER; \
|
echo "" >> $$NEW_HEADER; \
|
||||||
echo "extern char* g_prog_name;" >> $$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 "" >> $$NEW_HEADER; \
|
echo "" >> $$NEW_HEADER; \
|
||||||
echo "#endif" >> $$NEW_HEADER; \
|
echo "#endif" >> $$NEW_HEADER; \
|
||||||
if test ! -f $(VERSION_HEADER) || ! cmp -s $$NEW_HEADER $(VERSION_HEADER); then \
|
if test ! -f $(VERSION_HEADER) || ! cmp -s $$NEW_HEADER $(VERSION_HEADER); then \
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ cli_handle_ttl(const char *arg, void *config_void)
|
||||||
struct ping_config *config = (struct ping_config *)config_void;
|
struct ping_config *config = (struct ping_config *)config_void;
|
||||||
uint64_t ttl;
|
uint64_t ttl;
|
||||||
|
|
||||||
if (0 != cli_parse_uint64(arg, &ttl) || ttl > 255 || 0 == ttl)
|
if (0 != cli_parse_uint64(arg, &ttl) || ttl > 255)
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
|
|
||||||
config->ttl = (uint8_t)ttl;
|
config->ttl = (uint8_t)ttl;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
#include "ping/cli.h"
|
#include "ping/cli.h"
|
||||||
#include "version_gen.h"
|
#include "version_gen.h"
|
||||||
|
|
||||||
|
#define CMD_NAME g_prog_name.name
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
static inline void print_suggest_help(void);
|
static inline void print_suggest_help(void);
|
||||||
/* -------------------- */
|
/* -------------------- */
|
||||||
|
|
@ -12,7 +14,7 @@ enum cli_code
|
||||||
error_unknown_opt(const char *current_opt)
|
error_unknown_opt(const char *current_opt)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: unknown option -- '%s'\n",
|
dprintf(STDERR_FILENO, "%s: unknown option -- '%s'\n",
|
||||||
g_prog_name, current_opt);
|
CMD_NAME, current_opt);
|
||||||
print_suggest_help();
|
print_suggest_help();
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
}
|
}
|
||||||
|
|
@ -21,7 +23,7 @@ enum cli_code
|
||||||
error_invalid_arg(const char *current_opt)
|
error_invalid_arg(const char *current_opt)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: option '%s' requires an argument\n",
|
dprintf(STDERR_FILENO, "%s: option '%s' requires an argument\n",
|
||||||
g_prog_name, current_opt);
|
CMD_NAME, current_opt);
|
||||||
print_suggest_help();
|
print_suggest_help();
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +32,7 @@ enum cli_code
|
||||||
error_invalid_opt(const char *current_opt)
|
error_invalid_opt(const char *current_opt)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: invalid option '%s'\n",
|
dprintf(STDERR_FILENO, "%s: invalid option '%s'\n",
|
||||||
g_prog_name, current_opt);
|
CMD_NAME, current_opt);
|
||||||
print_suggest_help();
|
print_suggest_help();
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
}
|
}
|
||||||
|
|
@ -39,7 +41,7 @@ enum cli_code
|
||||||
error_duplicate_opt(const char *current_opt)
|
error_duplicate_opt(const char *current_opt)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: duplicate option '%s'\n",
|
dprintf(STDERR_FILENO, "%s: duplicate option '%s'\n",
|
||||||
g_prog_name, current_opt);
|
CMD_NAME, current_opt);
|
||||||
print_suggest_help();
|
print_suggest_help();
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +50,7 @@ enum cli_code
|
||||||
error_missing_dest(void)
|
error_missing_dest(void)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: usage error: Destination address required\n",
|
dprintf(STDERR_FILENO, "%s: usage error: Destination address required\n",
|
||||||
g_prog_name);
|
CMD_NAME);
|
||||||
print_suggest_help();
|
print_suggest_help();
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
}
|
}
|
||||||
|
|
@ -56,7 +58,7 @@ error_missing_dest(void)
|
||||||
enum cli_code
|
enum cli_code
|
||||||
error_invalid_dest(void)
|
error_invalid_dest(void)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: unknown host\n", g_prog_name);
|
dprintf(STDERR_FILENO, "%s: unknown host\n", CMD_NAME);
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,5 +66,5 @@ static inline void
|
||||||
print_suggest_help(void)
|
print_suggest_help(void)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "Try '%s --help' for more information.\n",
|
dprintf(STDERR_FILENO, "Try '%s --help' for more information.\n",
|
||||||
g_prog_name);
|
CMD_NAME);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
void
|
void
|
||||||
print_version(void)
|
print_version(void)
|
||||||
{
|
{
|
||||||
printf("%s version %s\n", g_prog_name, PING_VERSION);
|
printf("%s version %s\n", g_prog_name.name, PING_VERSION);
|
||||||
printf("Copyright (C) 2026 lohhiicccc\n");
|
printf("Copyright (C) 2026 lohhiicccc\n");
|
||||||
printf("License GPLv3+:");
|
printf("License GPLv3+:");
|
||||||
printf("GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>\n");
|
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)
|
if (NULL == handle)
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: requires CAP_NET_RAW or root privileges\n",
|
dprintf(STDERR_FILENO, "%s: requires CAP_NET_RAW or root privileges\n",
|
||||||
g_prog_name);
|
g_prog_name.name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (HAS_FLAG(config->flags, FLAG_DONT_FRAGMENT)
|
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;
|
state->send_flag = 0;
|
||||||
if (0 != ping_send_one(state, payload_len))
|
if (0 != ping_send_one(state, payload_len))
|
||||||
dprintf(STDERR_FILENO, "%s: %s\n",
|
dprintf(STDERR_FILENO, "%s: %s\n",
|
||||||
g_prog_name, icmp_strerror(state->handle));
|
g_prog_name.name, icmp_strerror(state->handle));
|
||||||
if (HAS_FLAG(state->config->flags, FLAG_FLOOD))
|
if (HAS_FLAG(state->config->flags, FLAG_FLOOD))
|
||||||
ping_output_flood_dot();
|
ping_output_flood_dot();
|
||||||
if (0 != ping_scheduler_arm(state->config))
|
if (0 != ping_scheduler_arm(state->config))
|
||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "%s: setitimer: %s\n",
|
dprintf(STDERR_FILENO, "%s: setitimer: %s\n",
|
||||||
g_prog_name, strerror(errno));
|
g_prog_name.name, strerror(errno));
|
||||||
state->stop_flag = 1;
|
state->stop_flag = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@
|
||||||
#include "ping/cli.h"
|
#include "ping/cli.h"
|
||||||
#include "version_gen.h"
|
#include "version_gen.h"
|
||||||
|
|
||||||
char *g_prog_name = NULL;
|
/* Forward declarations */
|
||||||
|
static int init_prog_name(char *name);
|
||||||
|
struct prog_name g_prog_name = {NULL, NULL};
|
||||||
|
/* -------------------- */
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
|
|
@ -12,8 +15,8 @@ main(int argc, char **argv)
|
||||||
struct ping_config config = {0};
|
struct ping_config config = {0};
|
||||||
int ret = CLI_ERROR;
|
int ret = CLI_ERROR;
|
||||||
|
|
||||||
g_prog_name = argv[0];
|
if (0 != init_prog_name(argv[0]))
|
||||||
cli_set_prog_name(g_prog_name);
|
goto cleanup;
|
||||||
ret = cli_parse_arguments(argc, argv, &config);
|
ret = cli_parse_arguments(argc, argv, &config);
|
||||||
if (ret != CLI_SUCCESS)
|
if (ret != CLI_SUCCESS)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
@ -22,6 +25,18 @@ main(int argc, char **argv)
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
cli_config_free(&config);
|
cli_config_free(&config);
|
||||||
|
free(g_prog_name.alloc);
|
||||||
return (ret == CLI_ERROR) ? 2 : EXIT_SUCCESS;
|
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