aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPlex <thinkplex@riseup.net>2021-10-25 21:14:23 +0200
committerPlex <thinkplex@riseup.net>2021-10-25 21:14:23 +0200
commitc71e08d9daa3444318c6f768894b97b1b6d65bc9 (patch)
tree47886cd46919a603687522ad183231a3b93e0760
parent38002707bd75ec5f3c2deb25ce3463d27c467de9 (diff)
getopt wackiness
-rw-r--r--mcping.c14
1 files changed, 8 insertions, 6 deletions
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++;