Blob Blame History Raw
autofs-5.1.3 - add function construct_argv()

From: Ian Kent <raven@themaw.net>

Add a function to decompose a string into a program path and an
arguments vector ready for an execv(3) invocation.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG            |    1 
 include/parse_subs.h |    1 
 lib/parse_subs.c     |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+)

--- autofs-5.0.7.orig/CHANGELOG
+++ autofs-5.0.7/CHANGELOG
@@ -280,6 +280,7 @@
 - remove path restriction of amd external mount.
 - add function umount_amd_ext_mount().
 - add function ext_mount_inuse().
+- add function construct_argv().
 
 25/07/2012 autofs-5.0.7
 =======================
--- autofs-5.0.7.orig/include/parse_subs.h
+++ autofs-5.0.7/include/parse_subs.h
@@ -125,6 +125,7 @@ char *sanitize_path(const char *, int, u
 char *merge_options(const char *, const char *);
 int expandamdent(const char *, char *, const struct substvar *);
 int expand_selectors(struct autofs_point *, const char *, char **, struct substvar *);
+int construct_argv(char *, char **, char ***);
 void free_map_type_info(struct map_type_info *);
 struct map_type_info *parse_map_type_info(const char *);
 
--- autofs-5.0.7.orig/lib/parse_subs.c
+++ autofs-5.0.7/lib/parse_subs.c
@@ -1191,6 +1191,86 @@ int expand_selectors(struct autofs_point
 	return len;
 }
 
+/* Get next space seperated argument, arguments containing
+ * space characters may be single quoted.
+ */
+static char *next_arg(char *str, char **next)
+{
+	char *start;
+	char *ptr;
+
+	if (!*str)
+		return NULL;
+
+	start = ptr = str;
+
+	/* The amd map format parser should ensure there
+	 * are matching single quotes.
+	 */
+	if (*start == 39) {
+		start++;
+		ptr++;
+		while (*ptr && *ptr != 39)
+			ptr++;
+	} else {
+		while (*ptr && *ptr != ' ')
+			ptr++;
+	}
+
+	if (*ptr)
+		*ptr++ = 0;
+	*next = ptr;
+
+	return start;
+}
+
+/* Construct program path name plus argument array for use with
+ * execv(3).
+ */
+int construct_argv(char *str, char **prog, char ***argv)
+{
+	char *program = NULL;
+	char *start, *next;
+	char **args, *arg;
+	int argc;
+
+	start = str;
+
+	args = malloc(sizeof(char *));
+	if (!args)
+		return -1;
+
+	args[0] = NULL;
+	argc = 0;
+
+	next = NULL;
+	program = next_arg(str, &next);
+	if (!program) {
+		free(args);
+		return -1;
+	}
+
+	start = next;
+
+	while (1) {
+		if (!*next)
+			break;
+		arg = next_arg(start, &next);
+		if (arg) {
+			argc++;
+			args = add_argv(argc, args, arg);
+			if (!args)
+				return -1;
+		}
+		start = next;
+	}
+
+	*prog = program;
+	*argv = args;
+
+	return argc;
+}
+
 void free_map_type_info(struct map_type_info *info)
 {
 	if (info->type)