comparison dcclib/get_secs.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * Copyright (c) 2008 by Rhyolite Software, LLC
4 *
5 * This agreement is not applicable to any entity which sells anti-spam
6 * solutions to others or provides an anti-spam solution as part of a
7 * security solution sold to other entities, or to a private network
8 * which employs the DCC or uses data provided by operation of the DCC
9 * but does not provide corresponding data to other users.
10 *
11 * Permission to use, copy, modify, and distribute this software without
12 * changes for any purpose with or without fee is hereby granted, provided
13 * that the above copyright notice and this permission notice appear in all
14 * copies and any distributed versions or copies are either unchanged
15 * or not called anything similar to "DCC" or "Distributed Checksum
16 * Clearinghouse".
17 *
18 * Parties not eligible to receive a license under this agreement can
19 * obtain a commercial license to use DCC by contacting Rhyolite Software
20 * at sales@rhyolite.com.
21 *
22 * A commercial license would be for Distributed Checksum and Reputation
23 * Clearinghouse software. That software includes additional features. This
24 * free license for Distributed ChecksumClearinghouse Software does not in any
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
26 * software
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 * SOFTWARE.
36 *
37 * Rhyolite Software DCC 1.3.103-1.24 $Revision$
38 */
39
40 #include "dcc_defs.h"
41
42
43 /* because tv.tv_sec is not a time_t on all systems
44 * and to be thread safe on WIN32 */
45 const struct tm *
46 dcc_localtime(time_t secs, struct tm *result)
47 {
48 #ifdef HAVE_LOCALTIME_R
49 result = localtime_r(&secs, result);
50 #else
51 dcc_localtime_lock();
52 result = localtime(&secs);
53 dcc_localtime_unlock();
54 #endif
55 return result;
56 }
57
58
59
60 const char *
61 dcc_time2str(char *buf, size_t buf_len, const char *pat, time_t secs)
62 {
63 struct tm tm;
64
65 if (0 >= strftime(buf, buf_len, pat, dcc_localtime(secs, &tm)))
66 STRLCPY(buf, "?", buf_len);
67 return buf;
68 }
69
70
71
72 int
73 dcc_get_secs(const char *s, const char **end, int min, int max, int def)
74 {
75 const char *cp;
76 char *p;
77 int secs;
78
79 if (*s == '\0' || *s == ',') {
80 secs = def;
81 cp = s;
82 } else if (min > 0
83 && !CLITCMP(s, "never")) {
84 cp = s+LITZ("never");
85 secs = max;
86 } else if ((secs = strtoul(s, &p, 10)) >= DCC_MAX_SECS/60) {
87 return -1;
88 } else if (*(cp = p) == '\0' || *cp == ',') {
89 ;
90 } else if (!CLITCMP(cp, "seconds")) {
91 cp += LITZ("seconds");
92 } else if (!CLITCMP(cp, "s")) {
93 cp += LITZ("s");
94
95 } else if (!CLITCMP(cp, "minutes")) {
96 cp += LITZ("minutes");
97 secs *= 60;
98 } else if (!CLITCMP(cp, "minute")) {
99 cp += LITZ("minute");
100 secs *= 60;
101 } else if (!CLITCMP(cp, "m")) {
102 cp += LITZ("m");
103 secs *= 60;
104
105 } else if (secs >= DCC_MAX_SECS/(60*60)) {
106 return -1;
107 } else if (!CLITCMP(cp, "hours")) {
108 cp += LITZ("hours");
109 secs *= 60*60;
110 } else if (!CLITCMP(cp, "hour")) {
111 cp += LITZ("hour");
112 secs *= 60*60;
113 } else if (!CLITCMP(cp, "h")) {
114 cp += LITZ("h");
115 secs *= 60*60;
116
117 } else if (secs >= DCC_MAX_SECS/(24*60*60)) {
118 return -1;
119 } else if (!CLITCMP(cp, "days")) {
120 cp += LITZ("days");
121 secs *= 24*60*60;
122 } else if (!CLITCMP(cp, "day")) {
123 cp += LITZ("day");
124 secs *= 24*60*60;
125 } else if (!CLITCMP(cp, "d")) {
126 cp += LITZ("d");
127 secs *= 24*60*60;
128
129 } else if (secs >= DCC_MAX_SECS/(7*24*60*60)) {
130 return -1;
131 } else if (!CLITCMP(cp, "weeks")) {
132 cp += LITZ("weeks");
133 secs *= 7*24*60*60;
134 } else if (!CLITCMP(cp, "week")) {
135 cp += LITZ("week");
136 secs *= 7*24*60*60;
137 } else if (!CLITCMP(cp, "w")) {
138 cp += LITZ("w");
139 secs *= 7*24*60*60;
140
141 } else {
142 return -1;
143 }
144
145 if (secs > max)
146 return -1;
147 if (secs < min && secs != 0)
148 return -1;
149
150 if (*cp != '\0') {
151 if (*cp != ',' || !end)
152 return -1;
153 ++cp;
154 }
155 if (end)
156 *end = cp;
157 return secs;
158 }
159
160
161
162 time_t
163 tv_diff2us(const struct timeval *tv1, const struct timeval *tv2)
164 {
165 time_t us;
166
167 /* prevent overflow */
168 us = tv1->tv_sec - tv2->tv_sec;
169 if (us <= -FOREVER_SECS)
170 return -FOREVER_US;
171 if (us >= FOREVER_SECS)
172 return FOREVER_US;
173 us = us*DCC_US + (tv1->tv_usec - tv2->tv_usec);
174 return us;
175 }
176
177
178
179 void
180 tv_add(struct timeval *tgt,
181 const struct timeval *a,
182 const struct timeval *b)
183 {
184 tgt->tv_sec = a->tv_sec + b->tv_sec;
185 tgt->tv_usec = a->tv_usec + b->tv_usec;
186 tgt->tv_sec += tgt->tv_usec / DCC_US;
187 tgt->tv_usec %= DCC_US;
188 }
189
190
191
192 void
193 tv_add_us(struct timeval *tgt,
194 time_t us)
195 {
196 time_t secs;
197
198 us = tgt->tv_usec + us;
199 if (us >= 0) {
200 tgt->tv_sec += us / DCC_US;
201 tgt->tv_usec = us % DCC_US;
202 } else {
203 secs = (DCC_US - 1 - us) / DCC_US;
204 tgt->tv_sec -= secs;
205 tgt->tv_usec = (secs * DCC_US) + us;
206 }
207 }