This wrapper calculates the destination buffer's size, to avoid errors in the size calculation. A curious fact: this macro did exist in Version 7 Unix (with a slightly different name). I found it by chance, investigating the origins of strncpy(3) and strncat(3) in V7, after Branden suggested me to do so, related to recent discussions about string_copying(7). alx@debian:~/src/unix/unix/Research-V7$ grepc SCPYN . ./usr/src/cmd/login.c:#define SCPYN(a, b) strncpy(a, b, sizeof(a)) Our implementation is slightly better, because using nitems() we're protected against passing a pointer instead of an array, and it's also conceptually more appropriate: for wide characters, it would be #define WCSNCPY(dst, src) wcsncpy(dst, src, NITEMS(dst)) Cc: "G. Branden Robinson" <branden@debian.org> Signed-off-by: Alejandro Colomar <alx@kernel.org>
22 lines
345 B
C
22 lines
345 B
C
/*
|
|
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
|
|
#ifndef SHADOW_INCLUDE_LIB_STRNCPY_H_
|
|
#define SHADOW_INCLUDE_LIB_STRNCPY_H_
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "sizeof.h"
|
|
|
|
|
|
#define STRNCPY(dst, src) strncpy(dst, src, NITEMS(dst))
|
|
|
|
|
|
#endif // include guard
|