PMFSTRING(3)

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

πŸ’– NAME πŸ’–

okay bestie, listen up β€” pmfstring is your girl for safe string scanning. that's literally it, that's the whole vibe. ✨


🌸 C SYNOPSIS 🌸

like, here's how you actually use her:

#include <pcp/pmapi.h>

ssize_t pmfstring(FILE *f, char **str);

and when you compile, don't forget:

cc ... -lpcp

πŸŽ€ DESCRIPTION πŸŽ€

so pmfstring is a safe string scanning routine β€” she does basically the same thing as fscanf(3) with the %s format specifier, but make it responsible. πŸ’…

here's the tea on what she does: she scans the input stream from f, skips over all the initial whitespace characters, and then just... accumulates every subsequent non-whitespace character. like a little vacuum but for chars. 🧹

the main difference (and this is the important part, no cap) is that pmfstring allocates the result buffer str using the malloc(3) family β€” and she makes sure that str is:

  • (a) large enough, and
  • (b) null-byte terminated

so you literally never have to worry about that yourself. she handles it. icon behaviour, honestly. πŸ‘‘

on top of all that, pmfstring does not consider \n to be a whitespace character in that initial scan (the one before filling str). so she will not scan past the end of the current line. this is different from fscanf(3) and β€” good news β€” way better aligned with the PCP use cases. fr fr. πŸ¦‹

bestie, heads up: you are responsible for maintaining a reference to str, or calling free(3) to release the associated storage. don't be the person who leaks. we don't know her. 🫢

on success, pmfstring returns the length of str β€” same length strlen(3) would return β€” and it's guaranteed to be not less than 1. periodt.

if it fails, you get one of these, and str is not assigned a value:

  • 0 β€” meaning no non-whitespace characters were found before the end of the current line from the stream f
  • -1 (aka EOF) β€” meaning end of file on the stream f
  • -2 β€” meaning some more serious failure, probably in the malloc(3) routines; refer to errno for more information 😭

🌈 COMPATIBILITY 🌈

pmfstring has similar semantics to the %ms format specifier in some versions of fscanf(3), and to the C99 fscanf_s(3) routine. and the tragic part? unfortunately neither of these is portable. πŸ’€ so yeah. that's why she exists.


⭐ SEE ALSO ⭐

free(3), fscanf(3), malloc(3) and strlen(3).


k bye, this page came to you straight from Performance Co-Pilot (PCP) β€” the PMFSTRING(3) girlies know πŸ’œ β€” mwah, go free your strings πŸŽ€βœ¨

πŸ“¦ where this page comes from