unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Blake Jones <blakej@foo.net>
To: notmuch@notmuchmail.org
Subject: [PATCH v3 10/10] timegm: add portable implementation (Solaris support)
Date: Tue, 13 Nov 2012 18:10:05 -0800	[thread overview]
Message-ID: <1352859005-18631-11-git-send-email-blakej@foo.net> (raw)
In-Reply-To: <1352859005-18631-1-git-send-email-blakej@foo.net>

The timegm(3) function is a non-standard extension to libc which is
available in GNU libc and on some BSDs.  Although SunOS had this
function in its libc, Solaris (unfortunately) removed it.  This patch
implements a very simple version of timegm() which is good enough for
parse-time-string.c.

One complication of this fix is that libnotmuch.a includes a call to
parse_time_string() from parse-time-vrp.o, and parse_time_string() in
libparse-time-string.a is the thing which needs to call timegm().  A
straightforward attempt to have the two static libraries reconcile their
symbols from one another fails, because the symbols come from different
.o's, and the linker only does a single pass on each ".a" looking for
dependencies.  To solve this, libparse-time-string includes "compat.h",
and pulls in .o's from the compat directory, in order to get everything
that it needs.
---
 compat/Makefile.local                 |    4 +++
 compat/compat.h                       |   19 ++++++++++------
 compat/have_timegm.c                  |    7 ++++++
 compat/timegm.c                       |   37 +++++++++++++++++++++++++++++++++
 configure                             |   11 +++++++++
 parse-time-string/Makefile.local      |    4 ++-
 parse-time-string/parse-time-string.c |    1 +
 7 files changed, 75 insertions(+), 8 deletions(-)
 create mode 100644 compat/have_timegm.c
 create mode 100644 compat/timegm.c

diff --git a/compat/Makefile.local b/compat/Makefile.local
index 2c4f65f..b0d5417 100644
--- a/compat/Makefile.local
+++ b/compat/Makefile.local
@@ -17,4 +17,8 @@ ifneq ($(HAVE_STRSEP),1)
 notmuch_compat_srcs += $(dir)/strsep.c
 endif
 
+ifneq ($(HAVE_TIMEGM),1)
+notmuch_compat_srcs += $(dir)/timegm.c
+endif
+
 SRCS := $(SRCS) $(notmuch_compat_srcs)
diff --git a/compat/compat.h b/compat/compat.h
index 0b5e465..5a402d5 100644
--- a/compat/compat.h
+++ b/compat/compat.h
@@ -30,6 +30,13 @@
 extern "C" {
 #endif
 
+#if !STD_GETPWUID
+#define _POSIX_PTHREAD_SEMANTICS 1
+#endif
+#if !STD_ASCTIME
+#define _POSIX_PTHREAD_SEMANTICS 1
+#endif
+
 #if !HAVE_GETLINE
 #include <stdio.h>
 #include <unistd.h>
@@ -50,6 +57,11 @@ char* strcasestr(const char *haystack, const char *needle);
 char *strsep(char **stringp, const char *delim);
 #endif /* !HAVE_STRSEP */
 
+#if !HAVE_TIMEGM
+#include <time.h>
+time_t timegm (struct tm *tm);
+#endif /* !HAVE_TIMEGM */
+
 /* Silence gcc warnings about unused results.  These warnings exist
  * for a reason; any use of this needs to be justified. */
 #ifdef __GNUC__
@@ -58,13 +70,6 @@ char *strsep(char **stringp, const char *delim);
 #define IGNORE_RESULT(x) x
 #endif /* __GNUC__ */
 
-#if !STD_GETPWUID
-#define _POSIX_PTHREAD_SEMANTICS 1
-#endif
-#if !STD_ASCTIME
-#define _POSIX_PTHREAD_SEMANTICS 1
-#endif
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/compat/have_timegm.c b/compat/have_timegm.c
new file mode 100644
index 0000000..b62b793
--- /dev/null
+++ b/compat/have_timegm.c
@@ -0,0 +1,7 @@
+#include <time.h>
+#include "compat.h"
+
+int main()
+{
+    return (int) timegm((struct tm *)0);
+}
diff --git a/compat/timegm.c b/compat/timegm.c
new file mode 100644
index 0000000..6d76164
--- /dev/null
+++ b/compat/timegm.c
@@ -0,0 +1,37 @@
+#include <time.h>
+#include "compat.h"
+
+static int
+leapyear (int year)
+{
+    return ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
+}
+
+/*
+ * This is a simple implementation of timegm() which does what is needed
+ * by create_output() -- just turns the "struct tm" into a GMT time_t.
+ * It does not normalize any of the fields of the "struct tm", nor does
+ * it set tm_wday or tm_yday.
+ */
+time_t
+timegm (struct tm *tm)
+{
+    int	monthlen[2][12] = {
+	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+    };
+    int	year, month, days;
+
+    days = 365 * (tm->tm_year - 70);
+    for (year = 70; year < tm->tm_year; year++) {
+	if (leapyear(1900 + year)) {
+	    days++;
+	}
+    }
+    for (month = 0; month < tm->tm_mon; month++) {
+	days += monthlen[leapyear(1900 + year)][month];
+    }
+    days += tm->tm_mday - 1;
+
+    return ((((days * 24) + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec);
+}
diff --git a/configure b/configure
index ab8357f..f3ec9a2 100755
--- a/configure
+++ b/configure
@@ -523,6 +523,17 @@ else
 fi
 rm -f compat/have_strsep
 
+printf "Checking for timegm... "
+if ${CC} -o compat/have_timegm "$srcdir"/compat/have_timegm.c > /dev/null 2>&1
+then
+    printf "Yes.\n"
+    have_timegm="1"
+else
+    printf "No (will use our own instead).\n"
+    have_timegm="0"
+fi
+rm -f compat/have_timegm
+
 printf "Checking for standard version of getpwuid_r... "
 if ${CC} -o compat/check_getpwuid "$srcdir"/compat/check_getpwuid.c > /dev/null 2>&1
 then
diff --git a/parse-time-string/Makefile.local b/parse-time-string/Makefile.local
index 53534f3..c011e0b 100644
--- a/parse-time-string/Makefile.local
+++ b/parse-time-string/Makefile.local
@@ -1,7 +1,9 @@
 dir := parse-time-string
 extra_cflags += -I$(srcdir)/$(dir)
 
-libparse-time-string_c_srcs := $(dir)/parse-time-string.c
+libparse-time-string_c_srcs :=		\
+	$(notmuch_compat_srcs)		\
+	$(dir)/parse-time-string.c
 
 libparse-time-string_modules := $(libparse-time-string_c_srcs:.c=.o)
 
diff --git a/parse-time-string/parse-time-string.c b/parse-time-string/parse-time-string.c
index 584067d..ccad422 100644
--- a/parse-time-string/parse-time-string.c
+++ b/parse-time-string/parse-time-string.c
@@ -32,6 +32,7 @@
 #include <sys/time.h>
 #include <sys/types.h>
 
+#include "compat.h"
 #include "parse-time-string.h"
 
 /*
-- 
1.7.3.2

  parent reply	other threads:[~2012-11-14  2:10 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-14  2:09 [PATCH v3 00/10] Solaris support Blake Jones
2012-11-14  2:09 ` [PATCH v3 01/10] getpwuid: check for standards compliance (Solaris support) Blake Jones
2012-11-14  2:09 ` [PATCH v3 02/10] asctime: " Blake Jones
2012-11-14  2:09 ` [PATCH v3 03/10] gethostbyname: check for libnsl " Blake Jones
2012-11-14  2:09 ` [PATCH v3 04/10] configure: check for -Wl,-rpath " Blake Jones
2012-11-14  2:10 ` [PATCH v3 05/10] install: check for non-SysV version " Blake Jones
2012-11-14  2:10 ` [PATCH v3 06/10] strsep: check for availability " Blake Jones
2012-11-14  2:10 ` [PATCH v3 07/10] gen-version-script: parse Solaris "nm" output " Blake Jones
2012-11-14  2:10 ` [PATCH v3 08/10] notmuch-config: use strchr(), not index() " Blake Jones
2013-08-24  9:41   ` David Bremner
2012-11-14  2:10 ` [PATCH v3 09/10] debugger.c: correct return type from getppid() " Blake Jones
2012-11-14  2:10 ` Blake Jones [this message]
2012-11-15 15:55 ` [PATCH v3 00/10] Solaris support Tomi Ollila
2012-11-15 18:42   ` Blake Jones
2012-11-16  6:59     ` Tomi Ollila

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

  List information: https://notmuchmail.org/

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

  git send-email \
    --in-reply-to=1352859005-18631-11-git-send-email-blakej@foo.net \
    --to=blakej@foo.net \
    --cc=notmuch@notmuchmail.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

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 read-only IMAP folder(s) and NNTP newsgroup(s).