chiark / gitweb /
udevadm: fix option parsing breakage with klibc
authorSergey Vlasov <vsu@altlinux.ru>
Fri, 14 Nov 2008 21:34:43 +0000 (00:34 +0300)
committerKay Sievers <kay.sievers@vrfy.org>
Sat, 15 Nov 2008 01:01:15 +0000 (02:01 +0100)
The klibc implementation of getopt_long() behaves slightly different
from the glibc one - in particular, it treats the change of the option
string argument between invocations as start of parsing a different
command line, and resets its state.  However, the udevadm code
expected getopt_long() invocations in subcommands to continue parsing
the rest of command line after initial options has been parsed at the
top level; with klibc this broke, causing all udevadm subcommands to
stop recognizing their options.

Instead of relying on the glibc behavior, reset the getopt_long()
state properly before invoking the subcommand handler: move argv to
point to the subcommand name, decrease argc appropriately, and set
optind = 0.  This also fixes a minor bug visible with glibc - without
setting optind = 0 all getopt_long() calls in subcommand handlers were
behaving as if "+" was specified as the first character of the option
string (which disables option reordering), because that state was set
by the first getopt_long() call at the top level, and was not reset
when parsing subcommand options.

Signed-off-by: Sergey Vlasov <vsu@altlinux.ru>
udev/udevadm.c

index 2de9fbcf2212680be0474b0553b3f3e39faabfd9..0927981ab284371460cbcb39e66ece82ba154568 100644 (file)
@@ -200,7 +200,9 @@ int main(int argc, char *argv[])
        if (command != NULL)
                for (i = 0; cmds[i].cmd != NULL; i++) {
                        if (strcmp(cmds[i].name, command) == 0) {
        if (command != NULL)
                for (i = 0; cmds[i].cmd != NULL; i++) {
                        if (strcmp(cmds[i].name, command) == 0) {
-                               optind++;
+                               argc -= optind;
+                               argv += optind;
+                               optind = 0;
                                rc = run_command(udev, &cmds[i], argc, argv);
                                goto out;
                        }
                                rc = run_command(udev, &cmds[i], argc, argv);
                                goto out;
                        }