Re: [PATCH 4/4] timegm: add portable implementation (Solaris support)
[notmuch-archives.git] / 0a / ced169d252dcee0e2a0f25c428727bb0214647
1 Return-Path: <blakej@foo.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 47A2C431FD8\r
6         for <notmuch@notmuchmail.org>; Tue, 20 Aug 2013 08:33:29 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id E1Gh1fSFyAih for <notmuch@notmuchmail.org>;\r
16         Tue, 20 Aug 2013 08:33:23 -0700 (PDT)\r
17 Received: from foo.net (70-36-235-136.dsl.static.sonic.net [70.36.235.136])\r
18         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id B7AD7431FB6\r
21         for <notmuch@notmuchmail.org>; Tue, 20 Aug 2013 08:33:22 -0700 (PDT)\r
22 Received: from foo.net (localhost [127.0.0.1])\r
23         by foo.net (8.14.5+Sun/8.14.5) with ESMTP id r7KFXJuU014322;\r
24         Tue, 20 Aug 2013 08:33:19 -0700 (PDT)\r
25 To: Vladimir.Marek@oracle.com\r
26 Subject: Re: [PATCH 4/4] timegm: add portable implementation (Solaris support)\r
27 In-Reply-To: Your message of "Tue, 20 Aug 2013 11:28:55 +0200."\r
28         <1376990935-12411-1-git-send-email-Vladimir.Marek@oracle.com> \r
29 Date: Tue, 20 Aug 2013 08:33:19 -0700\r
30 Message-ID: <14321.1377012799@foo.net>\r
31 From: Blake Jones <blakej@foo.net>\r
32 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0.2\r
33         (foo.net [127.0.0.1]); Tue, 20 Aug 2013 08:33:19 -0700 (PDT)\r
34 Cc: notmuch@notmuchmail.org, Vladimir Marek <vlmarek@volny.cz>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Tue, 20 Aug 2013 15:33:29 -0000\r
48 \r
49 > From: Blake Jones <blakej@foo.net>\r
50\r
51 > The timegm(3) function is a non-standard extension to libc which is\r
52 > available in GNU libc and on some BSDs.  Although SunOS had this\r
53 > function in its libc, Solaris (unfortunately) removed it.  This patch\r
54 > implements a very simple version of timegm() which is good enough for\r
55 > parse-time-string.c.\r
56 \r
57 LGTM.\r
58 \r
59 Blake\r
60 \r
61 > Signed-off-by: Vladimir Marek <vlmarek@volny.cz>\r
62 > ---\r
63 >  compat/Makefile.local                 |    4 +++\r
64 >  compat/compat.h                       |    5 +++\r
65 >  compat/have_timegm.c                  |    7 ++++\r
66 >  compat/timegm.c                       |   57 +++++++++++++++++++++++++++++++++\r
67 >  configure                             |   11 +++++++\r
68 >  parse-time-string/parse-time-string.c |    1 +\r
69 >  6 files changed, 85 insertions(+)\r
70 >  create mode 100644 compat/have_timegm.c\r
71 >  create mode 100644 compat/timegm.c\r
72\r
73 > diff --git a/compat/Makefile.local b/compat/Makefile.local\r
74 > index 2c4f65f..b0d5417 100644\r
75 > --- a/compat/Makefile.local\r
76 > +++ b/compat/Makefile.local\r
77 > @@ -17,4 +17,8 @@ ifneq ($(HAVE_STRSEP),1)\r
78 >  notmuch_compat_srcs += $(dir)/strsep.c\r
79 >  endif\r
80 >  \r
81 > +ifneq ($(HAVE_TIMEGM),1)\r
82 > +notmuch_compat_srcs += $(dir)/timegm.c\r
83 > +endif\r
84 > +\r
85 >  SRCS := $(SRCS) $(notmuch_compat_srcs)\r
86 > diff --git a/compat/compat.h b/compat/compat.h\r
87 > index ae762c3..5a402d5 100644\r
88 > --- a/compat/compat.h\r
89 > +++ b/compat/compat.h\r
90 > @@ -57,6 +57,11 @@ char* strcasestr(const char *haystack, const char *needle);\r
91 >  char *strsep(char **stringp, const char *delim);\r
92 >  #endif /* !HAVE_STRSEP */\r
93 >  \r
94 > +#if !HAVE_TIMEGM\r
95 > +#include <time.h>\r
96 > +time_t timegm (struct tm *tm);\r
97 > +#endif /* !HAVE_TIMEGM */\r
98 > +\r
99 >  /* Silence gcc warnings about unused results.  These warnings exist\r
100 >   * for a reason; any use of this needs to be justified. */\r
101 >  #ifdef __GNUC__\r
102 > diff --git a/compat/have_timegm.c b/compat/have_timegm.c\r
103 > new file mode 100644\r
104 > index 0000000..b62b793\r
105 > --- /dev/null\r
106 > +++ b/compat/have_timegm.c\r
107 > @@ -0,0 +1,7 @@\r
108 > +#include <time.h>\r
109 > +#include "compat.h"\r
110 > +\r
111 > +int main()\r
112 > +{\r
113 > +    return (int) timegm((struct tm *)0);\r
114 > +}\r
115 > diff --git a/compat/timegm.c b/compat/timegm.c\r
116 > new file mode 100644\r
117 > index 0000000..213963b\r
118 > --- /dev/null\r
119 > +++ b/compat/timegm.c\r
120 > @@ -0,0 +1,57 @@\r
121 > +/* timegm.c --- Implementation of replacement timegm function.\r
122 > +   Copyright (C) 2012 Free Software Foundation, Inc.\r
123 > +\r
124 > +   This program is free software; you can redistribute it and/or\r
125 > +   modify it under the terms of the GNU General Public License as\r
126 > +   published by the Free Software Foundation; either version 3, or (at\r
127 > +   your option) any later version.\r
128 > +\r
129 > +   This program is distributed in the hope that it will be useful, but\r
130 > +   WITHOUT ANY WARRANTY; without even the implied warranty of\r
131 > +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
132 > +   General Public License for more details.\r
133 > +\r
134 > +   You should have received a copy of the GNU General Public License\r
135 > +   along with this program; if not, write to the Free Software\r
136 > +   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\r
137 > +   02110-1301, USA.  */\r
138 > +\r
139 > +/* Written by Blake Jones. */\r
140 > +\r
141 > +#include <time.h>\r
142 > +#include "compat.h"\r
143 > +\r
144 > +static int\r
145 > +leapyear (int year)\r
146 > +{\r
147 > +    return ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));\r
148 > +}\r
149 > +\r
150 > +/*\r
151 > + * This is a simple implementation of timegm() which does what is needed\r
152 > + * by create_output() -- just turns the "struct tm" into a GMT time_t.\r
153 > + * It does not normalize any of the fields of the "struct tm", nor does\r
154 > + * it set tm_wday or tm_yday.\r
155 > + */\r
156 > +time_t\r
157 > +timegm (struct tm *tm)\r
158 > +{\r
159 > +    int      monthlen[2][12] = {\r
160 > +     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r
161 > +     { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r
162 > +    };\r
163 > +    int      year, month, days;\r
164 > +\r
165 > +    days = 365 * (tm->tm_year - 70);\r
166 > +    for (year = 70; year < tm->tm_year; year++) {\r
167 > +     if (leapyear(1900 + year)) {\r
168 > +         days++;\r
169 > +     }\r
170 > +    }\r
171 > +    for (month = 0; month < tm->tm_mon; month++) {\r
172 > +     days += monthlen[leapyear(1900 + year)][month];\r
173 > +    }\r
174 > +    days += tm->tm_mday - 1;\r
175 > +\r
176 > +    return ((((days * 24) + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec);\r
177 > +}\r
178 > diff --git a/configure b/configure\r
179 > index ac44857..6166917 100755\r
180 > --- a/configure\r
181 > +++ b/configure\r
182 > @@ -530,6 +530,17 @@ else\r
183 >  fi\r
184 >  rm -f compat/have_strsep\r
185 >  \r
186 > +printf "Checking for timegm... "\r
187 > +if ${CC} -o compat/have_timegm "$srcdir"/compat/have_timegm.c > /dev/null 2>&1\r
188 > +then\r
189 > +    printf "Yes.\n"\r
190 > +    have_timegm="1"\r
191 > +else\r
192 > +    printf "No (will use our own instead).\n"\r
193 > +    have_timegm="0"\r
194 > +fi\r
195 > +rm -f compat/have_timegm\r
196 > +\r
197 >  printf "Checking for standard version of getpwuid_r... "\r
198 >  if ${CC} -o compat/check_getpwuid "$srcdir"/compat/check_getpwuid.c > /dev/null 2>&1\r
199 >  then\r
200 > diff --git a/parse-time-string/parse-time-string.c b/parse-time-string/parse-time-string.c\r
201 > index 584067d..ccad422 100644\r
202 > --- a/parse-time-string/parse-time-string.c\r
203 > +++ b/parse-time-string/parse-time-string.c\r
204 > @@ -32,6 +32,7 @@\r
205 >  #include <sys/time.h>\r
206 >  #include <sys/types.h>\r
207 >  \r
208 > +#include "compat.h"\r
209 >  #include "parse-time-string.h"\r
210 >  \r
211 >  /*\r
212 > -- \r
213 > 1.7.9.2\r
214\r
215 > _______________________________________________\r
216 > notmuch mailing list\r
217 > notmuch@notmuchmail.org\r
218 > http://notmuchmail.org/mailman/listinfo/notmuch\r
219\r