List for cgit developers and users
 help / color / mirror / Atom feed
From: Markus Mayer <code@mmayer.net>
To: CGIT List <cgit@lists.zx2c4.com>
Cc: Markus Mayer <code@mmayer.net>
Subject: [PATCH 1/1] global: provide memrchr implementation for macOS
Date: Fri, 27 Jan 2023 15:15:43 -0800	[thread overview]
Message-ID: <20230127231543.17977-2-code@mmayer.net> (raw)
In-Reply-To: <20230127231543.17977-1-code@mmayer.net>

macOS doesn't come with a memrchr() of its own, so let's provide an
implementation it can use.

memrchr.c was taken from Apple's own open source site.

https://opensource.apple.com/source/sudo/sudo-87.80.2/sudo/lib/util/memrchr.c

It was modified in the following ways:

- Removed unnecessary includes (config.h, sudo_compat.h).
- Removed unnecessary include guard (HAVE_MEMRCHR).
- Removed comment referencing an unrelated project.
- Added comment mentioning URL to the original location of the file.

Signed-off-by: Markus Mayer <code@mmayer.net>
---
 cgit.h    |  4 ++++
 cgit.mk   |  9 +++++++++
 memrchr.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 memrchr.c

diff --git a/cgit.h b/cgit.h
index ddd2ccba0b88..19f810f22b61 100644
--- a/cgit.h
+++ b/cgit.h
@@ -397,4 +397,8 @@ extern char *expand_macros(const char *txt);
 
 extern char *get_mimetype_for_filename(const char *filename);
 
+#ifdef NEED_MEMRCHR
+extern void *memrchr(const void *s, int c, size_t n);
+#endif
+
 #endif /* CGIT_H */
diff --git a/cgit.mk b/cgit.mk
index 3fcc1ca31440..f85018c38b78 100644
--- a/cgit.mk
+++ b/cgit.mk
@@ -63,10 +63,19 @@ ifeq ($(uname_S),Linux)
 	HAVE_LINUX_SENDFILE = YesPlease
 endif
 
+ifeq ($(uname_S),Darwin)
+	IS_DARWIN = yes
+endif
+
 ifdef HAVE_LINUX_SENDFILE
 	CGIT_CFLAGS += -DHAVE_LINUX_SENDFILE
 endif
 
+ifdef IS_DARWIN
+	CGIT_CFLAGS += -DNEED_MEMRCHR
+	CGIT_OBJ_NAMES += memrchr.o
+endif
+
 CGIT_OBJ_NAMES += cgit.o
 CGIT_OBJ_NAMES += cache.o
 CGIT_OBJ_NAMES += cmd.o
diff --git a/memrchr.c b/memrchr.c
new file mode 100644
index 000000000000..75c6dcde7432
--- /dev/null
+++ b/memrchr.c
@@ -0,0 +1,44 @@
+/*
+ * SPDX-License-Identifier: ISC
+ *
+ * Copyright (c) 2007, 2010-2014
+ *	Todd C. Miller <Todd.Miller@sudo.ws>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * Imported into CGIT from
+ * https://opensource.apple.com/source/sudo/sudo-87.80.2/sudo/lib/util/memrchr.c
+ */
+
+#include <sys/types.h>
+
+/*
+ * Reverse memchr()
+ * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
+ */
+void *
+memrchr(const void *s, int c, size_t n)
+{
+    const unsigned char *cp;
+
+    if (n != 0) {
+	cp = (unsigned char *)s + n;
+	do {
+	    if (*(--cp) == (unsigned char)c)
+		return (void *)cp;
+	} while (--n != 0);
+    }
+    return (void *)0;
+}
-- 
2.39.0


  reply	other threads:[~2023-01-27 23:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-27 23:15 [PATCH 0/1] cgit " Markus Mayer
2023-01-27 23:15 ` Markus Mayer [this message]
     [not found]   ` <fe46d404-8659-7260-63c1-38bbc9c1e5ad@gmail.com>
2023-01-28  0:35     ` [PATCH 1/1] global: provide memrchr implementation " Markus Mayer
     [not found]       ` <Y9R3X7bhwUAWofJ/@xps13>
2023-01-29 21:43         ` Markus Mayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230127231543.17977-2-code@mmayer.net \
    --to=code@mmayer.net \
    --cc=cgit@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).