---
name: "pmfstring"
section: "3"
platform: "linux"
package: "pcp"
version: "7.2.2-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/pmfstring"
---

# pmfstring(3)

safe string scanning

## NAME

**pmfstring** - safe string scanning

## C SYNOPSIS

**#include \<pcp/pmapi.h\>**

**ssize\_t pmfstring(FILE \****f***, char \*\****str***);**

**cc ... -lpcp**

## DESCRIPTION

**pmfstring** is a safe string scanning routine with semantics similar to [**fscanf**(3)](/fscanf/3.md) with the **%s** format specifier. It scans the input stream from *f* skipping initial whitespace characters, then accumulating all the subsequent non-whitespace characters.

The main difference is that **pmfstring** allocates the result buffer *str* using the [**malloc**(3)](/malloc/3.md) family and ensures that *str* is (a) large enough and (b) null-byte terminated.

Additionally **pmfstring** does not consider \\n to be a whitespace character in the initial scan (before filling *str*) and so will not scan past the end of the current line, which is different to [**fscanf**(3)](/fscanf/3.md) and better aligned with the PCP use cases.

The caller is responsible for maintaining a reference to *str* or calling [**free**(3)](/free/3.md) to release the associated storage.

On success, **pmfstring** returns the length of *str* (the same length as [**strlen**(3)](/strlen/3.md) would return) that is guaranteed to be not less than 1.

Failure is indicated by one of the following, and *str* is not assigned a value:

- •

  0 to indicate no non-whitespace characters were found before the end of the current line from the stream *f*

- •

  \-1 ( aka **EOF**) to indicate end of file on the stream *f*

- •

  \-2 to indicate some more serious failure, probably in the [**malloc**(3)](/malloc/3.md) routines; refer to *errno* for more information

## COMPATIBILITY

**pmfstring** has similar semantics to the **%ms** format specifier in some versions of [**fscanf**(3)](/fscanf/3.md) and the C99 **fscanf\_s**(3) routine - unfortunately neither of these is portable.

## SEE ALSO

[**free**(3)](/free/3.md), [**fscanf**(3)](/fscanf/3.md), [**malloc**(3)](/malloc/3.md) and [**strlen**(3)](/strlen/3.md).
