From af5c3afa91cc29268c6754ea70f15cae30f22403 Mon Sep 17 00:00:00 2001 From: David Bremner Date: Mon, 27 Jan 2014 10:12:12 -0400 Subject: compat: add canonicalize_file_name the POSIX 2008 behaviour of realpath is not available everywhere so we provide a simple wrapper function. We use (and provide) the gnu extension canonicalize_file_name to make it cleaner to test for the feature we need; otherwise we have to rely on realpath segfaulting if the second argument is null. --- compat/Makefile.local | 4 ++++ compat/canonicalize_file_name.c | 18 ++++++++++++++++++ compat/compat.h | 8 ++++++++ compat/have_canonicalize_file_name.c | 10 ++++++++++ 4 files changed, 40 insertions(+) create mode 100644 compat/canonicalize_file_name.c create mode 100644 compat/have_canonicalize_file_name.c (limited to 'compat') diff --git a/compat/Makefile.local b/compat/Makefile.local index b0d5417f..bcb9f0ec 100644 --- a/compat/Makefile.local +++ b/compat/Makefile.local @@ -5,6 +5,10 @@ extra_cflags += -I$(srcdir)/$(dir) notmuch_compat_srcs := +ifneq ($(HAVE_CANONICALIZE_FILE_NAME),1) +notmuch_compat_srcs += $(dir)/canonicalize_file_name.c +endif + ifneq ($(HAVE_GETLINE),1) notmuch_compat_srcs += $(dir)/getline.c $(dir)/getdelim.c endif diff --git a/compat/canonicalize_file_name.c b/compat/canonicalize_file_name.c new file mode 100644 index 00000000..e92c0f62 --- /dev/null +++ b/compat/canonicalize_file_name.c @@ -0,0 +1,18 @@ +#include "compat.h" +#include +#undef _GNU_SOURCE +#include + +char * +canonicalize_file_name (const char * path) +{ +#ifdef PATH_MAX + char *resolved_path = malloc (PATH_MAX+1); + if (resolved_path == NULL) + return NULL; + + return realpath (path, resolved_path); +#else +#error undefined PATH_MAX _and_ missing canonicalize_file_name not supported +#endif +} diff --git a/compat/compat.h b/compat/compat.h index 5a402d5c..634d505b 100644 --- a/compat/compat.h +++ b/compat/compat.h @@ -37,6 +37,14 @@ extern "C" { #define _POSIX_PTHREAD_SEMANTICS 1 #endif +#if !HAVE_CANONICALIZE_FILE_NAME +/* we only call this function from C, and this makes testing easier */ +#ifndef __cplusplus +char * +canonicalize_file_name (const char *path); +#endif +#endif + #if !HAVE_GETLINE #include #include diff --git a/compat/have_canonicalize_file_name.c b/compat/have_canonicalize_file_name.c new file mode 100644 index 00000000..24c848ec --- /dev/null +++ b/compat/have_canonicalize_file_name.c @@ -0,0 +1,10 @@ +#define _GNU_SOURCE +#include + +int main() +{ + char *found; + char *string; + + found = canonicalize_file_name (string); +} -- cgit v1.2.3