Blame SOURCES/autofs-5.0.9-amd-lookup-add-merge_options-function.patch

6bbd11
autofs-5.0.9 - mad lookup add merge_options() function
6bbd11
6bbd11
From: Ian Kent <raven@themaw.net>
6bbd11
6bbd11
6bbd11
---
6bbd11
 include/parse_subs.h |    1 
6bbd11
 lib/parse_subs.c     |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++
6bbd11
 2 files changed, 114 insertions(+)
6bbd11
6bbd11
diff --git a/include/parse_subs.h b/include/parse_subs.h
6bbd11
index c0da5ae..e57cf4a 100644
6bbd11
--- a/include/parse_subs.h
6bbd11
+++ b/include/parse_subs.h
6bbd11
@@ -41,6 +41,7 @@ int strmcmp(const char *, const char *, int);
6bbd11
 char *dequote(const char *, int, unsigned int);
6bbd11
 int span_space(const char *, unsigned int);
6bbd11
 char *sanitize_path(const char *, int, unsigned int, unsigned int);
6bbd11
+char *merge_options(const char *, const char *);
6bbd11
 void free_map_type_info(struct map_type_info *);
6bbd11
 struct map_type_info *parse_map_type_info(const char *);
6bbd11
 
6bbd11
diff --git a/lib/parse_subs.c b/lib/parse_subs.c
6bbd11
index b77d890..99075b1 100644
6bbd11
--- a/lib/parse_subs.c
6bbd11
+++ b/lib/parse_subs.c
6bbd11
@@ -23,6 +23,9 @@
6bbd11
 #include <net/if.h>
6bbd11
 #include "automount.h"
6bbd11
 
6bbd11
+#define MAX_OPTIONS_LEN		256
6bbd11
+#define MAX_OPTION_LEN		40
6bbd11
+
6bbd11
 #define MAX_NETWORK_LEN		255
6bbd11
 
6bbd11
 #define MAX_IFC_BUF		2048
6bbd11
@@ -523,6 +526,116 @@ char *sanitize_path(const char *path, int origlen, unsigned int type, unsigned i
6bbd11
 	return s_path;
6bbd11
 }
6bbd11
 
6bbd11
+static char *hasopt(const char *str, const char *opt)
6bbd11
+{
6bbd11
+	const size_t optlen = strlen(opt);
6bbd11
+	char *rest = (char *) str, *p;
6bbd11
+
6bbd11
+	while ((p = strstr(rest, opt)) != NULL) {
6bbd11
+		if ((p == rest || p[-1] == ',') &&
6bbd11
+		    (p[optlen] == '\0' || p[optlen] == '=' ||
6bbd11
+		     p[optlen] == ','))
6bbd11
+			return p;
6bbd11
+
6bbd11
+		rest = strchr (p, ',');
6bbd11
+		if (rest == NULL)
6bbd11
+			break;
6bbd11
+		++rest;
6bbd11
+	}
6bbd11
+
6bbd11
+	return NULL;
6bbd11
+}
6bbd11
+
6bbd11
+char *merge_options(const char *opt1, const char *opt2)
6bbd11
+{
6bbd11
+	char str[MAX_OPTIONS_LEN];
6bbd11
+	char result[MAX_OPTIONS_LEN];
6bbd11
+	char neg[MAX_OPTION_LEN];
6bbd11
+	char *tok, *ptr = NULL;
6bbd11
+	size_t len;
6bbd11
+
6bbd11
+	if (!opt1 && !opt2)
6bbd11
+		return NULL;
6bbd11
+
6bbd11
+	if (!opt2)
6bbd11
+		return strdup(opt1);
6bbd11
+
6bbd11
+	if (!opt1)
6bbd11
+		return strdup(opt2);
6bbd11
+
6bbd11
+	if (!strcmp(opt1, opt2))
6bbd11
+		return strdup(opt1);
6bbd11
+
6bbd11
+	memset(result, 0, sizeof(result));
6bbd11
+	strcpy(str, opt1);
6bbd11
+
6bbd11
+	tok = strtok_r(str, ",", &ptr);
6bbd11
+	while (tok) {
6bbd11
+		const char *this = (const char *) tok;
6bbd11
+		char *eq = strchr(this, '=');
6bbd11
+		if (eq) {
6bbd11
+			*eq = '\0';
6bbd11
+			if (!hasopt(opt2, this)) {
6bbd11
+				*eq = '=';
6bbd11
+				if (!*result)
6bbd11
+					strcpy(result, this);
6bbd11
+				else
6bbd11
+					strcat(result, this);
6bbd11
+				strcat(result, ",");
6bbd11
+				goto next;
6bbd11
+			}
6bbd11
+		}
6bbd11
+
6bbd11
+		if (!strcmp(this, "rw") && hasopt(opt2, "ro"))
6bbd11
+			goto next;
6bbd11
+		if (!strcmp(this, "ro") && hasopt(opt2, "rw"))
6bbd11
+			goto next;
6bbd11
+		if (!strcmp(this, "bg") && hasopt(opt2, "fg"))
6bbd11
+			goto next;
6bbd11
+		if (!strcmp(this, "fg") && hasopt(opt2, "bg"))
6bbd11
+			goto next;
6bbd11
+		if (!strcmp(this, "bg") && hasopt(opt2, "fg"))
6bbd11
+			goto next;
6bbd11
+		if (!strcmp(this, "soft") && hasopt(opt2, "hard"))
6bbd11
+			goto next;
6bbd11
+		if (!strcmp(this, "hard") && hasopt(opt2, "soft"))
6bbd11
+			goto next;
6bbd11
+
6bbd11
+		if (!strncmp(this, "no", 2)) {
6bbd11
+			strcpy(neg, this + 2);
6bbd11
+			if (hasopt(opt2, neg))
6bbd11
+				goto next;
6bbd11
+		} else {
6bbd11
+			strcpy(neg, "no");
6bbd11
+			strcat(neg, this);
6bbd11
+			if (hasopt(opt2, neg))
6bbd11
+				goto next;
6bbd11
+		}
6bbd11
+
6bbd11
+		if (hasopt(opt2, tok))
6bbd11
+			goto next;
6bbd11
+
6bbd11
+		if (!*result)
6bbd11
+			strcpy(result, this);
6bbd11
+		else
6bbd11
+			strcat(result, this);
6bbd11
+		strcat(result, ",");
6bbd11
+next:
6bbd11
+		tok = strtok_r(NULL, ",", &ptr);
6bbd11
+	}
6bbd11
+
6bbd11
+	if (!*result)
6bbd11
+		strcpy(result, opt2);
6bbd11
+	else
6bbd11
+		strcat(result, opt2);
6bbd11
+
6bbd11
+	len = strlen(result);
6bbd11
+	if (len && result[len - 1] == ',')
6bbd11
+		result[len - 1] = '\0';
6bbd11
+
6bbd11
+	return strdup(result);
6bbd11
+}
6bbd11
+
6bbd11
 void free_map_type_info(struct map_type_info *info)
6bbd11
 {
6bbd11
 	if (info->type)