]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
*/: s/STRNCPY/strncpy_a/
authorAlejandro Colomar <alx@kernel.org>
Tue, 14 Oct 2025 10:54:04 +0000 (12:54 +0200)
committerSerge Hallyn <serge@hallyn.com>
Fri, 28 Nov 2025 02:50:48 +0000 (20:50 -0600)
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/log.c
lib/string/README
lib/string/strcpy/strncpy.h
lib/utmp.c
tests/unit/test_strncpy.c

index fb09615ea02541f38f154d877926e854f35043d9..10da612b94ce038676aa3ad7a8441775d132a541 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -83,7 +83,7 @@ void dolastlog (
        newlog.ll_time = ll_time;
        STRTCPY(newlog.ll_line, line);
 #if HAVE_LL_HOST
-       STRNCPY(newlog.ll_host, host);
+       strncpy_a(newlog.ll_host, host);
 #endif
        if (   (lseek (fd, offset, SEEK_SET) != offset)
            || (write_full(fd, &newlog, sizeof newlog) == -1)) {
index 1123206b7e587e9ccbcb90eca26a77ac8b81b8f7..c2a7f2251c0144b18128783ea7981c9ce917303d 100644 (file)
@@ -166,7 +166,7 @@ strdup/ - Memory duplication
 strcpy/ - String copying
 
   n/
-    STRNCPY()
+    strncpy_a()
        Like strncpy(3), but takes an array.
        Use it *exclusively* for copying from a string into a utmp(5)
        member.
index c9a5d01933ed954e98b329a0c7ed52de91cc5c1a..518e9674d29b693b1ed1cc30ea006ac033cbd635 100644 (file)
@@ -13,7 +13,8 @@
 #include "sizeof.h"
 
 
-#define STRNCPY(dst, src)  strncpy(dst, src, countof(dst))
+// strncpy_a - nonstring copy array
+#define strncpy_a(dst, src)  strncpy(dst, src, countof(dst))
 
 
 #endif  // include guard
index 8c1af1eef407dcb3d4cd1f624a226c33c11bc5aa..baf29764b02caf22a509dd2ffb0490c3f4fc7638 100644 (file)
@@ -289,21 +289,21 @@ prepare_utmp(const char *name, const char *line, const char *host,
 
        utent->ut_type = USER_PROCESS;
        utent->ut_pid = main_pid;
-       STRNCPY(utent->ut_line, line);
+       strncpy_a(utent->ut_line, line);
        if (   (NULL != ut)
            && ('\0' != ut->ut_id[0])) {
-               STRNCPY(utent->ut_id, ut->ut_id);
+               strncpy_a(utent->ut_id, ut->ut_id);
        } else {
-               STRNCPY(utent->ut_id, strnul(line) - MIN(strlen(line), countof(utent->ut_id)));
+               strncpy_a(utent->ut_id, strnul(line) - MIN(strlen(line), countof(utent->ut_id)));
        }
 #if defined(HAVE_STRUCT_UTMPX_UT_NAME)
-       STRNCPY(utent->ut_name, name);
+       strncpy_a(utent->ut_name, name);
 #endif
-       STRNCPY(utent->ut_user, name);
+       strncpy_a(utent->ut_user, name);
        if (NULL != hostname) {
                struct addrinfo *info = NULL;
 #if defined(HAVE_STRUCT_UTMPX_UT_HOST)
-               STRNCPY(utent->ut_host, hostname);
+               strncpy_a(utent->ut_host, hostname);
 #endif
 #if defined(HAVE_STRUCT_UTMPX_UT_SYSLEN)
                utent->ut_syslen = MIN (strlen (hostname),
index a6049ca58024a2a9c5dcb8a5f842ed66c74f9a9d..e5655920cf949ffa194ee0d31114b5947c6b2e24 100644 (file)
 #include "string/strcpy/strncpy.h"
 
 
-static void test_STRNCPY_trunc(void **state);
-static void test_STRNCPY_fit(void **state);
-static void test_STRNCPY_pad(void **state);
+static void test_strncpy_a_trunc(void **state);
+static void test_strncpy_a_fit(void **state);
+static void test_strncpy_a_pad(void **state);
 
 
 int
 main(void)
 {
     const struct CMUnitTest  tests[] = {
-        cmocka_unit_test(test_STRNCPY_trunc),
-        cmocka_unit_test(test_STRNCPY_fit),
-        cmocka_unit_test(test_STRNCPY_pad),
+        cmocka_unit_test(test_strncpy_a_trunc),
+        cmocka_unit_test(test_strncpy_a_fit),
+        cmocka_unit_test(test_strncpy_a_pad),
     };
 
     return cmocka_run_group_tests(tests, NULL, NULL);
@@ -37,49 +37,49 @@ main(void)
 
 
 static void
-test_STRNCPY_trunc(void **state)
+test_strncpy_a_trunc(void **state)
 {
        char  buf[3];
 
        char  src1[4] = {'f', 'o', 'o', 'o'};
        char  res1[3] = {'f', 'o', 'o'};
-       assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
+       assert_true(memcmp(res1, strncpy_a(buf, src1), sizeof(buf)) == 0);
 
        char  src2[5] = "barb";
        char  res2[3] = {'b', 'a', 'r'};
-       assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
+       assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0);
 }
 
 
 static void
-test_STRNCPY_fit(void **state)
+test_strncpy_a_fit(void **state)
 {
        char  buf[3];
 
        char  src1[3] = {'b', 'a', 'z'};
        char  res1[3] = {'b', 'a', 'z'};
-       assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
+       assert_true(memcmp(res1, strncpy_a(buf, src1), sizeof(buf)) == 0);
 
        char  src2[4] = "qwe";
        char  res2[3] = {'q', 'w', 'e'};
-       assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
+       assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0);
 }
 
 
 static void
-test_STRNCPY_pad(void **state)
+test_strncpy_a_pad(void **state)
 {
        char  buf[3];
 
        char  src1[3] = "as";
        char  res1[3] = {'a', 's', 0};
-       assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
+       assert_true(memcmp(res1, strncpy_a(buf, src1), sizeof(buf)) == 0);
 
        char  src2[3] = "";
        char  res2[3] = {0, 0, 0};
-       assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
+       assert_true(memcmp(res2, strncpy_a(buf, src2), sizeof(buf)) == 0);
 
        char  src3[3] = {'a', 0, 'b'};
        char  res3[3] = {'a', 0, 0};
-       assert_true(memcmp(res3, STRNCPY(buf, src3), sizeof(buf)) == 0);
+       assert_true(memcmp(res3, strncpy_a(buf, src3), sizeof(buf)) == 0);
 }