explain_strndup(3)
β¨ explain_strndup(3) β¨
Library Functions Manual π β yeah bestie, this is the libexplain page that turns your cursed strndup(3) errors into actual sentences. Let's get into it π
πΈ NAME πΈ
explain_strndup β explain strndup(3) errors π
β¨ SYNOPSIS β¨
#include <libexplain/strndup.h>
const char *explain_strndup(const char *data, size_t data_size);
const char *explain_errno_strndup(int errnum, const char *data, size_t
data_size);
void explain_message_strndup(char *message, int message_size, const char
*data, size_t data_size);
void explain_message_errno_strndup(char *message, int message_size, int
errnum, const char *data, size_t data_size);
π¦ DESCRIPTION π¦
okay so, the whole vibe of these functions? They hand you explanations for errors that came back from the strndup(3) system call. That's it. That's the job. π
π explain_strndup
const char *explain_strndup(const char *data, size_t data_size);
So explain_strndup is your girl for getting an explanation of an error returned by the strndup(3) system call. The absolute least the message will contain is the value of strerror(errno) β but honestly? Usually it does so much better and points at the actual underlying cause in more detail. no cap π₯
The errno global variable is what gets used to grab the error value that needs decoding. periodt.
dataβ the original data, exactly as passed to thestrndup(3)system call. like, exactly. don't get cute with it πdata_sizeβ the originaldata_size, exactly as passed to thestrndup(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 gets overwritten by the next call to any libexplain function that shares this buffer, including from other threads. so yeah π
Note: this function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library too. it's giving communal energy in the worst way π
Example: this function is meant to be used in a fashion similar to the following example:
char *result = strndup(data, data_size);
if (!result)
{
fprintf(stderr, "%s\n", explain_strndup(data, data_size));
exit(EXIT_FAILURE);
}
The above code example is available preβpackaged as the explain_strndup_or_die(3) function. iconic, honestly π«Ά
π explain_errno_strndup
const char *explain_errno_strndup(int errnum, const char *data, size_t
data_size);
explain_errno_strndup is here to obtain an explanation of an error returned by the strndup(3) system call. Least it'll give you is the value of strerror(errno), but usually it goes way harder and shows the underlying cause in more detail. β
errnumβ the error value to be decoded, usually grabbed from theerrnoglobal variable right before this function is called. Why does that matter? Because if you need to run any code between the system call you're explaining and this function, you gotta stash it β tons of libc functions will mess with the value oferrnobehind your back. rude of them fr fr πdataβ the original data, exactly as passed to thestrndup(3)system call.data_sizeβ the originaldata_size, exactly as passed to thestrndup(3)system call.
Returns: the message explaining the error. Same deal as before: this message buffer is shared by all libexplain functions which do not supply a buffer in their argument list, and it will be overwritten by the next call to any libexplain function sharing this buffer β other threads included. π
Note: not thread safe, because it shares a return buffer across all threads, and many other functions in this library. same warning, different function, still important bestie π
Example: this function is meant to be used in a fashion similar to the following example:
char *result = strndup(data, data_size);
if (!result)
{
int err = errno;
fprintf(stderr, "%s\n", explain_errno_strndup(err, data,
data_size));
exit(EXIT_FAILURE);
}
The above code example is available preβpackaged as the explain_strndup_or_die(3) function. slay π
π explain_message_strndup
void explain_message_strndup(char *message, int message_size, const char
*data, size_t data_size);
explain_message_strndup obtains an explanation of an error returned by the strndup(3) system call. The least the message will contain is the value of strerror(errno), but usually it does much better and indicates the underlying cause in more detail. π
The errno global variable is what's used to obtain the error value to be decoded.
messageβ where to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. look at her go, being all safe and considerate β¨message_sizeβ the size in bytes of the location in which to store the returned message.dataβ the original data, exactly as passed to thestrndup(3)system call.data_sizeβ the originaldata_size, exactly as passed to thestrndup(3)system call.
Example: this function is meant to be used in a fashion similar to the following example:
char *result = strndup(data, data_size);
if (!result)
{
char message[3000];
explain_message_strndup(message, sizeof(message), data,
data_size);
fprintf(stderr, "%s\n", message);
exit(EXIT_FAILURE);
}
The above code example is available preβpackaged as the explain_strndup_or_die(3) function. π¦
β explain_message_errno_strndup
void explain_message_errno_strndup(char *message, int message_size, int
errnum, const char *data, size_t data_size);
explain_message_errno_strndup obtains an explanation of an error returned by the strndup(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. main character energy fr π
messageβ where 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 theerrnoglobal 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 oferrno. (yes, again. they do it every time π)dataβ the original data, exactly as passed to thestrndup(3)system call.data_sizeβ the originaldata_size, exactly as passed to thestrndup(3)system call.
Example: this function is meant to be used in a fashion similar to the following example:
char *result = strndup(data, data_size);
if (!result)
{
int err = errno;
char message[3000];
explain_message_errno_strndup(message, sizeof(message), err,
data, data_size);
fprintf(stderr, "%s\n", message);
exit(EXIT_FAILURE);
}
The above code example is available preβpackaged as the explain_strndup_or_die(3) function. ate. no crumbs. π
π SEE ALSO π
strndup(3)β duplicate a stringexplain_strndup_or_die(3)β duplicate a string and report errors
π COPYRIGHT π
libexplain version 1.4Copyright (C) 2009 Peter Miller
and that's the whole page bestie π thanks for reading the explain_strndup(3) manual, brought to you by libexplain version 1.4, with Copyright (C) 2009 Peter Miller holding it down since way back. mwah πβ¨π
π¦ where this page comes from
- debian bookworm: libexplain-dev 1.4.D001-12+b1 Β· LGPL-3.0-or-later
- debian trixie: libexplain-dev 1.4.D001-16 Β· LGPL-3.0-or-later
- ubuntu jammy: libexplain-dev 1.4.D001-12 Β· LGPL-3.0-or-later
- ubuntu noble: libexplain-dev 1.4.D001-13 Β· LGPL-3.0-or-later
- ubuntu resolute: libexplain-dev 1.4.D001-16 Β· LGPL-3.0-or-later
- Copyright (C) 2009-2011 Peter Miller
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms