WCRTOMB(3POSIX)

πŸ¦‹ wcrtomb(3POSIX) β€” the POSIX Programmer's Manual, but make it make sense πŸ¦‹

okay bestie, so this is wcrtomb from the POSIX Programmer's Manual, and I need you to clock the PROLOG before we do literally anything else, because it's important:

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details on Linux behavior), or the interface may not be implemented on Linux.

like. not me warning you about Linux behavior differences in the first sentence πŸ’€ read the Linux page too, ok?


✨ NAME ✨

wcrtomb β€” convert a wide-character code to a character (restartable) πŸŽ€

that's it. that's the girl. she's restartable. she's a wide char to char converter. slay.


🌸 SYNOPSIS 🌸

#include <wchar.h>

size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict ps);

look at that signature. restrict twice. she's efficient, she's telling the compiler nobody's aliasing anything. queen behavior πŸ‘‘


πŸ’– DESCRIPTION πŸ’–

so the functionality on this reference page is aligned with the ISO C standard. and like, if there's any conflict between the requirements described here and the ISO C standard? that conflict is unintentional. This volume of POSIX.1‐2017 defers to the ISO C standard, periodt.

now. here's the tea. β˜•

If s is a null pointer, the wcrtomb() function shall be equivalent to this call:

wcrtomb(buf, L'\0', ps)

where buf is an internal buffer. cute, right? internal buffer, not your problem.

If s is not a null pointer, wcrtomb() shall figure out how many bytes are needed to represent the character that corresponds to the wide character given by wc β€” including any shift sequences, don't forget the shift sequences bestie β€” and store the resulting bytes in the array whose first element is pointed to by s. At most {MB_CUR_MAX} bytes get stored. and if wc is a null wide character, a null byte shall be stored, preceded by any shift sequence needed to restore the initial shift state. The resulting state described shall be the initial conversion state. we love a reset. we love a clean slate. 🌈

If ps is a null pointer, wcrtomb() shall use its own internal mbstate_t object, which is initialized at program start-up to the initial conversion state. Otherwise, the mbstate_t object pointed to by ps shall be used to completely describe the current conversion state of the associated character sequence. and the implementation shall behave as if no function defined in this volume of POSIX.1‐2017 calls wcrtomb().

🚨 the wcrtomb() function need not be thread-safe if called with a NULL ps argument. i know. i KNOW. protect your threads accordingly, bestie 😭

The behavior of this function shall be affected by the LC_CTYPE category of the current locale. locale girlies stay winning πŸ’…

and ooh, one more: wcrtomb() shall not change the setting of errno if successful. respectful king. doesn't touch your errno unless it has to.


πŸ”₯ RETURN VALUE πŸ”₯

wcrtomb() shall return the number of bytes stored in the array object β€” including any shift sequences, always including the shift sequences!!

when wc is not a valid wide character, an encoding error shall occur. and in that case:

  • the function shall store the value of the macro [EILSEQ] in errno
  • and shall return (size_t)-1
  • and the conversion state shall be undefined 🫠

undefined conversion state. not vague. not "kinda weird". undefined. don't trust it after that, ok?


πŸŽ€ ERRORS πŸŽ€

wcrtomb() shall fail if:

  • EILSEQ β€” an invalid wide-character code is detected.

wcrtomb() may fail if:

  • EINVAL β€” ps points to an object that contains an invalid conversion state.

and then, quote unquote: "The following sections are informative." β€” which is manpage for "the next bits are lore, not law" 🧚


🌟 EXAMPLES 🌟

None.

literally nothing. the example section said "i'm not doing that" πŸ’€ iconic honestly


πŸ’œ APPLICATION USAGE πŸ’œ

None.

also nothing!! the silence is deafening πŸ¦‹


⭐ RATIONALE ⭐

None.

bestie there is no rationale. she just IS. main character energy, no explanation needed πŸ‘‘


πŸ“ FUTURE DIRECTIONS πŸ“

None.

the future is... also none. no notes. moving on πŸ™ˆ


🌈 SEE ALSO 🌈

  • mbsinit()
  • wcsrtombs()

and also, from The Base Definitions volume of POSIX.1‐2017: <wchar.h> πŸ’–


ok so legally I have to say all of this and I'm gonna say all of it, no skips:

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!! brought to you by IEEE/The Open Group, dated 2017, and filed under WCRTOMB(3POSIX). go forth and convert your wide characters, safely, with a valid mbstate_t if you can help it 🫢✨