PMSTRNCPY(3)

πŸ“¦
version 7.2.2 Β· 3 versions
, in arch rolling, centos-stream 10, fedora 43, fedora 44, ubuntu resolute, ubuntu stonking

πŸ’– 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:

  • src must be null-byte terminated for pmstrncpy 🌟
  • pmstrncpy makes sure that dest is null-byte terminated, even when src is larger than destlen; this may result in truncated copying of src βœ‚οΈ
  • when src is shorter than destlen, pmstrncpy does not null-byte fill the remainder of dest after src has been copied and null-byte terminated πŸ•³οΈ
  • the length argument has the same semantics for pmstrncpy and pmstrncat(3), unlike strncpy(3) and strncat(3) β€” consistency queen πŸ‘‘
  • the return value from pmstrncpy is actually useful 😭 (revolutionary, I know)
  • the order of the arguments for pmstrncpy has been deliberately changed from the order of the arguments for strncpy(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