xref: /titanic_50/usr/src/lib/libast/common/string/strelapsed.c (revision 9ec394dbf343c1f23c6e13c39df427f238e5a369)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * parse elapsed time in 1/n secs from s
28  * compatible with fmtelapsed()
29  * also handles ps [day-][hour:]min:sec
30  * also handles coshell % for 'infinity'
31  * if e!=0 then it is set to first unrecognized char
32  */
33 
34 #include <ast.h>
35 #include <ctype.h>
36 
37 unsigned long
38 strelapsed(register const char* s, char** e, int n)
39 {
40 	register int		c;
41 	register unsigned long	v;
42 	unsigned long		t = 0;
43 	int			f = 0;
44 	int			p = 0;
45 	int			m;
46 	const char*		last;
47 
48 	for (;;)
49 	{
50 		while (isspace(*s) || *s == '_')
51 			s++;
52 		if (!*(last = s))
53 			break;
54 		v = 0;
55 		while ((c = *s++) >= '0' && c <= '9')
56 			v = v * 10 + c - '0';
57 		v *= n;
58 		if (c == '.')
59 			for (m = n; (c = *s++) >= '0' && c <= '9';)
60 				f += (m /= 10) * (c - '0');
61 		if (c == '%')
62 		{
63 			t = ~t;
64 			last = s;
65 			break;
66 		}
67 		if (s == last + 1)
68 			break;
69 		if (!p)
70 			while (isspace(c) || c == '_')
71 				c = *s++;
72 		switch (c)
73 		{
74 		case 'S':
75 			if (*s == 'E' || *s == 'e')
76 			{
77 				v += f;
78 				f = 0;
79 			}
80 			else
81 				v *= 20 * 12 * 4 * 7 * 24 * 60 * 60;
82 			break;
83 		case 'y':
84 		case 'Y':
85 			v *= 12 * 4 * 7 * 24 * 60 * 60;
86 			break;
87 		case 'M':
88 			if (*s == 'I' || *s == 'i')
89 				v *= 60;
90 			else
91 				v *= 4 * 7 * 24 * 60 * 60;
92 			break;
93 		case 'w':
94 			v *= 7 * 24 * 60 * 60;
95 			break;
96 		case '-':
97 			p = 1;
98 			/*FALLTHROUGH*/
99 		case 'd':
100 			v *= 24 * 60 * 60;
101 			break;
102 		case 'h':
103 			v *= 60 * 60;
104 			break;
105 		case ':':
106 			p = 1;
107 			v *= strchr(s, ':') ? (60 * 60) : 60;
108 			break;
109 		case 'm':
110 			if (*s == 'o')
111 				v *= 4 * 7 * 24 * 60 * 60;
112 			else
113 				v *= 60;
114 			break;
115 		case 's':
116 			if (*s == 'c')
117 			{
118 				v *= 20 * 12 * 4 * 7 * 24 * 60 * 60;
119 				break;
120 			}
121 			/*FALLTHROUGH*/
122 		case 0:
123 			v += f;
124 			f = 0;
125 			break;
126 		default:
127 			if (p)
128 			{
129 				last = s - 1;
130 				t += v + f;
131 			}
132 			goto done;
133 		}
134 		t += v;
135 		while (isalpha(*s))
136 			s++;
137 	}
138  done:
139 	if (e)
140 		*e = (char*)last;
141 	return t;
142 }
143