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