summaryrefslogtreecommitdiffstats
path: root/man3/stpncpy.3
diff options
context:
space:
mode:
Diffstat (limited to 'man3/stpncpy.3')
-rw-r--r--man3/stpncpy.371
1 files changed, 43 insertions, 28 deletions
diff --git a/man3/stpncpy.3 b/man3/stpncpy.3
index 617eb9b..c4b5db4 100644
--- a/man3/stpncpy.3
+++ b/man3/stpncpy.3
@@ -3,32 +3,32 @@
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
-.TH stpncpy 3 2023-07-20 "Linux man-pages 6.05.01"
+.TH stpncpy 3 2024-02-12 "Linux man-pages 6.7"
.SH NAME
stpncpy, strncpy
-\- zero a fixed-width buffer and
-copy a string into a character sequence with truncation
-and zero the rest of it
+\-
+fill a fixed-size buffer with non-null bytes from a string,
+padding with null bytes as needed
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <string.h>
-.PP
-.BI "char *strncpy(char " dst "[restrict ." sz "], \
+.P
+.BI "char *strncpy(char " dst "[restrict ." dsize "], \
const char *restrict " src ,
-.BI " size_t " sz );
-.BI "char *stpncpy(char " dst "[restrict ." sz "], \
+.BI " size_t " dsize );
+.BI "char *stpncpy(char " dst "[restrict ." dsize "], \
const char *restrict " src ,
-.BI " size_t " sz );
+.BI " size_t " dsize );
.fi
-.PP
+.P
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
-.PP
+.P
.BR stpncpy ():
.nf
Since glibc 2.10:
@@ -37,32 +37,36 @@ Feature Test Macro Requirements for glibc (see
_GNU_SOURCE
.fi
.SH DESCRIPTION
-These functions copy the string pointed to by
+These functions copy non-null bytes from the string pointed to by
.I src
-into a null-padded character sequence at the fixed-width buffer pointed to by
+into the array pointed to by
.IR dst .
+If the source has too few non-null bytes to fill the destination,
+the functions pad the destination with trailing null bytes.
If the destination buffer,
limited by its size,
isn't large enough to hold the copy,
the resulting character sequence is truncated.
For the difference between the two functions, see RETURN VALUE.
-.PP
+.P
An implementation of these functions might be:
-.PP
+.P
.in +4n
.EX
char *
-strncpy(char *restrict dst, const char *restrict src, size_t sz)
+strncpy(char *restrict dst, const char *restrict src, size_t dsize)
{
- stpncpy(dst, src, sz);
+ stpncpy(dst, src, dsize);
return dst;
}
\&
char *
-stpncpy(char *restrict dst, const char *restrict src, size_t sz)
+stpncpy(char *restrict dst, const char *restrict src, size_t dsize)
{
- bzero(dst, sz);
- return mempcpy(dst, src, strnlen(src, sz));
+ size_t dlen;
+\&
+ dlen = strnlen(src, dsize);
+ return memset(mempcpy(dst, src, dlen), 0, dsize \- dlen);
}
.EE
.in
@@ -90,7 +94,6 @@ T{
.BR strncpy ()
T} Thread safety MT-Safe
.TE
-.sp 1
.SH STANDARDS
.TP
.BR strncpy ()
@@ -98,7 +101,7 @@ C11, POSIX.1-2008.
.TP
.BR stpncpy ()
POSIX.1-2008.
-.SH STANDARDS
+.SH HISTORY
.TP
.BR strncpy ()
C89, POSIX.1-2001, SVr4, 4.3BSD.
@@ -111,13 +114,23 @@ The name of these functions is confusing.
These functions produce a null-padded character sequence,
not a string (see
.BR string_copying (7)).
-.PP
+For example:
+.P
+.in +4n
+.EX
+strncpy(buf, "1", 5); // { \[aq]1\[aq], 0, 0, 0, 0 }
+strncpy(buf, "1234", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], 0 }
+strncpy(buf, "12345", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], \[aq]5\[aq] }
+strncpy(buf, "123456", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], \[aq]5\[aq] }
+.EE
+.in
+.P
It's impossible to distinguish truncation by the result of the call,
from a character sequence that just fits the destination buffer;
truncation should be detected by
comparing the length of the input string
with the size of the destination buffer.
-.PP
+.P
If you're going to use this function in chained calls,
it would be useful to develop a similar function that accepts
a pointer to the end (one after the last element) of the destination buffer
@@ -139,20 +152,22 @@ main(void)
size_t len;
\&
if (sizeof(buf2) < strlen("Hello world!"))
- warnx("strncpy: truncating character sequence");
+ errx("strncpy: truncating character sequence");
strncpy(buf2, "Hello world!", sizeof(buf2));
len = strnlen(buf2, sizeof(buf2));
\&
printf("[len = %zu]: ", len);
- printf("%.*s\en", (int) len, buf2); // "Hello world!"
+ fwrite(buf2, 1, len, stdout);
+ putchar(\[aq]\en\[aq]);
\&
if (sizeof(buf1) < strlen("Hello world!"))
- warnx("stpncpy: truncating character sequence");
+ errx("stpncpy: truncating character sequence");
p = stpncpy(buf1, "Hello world!", sizeof(buf1));
len = p \- buf1;
\&
printf("[len = %zu]: ", len);
- printf("%.*s\en", (int) len, buf1); // "Hello world!"
+ fwrite(buf1, 1, len, stdout);
+ putchar(\[aq]\en\[aq]);
\&
exit(EXIT_SUCCESS);
}