STRNCPY(3POSIX)
π STRNCPY(3POSIX) π
okay bestie, welcome to the POSIX Programmer's Manual β¨ this one's about stpncpy and strncpy, and yes they're siblings, and yes one of them has a whole extra letter that changes everything. iykyk.
πΈ PROLOG πΈ
so like, real talk before we even start: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β like, consult the corresponding Linux manual page for the deets on Linux behavior, fr fr. or honestly? the interface might not even be implemented on Linux at all. vibe check accordingly π«Ά
β¨ NAME β¨
stpncpy, strncpy β copy a fixed length string, and (for one of them) hand you back a pointer to the array end. iconic behavior honestly π
π SYNOPSIS π
#include <string.h>
char *stpncpy(char *restrict s1, const char *restrict s2, size_t n);
char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
π¦ DESCRIPTION π¦
so for strncpy() specifically: the functionality described on this reference page is aligned with the ISO C standard. if anything here conflicts with the ISO C standard, that conflict is unintentional β not me being messy, just the rules. this volume of POSIX.1β2017 defers to the ISO C standard, periodt.
the stpncpy() and strncpy() functions shall copy not more than n bytes β and just so we're clear, bytes that follow a NUL character are not copied β from the array pointed to by s2 to the array pointed to by s1. π§
if the array pointed to by s2 is a string that is shorter than n bytes, then NUL characters shall be appended to the copy in the array pointed to by s1, until n bytes in all are written. like, it pads it out. it commits to the full n. π
β οΈ and now the scary part: if copying takes place between objects that overlap, the behavior is undefined. UNDEFINED bestie. don't do it. π
π RETURN VALUE π
if a NUL character is written to the destination, the stpncpy() function shall return the address of the first such NUL character. otherwise, it shall return &s1[n]. so it's giving "here's where the vibes ended" energy π
the strncpy() function shall return s1. simple. no drama. just s1.
and no return values are reserved to indicate an error. nope, none, not a single one. π
π ERRORS π
no errors are defined. that's it, that's the section β¨
the following sections are informative. (translation: the required reading is over, the rest is bonus content π)
π EXAMPLES π
none. literally none. moving on π
π«Ά APPLICATION USAGE π«Ά
applications must provide the space in s1 for the n bytes to be transferred β like, you gotta bring your own room, bestie β as well as ensure that the s2 and s1 arrays do not overlap. π«
character movement is performed differently in different implementations. so overlapping moves may yield surprises. and not the cute kind of surprise. π
and one more thing: if there is no NUL character byte in the first n bytes of the array pointed to by s2, the result is not null-terminated. no cap. that string is just out there, raw, unterminated, living its truth. β
π RATIONALE π
none. iconic of it honestly πΈ
π FUTURE DIRECTIONS π
none. the future is unclear but this section is not π¦
π₯ SEE ALSO π₯
strcpy()wcsncpy()
and also, from The Base Definitions volume of POSIX.1β2017: <string.h> β¨
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html . π
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . π
okay that's the whole page bestie, you ate that π and a lil sign-off for the record: this came from the IEEE/The Open Group, 2017 β yeah, 2017, it's vintage but it's still serving πβ¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms