STRCSTR(3pub)
β¨ NAME β¨
okay so bestie, we're reading the C Programmer's Manual today, and this page is all about strcstr β the girl whose whole job is converting a memory block into printable C string notation. no cap, that's it, that's the gig. π
πΈ SYNOPSIS πΈ
#include <publib.h>
void strcstr(char *str, size_t max, const void *block, size_t n);
π DESCRIPTION π
so strcstr takes an arbitrary memory block β and bestie, listen, it does not have to be a zero terminated string, we are not gatekeeping β and converts what's inside it into a printable notation using normal C string literal syntax. iconic behaviour honestly. it's giving "put potentially binary data into a file" energy, and it's also giving "debugging output" energy. both slay. π₯
here's the tea on how each character gets treated:
- every character that has a simple shorthand escape sequence β that's
',",?,\,\a,\b,\f,\n,\r,\t,\vβ gets stored using that notation. periodt. \0is stored as\0. (yes ma'am, exactly like that.)- every other non-printable character gets stored using a hexadecimal escape sequence. β¨
- every other printable character is stored as is. no notes.
and here's the part that matters, so don't scroll past it: the isprint(3) macro is what decides whether a character counts as printable (like, whether it gets printed as is or gets the special notation treatment). which means β say it with me β the output depends on the locale. π your vibe check on "printable" is your locale's vibe check. iykyk.
π¦ RETURN VALUE π¦
strcstr returns nothing. literally nothing. void. empty-handed but stunning. π
π EXAMPLE π
the code below dumps input to the standard output in a guaranteed (modulo locale bugs π) printable format. debugging bestie, this one's for you.
#include <stdio.h>
#include <publib.h>
int main(void) {
char line[512];
char cstr[512*(CHAR_BIT/4+1+2)+1]; /* +2 for \x, +1 for \0,
the rest to be able to
store the hex code for
512 chars. */
while (fgets(line, sizeof(line), stdin) != NULL) {
strcstr(cstr, sizeof(cstr), line, strlen(line));
printf("%s0, cstr);
}
return 0;
}
and no, i did not fumble that printf line, that is character-for-character what the page says. i keep the facts, i just add the sparkle. π
π SEE ALSO π
π AUTHOR π
Lars Wirzenius (lars.wirzenius@helsinki.fi) β the original main character, we love and respect him. π«Ά
and that's the whole page, bestie! this little gem lives in the Publib crew, filed under the C Programmer's Manual, and it's the STRCSTR(3pub) entry itself. no date on the label, just vibes and a version tag. mwah. πβ¨π
π¦ where this page comes from
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms