fmtmsg(3)

πŸ’… fmtmsg(3) β€” print formatted error messages πŸ’…

okay bestie, come sit with me because this one is genuinely iconic. this is a library functions manual page, and we're talking about fmtmsg from the standard C library (libc, -lc). she prints formatted error messages. periodt.


✨ SYNOPSIS ✨

#include <fmtmsg.h>

int fmtmsg(long classification, const char *label,
           int severity, const char *text,
           const char *action, const char *tag);

πŸ¦‹ DESCRIPTION πŸ¦‹

so what does she do. fmtmsg() displays a message described by its arguments, and it puts it on the device(s) specified in the classification argument. for messages written to stderr, the format depends on the MSGVERB environment variable. lowkey a whole vibe.

the label argument identifies the source of the message. the string must consist of two colon separated parts where the first part has not more than 10 and the second part not more than 14 characters. so count your characters, bestie, she's strict πŸ’…

the text argument describes the condition of the error. the action argument describes possible steps to recover from the error. if it is printed, it is prefixed by "TO FIX: ". and the tag argument is a reference to the online documentation where more information can be found β€” it should contain the label value and a unique identification number. iykyk.

πŸ™ˆ Dummy arguments

each of the arguments can have a dummy value, no cap:

  • the dummy classification value MM_NULLMC (0L) does not specify any output, so nothing is printed.
  • the dummy severity value NO_SEV (0) says that no severity is supplied.
  • MM_NULLLBL, MM_NULLTXT, MM_NULLACT, MM_NULLTAG are synonyms for ((char *) 0), the empty string, and MM_NULLSEV is a synonym for NO_SEV.

πŸ‘‘ The classification argument

classification is the sum of values describing 4 types of information. four!! she's layered.

the first value defines the output channel

  • MM_PRINT β€” output to stderr.
  • MM_CONSOLE β€” output to the system console.
  • MM_PRINT | MM_CONSOLE β€” output to both. both!! she's doing the most and we love it.

the second value is the source of the error

  • MM_HARD β€” a hardware error occurred.
  • MM_FIRM β€” a firmware error occurred.
  • MM_SOFT β€” a software error occurred.

the third value encodes the detector of the problem

  • MM_APPL β€” it is detected by an application.
  • MM_UTIL β€” it is detected by a utility.
  • MM_OPSYS β€” it is detected by the operating system.

the fourth value shows the severity of the incident

  • MM_RECOVER β€” it is a recoverable error.
  • MM_NRECOV β€” it is a nonrecoverable error.

🌸 The severity argument

severity can take one of the following values:

  • MM_NOSEV β€” no severity is printed.
  • MM_HALT β€” this value is printed as HALT.
  • MM_ERROR β€” this value is printed as ERROR.
  • MM_WARNING β€” this value is printed as WARNING.
  • MM_INFO β€” this value is printed as INFO.

the numeric values are between 0 and 4. and using addseverity(3) or the environment variable SEV_LEVEL you can add more levels and strings to print. so you can customize her. slay.


πŸŽ€ RETURN VALUE πŸŽ€

she can return 4 values, bestie:

  • MM_OK β€” everything went smooth. πŸ’–
  • MM_NOTOK β€” complete failure. πŸ’€
  • MM_NOMSG β€” error writing to stderr.
  • MM_NOCON β€” error writing to the console.

🌈 ENVIRONMENT 🌈

the environment variable MSGVERB ("message verbosity") can be used to suppress parts of the output to stderr. (it does not influence output to the console. mind you.) when this variable is defined, is non-NULL, and is a colon-separated list of valid keywords, then only the parts of the message corresponding to these keywords is printed. valid keywords are "label", "severity", "text", "action", and "tag".

the environment variable SEV_LEVEL can be used to introduce new severity levels. by default, only the five severity levels described above are available. any other numeric value would make fmtmsg() print nothing. πŸ«₯

if the user puts SEV_LEVEL with a format like

SEV_LEVEL=[description[:description[:...]]]

in the environment of the process before the first call to fmtmsg(), where each description is of the form

severity-keyword,level,printstring

then fmtmsg() will also accept the indicated values for the level (in addition to the standard levels 0–4), and use the indicated printstring when such a level occurs.

the severity-keyword part is not used by fmtmsg() but it has to be present. (cursed but okay.) the level part is a string representation of a number. the numeric value must be a number greater than 4. this value must be used in the severity argument of fmtmsg() to select this class. it is not possible to overwrite any of the predefined classes. the printstring is the string printed when a message of this class is processed by fmtmsg().

⚠️ heads up bestie: that "any other numeric value would make fmtmsg() print nothing" thing is real. don't be out here raw-dogging random numbers.

⭐ ATTRIBUTES ⭐

for an explanation of the terms used in this section, see attributes(7).

  • Interface: fmtmsg()
  • Attribute: Thread safety
  • Value: glibc >= 2.16: MT-Safe; glibc < 2.16: MT-Unsafe

before glibc 2.16, the fmtmsg() function uses a static variable that is not protected, so it is not thread-safe. 😭

since glibc 2.16, the fmtmsg() function uses a lock to protect the static variable, so it is thread-safe. glow up. ✨


πŸ’œ STANDARDS πŸ’œ

  • fmtmsg()
  • MSGVERB β€” POSIX.1-2008.

🧚 HISTORY 🧚

  • fmtmsg() β€” System V. POSIX.1-2001 and POSIX.1-2008. glibc 2.1.
  • MSGVERB β€” System V. POSIX.1-2001 and POSIX.1-2008.
  • SEV_LEVEL β€” System V.

System V and UnixWare man pages tell us that these functions have been replaced by "pfmt() and addsev()" or by "pfmt(), vpfmt(), lfmt(), and vlfmt()", and will be removed later. πŸ’€ the ancestors are saying she's on the way out, but she's still here and still serving.


πŸ“ EXAMPLES πŸ“

okay this is the part where it gets real. here's the whole thing, verbatim:

#include <fmtmsg.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    long c = MM_PRINT | MM_SOFT | MM_OPSYS | MM_RECOVER;
    int err;

    err = fmtmsg(c, "util-linux:mount", MM_ERROR,
                 "unknown mount option", "See mount(8).",
                 "util-linux:mount:017");
    switch (err) {
    case MM_OK:
        break;
    case MM_NOTOK:
        printf("Nothing printed\n");
        break;
    case MM_NOMSG:
        printf("Nothing printed to stderr\n");
        break;
    case MM_NOCON:
        printf("No console output\n");
        break;
    default:
        printf("Unknown error from fmtmsg()\n");
    }
    exit(EXIT_SUCCESS);
}

the output should be:

    util-linux:mount: ERROR: unknown mount option
    TO FIX: See mount(8).  util-linux:mount:017

and after

MSGVERB=text:action; export MSGVERB

the output becomes:

    unknown mount option
    TO FIX: See mount(8).

see how MSGVERB just quietly edited her whole outfit? that's the power. πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘


okay bestie, that's the whole page 🫢 this was Linux man-pages 6.20, dated 2026-02-08, and she ate, no crumbs. go print some formatted error messages. πŸ’…βœ¨

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms