udhcpc: fix a problem with binary-encoded options #2
[oweals/busybox.git] / modutils / modutils.c
index 850a8683bac1a2aed989acf9e779986d39b0bb1a..6187ca72fd3835fa28a18158205a5856913cfd75 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 #include "modutils.h"
 
@@ -62,7 +62,7 @@ char* FAST_FUNC filename2modname(const char *filename, char *modname)
        return modname;
 }
 
-char* FAST_FUNC parse_cmdline_module_options(char **argv)
+char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
 {
        char *options;
        int optlen;
@@ -70,10 +70,31 @@ char* FAST_FUNC parse_cmdline_module_options(char **argv)
        options = xzalloc(1);
        optlen = 0;
        while (*++argv) {
-               options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
-               /* Spaces handled by "" pairs, but no way of escaping quotes */
-               optlen += sprintf(options + optlen, (strchr(*argv, ' ') ? "\"%s\" " : "%s "), *argv);
+               const char *fmt;
+               const char *var;
+               const char *val;
+
+               var = *argv;
+               options = xrealloc(options, optlen + 2 + strlen(var) + 2);
+               fmt = "%.*s%s ";
+               val = strchrnul(var, '=');
+               if (quote_spaces) {
+                       /*
+                        * modprobe (module-init-tools version 3.11.1) compat:
+                        * quote only value:
+                        * var="val with spaces", not "var=val with spaces"
+                        * (note: var *name* is not checked for spaces!)
+                        */
+                       if (*val) { /* has var=val format. skip '=' */
+                               val++;
+                               if (strchr(val, ' '))
+                                       fmt = "%.*s\"%s\" ";
+                       }
+               }
+               optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
        }
+       /* Remove trailing space. Disabled */
+       /* if (optlen != 0) options[optlen-1] = '\0'; */
        return options;
 }