Debian Codegraph @

upstream/debian-trixie/man3/stpncpy.3

raw
1 '\" t
2 .\" Copyright 2022 Alejandro Colomar <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH stpncpy 3 2024-06-15 "Linux man-pages 6.9.1"
7 .SH NAME
8 stpncpy, strncpy
9 \-
10 fill a fixed-size buffer with non-null bytes from a string,
11 padding with null bytes as needed
12 .SH LIBRARY
13 Standard C library
14 .RI ( libc ", " \-lc )
15 .SH SYNOPSIS
16 .nf
17 .B #include <string.h>
18 .P
19 .BI "char *strncpy(char " dst "[restrict ." dsize "], \
20 const char *restrict " src ,
21 .BI " size_t " dsize );
22 .BI "char *stpncpy(char " dst "[restrict ." dsize "], \
23 const char *restrict " src ,
24 .BI " size_t " dsize );
25 .fi
26 .P
27 .RS -4
28 Feature Test Macro Requirements for glibc (see
29 .BR feature_test_macros (7)):
30 .RE
31 .P
32 .BR stpncpy ():
33 .nf
34 Since glibc 2.10:
35 _POSIX_C_SOURCE >= 200809L
36 Before glibc 2.10:
37 _GNU_SOURCE
38 .fi
39 .SH DESCRIPTION
40 These functions copy non-null bytes from the string pointed to by
41 .I src
42 into the array pointed to by
43 .IR dst .
44 If the source has too few non-null bytes to fill the destination,
45 the functions pad the destination with trailing null bytes.
46 If the destination buffer,
47 limited by its size,
48 isn't large enough to hold the copy,
49 the resulting character sequence is truncated.
50 For the difference between the two functions, see RETURN VALUE.
51 .P
52 An implementation of these functions might be:
53 .P
54 .in +4n
55 .EX
56 char *
57 strncpy(char *restrict dst, const char *restrict src, size_t dsize)
58 {
59 stpncpy(dst, src, dsize);
60 return dst;
61 }
62 \&
63 char *
64 stpncpy(char *restrict dst, const char *restrict src, size_t dsize)
65 {
66 size_t dlen;
67 \&
68 dlen = strnlen(src, dsize);
69 return memset(mempcpy(dst, src, dlen), 0, dsize \- dlen);
70 }
71 .EE
72 .in
73 .SH RETURN VALUE
74 .TP
75 .BR strncpy ()
76 returns
77 .IR dst .
78 .TP
79 .BR stpncpy ()
80 returns a pointer to
81 one after the last character in the destination character sequence.
82 .SH ATTRIBUTES
83 For an explanation of the terms used in this section, see
84 .BR attributes (7).
85 .TS
86 allbox;
87 lbx lb lb
88 l l l.
89 Interface Attribute Value
90 T{
91 .na
92 .nh
93 .BR stpncpy (),
94 .BR strncpy ()
95 T} Thread safety MT-Safe
96 .TE
97 .SH STANDARDS
98 .TP
99 .BR strncpy ()
100 C11, POSIX.1-2008.
101 .TP
102 .BR stpncpy ()
103 POSIX.1-2008.
104 .SH HISTORY
105 .TP
106 .BR strncpy ()
107 C89, POSIX.1-2001, SVr4, 4.3BSD.
108 .TP
109 .BR stpncpy ()
110 glibc 1.07.
111 POSIX.1-2008.
112 .SH CAVEATS
113 The name of these functions is confusing.
114 These functions produce a null-padded character sequence,
115 not a string (see
116 .BR string_copying (7)).
117 For example:
118 .P
119 .in +4n
120 .EX
121 strncpy(buf, "1", 5); // { \[aq]1\[aq], 0, 0, 0, 0 }
122 strncpy(buf, "1234", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], 0 }
123 strncpy(buf, "12345", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], \[aq]5\[aq] }
124 strncpy(buf, "123456", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], \[aq]5\[aq] }
125 .EE
126 .in
127 .P
128 It's impossible to distinguish truncation by the result of the call,
129 from a character sequence that just fits the destination buffer;
130 truncation should be detected by
131 comparing the length of the input string
132 with the size of the destination buffer.
133 .P
134 If you're going to use this function in chained calls,
135 it would be useful to develop a similar function that accepts
136 a pointer to the end (one after the last element) of the destination buffer
137 instead of its size.
138 .SH EXAMPLES
139 .\" SRC BEGIN (stpncpy.c)
140 .EX
141 #include <err.h>
142 #include <stdio.h>
143 #include <stdlib.h>
144 #include <string.h>
145 \&
146 int
147 main(void)
148 {
149 char *p;
150 char buf1[20];
151 char buf2[20];
152 size_t len;
153 \&
154 if (sizeof(buf2) < strlen("Hello world!"))
155 errx("strncpy: truncating character sequence");
156 strncpy(buf2, "Hello world!", sizeof(buf2));
157 len = strnlen(buf2, sizeof(buf2));
158 \&
159 printf("[len = %zu]: ", len);
160 fwrite(buf2, 1, len, stdout);
161 putchar(\[aq]\[rs]n\[aq]);
162 \&
163 if (sizeof(buf1) < strlen("Hello world!"))
164 errx("stpncpy: truncating character sequence");
165 p = stpncpy(buf1, "Hello world!", sizeof(buf1));
166 len = p \- buf1;
167 \&
168 printf("[len = %zu]: ", len);
169 fwrite(buf1, 1, len, stdout);
170 putchar(\[aq]\[rs]n\[aq]);
171 \&
172 exit(EXIT_SUCCESS);
173 }
174 .EE
175 .\" SRC END
176 .SH SEE ALSO
177 .BR wcpncpy (3),
178 .BR string_copying (7)