From: Petr Gotthard Date: Mon, 25 Sep 2023 09:00:27 +0000 (+0200) Subject: Use a local variable instead of the optind (#86) X-Git-Tag: v10.0.3~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8ac0147364222df1d7cff599ed2a538a3965705a;p=thirdparty%2Fdhcpcd.git Use a local variable instead of the optind (#86) The optind get overwritten by reload_config(), so the reconf_reboot() used a wrong argument count. Signed-off-by: Petr Gotthard --- diff --git a/src/dhcpcd.c b/src/dhcpcd.c index cc5dfb58..0cc62226 100644 --- a/src/dhcpcd.c +++ b/src/dhcpcd.c @@ -1498,7 +1498,7 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, { struct interface *ifp; unsigned long long opts; - int opt, oi, do_reboot, do_renew, af = AF_UNSPEC; + int opt, oi, oifind, do_reboot, do_renew, af = AF_UNSPEC; size_t len, l, nifaces; char *tmp, *p; @@ -1514,7 +1514,7 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, return control_queue(fd, UNCONST(fd->ctx->cffile), strlen(fd->ctx->cffile) + 1); } else if (strcmp(*argv, "--getinterfaces") == 0) { - optind = argc = 0; + oifind = argc = 0; goto dumplease; } else if (strcmp(*argv, "--listen") == 0) { fd->flags |= FD_LISTEN; @@ -1577,6 +1577,9 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, } } + /* store the index; the optind will change when a getopt get called */ + oifind = optind; + if (opts & DHCPCD_DUMPLEASE) { ctx->options |= DHCPCD_DUMPLEASE; dumplease: @@ -1584,11 +1587,11 @@ dumplease: TAILQ_FOREACH(ifp, ctx->ifaces, next) { if (!ifp->active) continue; - for (oi = optind; oi < argc; oi++) { + for (oi = oifind; oi < argc; oi++) { if (strcmp(ifp->name, argv[oi]) == 0) break; } - if (optind == argc || oi < argc) { + if (oifind == argc || oi < argc) { opt = send_interface(NULL, ifp, af); if (opt == -1) goto dumperr; @@ -1600,11 +1603,11 @@ dumplease: TAILQ_FOREACH(ifp, ctx->ifaces, next) { if (!ifp->active) continue; - for (oi = optind; oi < argc; oi++) { + for (oi = oifind; oi < argc; oi++) { if (strcmp(ifp->name, argv[oi]) == 0) break; } - if (optind == argc || oi < argc) { + if (oifind == argc || oi < argc) { if (send_interface(fd, ifp, af) == -1) goto dumperr; } @@ -1623,12 +1626,12 @@ dumperr: } if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) { - if (optind == argc) { + if (oifind == argc) { stop_all_interfaces(ctx, opts); eloop_exit(ctx->eloop, EXIT_SUCCESS); return 0; } - for (oi = optind; oi < argc; oi++) { + for (oi = oifind; oi < argc; oi++) { if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL) continue; if (!ifp->active) @@ -1642,11 +1645,11 @@ dumperr: } if (do_renew) { - if (optind == argc) { + if (oifind == argc) { dhcpcd_renew(ctx); return 0; } - for (oi = optind; oi < argc; oi++) { + for (oi = oifind; oi < argc; oi++) { if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL) continue; dhcpcd_ifrenew(ifp); @@ -1656,7 +1659,7 @@ dumperr: reload_config(ctx); /* XXX: Respect initial commandline options? */ - reconf_reboot(ctx, do_reboot, argc, argv, optind - 1); + reconf_reboot(ctx, do_reboot, argc, argv, oifind); return 0; }