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>
This commit is contained in:
Andrew Gregory 2019-10-11 20:11:51 -07:00
parent a2c4ad4675
commit a82b0028e4
3 changed files with 29 additions and 21 deletions

View file

@ -1066,28 +1066,12 @@ static int parseargs(int argc, char *argv[])
*/ */
static void cl_to_log(int argc, char *argv[]) static void cl_to_log(int argc, char *argv[])
{ {
size_t size = 0; char *cl_text = arg_to_string(argc, argv);
int i; if(cl_text) {
for(i = 0; i < argc; i++) { alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
size += strlen(argv[i]) + 1; "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. /** Main function.

View file

@ -1771,3 +1771,26 @@ int pm_vfprintf(FILE *stream, alpm_loglevel_t level, const char *format, va_list
ret = vfprintf(stream, format, args); ret = vfprintf(stream, format, args);
return ret; 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;
}

View file

@ -76,6 +76,7 @@ int multiselect_question(char *array, int count);
int colon_printf(const char *format, ...) __attribute__((format(printf, 1, 2))); int colon_printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
int yesno(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))); 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_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))); int pm_asprintf(char **string, const char *format, ...) __attribute__((format(printf,2,3)));