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