diff options
context:
space:
mode:
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Replace the glibc-only `memrchr` for portability
-rw-r--r--source/ui-shared.c13
1 file changed, 12 insertions, 1 deletion
diff --git a/source/ui-shared.c b/source/ui-shared.c
index df52a9b..eb68ab7 100644
--- a/source/ui-shared.c
+++ b/source/ui-shared.c
@@ -1247,6 +1247,17 @@ void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
strbuf_release(&filename);
}
+/* Portable reverse memchr: memrchr is a glibc extension absent on
+ * macOS and the BSDs. */
+static const char *find_last_char(const char *start, const char *end, int c)
+{
+ while (end > start) {
+ if (*--end == c)
+ return end;
+ }
+ return NULL;
+}
+
void cgit_set_title_from_path(const char *path)
{
struct strbuf sb = STRBUF_INIT;
@@ -1255,7 +1266,7 @@ void cgit_set_title_from_path(const char *path)
if (!path)
return;
- for (last_slash = path + strlen(path); (slash = memrchr(path, '/', last_slash - path)) != NULL; last_slash = slash) {
+ for (last_slash = path + strlen(path); (slash = find_last_char(path, last_slash, '/')) != NULL; last_slash = slash) {
strbuf_add(&sb, slash + 1, last_slash - slash - 1);
strbuf_addstr(&sb, " \xc2\xab ");
}