From: Alejandro Colomar Date: Sat, 19 Jul 2025 08:56:28 +0000 (+0200) Subject: src/chfn.c: Use stpeprintf() to improve readability X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bff2961dbe24436f7f2b508bf489007aa73425b8;p=thirdparty%2Fshadow.git src/chfn.c: Use stpeprintf() to improve readability This allows us to split the formation of the string into several s*printf() calls. Shorten comment, to make it fit in one line. Signed-off-by: Alejandro Colomar --- diff --git a/src/chfn.c b/src/chfn.c index ca654e0d7..7289c6a8a 100644 --- a/src/chfn.c +++ b/src/chfn.c @@ -32,8 +32,9 @@ #include "pwauth.h" #include "pwio.h" #include "shadowlog.h" +#include "sizeof.h" #include "sssd.h" -#include "string/sprintf/snprintf.h" +#include "string/sprintf/stpeprintf.h" #include "string/strcmp/streq.h" #include "string/strcpy/strtcpy.h" #include "string/strdup/xstrdup.h" @@ -561,9 +562,8 @@ static void check_fields (void) */ int main (int argc, char **argv) { - int ret; char new_gecos[80]; - char *user; + char *user, *p, *e; const struct passwd *pw; sanitize_env (); @@ -640,14 +640,17 @@ int main (int argc, char **argv) */ check_fields (); - /* - * Build the new GECOS field by plastering all the pieces together, - * if they will fit ... - */ - ret = SNPRINTF(new_gecos, "%s,%s,%s,%s%s%s", - fullnm, roomno, workph, homeph, - (!streq(slop, "")) ? "," : "", slop); - if (ret == -1) { + /* Build the new GECOS field by plastering all the pieces together. */ + p = new_gecos; + e = new_gecos + countof(new_gecos); + p = stpeprintf(p, e, "%s", fullnm); + p = stpeprintf(p, e, ",%s", roomno); + p = stpeprintf(p, e, ",%s", workph); + p = stpeprintf(p, e, ",%s", homeph); + if (!streq(slop, "")) + p = stpeprintf(p, e, ",%s", slop); + + if (p == e || p == NULL) { fprintf (stderr, _("%s: fields too long\n"), Prog); fail_exit (E_NOPERM); }