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

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