extern void unmangle_to_buffer(const char *s, char *buf, size_t len);
extern size_t unhexmangle_to_buffer(const char *s, char *buf, size_t len);
+extern size_t unescape_to_buffer(const char *s, const char *wanted, char *buf, size_t len);
extern char *unmangle(const char *s, const char **end);
unhexmangle_to_buffer(s, s, strlen(s) + 1);
}
+static inline void unescape_string(char *s, const char *wanted)
+{
+ if (s)
+ unescape_to_buffer(s, wanted, s, strlen(s) + 1);
+}
+
#endif /* UTIL_LINUX_MANGLE_H */
return buf - buf0 + 1;
}
+size_t unescape_to_buffer(const char *s, const char *wanted, char *buf, size_t len)
+{
+ size_t sz = 0;
+ const char *buf0 = buf;
+
+ while (*s && sz < len - 1) {
+ if (*s == '\\' && sz + 1 < len - 1 && strchr(wanted, s[1])) {
+ *buf++ = s[1];
+ s += 2;
+ sz += 2;
+ } else {
+ *buf++ = *s++;;
+ sz++;
+ }
+ }
+ *buf = '\0';
+ return buf - buf0 + 1;
+}
+
static inline const char *skip_nonspaces(const char *s)
{
while (s && *s && !(*s == ' ' || *s == '\t'))
{
char *p = NULL;
if (argc < 3) {
- fprintf(stderr, "usage: %s --mangle|unmangle <string>\n",
+ fprintf(stderr, "usage: %s --mangle|unmangle|unescape <string>\n",
program_invocation_short_name);
return EXIT_FAILURE;
}
}
}
+ else if (!strcmp(argv[1], "--unescape")) {
+ char *x = strdup(argv[2]);
+ if (x) {
+ unescape_to_buffer(x, ",\"", x, strlen(x) + 1);
+
+ printf("self-unescaped: '%s'\n", x);
+ free(x);
+ }
+ }
+
return EXIT_SUCCESS;
}
#endif /* TEST_PROGRAM_MANGLE */
#include "mountP.h"
#include "fileutils.h" /* statx() fallback */
#include "strutils.h"
+#include "mangle.h"
#include "linux_version.h"
#ifdef USE_LIBMOUNT_MOUNTFD_SUPPORT
int rc;
char *s = NULL;
- /* "\," is a way to use comma in values, let's remove \ escape */
- if (value && strstr(value, "\\,")) {
- char *x, *p;
-
+ /* Remove \, and \" escapes, both supported by ul_optstr_next() */
+ if (value && strchr(value, '\\')) {
s = strdup(value);
if (!s)
- return -EINVAL;
- for (x = p = s; *x; p++, x++) {
- if (*x == '\\' && *(x + 1) == ',')
- x++;
- *p = *x;
- }
- *p = '\0';
+ return -ENOMEM;
+ unescape_string(s, ",\"");
value = s;
}