From c71e08d9daa3444318c6f768894b97b1b6d65bc9 Mon Sep 17 00:00:00 2001 From: Plex Date: Mon, 25 Oct 2021 21:14:23 +0200 Subject: getopt wackiness --- mcping.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'mcping.c') diff --git a/mcping.c b/mcping.c index e8adcf2..8735141 100644 --- a/mcping.c +++ b/mcping.c @@ -12,15 +12,12 @@ int main(int argc, char *argv[]) char *port_s, *address; unsigned short port; unsigned int protocol_ver, p_flag = 0; - if (argc == 1) { - printf - ("Usage: mcping ADDRESS[:PORT]\nPings a Minecraft server using the PING protocol.\n\n -h\t\t\tdisplay this help and exit\n -p PROTOCOL NUMBER\tUses specified protocol number.\n"); - exit(EXIT_SUCCESS); - } int opt; while ((opt = getopt(argc, argv, "hp:")) != -1) { switch (opt) { case 'h': + printf + ("Usage: mcping [-p PROTOCOL] ADDRESS[:PORT]\nPings a Minecraft server using the PING protocol.\n\n -h\t\t\tdisplay this help and exit\n -p PROTOCOL\tUses specified protocol number.\n"); exit(EXIT_SUCCESS); case 'p': protocol_ver = atoi(optarg); @@ -30,7 +27,12 @@ int main(int argc, char *argv[]) break; } } - address = argv[1]; + if (optind >= argc) { + printf + ("Usage: mcping [-p PROTOCOL] ADDRESS[:PORT]\nPings a Minecraft server using the PING protocol.\n\n -h\t\tdisplay this help and exit\n -p PROTOCOL\tUses specified protocol number.\n"); + exit(EXIT_FAILURE); + } + address = argv[optind]; if ((port_s = strchr(address, ':')) != NULL) { address[strlen(address) - strlen(port_s)] = '\0'; port_s++; -- cgit v1.2.3