PERROR(3POSIX)

πŸ’– PERROR β€” bestie edition πŸ’–

ok so we're doing perror(3POSIX) today, from the POSIX Programmer's Manual. pull up a chair, grab a strawberry πŸ“, and let's get into it.


✨ PROLOG ✨

so like, heads up before we even start: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (pls consult the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might just not be implemented on Linux at all. it's giving "your mileage may vary" πŸ’€


🌸 NAME 🌸

perror β€” write error messages to standard error. that's it, that's the whole job, and she does it flawlessly πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <stdio.h>

void perror(const char *s);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so first things first: the functionality described on this reference page is aligned with the ISO C standard. any conflict between the requirements described here and the ISO C standard is unintentional β€” nobody's trying to start drama. this volume of POSIX.1‐2017 defers to the ISO C standard. periodt.

The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the standard error stream, and here's the order it does it in:

  • First (if s is not a null pointer and the character pointed to by s is not the null byte), the string pointed to by s followed by a <colon> and a <space>. so it's like your-label: β€” the little intro before the actual tea β˜•
  • Then an error message string followed by a <newline>.

The contents of the error message strings shall be the same as those returned by strerror() with argument errno. same strings, no cap.

The perror() function shall mark for update the last data modification and last file status change timestamps of the file associated with the standard error stream at some time between its successful completion and exit(), abort(), or the completion of fflush() or fclose() on stderr. yeah she's updating timestamps, she's that girl πŸ•

The perror() function shall not change the orientation of the standard error stream. nope, not touching that. hands off. πŸ™ˆ

On error, perror() shall set the error indicator for the stream to which stderr points, and shall set errno to indicate the error.

⚠️ and listen β€” since no value is returned, an application wishing to check for error situations should call clearerr(stderr) before calling perror(), then if ferror(stderr) returns non-zero, the value of errno indicates which error occurred. don't skip this step bestie, it's the whole plot twist 😭

πŸ‘‘ RETURN VALUE πŸ‘‘

The perror() function shall not return a value. nothing. void. she gives you nothing back and honestly? iconic.


πŸ’€ ERRORS πŸ’€

Refer to fputc().

The following sections are informative.


🌈 EXAMPLES 🌈

Printing an Error Message for a Function

The following example replaces bufptr with a buffer that is the necessary size. If an error occurs, the perror() function prints a message and the program exits.

#include <stdio.h>
#include <stdlib.h>
...
char *bufptr;
size_t szbuf;
...
if ((bufptr = malloc(szbuf)) == NULL) {
    perror("malloc"); exit(2);
}
...

see how tidy that is? perror("malloc") and then dip. main character exit behavior πŸšͺ✨


πŸ’œ APPLICATION USAGE πŸ’œ

Application writers may prefer to use alternative interfaces instead of perror(), such as strerror_r() in combination with fprintf(). like, if you want more control over the vibe, that's your lane 🫢


⭐ RATIONALE ⭐

None. no thoughts, head empty, nothing to justify πŸ’…


🌸 FUTURE DIRECTIONS 🌸

None. she's staying exactly as she is, forever, periodt.


πŸ”₯ SEE ALSO πŸ”₯

  • fprintf()
  • fputc()
  • psiginfo()
  • strerror()
  • The Base Definitions volume of POSIX.1‐2017, <stdio.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie 🫢 this one came to you courtesy of IEEE/The Open Group, dated 2017, and it's officially a wrap on PERROR(3POSIX). go write some error messages, you absolute legend πŸ’–βœ¨πŸŽ€