File: get_secs.c

package info (click to toggle)
dcc 1.2.74-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,552 kB
  • ctags: 4,041
  • sloc: ansic: 41,034; perl: 2,310; sh: 2,186; makefile: 224
file content (129 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Distributed Checksum Clearinghouse
 *
 * Copyright (c) 2005 by Rhyolite Software
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 * Rhyolite Software DCC 1.2.74-1.10 $Revision$
 */

#include "dcc_defs.h"


/* because tv.tv_sec is not a time_t on all systems
 * and to be thread safe on WIN32 */
const struct tm *
dcc_localtime(time_t secs, struct tm *result)
{
#ifdef HAVE_LOCALTIME_R
	return localtime_r(&secs, result);
#else
	dcc_localtime_lock();
	result = localtime(&secs);
	dcc_localtime_unlock();
	return result;
#endif
}



int
dcc_get_secs(char *s, char **end, int min, int max, int def)
{
	static time_t epoch = 0;
	char *p;
	int secs;

	if (*s == '\0' || *s == ',') {
		secs = def;
		p = s;
	} else if (min > 0
		   && !CSTRCMP(s, "never")) {
		p = s+STRZ("never");
		if (!epoch)
			epoch = time(0);
		secs = epoch;
	} else if ((secs = strtoul(s, &p, 0)) >= DCC_MAX_SECS/60) {
		return -1;
	} else if (*p == '\0' || *p == ',') {
		;
	} else if (!CSTRCMP(p, "seconds")) {
		p += STRZ("seconds");
	} else if (!CSTRCMP(p, "s")) {
		p += STRZ("s");

	} else if (!CSTRCMP(p, "minutes")) {
		p += STRZ("minutes");
		secs *= 60;
	} else if (!CSTRCMP(p, "minute")) {
		p += STRZ("minute");
		secs *= 60;
	} else if (!CSTRCMP(p, "m")) {
		p += STRZ("m");
		secs *= 60;

	} else if (secs >= DCC_MAX_SECS/(60*60)) {
		return -1;
	} else if (!CSTRCMP(p, "hours")) {
		p += STRZ("hours");
		secs *= 60*60;
	} else if (!CSTRCMP(p, "hour")) {
		p += STRZ("hour");
		secs *= 60*60;
	} else if (!CSTRCMP(p, "h")) {
		p += STRZ("h");
		secs *= 60*60;

	} else if (secs >= DCC_MAX_SECS/(24*60*60)) {
		return -1;
	} else if (!CSTRCMP(p, "days")) {
		p += STRZ("days");
		secs *= 24*60*60;
	} else if (!CSTRCMP(p, "day")) {
		p += STRZ("day");
		secs *= 24*60*60;
	} else if (!CSTRCMP(p, "d")) {
		p += STRZ("d");
		secs *= 24*60*60;

	} else if (secs >= DCC_MAX_SECS/(7*24*60*60)) {
		return -1;
	} else if (!CSTRCMP(p, "weeks")) {
		p += STRZ("weeks");
		secs *= 7*24*60*60;
	} else if (!CSTRCMP(p, "week")) {
		p += STRZ("week");
		secs *= 7*24*60*60;
	} else if (!CSTRCMP(p, "w")) {
		p += STRZ("w");
		secs *= 7*24*60*60;

	} else {
		return -1;
	}

	if (secs > max)
		return -1;
	if (secs < min && secs != 0)
		return -1;

	if (*p != '\0') {
		if (*p != ',' || !end)
			return -1;
		++p;
	}
	if (end)
		*end = p;
	return secs;
}