mbsnrtowcs(3)

✨ mbsnrtowcs ✨

okay bestie so this is mbsnrtowcs from the mbsnrtowcs(3) manual, and she is the multibyte-string-to-wide-character-string converter with a byte limit. she lives in the Standard C library, so you get her with libc, -lc. no cap πŸ’…


πŸŽ€ LIBRARY

Standard C library (libc, -lc). she's already in the building, you just gotta link her.


πŸ¦‹ SYNOPSIS

#include <wchar.h>

size_t mbsnrtowcs(size_t size;
                  wchar_t dest[restrict size], const char **restrict src,
                  size_t nms, size_t size, mbstate_t *restrict ps);

Feature Test Macro Requirements for glibc (peep feature_test_macros(7) for the full tea):

  • mbsnrtowcs():
    • Since glibc 2.10:
      • _POSIX_C_SOURCE >= 200809L
    • Before glibc 2.10:
      • _GNU_SOURCE

πŸ’– DESCRIPTION

so mbsnrtowcs() is literally just like mbsrtowcs(3), except the number of bytes she converts, starting at *src, is capped at most nms bytes. that's the whole plot twist. πŸ“

If dest is not NULL, mbsnrtowcs() converts at most nms bytes from the multibyte string *src into a wide-character string starting at dest. At most size wide characters get written to dest. The shift state *ps gets updated. Under the hood the conversion is effectively done by repeatedly calling mbrtowc(dest, *src, n, ps) where n is some positive number, for as long as that call succeeds, and then bumping dest up by one and *src up by however many bytes got consumed. 🌈

The conversion can stop for three reasons:

  • An invalid multibyte sequence has been encountered. In this case, *src is left pointing at the invalid multibyte sequence, (size_t) -1 is returned, and errno is set to EILSEQ. πŸ’€
  • The nms limit forces a stop, or size non-L'\0' wide characters have been stored at dest. In this case, *src is left pointing at the next multibyte sequence to be converted, and the number of wide characters written to dest is returned. slay, partial progress.
  • The multibyte string has been completely converted, including the terminating null wide character ('\0') β€” which has the side effect of bringing *ps back to the initial state. In this case, *src is set to NULL, and the number of wide characters written to dest, excluding the terminating null wide character, is returned. πŸ‘‘

According to POSIX.1, if the input buffer ends with an incomplete character, it is unspecified whether conversion stops at the end of the previous character (if any), or at the end of the input buffer. The glibc implementation adopts the former behavior. iykyk ✨

If dest is NULL, size is ignored, and the conversion proceeds as described above, except that the converted wide characters are not written out to memory, and no destination size limit exists. (yes, she's just vibing and counting.)

In both of the above cases, if ps is NULL, a static anonymous state known only to the mbsnrtowcs() function is used instead.

⚠️ The programmer must ensure that there is room for at least size wide characters at dest. no room = no vibe. 🫢

🌸 RETURN VALUE

mbsnrtowcs() returns the number of wide characters that make up the converted part of the wide-character string, not including the terminating null wide character. If an invalid multibyte sequence was encountered, (size_t) -1 is returned, and errno set to EILSEQ.


πŸ‘‘ ATTRIBUTES

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

  • Interface: mbsnrtowcs()
  • Attribute: Thread safety
  • Value: MT-Unsafe race:mbsnrtowcs/!ps

πŸ’œ STANDARDS

POSIX.1-2008. she's official, bestie.


⭐ NOTES

  • The behavior of mbsnrtowcs() depends on the LC_CTYPE category of the current locale. 🌍
  • Passing NULL as ps is not multithread safe. 😭

πŸ”₯ SEE ALSO


okay that's the whole page, mwah πŸ’‹ this was mbsnrtowcs(3) from Linux man-pages 6.20, dated 2026-02-08. go forth and convert your bytes responsibly, bestie πŸ§šπŸ’–

πŸ“¦ 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-2.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms