string_escape_mem(9)
okay bestie, welcome to string_escape_mem(9) from the Kernel API Manual β¨ this is literally the page about quoting characters in the given memory buffer, and we are keeping every flag, every byte, and every caveat. no cap π
π NAME π
string_escape_mem β quote characters in the given memory buffer. That's the whole vibe π
πΈ SYNOPSIS πΈ
int string_escape_mem (const char *src , size_t isz , char *dst , size_t osz , unsigned int flags , const char *only );
π¦ ARGUMENTS π¦
srcβ source buffer (unescaped)iszβ source buffer sizedstβ destination buffer (escaped)oszβ destination buffer sizeflagsβ combination of the flagsonlyβ NULL-terminated string containing characters used to limit the selected escape class. If characters are included inonlythat would not normally be escaped by the classes selected inflags, they will be copied todstunescaped.
β¨ DESCRIPTION β¨
The process of escaping byte buffer includes several parts. They are applied in the following sequence.
- The character is not matched to the one from
onlystring and thus must go as-is to the output. - The character is matched to the printable and ASCII classes, if asked, and in case of match it passes through to the output.
- The character is matched to the printable or ASCII class, if asked, and in case of match it passes through to the output.
- The character is checked if it falls into the class given by
flags.ESCAPE_OCTALandESCAPE_HEXare going last since they cover any character. Note that they actually can't go together, otherwiseESCAPE_HEXwill be ignored.
Caller must provide valid source and destination pointers. Be aware that destination buffer will not be NULL-terminated, thus caller have to append it if needs. This part is important, bestie π
The supported flags are:
ESCAPE_SPACEβ (special white space, not space itself)'\f'β form feed'\r'β carriage return'\n'β newline'\t'β horizontal tab'\v'β vertical tab
ESCAPE_SPECIALβ this one covers the specials:'\''β single quote'\\'β backslash'\a'β alert (BEL)'\e'β escape
ESCAPE_NULLβ'\0'β nullESCAPE_OCTALβ'\NNN'β byte with octal value NNN (3 digits)ESCAPE_ANYβ all previous togetherESCAPE_NPβ escape only non-printable characters, checked byisprintESCAPE_ANY_NPβ all previous togetherESCAPE_HEXβ'\xHH'β byte with hexadecimal value HH (2 digits)ESCAPE_NAβ escape only non-ascii characters, checked byisasciiESCAPE_NAPβ escape only non-printable or non-ascii charactersESCAPE_APPENDβ append characters fromonlyto be escaped by the given classes
ESCAPE_APPEND would help to pass additional characters to the escaped, when one of ESCAPE_NP, ESCAPE_NA, or ESCAPE_NAP is provided.
One notable caveat: theESCAPE_NAP,ESCAPE_NPandESCAPE_NAhave the higher priority than the rest of the flags (ESCAPE_NAPis the highest). It doesn't make much sense to use either of them withoutESCAPE_OCTALorESCAPE_HEX, because they cover most of the other character classes.ESCAPE_NAPcan utilizeESCAPE_SPACEorESCAPE_SPECIALin addition to the above.
π RETURN π
The total size of the escaped output that would be generated for the given input and flags. To check whether the output was truncated, compare the return value to osz. There is room left in dst for a '\0' terminator if and only if ret < osz.
π SEE ALSO π
Kernel file ./lib/string_helpers.c, plus the whole squad: string_get_size(9), parse_int_array_user(9), string_unescape(9), kasprintf_strarray(9), kfree_strarray(9), skip_spaces(9), strim(9), sysfs_streq(9), match_string(9), __sysfs_match_string(9), strreplace(9), memcpy_and_pad(9).
made with π
from source ./lib, dated October 2026, page string_escape_mem(9) β mwah π«Ά
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms