Merge pull request #17493 from Villemoes/va-arg-simplifications

Some vararg simplifications
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-11-04 09:22:15 +01:00 committed by GitHub
commit 24309e6683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 79 deletions

View File

@ -49,16 +49,7 @@ static int sigaction_many_ap(const struct sigaction *sa, int sig, va_list ap) {
int r = 0;
/* negative signal ends the list. 0 signal is skipped. */
if (sig < 0)
return 0;
if (sig > 0) {
if (sigaction(sig, sa, NULL) < 0)
r = -errno;
}
while ((sig = va_arg(ap, int)) >= 0) {
for (; sig >= 0; sig = va_arg(ap, int)) {
if (sig == 0)
continue;

View File

@ -145,57 +145,32 @@ char *strnappend(const char *s, const char *suffix, size_t b) {
char *strjoin_real(const char *x, ...) {
va_list ap;
size_t l;
size_t l = 1;
char *r, *p;
va_start(ap, x);
for (const char *t = x; t; t = va_arg(ap, const char *)) {
size_t n;
if (x) {
l = strlen(x);
for (;;) {
const char *t;
size_t n;
t = va_arg(ap, const char *);
if (!t)
break;
n = strlen(t);
if (n > ((size_t) -1) - l) {
va_end(ap);
return NULL;
}
l += n;
n = strlen(t);
if (n > SIZE_MAX - l) {
va_end(ap);
return NULL;
}
} else
l = 0;
l += n;
}
va_end(ap);
r = new(char, l+1);
p = r = new(char, l);
if (!r)
return NULL;
if (x) {
p = stpcpy(r, x);
va_start(ap, x);
for (const char *t = x; t; t = va_arg(ap, const char *))
p = stpcpy(p, t);
va_end(ap);
va_start(ap, x);
for (;;) {
const char *t;
t = va_arg(ap, const char *);
if (!t)
break;
p = stpcpy(p, t);
}
va_end(ap);
} else
r[0] = 0;
*p = 0;
return r;
}

View File

@ -123,7 +123,6 @@ size_t strv_length(char * const *l) {
}
char **strv_new_ap(const char *x, va_list ap) {
const char *s;
_cleanup_strv_free_ char **a = NULL;
size_t n = 0, i = 0;
va_list aq;
@ -133,43 +132,28 @@ char **strv_new_ap(const char *x, va_list ap) {
* STRV_IFNOTNULL() macro to include possibly NULL strings in
* the string list. */
if (x) {
n = x == STRV_IGNORE ? 0 : 1;
va_copy(aq, ap);
for (const char *s = x; s; s = va_arg(aq, const char*)) {
if (s == STRV_IGNORE)
continue;
va_copy(aq, ap);
while ((s = va_arg(aq, const char*))) {
if (s == STRV_IGNORE)
continue;
n++;
}
va_end(aq);
n++;
}
va_end(aq);
a = new(char*, n+1);
if (!a)
return NULL;
if (x) {
if (x != STRV_IGNORE) {
a[i] = strdup(x);
if (!a[i])
return NULL;
i++;
}
for (const char *s = x; s; s = va_arg(ap, const char*)) {
if (s == STRV_IGNORE)
continue;
while ((s = va_arg(ap, const char*))) {
a[i] = strdup(s);
if (!a[i])
return NULL;
if (s == STRV_IGNORE)
continue;
a[i] = strdup(s);
if (!a[i])
return NULL;
i++;
}
i++;
}
a[i] = NULL;