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.
  • \0 is 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