Slight tweaks to argument parsing

This commit is contained in:
XANTRONIX Development 2020-10-05 23:28:45 -04:00 committed by XANTRONIX Industrial
parent 379264a956
commit 6942af9832
2 changed files with 10 additions and 18 deletions

View file

@ -59,31 +59,19 @@ int main(int argc, char **argv) {
while ((ch = getopt_long(argc, argv, "s:i:", opts, &index)) >= 0) { while ((ch = getopt_long(argc, argv, "s:i:", opts, &index)) >= 0) {
switch (ch) { switch (ch) {
case '?':
usage(argc, argv, NULL);
break;
case 's': sock = optarg; break; case 's': sock = optarg; break;
case 'i': ifname = optarg; break; case 'i': ifname = optarg; break;
default: default:
break; usage(argc, argv, NULL);
} }
} }
if (argc < 2) {
usage(argc, argv, "Not enough arguments provided");
}
memset(&info, '\0', sizeof(info)); memset(&info, '\0', sizeof(info));
if (ifname) { if (ifname) {
patty_client_setsockopt_if ifreq; patty_client_setsockopt_if ifreq;
if (argc < 3) {
usage(argc, argv, "No interface name provided");
}
info.flags = PATTY_KISS_TNC_FD; info.flags = PATTY_KISS_TNC_FD;
if ((client = patty_client_new(sock)) == NULL) { if ((client = patty_client_new(sock)) == NULL) {
@ -113,6 +101,10 @@ int main(int argc, char **argv) {
} else { } else {
patty_error e; patty_error e;
if (argc < 2) {
usage(argc, argv, "Not enough arguments provided");
}
if (patty_bin_kiss_config(argc - 1, argv + 1, &info, &e) < 0) { if (patty_bin_kiss_config(argc - 1, argv + 1, &info, &e) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n", fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "patty_bin_kiss_config()", sock, patty_error_string(&e)); argv[0], "patty_bin_kiss_config()", sock, patty_error_string(&e));

View file

@ -178,15 +178,11 @@ int main(int argc, char **argv) {
while ((ch = getopt_long(argc, argv, "s:i:", opts, &index)) >= 0) { while ((ch = getopt_long(argc, argv, "s:i:", opts, &index)) >= 0) {
switch (ch) { switch (ch) {
case '?':
usage(argc, argv, NULL);
break;
case 's': sock = optarg; break; case 's': sock = optarg; break;
case 'i': ifname = optarg; break; case 'i': ifname = optarg; break;
default: default:
break; usage(argc, argv, NULL);
} }
} }
@ -194,6 +190,10 @@ int main(int argc, char **argv) {
usage(argc, argv, "No interface name provided"); usage(argc, argv, "No interface name provided");
} }
if (optind < argc) {
usage(argc, argv, "Invalid arguments provided");
}
if ((client = patty_client_new(sock)) == NULL) { if ((client = patty_client_new(sock)) == NULL) {
fprintf(stderr, "%s: %s: %s\n", fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_client_new()", strerror(errno)); argv[0], "patty_client_new()", strerror(errno));