STRRCHR(3P)

πŸ’– STRRCHR(3P) β€” the string-scanning girlie πŸ’–

okay bestie, so this one's from the POSIX Programmer's Manual. Quick heads-up before we get into it: this whole page is part of that manual, and the Linux implementation of this interface may differ β€” so if you're on Linux, go peek at the corresponding Linux manual page for the deets. Or, plot twist, the interface might not even be implemented on Linux at all. πŸ™ˆ Not us finding out the hard way.


✨ NAME ✨

strrchr β€” string scanning operation

no cap, that's the whole vibe. it scans. it finds. it serves. πŸ’…


🌸 SYNOPSIS 🌸

#include <string.h>

char *strrchr(const char *s, int c);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so first things first, the tea: everything described on this reference page is aligned with the ISO C standard. If anything here conflicts with the requirements in the ISO C standard, that conflict is unintentional β€” nobody's trying to start drama. This volume of POSIX.1‐2017 defers to the ISO C standard. periodt.

Now the actual function, because this is the good part:

The strrchr() function shall locate the last occurrence of c (converted to a char) in the string pointed to by s. πŸ”

and bestie, listen β€” the terminating NUL character is considered to be part of the string. So yes, that NUL is invited to the party. it's a main character too. πŸ‘‘


πŸ”₯ RETURN VALUE πŸ”₯

Upon successful completion, strrchr() shall return a pointer to the byte β€” or a null pointer if c does not occur in the string. that's it. that's the whole return. if she's not there, you get NULL and you just have to cope. 😭


πŸ’€ ERRORS πŸ’€

No errors are defined. not a single one. she's unproblematic fr fr.

Also, quick note: the following sections are informative (iykyk β€” that means they're the lore, not the law).


πŸŽ€ EXAMPLES πŸŽ€

Finding the Base Name of a File

okay this example is genuinely iconic. The following example uses strrchr() to get a pointer to the base name of a file.

Here's the move: strrchr() searches backwards through the name of the file to find the last '/' character in name. That pointer plus one will point to the base name of the file. 🫢

#include <string.h>
...
const char *name;
char *basename;
...
basename = strrchr(name, '/') + 1;
...

the + 1 is doing SO much heavy lifting and honestly? we love her for it. ⭐


🌈 APPLICATION USAGE 🌈

None. nothing to add, she said what she said. πŸ’…


🧚 RATIONALE 🧚

None. no thoughts, head empty, only function. ✨


πŸ’œ FUTURE DIRECTIONS πŸ’œ

None. the future is... also none. she's stable. she's thriving. she's not going anywhere. πŸ‘‘


🌸 SEE ALSO 🌸

  • strchr()
  • The Base Definitions volume of POSIX.1‐2017, <string.h>

okay so the legal bit, and we're keeping it exactly as it is because lawyers are scary and also correct:

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 .

also: 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 page bestie!! πŸ’– this one's IEEE/The Open Group, 2017, and it's the STRRCHR(3P) page β€” go forth and find your last occurrence, you absolute legend. πŸŽ€πŸ¦‹βœ¨

πŸ“¦ where this page comes from