Annotation of 42BSD/usr.lib/sendmail/src/convtime.c, revision 1.1.1.1

1.1       root        1: # include <ctype.h>
                      2: # include "useful.h"
                      3: 
                      4: SCCSID(@(#)convtime.c  4.1             7/25/83);
                      5: 
                      6: /*
                      7: **  CONVTIME -- convert time
                      8: **
                      9: **     Takes a time as an ascii string with a trailing character
                     10: **     giving units:
                     11: **       s -- seconds
                     12: **       m -- minutes
                     13: **       h -- hours
                     14: **       d -- days (default)
                     15: **       w -- weeks
                     16: **     For example, "3d12h" is three and a half days.
                     17: **
                     18: **     Parameters:
                     19: **             p -- pointer to ascii time.
                     20: **
                     21: **     Returns:
                     22: **             time in seconds.
                     23: **
                     24: **     Side Effects:
                     25: **             none.
                     26: */
                     27: 
                     28: time_t
                     29: convtime(p)
                     30:        char *p;
                     31: {
                     32:        register time_t t, r;
                     33:        register char c;
                     34: 
                     35:        r = 0;
                     36:        while (*p != '\0')
                     37:        {
                     38:                t = 0;
                     39:                while (isdigit(c = *p++))
                     40:                        t = t * 10 + (c - '0');
                     41:                if (c == '\0')
                     42:                        p--;
                     43:                switch (c)
                     44:                {
                     45:                  case 'w':             /* weeks */
                     46:                        t *= 7;
                     47: 
                     48:                  case 'd':             /* days */
                     49:                  default:
                     50:                        t *= 24;
                     51: 
                     52:                  case 'h':             /* hours */
                     53:                        t *= 60;
                     54: 
                     55:                  case 'm':             /* minutes */
                     56:                        t *= 60;
                     57: 
                     58:                  case 's':             /* seconds */
                     59:                        break;
                     60:                }
                     61:                r += t;
                     62:        }
                     63: 
                     64:        return (r);
                     65: }
                     66: /*
                     67: **  PINTVL -- produce printable version of a time interval
                     68: **
                     69: **     Parameters:
                     70: **             intvl -- the interval to be converted
                     71: **             brief -- if TRUE, print this in an extremely compact form
                     72: **                     (basically used for logging).
                     73: **
                     74: **     Returns:
                     75: **             A pointer to a string version of intvl suitable for
                     76: **                     printing or framing.
                     77: **
                     78: **     Side Effects:
                     79: **             none.
                     80: **
                     81: **     Warning:
                     82: **             The string returned is in a static buffer.
                     83: */
                     84: 
                     85: # define PLURAL(n)     ((n) == 1 ? "" : "s")
                     86: 
                     87: char *
                     88: pintvl(intvl, brief)
                     89:        time_t intvl;
                     90:        bool brief;
                     91: {
                     92:        static char buf[256];
                     93:        register char *p;
                     94:        int wk, dy, hr, mi, se;
                     95: 
                     96:        if (intvl == 0 && !brief)
                     97:                return ("zero seconds");
                     98: 
                     99:        /* decode the interval into weeks, days, hours, minutes, seconds */
                    100:        se = intvl % 60;
                    101:        intvl /= 60;
                    102:        mi = intvl % 60;
                    103:        intvl /= 60;
                    104:        hr = intvl % 24;
                    105:        intvl /= 24;
                    106:        if (brief)
                    107:                dy = intvl;
                    108:        else
                    109:        {
                    110:                dy = intvl % 7;
                    111:                intvl /= 7;
                    112:                wk = intvl;
                    113:        }
                    114: 
                    115:        /* now turn it into a sexy form */
                    116:        p = buf;
                    117:        if (brief)
                    118:        {
                    119:                if (dy > 0)
                    120:                {
                    121:                        (void) sprintf(p, "%d+", dy);
                    122:                        p += strlen(p);
                    123:                }
                    124:                (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
                    125:                return (buf);
                    126:        }
                    127: 
                    128:        /* use the verbose form */
                    129:        if (wk > 0)
                    130:        {
                    131:                (void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
                    132:                p += strlen(p);
                    133:        }
                    134:        if (dy > 0)
                    135:        {
                    136:                (void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
                    137:                p += strlen(p);
                    138:        }
                    139:        if (hr > 0)
                    140:        {
                    141:                (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
                    142:                p += strlen(p);
                    143:        }
                    144:        if (mi > 0)
                    145:        {
                    146:                (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
                    147:                p += strlen(p);
                    148:        }
                    149:        if (se > 0)
                    150:        {
                    151:                (void) sprintf(p, ", %d second%s", se, PLURAL(se));
                    152:                p += strlen(p);
                    153:        }
                    154: 
                    155:        return (buf + 2);
                    156: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.