diff options
author | Alejandro Colomar <alx@kernel.org> | 2023-12-04 13:26:42 +0100 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2023-12-04 13:32:07 +0100 |
commit | 1911b2b946f6914e90c2be4e743d03f1c25d6c95 (patch) | |
tree | 0ce168d23653c2a72fbe76dc663a4cc260e02dd7 | |
parent | 9aeebbdeef81301fb9edf665282be3c25484fc3d (diff) |
string_copying.7: Use real examples with utmp(5)
For the functions that are designed to be used with utmp(5) or similar
APIs, use utmp(5) in the example, which results in a clearer example,
and also more realistic. It better clarifies how these functions are to
be used.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | man7/string_copying.7 | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/man7/string_copying.7 b/man7/string_copying.7 index 6b7bbb2b0..a096bf21e 100644 --- a/man7/string_copying.7 +++ b/man7/string_copying.7 @@ -626,33 +626,31 @@ puts(buf); .TP .BR stpncpy (3) .EX -p = stpncpy(buf, "Hello world!", sizeof(buf)); -if (sizeof(buf) < strlen("Hello world!")) +p = stpncpy(u->ut_user, "alx", sizeof(u->ut_user)); +if (sizeof(u->ut_user) < strlen("alx")) goto toolong; -len = p \- buf; -for (size_t i = 0; i < sizeof(buf); i++) - putchar(buf[i]); +len = p \- u->ut_user; +for (size_t i = 0; i < sizeof(u->ut_user); i++) + putchar(u->ut_user[i]); .EE .\" ----- EXAMPLES :: strncpy(3) --------------------------------------/ .TP .BR strncpy (3) .EX -strncpy(buf, "Hello world!", sizeof(buf)); -if (sizeof(buf) < strlen("Hello world!")) +strncpy(u->ut_user, "alx", sizeof(u->ut_user)); +if (sizeof(u->ut_user) < strlen("alx")) goto toolong; -len = strnlen(buf, sizeof(buf)); -for (size_t i = 0; i < sizeof(buf); i++) - putchar(buf[i]); +len = strnlen(u->ut_user, sizeof(u->ut_user)); +for (size_t i = 0; i < sizeof(u->ut_user); i++) + putchar(u->ut_user[i]); .EE .\" ----- EXAMPLES :: mempcpy(dst, src, strnlen(src, NITEMS(src))) ----/ .TP .I mempcpy(dst, src, strnlen(src, NITEMS(src))) .EX -char h[42] = "Hello "; -char w[6] = "world!"; +char buf[NITEMS(u->ut_user)]; p = buf; -p = mempcpy(p, h, strnlen(h, NITEMS(h))); -p = mempcpy(p, w, strnlen(w, NITEMS(w))); +p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user))); len = p \- buf; printf("%.*s\en", (int) len, buf); .EE @@ -660,11 +658,9 @@ printf("%.*s\en", (int) len, buf); .TP .I stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), \[dq]\[dq]) .EX -char h[42] = "Hello "; -char w[6] = "world!"; +char buf[NITEMS(u->ut_user) + 1]; p = buf; -p = mempcpy(p, h, strnlen(h, NITEMS(h))); -p = mempcpy(p, w, strnlen(w, NITEMS(w))); +p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user))); p = stpcpy(p, ""); len = p \- buf; puts(buf); @@ -673,11 +669,9 @@ puts(buf); .TP .BR strncat (3) .EX -char h[42] = "Hello "; -char w[6] = "world!"; +char buf[NITEMS(u->ut_user) + 1]; strcpy(buf, ""); -strncat(buf, h, NITEMS(h)); -strncat(buf, w, NITEMS(w)); +strncat(buf, u->ut_user, NITEMS(u->ut_user)); len = strlen(buf); puts(buf); .EE |