summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gregory <andrew.gregory.8@gmail.com>2019-10-12 05:11:51 +0200
committerAndrew Gregory <andrew.gregory.8@gmail.com>2019-10-12 05:14:29 +0200
commita82b0028e431dbd8bb3512c3193b52985da82ec2 (patch)
tree7d8b96002c05ba3f5a621bf8efc74530b59b3a01
parenta2c4ad46751e4dcb85a739437d9331bf9282d9be (diff)
downloadpacman-a82b0028e431dbd8bb3512c3193b52985da82ec2.tar.gz
pacman-a82b0028e431dbd8bb3512c3193b52985da82ec2.tar.xz
add arg_to_string helper
Converts an argc/argv pair to a string for presentation to the user. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
-rw-r--r--src/pacman/pacman.c26
-rw-r--r--src/pacman/util.c23
-rw-r--r--src/pacman/util.h1
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)));