1stpncpy(3)                 Library Functions Manual                 stpncpy(3)
2
3
4

NAME

6       stpncpy,  strncpy  - zero a fixed-width buffer and copy a string into a
7       character sequence with truncation and zero the rest of it
8

LIBRARY

10       Standard C library (libc, -lc)
11

SYNOPSIS

13       #include <string.h>
14
15       char *strncpy(char dst[restrict .sz], const char *restrict src,
16                      size_t sz);
17       char *stpncpy(char dst[restrict .sz], const char *restrict src,
18                      size_t sz);
19
20   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22       stpncpy():
23           Since glibc 2.10:
24               _POSIX_C_SOURCE >= 200809L
25           Before glibc 2.10:
26               _GNU_SOURCE
27

DESCRIPTION

29       These functions copy the string pointed to by src  into  a  null-padded
30       character sequence at the fixed-width buffer pointed to by dst.  If the
31       destination buffer, limited by its size, isn't large enough to hold the
32       copy,  the  resulting character sequence is truncated.  For the differ‐
33       ence between the two functions, see RETURN VALUE.
34
35       An implementation of these functions might be:
36
37           char *
38           strncpy(char *restrict dst, const char *restrict src, size_t sz)
39           {
40               stpncpy(dst, src, sz);
41               return dst;
42           }
43
44           char *
45           stpncpy(char *restrict dst, const char *restrict src, size_t sz)
46           {
47               bzero(dst, sz);
48               return mempcpy(dst, src, strnlen(src, sz));
49           }
50

RETURN VALUE

52       strncpy()
53              returns dst.
54
55       stpncpy()
56              returns a pointer to one after the last character in the  desti‐
57              nation character sequence.
58

ATTRIBUTES

60       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
61       tributes(7).
62
63       ┌────────────────────────────────────────────┬───────────────┬─────────┐
64Interface                                   Attribute     Value   
65       ├────────────────────────────────────────────┼───────────────┼─────────┤
66stpncpy(), strncpy()                        │ Thread safety │ MT-Safe │
67       └────────────────────────────────────────────┴───────────────┴─────────┘
68

STANDARDS

70       strncpy()
71              C11, POSIX.1-2008.
72
73       stpncpy()
74              POSIX.1-2008.
75

STANDARDS

77       strncpy()
78              C89, POSIX.1-2001, SVr4, 4.3BSD.
79
80       stpncpy()
81              glibc 1.07.  POSIX.1-2008.
82

CAVEATS

84       The name of these functions is confusing.  These  functions  produce  a
85       null-padded character sequence, not a string (see string_copying(7)).
86
87       It's  impossible  to  distinguish truncation by the result of the call,
88       from a character  sequence  that  just  fits  the  destination  buffer;
89       truncation  should  be  detected  by  comparing the length of the input
90       string with the size of the destination buffer.
91
92       If you're going to use this function in  chained  calls,  it  would  be
93       useful  to develop a similar function that accepts a pointer to the end
94       (one after the last element) of the destination buffer instead  of  its
95       size.
96

EXAMPLES

98       #include <err.h>
99       #include <stdio.h>
100       #include <stdlib.h>
101       #include <string.h>
102
103       int
104       main(void)
105       {
106           char    *p;
107           char    buf1[20];
108           char    buf2[20];
109           size_t  len;
110
111           if (sizeof(buf2) < strlen("Hello world!"))
112               warnx("strncpy: truncating character sequence");
113           strncpy(buf2, "Hello world!", sizeof(buf2));
114           len = strnlen(buf2, sizeof(buf2));
115
116           printf("[len = %zu]: ", len);
117           printf("%.*s\n", (int) len, buf2);  // "Hello world!"
118
119           if (sizeof(buf1) < strlen("Hello world!"))
120               warnx("stpncpy: truncating character sequence");
121           p = stpncpy(buf1, "Hello world!", sizeof(buf1));
122           len = p - buf1;
123
124           printf("[len = %zu]: ", len);
125           printf("%.*s\n", (int) len, buf1);  // "Hello world!"
126
127           exit(EXIT_SUCCESS);
128       }
129

SEE ALSO

131       wcpncpy(3), string_copying(7)
132
133
134
135Linux man-pages 6.05              2023-07-20                        stpncpy(3)
Impressum