Debian Codegraph @

upstream/mageia-cauldron/man3/stpncpy.3

raw
1 '\" t
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH stpncpy 3 2026-01-06 "Linux man-pages 6.17"
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 .BR "char *strncpy(" "size_t dsize;"
20 .BI " char " dst "[restrict " dsize "], const char *restrict " src ,
21 .BI " size_t " dsize );
22 .BR "char *stpncpy(" "size_t dsize;"
23 .BI " char " dst "[restrict " dsize "], 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 past the last non-null character written,
82 that is,
83 .IR dest\~+\~strnlen(src,\~n) .
84 .SH ATTRIBUTES
85 For an explanation of the terms used in this section, see
86 .BR attributes (7).
87 .TS
88 allbox;
89 lbx lb lb
90 l l l.
91 Interface Attribute Value
92 T{
93 .na
94 .nh
95 .BR stpncpy (),
96 .BR strncpy ()
97 T} Thread safety MT-Safe
98 .TE
99 .SH STANDARDS
100 .TP
101 .BR strncpy ()
102 C11, POSIX.1-2008.
103 .TP
104 .BR stpncpy ()
105 POSIX.1-2008.
106 .SH HISTORY
107 .TP
108 .BR strncpy ()
109 C89, POSIX.1-2001, SVr4, 4.3BSD.
110 .TP
111 .BR stpncpy ()
112 glibc 1.07.
113 POSIX.1-2008.
114 .SH CAVEATS
115 The name of these functions is confusing.
116 These functions produce a null-padded character sequence,
117 not a string (see
118 .BR string_copying (7)).
119 For example:
120 .P
121 .in +4n
122 .EX
123 strncpy(buf, "1", 5); // { \[aq]1\[aq], 0, 0, 0, 0 }
124 strncpy(buf, "1234", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], 0 }
125 strncpy(buf, "12345", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], \[aq]5\[aq] }
126 strncpy(buf, "123456", 5); // { \[aq]1\[aq], \[aq]2\[aq], \[aq]3\[aq], \[aq]4\[aq], \[aq]5\[aq] }
127 .EE
128 .in
129 .P
130 It's impossible to distinguish truncation by the result of the call,
131 from a character sequence that just fits the destination buffer;
132 truncation should be detected by
133 comparing the length of the input string
134 with the size of the destination buffer.
135 .P
136 If you're going to use this function in chained calls,
137 it would be useful to develop a similar function that accepts
138 a pointer to the end (one after the last element) of the destination buffer
139 instead of its size.
140 .SH EXAMPLES
141 .\" SRC BEGIN (stpncpy.c)
142 .EX
143 #include <err.h>
144 #include <stdio.h>
145 #include <stdlib.h>
146 #include <string.h>
147 \&
148 int
149 main(void)
150 {
151 char *p;
152 char buf1[20];
153 char buf2[20];
154 size_t len;
155 \&
156 if (sizeof(buf2) < strlen("Hello world!"))
157 errx("strncpy: truncating character sequence");
158 strncpy(buf2, "Hello world!", sizeof(buf2));
159 len = strnlen(buf2, sizeof(buf2));
160 \&
161 printf("[len = %zu]: ", len);
162 fwrite(buf2, 1, len, stdout);
163 putchar(\[aq]\[rs]n\[aq]);
164 \&
165 if (sizeof(buf1) < strlen("Hello world!"))
166 errx("stpncpy: truncating character sequence");
167 p = stpncpy(buf1, "Hello world!", sizeof(buf1));
168 len = p \- buf1;
169 \&
170 printf("[len = %zu]: ", len);
171 fwrite(buf1, 1, len, stdout);
172 putchar(\[aq]\[rs]n\[aq]);
173 \&
174 exit(EXIT_SUCCESS);
175 }
176 .EE
177 .\" SRC END
178 .SH SEE ALSO
179 .BR wcpncpy (3),
180 .BR string_copying (7)