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