PMSTRNCPY(3)
π PMSTRNCPY(3) β safe string copy π
okay bestie, this one's from the Library Functions Manual and it's genuinely one of the good ones, so grab a boba and let's go π§β¨
πΈ NAME πΈ
pmstrncpy β safe string copy π«Ά
β¨ C SYNOPSIS β¨
#include <pcp/pmapi.h>
int pmstrncpy(char *dest, size_t destlen, char *src);
cc ... -lpcp
π¦ DESCRIPTION π¦
pmstrncpy is the safe string copying routine, with semantics kinda similar to strncpy(3). note the kinda β she's giving "inspired by, but evolved" π
The main differences between pmstrncpy and strncpy(3) are, and I need you to pay attention because this is the whole point:
srcmust be null-byte terminated forpmstrncpyπpmstrncpymakes sure thatdestis null-byte terminated, even whensrcis larger thandestlen; this may result in truncated copying ofsrcβοΈ- when
srcis shorter thandestlen,pmstrncpydoes not null-byte fill the remainder ofdestaftersrchas been copied and null-byte terminated π³οΈ - the length argument has the same semantics for
pmstrncpyandpmstrncat(3), unlikestrncpy(3)andstrncat(3)β consistency queen π - the return value from
pmstrncpyis actually useful π (revolutionary, I know) - the order of the arguments for
pmstrncpyhas been deliberately changed from the order of the arguments forstrncpy(3), to avoid accidental misuse that a compiler can easily detect π¨
On success, pmstrncpy returns 0, else -1 indicates that src is too big and the result has been truncated to ensure dest has not been overrun π₯
not me being obsessed with a function that tells you when it cut something off. communication!! we love to see it π
π HISTORY π
strncpy(3) first appeared in Edition 7 AT&T Unix around 1979. she was specifically crafted to copy file names from component names from on-disk directory entries that were of a fixed maximum size, and may have zero, one or more trailing null-bytes. very niche. very specific. very much not a general-purpose string copier π
The subsequent attempts to use strncpy(3) for more general string copying operations has been fraught with abuse and security issues; pmstrncpy is an attempt to address these problems. so basically: the group chat learned from the drama, and now we have boundaries β¨
π SEE ALSO π
PMAPI(3), pmstrncat(3), strncat(3) and strncpy(3) β go read the whole friend group, they're all connected π«Ά
that's the whole page bestie, no cap π this one's brought to you by Performance Co-Pilot / PCP, and yeah, she's literally still titled PMSTRNCPY(3) at the bottom too. periodt π
π
π¦ where this page comes from
- arch rolling: pcp 7.2.2-2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later AND CC-BY-3.0
- centos-stream 10: pcp-doc 7.1.5-6.el10 Β· GPL-2.0-or-later AND CC-BY-3.0
- fedora 43: pcp-doc 7.0.1-1.fc43 Β· GPL-2.0-or-later AND CC-BY-3.0
- fedora 43: pcp-doc 7.2.1-1.fc43
- fedora 43: pcp-doc 7.2.2-1.fc43
- fedora 43: pcp-doc 7.2.2-2.fc43 Β· GPL-2.0-or-later AND CC-BY-3.0
- fedora 44: pcp-doc 7.1.0-6.fc44 Β· GPL-2.0-or-later AND CC-BY-3.0
- fedora 44: pcp-doc 7.2.1-1.fc44
- fedora 44: pcp-doc 7.2.2-1.fc44
- fedora 44: pcp-doc 7.2.2-2.fc44 Β· GPL-2.0-or-later AND CC-BY-3.0
- ubuntu resolute: libpcp4-dev 7.1.1-1
- ubuntu stonking: libpcp4-dev 7.2.1-2build1
- Copyright (c) 2021 Ken McDonell
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms