--- /dev/null
+Return-Path: <too@guru-group.fi>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 07D9B431FBD\r
+ for <notmuch@notmuchmail.org>; Sat, 25 Jan 2014 13:59:18 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 4kxfLVEYeKE8 for <notmuch@notmuchmail.org>;\r
+ Sat, 25 Jan 2014 13:59:11 -0800 (PST)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id 80643431FBC\r
+ for <notmuch@notmuchmail.org>; Sat, 25 Jan 2014 13:59:11 -0800 (PST)\r
+Received: by guru.guru-group.fi (Postfix, from userid 501)\r
+ id 02DA81000C1; Sat, 25 Jan 2014 23:59:03 +0200 (EET)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH] configure: check for POSIX.1-2008 realpath(3)\r
+ implementation.\r
+Date: Sat, 25 Jan 2014 23:59:02 +0200\r
+Message-Id: <1390687142-16401-1-git-send-email-tomi.ollila@iki.fi>\r
+X-Mailer: git-send-email 1.8.0\r
+Cc: tomi.ollila@iki.fi\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 25 Jan 2014 21:59:18 -0000\r
+\r
+Check whether realpath(3) supports the resolved_path == NULL feature,\r
+standardized in POSIX.1-2008.\r
+\r
+This is tested by executing the realpath(3) with NULL as second\r
+argument and the program is expected to SIGSEGV in case the\r
+feature is not supported.\r
+\r
+If the feature is not supported the compatibility code calls realpath(3)\r
+with second argument pointing to buffer with MAX_PATH in size. With\r
+this more systems are supported; those that have POSIX.1-2008 -capable\r
+realpath(3) and those that have MAX_PATH defined and realpath(3) does\r
+not exceed that limit.\r
+---\r
+\r
+I tested running configure and then make test; then make clean,\r
+edited Makefile.config POSIX_2008_REALPATH = 0 and make clean\r
+again. then tested sigsegv'ing with (memcpy(0, 0, 4)...\r
+\r
+\r
+ configure | 32 ++++++++++++++++++++++++++++++--\r
+ notmuch-config.c | 21 ++++++++++++++++++++-\r
+ 2 files changed, 50 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/configure b/configure\r
+index 13b6062..8174219 100755\r
+--- a/configure\r
++++ b/configure\r
+@@ -454,6 +454,29 @@ echo $util_byte_order\r
+ \r
+ rm -f _byteorder _byteorder.c\r
+ \r
++printf "Checking for posix 2008 realpath()... "\r
++# resolved_path == NULL is standardized in POSIX.1-2008\r
++cat > _posix_2008_realpath.c <<EOF\r
++#define _BSD_SOURCE\r
++#include <limits.h>\r
++#include <stdlib.h>\r
++#include <signal.h>\r
++void exit1(int sig) { exit(1); }\r
++int main () {\r
++ signal(SIGSEGV, exit1);\r
++ int main () { realpath (".", (char*)0); return 0;\r
++}\r
++EOF\r
++${CC} ${CFLAGS} _posix_2008_realpath.c -o _posix_2008_realpath > /dev/null 2>&1\r
++if ./_posix_2008_realpath; then\r
++ echo Yes.\r
++ posix_2008_realpath=1\r
++else\r
++ echo No.\r
++ posix_2008_realpath=0\r
++fi\r
++rm -f _posix_2008_realpath _posix_2008_realpath.c\r
++\r
+ if [ $errors -gt 0 ]; then\r
+ cat <<EOF\r
+ \r
+@@ -718,6 +741,9 @@ libdir = ${LIBDIR:=\$(prefix)/lib}\r
+ # byte order within a 32 bit word. 1234 = little, 4321 = big, 0 = guess\r
+ UTIL_BYTE_ORDER = ${util_byte_order}\r
+ \r
++# Whether realpath(3) supports resolved_path == NULL feature\r
++POSIX_2008_REALPATH = ${posix_2008_realpath}\r
++\r
+ # Whether libdir is in a path configured into ldconfig\r
+ LIBDIR_IN_LDCONFIG = ${libdir_in_ldconfig}\r
+ \r
+@@ -824,7 +850,8 @@ CONFIGURE_CFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS) \\\r
+ -DSTD_GETPWUID=\$(STD_GETPWUID) \\\r
+ -DSTD_ASCTIME=\$(STD_ASCTIME) \\\r
+ -DHAVE_XAPIAN_COMPACT=\$(HAVE_XAPIAN_COMPACT) \\\r
+- -DUTIL_BYTE_ORDER=\$(UTIL_BYTE_ORDER)\r
++ -DUTIL_BYTE_ORDER=\$(UTIL_BYTE_ORDER) \\\r
++ -DPOSIX_2008_REALPATH=\$(POSIX_2008_REALPATH)\r
+ \r
+ CONFIGURE_CXXFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS) \\\r
+ \$(TALLOC_CFLAGS) -DHAVE_VALGRIND=\$(HAVE_VALGRIND) \\\r
+@@ -834,7 +861,8 @@ CONFIGURE_CXXFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS) \\\r
+ -DSTD_GETPWUID=\$(STD_GETPWUID) \\\r
+ -DSTD_ASCTIME=\$(STD_ASCTIME) \\\r
+ -DHAVE_XAPIAN_COMPACT=\$(HAVE_XAPIAN_COMPACT) \\\r
+- -DUTIL_BYTE_ORDER=\$(UTIL_BYTE_ORDER)\r
++ -DUTIL_BYTE_ORDER=\$(UTIL_BYTE_ORDER) \\\r
++ -DPOSIX_2008_REALPATH=\$(POSIX_2008_REALPATH)\r
+ \r
+ CONFIGURE_LDFLAGS = \$(GMIME_LDFLAGS) \$(TALLOC_LDFLAGS) \$(XAPIAN_LDFLAGS)\r
+ EOF\r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index 8d28653..14d0e5c 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -454,10 +454,26 @@ notmuch_config_save (notmuch_config_t *config)\r
+ }\r
+ \r
+ /* Try not to overwrite symlinks. */\r
++#if POSIX_2008_REALPATH\r
+ filename = realpath (config->filename, NULL);\r
++#else\r
++ /* compatibility with minor effort, not elegance, is the ruling factor\r
++ in these (two) else branches... */\r
++ char resolved_path[PATH_MAX];\r
++ filename = realpath (config->filename, resolved_path);\r
++#endif\r
+ if (! filename) {\r
+ if (errno == ENOENT) {\r
++#if POSIX_2008_REALPATH\r
+ filename = strdup (config->filename);\r
++#else\r
++ /* ... this is the other else... */\r
++ resolved_path[sizeof resolved_path - 1] = '\0';\r
++ strncpy(resolved_path, config->filename, sizeof resolved_path);\r
++ /* "faking" out of memory in case path too long -- close enough? */\r
++ filename = resolved_path[sizeof resolved_path - 1]?\r
++ resolved_path: NULL;\r
++#endif\r
+ if (! filename) {\r
+ fprintf (stderr, "Out of memory.\n");\r
+ g_free (data);\r
+@@ -480,12 +496,15 @@ notmuch_config_save (notmuch_config_t *config)\r
+ filename, error->message);\r
+ }\r
+ g_error_free (error);\r
++#if POSIX_2008_REALPATH\r
+ free (filename);\r
++#endif\r
+ g_free (data);\r
+ return 1;\r
+ }\r
+-\r
++#if POSIX_2008_REALPATH\r
+ free (filename);\r
++#endif\r
+ g_free (data);\r
+ return 0;\r
+ }\r
+-- \r
+1.8.5.3\r
+\r