xref: /titanic_50/usr/src/lib/libast/common/string/strelapsed.c (revision 9b79392525856301c6f8962f189c2a32242af618)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
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			z = 1;
46 	int			m;
47 	const char*		last;
48 
49 	for (;;)
50 	{
51 		while (isspace(*s) || *s == '_')
52 			s++;
53 		if (!*(last = s))
54 			break;
55 		if (z)
56 		{
57 			z = 0;
58 			if (*s == '0' && (!(c = *(s + 1)) || isspace(c) || c == '_'))
59 			{
60 				last = s + 1;
61 				break;
62 			}
63 		}
64 		v = 0;
65 		while ((c = *s++) >= '0' && c <= '9')
66 			v = v * 10 + c - '0';
67 		v *= n;
68 		if (c == '.')
69 			for (m = n; (c = *s++) >= '0' && c <= '9';)
70 				f += (m /= 10) * (c - '0');
71 		if (c == '%')
72 		{
73 			t = ~t;
74 			last = s;
75 			break;
76 		}
77 		if (s == last + 1)
78 			break;
79 		if (!p)
80 			while (isspace(c) || c == '_')
81 				c = *s++;
82 		switch (c)
83 		{
84 		case 'S':
85 			if (*s == 'E' || *s == 'e')
86 			{
87 				v += f;
88 				f = 0;
89 			}
90 			else
91 				v *= 20 * 12 * 4 * 7 * 24 * 60 * 60;
92 			break;
93 		case 'y':
94 		case 'Y':
95 			v *= 12 * 4 * 7 * 24 * 60 * 60;
96 			break;
97 		case 'M':
98 			if (*s == 'I' || *s == 'i')
99 				v *= 60;
100 			else
101 				v *= 4 * 7 * 24 * 60 * 60;
102 			break;
103 		case 'w':
104 			v *= 7 * 24 * 60 * 60;
105 			break;
106 		case '-':
107 			p = 1;
108 			/*FALLTHROUGH*/
109 		case 'd':
110 			v *= 24 * 60 * 60;
111 			break;
112 		case 'h':
113 			v *= 60 * 60;
114 			break;
115 		case ':':
116 			p = 1;
117 			v *= strchr(s, ':') ? (60 * 60) : 60;
118 			break;
119 		case 'm':
120 			if (*s == 'o')
121 				v *= 4 * 7 * 24 * 60 * 60;
122 			else
123 				v *= 60;
124 			break;
125 		case 's':
126 			if (*s == 'c')
127 			{
128 				v *= 20 * 12 * 4 * 7 * 24 * 60 * 60;
129 				break;
130 			}
131 			v += f;
132 			f = 0;
133 			break;
134 		case 0:
135 			s--;
136 			v += f;
137 			break;
138 		default:
139 			if (p)
140 			{
141 				last = s - 1;
142 				t += v + f;
143 			}
144 			goto done;
145 		}
146 		t += v;
147 		while (isalpha(*s))
148 			s++;
149 	}
150  done:
151 	if (e)
152 		*e = (char*)last;
153 	return t;
154 }
155