[pacman-dev] [PATCH v2 2/3] add arg_to_string helper

Andrew Gregory andrew.gregory.8 at gmail.com
Sat Oct 12 03:45:28 UTC 2019


Converts an argc/argv pair to a string for presentation to the user.

Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
---
 src/pacman/pacman.c | 26 +++++---------------------
 src/pacman/util.c   | 23 +++++++++++++++++++++++
 src/pacman/util.h   |  1 +
 3 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index b7406cea..c8e86b9d 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -1066,28 +1066,12 @@ static int parseargs(int argc, char *argv[])
  */
 static void cl_to_log(int argc, char *argv[])
 {
-	size_t size = 0;
-	int i;
-	for(i = 0; i < argc; i++) {
-		size += strlen(argv[i]) + 1;
+	char *cl_text = arg_to_string(argc, argv);
+	if(cl_text) {
+		alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
+				"Running '%s'\n", cl_text);
+		free(cl_text);
 	}
-	if(!size) {
-		return;
-	}
-	char *cl_text = malloc(size);
-	if(!cl_text) {
-		return;
-	}
-	char *p = cl_text;
-	for(i = 0; i + 1 < argc; i++) {
-		strcpy(p, argv[i]);
-		p += strlen(argv[i]);
-		*p++ = ' ';
-	}
-	strcpy(p, argv[i]);
-	alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
-			"Running '%s'\n", cl_text);
-	free(cl_text);
 }
 
 /** Main function.
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 8f6290db..68cdb2e9 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -1771,3 +1771,26 @@ int pm_vfprintf(FILE *stream, alpm_loglevel_t level, const char *format, va_list
 	ret = vfprintf(stream, format, args);
 	return ret;
 }
+
+char *arg_to_string(int argc, char *argv[])
+{
+	char *cl_text, *p;
+	size_t size = 0;
+	int i;
+	for(i = 0; i < argc; i++) {
+		size += strlen(argv[i]) + 1;
+	}
+	if(!size) {
+		return NULL;
+	}
+	if(!(cl_text = malloc(size))) {
+		return NULL;
+	}
+	for(p = cl_text, i = 0; i + 1 < argc; i++) {
+		strcpy(p, argv[i]);
+		p += strlen(argv[i]);
+		*p++ = ' ';
+	}
+	strcpy(p, argv[i]);
+	return cl_text;
+}
diff --git a/src/pacman/util.h b/src/pacman/util.h
index a1fbef46..4b049849 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -76,6 +76,7 @@ int multiselect_question(char *array, int count);
 int colon_printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
 int yesno(const char *format, ...) __attribute__((format(printf, 1, 2)));
 int noyes(const char *format, ...) __attribute__((format(printf, 1, 2)));
+char *arg_to_string(int argc, char *argv[]);
 
 int pm_printf(alpm_loglevel_t level, const char *format, ...) __attribute__((format(printf,2,3)));
 int pm_asprintf(char **string, const char *format, ...) __attribute__((format(printf,2,3)));
-- 
2.23.0


More information about the pacman-dev mailing list