1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate * All rights reserved.
4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5*7c478bd9Sstevel@tonic-gate * Copyright (c) 1988, 1993
6*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
7*7c478bd9Sstevel@tonic-gate *
8*7c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
9*7c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
10*7c478bd9Sstevel@tonic-gate * the sendmail distribution.
11*7c478bd9Sstevel@tonic-gate *
12*7c478bd9Sstevel@tonic-gate */
13*7c478bd9Sstevel@tonic-gate
14*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
15*7c478bd9Sstevel@tonic-gate
16*7c478bd9Sstevel@tonic-gate #include <sendmail.h>
17*7c478bd9Sstevel@tonic-gate
18*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: convtime.c,v 8.36 2001/02/13 22:32:08 ca Exp $")
19*7c478bd9Sstevel@tonic-gate
20*7c478bd9Sstevel@tonic-gate /*
21*7c478bd9Sstevel@tonic-gate ** CONVTIME -- convert time
22*7c478bd9Sstevel@tonic-gate **
23*7c478bd9Sstevel@tonic-gate ** Takes a time as an ascii string with a trailing character
24*7c478bd9Sstevel@tonic-gate ** giving units:
25*7c478bd9Sstevel@tonic-gate ** s -- seconds
26*7c478bd9Sstevel@tonic-gate ** m -- minutes
27*7c478bd9Sstevel@tonic-gate ** h -- hours
28*7c478bd9Sstevel@tonic-gate ** d -- days (default)
29*7c478bd9Sstevel@tonic-gate ** w -- weeks
30*7c478bd9Sstevel@tonic-gate ** For example, "3d12h" is three and a half days.
31*7c478bd9Sstevel@tonic-gate **
32*7c478bd9Sstevel@tonic-gate ** Parameters:
33*7c478bd9Sstevel@tonic-gate ** p -- pointer to ascii time.
34*7c478bd9Sstevel@tonic-gate ** units -- default units if none specified.
35*7c478bd9Sstevel@tonic-gate **
36*7c478bd9Sstevel@tonic-gate ** Returns:
37*7c478bd9Sstevel@tonic-gate ** time in seconds.
38*7c478bd9Sstevel@tonic-gate **
39*7c478bd9Sstevel@tonic-gate ** Side Effects:
40*7c478bd9Sstevel@tonic-gate ** none.
41*7c478bd9Sstevel@tonic-gate */
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate time_t
44*7c478bd9Sstevel@tonic-gate convtime(p, units)
45*7c478bd9Sstevel@tonic-gate char *p;
46*7c478bd9Sstevel@tonic-gate int units;
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate register time_t t, r;
49*7c478bd9Sstevel@tonic-gate register char c;
50*7c478bd9Sstevel@tonic-gate bool pos = true;
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate r = 0;
53*7c478bd9Sstevel@tonic-gate if (sm_strcasecmp(p, "now") == 0)
54*7c478bd9Sstevel@tonic-gate return NOW;
55*7c478bd9Sstevel@tonic-gate if (*p == '-')
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate pos = false;
58*7c478bd9Sstevel@tonic-gate ++p;
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate while (*p != '\0')
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate t = 0;
63*7c478bd9Sstevel@tonic-gate while ((c = *p++) != '\0' && isascii(c) && isdigit(c))
64*7c478bd9Sstevel@tonic-gate t = t * 10 + (c - '0');
65*7c478bd9Sstevel@tonic-gate if (c == '\0')
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate c = units;
68*7c478bd9Sstevel@tonic-gate p--;
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate else if (strchr("wdhms", c) == NULL)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate usrerr("Invalid time unit `%c'", c);
73*7c478bd9Sstevel@tonic-gate c = units;
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate switch (c)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate case 'w': /* weeks */
78*7c478bd9Sstevel@tonic-gate t *= 7;
79*7c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate case 'd': /* days */
82*7c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
83*7c478bd9Sstevel@tonic-gate default:
84*7c478bd9Sstevel@tonic-gate t *= 24;
85*7c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate case 'h': /* hours */
88*7c478bd9Sstevel@tonic-gate t *= 60;
89*7c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate case 'm': /* minutes */
92*7c478bd9Sstevel@tonic-gate t *= 60;
93*7c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate case 's': /* seconds */
96*7c478bd9Sstevel@tonic-gate break;
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate r += t;
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate return pos ? r : -r;
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate ** PINTVL -- produce printable version of a time interval
105*7c478bd9Sstevel@tonic-gate **
106*7c478bd9Sstevel@tonic-gate ** Parameters:
107*7c478bd9Sstevel@tonic-gate ** intvl -- the interval to be converted
108*7c478bd9Sstevel@tonic-gate ** brief -- if true, print this in an extremely compact form
109*7c478bd9Sstevel@tonic-gate ** (basically used for logging).
110*7c478bd9Sstevel@tonic-gate **
111*7c478bd9Sstevel@tonic-gate ** Returns:
112*7c478bd9Sstevel@tonic-gate ** A pointer to a string version of intvl suitable for
113*7c478bd9Sstevel@tonic-gate ** printing or framing.
114*7c478bd9Sstevel@tonic-gate **
115*7c478bd9Sstevel@tonic-gate ** Side Effects:
116*7c478bd9Sstevel@tonic-gate ** none.
117*7c478bd9Sstevel@tonic-gate **
118*7c478bd9Sstevel@tonic-gate ** Warning:
119*7c478bd9Sstevel@tonic-gate ** The string returned is in a static buffer.
120*7c478bd9Sstevel@tonic-gate */
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate #define PLURAL(n) ((n) == 1 ? "" : "s")
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate char *
pintvl(intvl,brief)125*7c478bd9Sstevel@tonic-gate pintvl(intvl, brief)
126*7c478bd9Sstevel@tonic-gate time_t intvl;
127*7c478bd9Sstevel@tonic-gate bool brief;
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate static char buf[256];
130*7c478bd9Sstevel@tonic-gate register char *p;
131*7c478bd9Sstevel@tonic-gate int wk, dy, hr, mi, se;
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate if (intvl == 0 && !brief)
134*7c478bd9Sstevel@tonic-gate return "zero seconds";
135*7c478bd9Sstevel@tonic-gate if (intvl == NOW)
136*7c478bd9Sstevel@tonic-gate return "too long";
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate /* decode the interval into weeks, days, hours, minutes, seconds */
139*7c478bd9Sstevel@tonic-gate se = intvl % 60;
140*7c478bd9Sstevel@tonic-gate intvl /= 60;
141*7c478bd9Sstevel@tonic-gate mi = intvl % 60;
142*7c478bd9Sstevel@tonic-gate intvl /= 60;
143*7c478bd9Sstevel@tonic-gate hr = intvl % 24;
144*7c478bd9Sstevel@tonic-gate intvl /= 24;
145*7c478bd9Sstevel@tonic-gate if (brief)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate dy = intvl;
148*7c478bd9Sstevel@tonic-gate wk = 0;
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate else
151*7c478bd9Sstevel@tonic-gate {
152*7c478bd9Sstevel@tonic-gate dy = intvl % 7;
153*7c478bd9Sstevel@tonic-gate intvl /= 7;
154*7c478bd9Sstevel@tonic-gate wk = intvl;
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate
157*7c478bd9Sstevel@tonic-gate /* now turn it into a sexy form */
158*7c478bd9Sstevel@tonic-gate p = buf;
159*7c478bd9Sstevel@tonic-gate if (brief)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate if (dy > 0)
162*7c478bd9Sstevel@tonic-gate {
163*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "%d+", dy);
164*7c478bd9Sstevel@tonic-gate p += strlen(p);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "%02d:%02d:%02d",
167*7c478bd9Sstevel@tonic-gate hr, mi, se);
168*7c478bd9Sstevel@tonic-gate return buf;
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate /* use the verbose form */
172*7c478bd9Sstevel@tonic-gate if (wk > 0)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d week%s", wk,
175*7c478bd9Sstevel@tonic-gate PLURAL(wk));
176*7c478bd9Sstevel@tonic-gate p += strlen(p);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate if (dy > 0)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d day%s", dy,
181*7c478bd9Sstevel@tonic-gate PLURAL(dy));
182*7c478bd9Sstevel@tonic-gate p += strlen(p);
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate if (hr > 0)
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d hour%s", hr,
187*7c478bd9Sstevel@tonic-gate PLURAL(hr));
188*7c478bd9Sstevel@tonic-gate p += strlen(p);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate if (mi > 0)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d minute%s", mi,
193*7c478bd9Sstevel@tonic-gate PLURAL(mi));
194*7c478bd9Sstevel@tonic-gate p += strlen(p);
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate if (se > 0)
197*7c478bd9Sstevel@tonic-gate {
198*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), ", %d second%s", se,
199*7c478bd9Sstevel@tonic-gate PLURAL(se));
200*7c478bd9Sstevel@tonic-gate p += strlen(p);
201*7c478bd9Sstevel@tonic-gate }
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate return (buf + 2);
204*7c478bd9Sstevel@tonic-gate }
205