explain_ungetc(3)

πŸ“¦
version 1.4.D001 Β· 2 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute

✨ NAME ✨

okay so bestie, meet explain_ungetc β€” it explains ungetc(3) errors πŸ’…

Like, ungetc(3) fails and you're standing there like 😭 what does that even mean β€” this whole library is here to translate the vibe for you. It lives in the Library Functions Manual, and it's giving "your emotional support manpage". 🫢


🌸 SYNOPSIS 🌸

#include <libexplain/ungetc.h>
const char *explain_ungetc(int c, FILE *fp);
const char *explain_errno_ungetc(int errnum, int c, FILE *fp);
void explain_message_ungetc(char *message, int message_size, int c, FILE *fp);
void explain_message_errno_ungetc(char *message, int message_size, int errnum, int c, FILE *fp);

πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea: these functions are your go-to for getting actual explanations for errors that come back from the ungetc(3) system call. Not just "it broke", but why. Obsessed. πŸ’–


πŸŽ€ explain_ungetc πŸŽ€

const char *explain_ungetc(int c, FILE *fp);

The explain_ungetc function is how you get an explanation of an error returned by the ungetc(3) system call. The least the message will contain is the value of strerror(errno) β€” but usually it does so much better and tells you the underlying cause in more detail. Main character energy, fr fr. ✨

The errno global variable will be used to obtain the error value to be decoded. (It just reads it off errno, no cap.)

  • c β€” the original c, exactly as passed to the ungetc(3) system call.
  • fp β€” the original fp, exactly as passed to the ungetc(3) system call.

Returns:

The message explaining the error. Heads up though β€” this message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. It will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. So don't go holding onto it like it's yours, bestie. πŸ’€

Note: this function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Not me being chaotic in multithreaded code πŸ™ˆ

Example: this function is intended to be used in a fashion similar to the following example:

if (ungetc(c, fp) < 0)
{
    fprintf(stderr, "%s\n", explain_ungetc(c, fp));
    exit(EXIT_FAILURE);
}

The above code example is available pre-packaged as the explain_ungetc_or_die(3) function. 🎁 (Already done for you, slay.)


🌈 explain_errno_ungetc 🌈

const char *explain_errno_ungetc(int errnum, int c, FILE *fp);

The explain_errno_ungetc function is used to obtain an explanation of an error returned by the ungetc(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. πŸ’–

  • errnum β€” the error value to be decoded, usually obtained from the errno global variable just before this function is called. This is necessary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. (Sneaky little things. 😀)
  • c β€” the original c, exactly as passed to the ungetc(3) system call.
  • fp β€” the original fp, exactly as passed to the ungetc(3) system call.

Returns:

The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. 🫠

Note: this function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library.

Example: this function is intended to be used in a fashion similar to the following example:

if (ungetc(c, fp) < 0)
{
    int err = errno;
    fprintf(stderr, "%s\n", explain_errno_ungetc(err, c, fp));
    exit(EXIT_FAILURE);
}

The above code example is available pre-packaged as the explain_ungetc_or_die(3) function. πŸŽ€


πŸ’œ explain_message_ungetc πŸ’œ

void explain_message_ungetc(char *message, int message_size, int c, FILE *fp);

The explain_message_ungetc function is used to obtain an explanation of an error returned by the ungetc(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. ✨

The errno global variable will be used to obtain the error value to be decoded.

  • message β€” the location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. πŸ‘‘ (Yes queen, you bring your own buffer, you get thread safety.)
  • message_size β€” the size in bytes of the location in which to store the returned message.
  • c β€” the original c, exactly as passed to the ungetc(3) system call.
  • fp β€” the original fp, exactly as passed to the ungetc(3) system call.

Example: this function is intended to be used in a fashion similar to the following example:

if (ungetc(c, fp) < 0)
{
    char message[3000];
    explain_message_ungetc(message, sizeof(message), c, fp);
    fprintf(stderr, "%s\n", message);
    exit(EXIT_FAILURE);
}

The above code example is available pre-packaged as the explain_ungetc_or_die(3) function. πŸ’–


🧚 explain_message_errno_ungetc 🧚

void explain_message_errno_ungetc(char *message, int message_size, int errnum, int c, FILE *fp);

The explain_message_errno_ungetc function is used to obtain an explanation of an error returned by the ungetc(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. 🌸

  • message β€” the location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe.
  • message_size β€” the size in bytes of the location in which to store the returned message.
  • errnum β€” the error value to be decoded, usually obtained from the errno global variable just before this function is called. This is necessary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. 🚨
  • c β€” the original c, exactly as passed to the ungetc(3) system call.
  • fp β€” the original fp, exactly as passed to the ungetc(3) system call.

Example: this function is intended to be used in a fashion similar to the following example:

if (ungetc(c, fp) < 0)
{
    int err = errno;
    char message[3000];
    explain_message_errno_ungetc(message, sizeof(message), err, c, fp);
    fprintf(stderr, "%s\n", message);
    exit(EXIT_FAILURE);
}

The above code example is available pre-packaged as the explain_ungetc_or_die(3) function. 🫢


⭐ SEE ALSO ⭐


libexplain version 1.4

Copyright (C) 2010 Peter Miller


and that's the whole page, bestie πŸ’… this was explain_ungetc(3) from the Library Functions Manual, brought to you by libexplain version 1.4, Copyright (C) 2010 Peter Miller β€” mwah, go push a character back to a stream, you got this πŸ¦‹βœ¨

πŸ“¦ where this page comes from