Annotation of researchv9/cmd/date.c, revision 1.1.1.1

1.1       root        1: static char *sccsid = "@(#)date.c      4.1 (Berkeley) 10/1/80";
                      2: #include <stdio.h>
                      3: /*
                      4:  * date : print date
                      5:  * date YYMMDDHHMM[.SS] : set date, if allowed
                      6:  * date -u ... : date in GMT
                      7:  */
                      8: #include <time.h>
                      9: #include <sys/types.h>
                     10: #include <sys/timeb.h>
                     11: #include <utmp.h>
                     12: long   timbuf;
                     13: char   *ap, *ep, *sp;
                     14: int    uflag;
                     15: int    nflag;
                     16: 
                     17: char   *timezone();
                     18: static int     dmsize[12] =
                     19: {
                     20:        31,
                     21:        28,
                     22:        31,
                     23:        30,
                     24:        31,
                     25:        30,
                     26:        31,
                     27:        31,
                     28:        30,
                     29:        31,
                     30:        30,
                     31:        31
                     32: };
                     33: 
                     34: struct utmp wtmp[2] = { {"|", "", 0}, {"{", "", 0}};
                     35: 
                     36: char   *ctime();
                     37: char   *asctime();
                     38: struct tm *localtime();
                     39: struct tm *gmtime();
                     40: 
                     41: usage()
                     42: {
                     43:        printf("Usage: date [-u] [-n] YYMMDDHHMM[.SS]\n");
                     44: }
                     45: 
                     46: main(argc, argv)
                     47: char *argv[];
                     48: {
                     49:        register char *tzn;
                     50:        struct timeb info;
                     51:        int wf, rc;
                     52:        extern char _sobuf[];
                     53:        extern long atol();
                     54: 
                     55:        setbuf(stdout, _sobuf);
                     56:        rc = 0;
                     57:        ftime(&info);
                     58:        while((argc>1) && (argv[1][0]=='-'))
                     59:                switch(argv[1][1])
                     60:                {
                     61:                case 'u':       uflag++; argc--; argv++; break;
                     62:                case 'n':       nflag++; argc--; argv++; break;
                     63:                default:        usage(); exit(1);
                     64:                }
                     65:        if(argc > 1) {
                     66:                ap = argv[1];
                     67:                if(nflag)
                     68:                        timbuf = atol(ap);
                     69:                else {
                     70:                        if (gtime()) {
                     71:                                printf("date: bad conversion\n");
                     72:                                exit(1);
                     73:                        }
                     74:                }
                     75:                /* convert to GMT assuming local time */
                     76:                if (uflag==0) {
                     77:                        timbuf += (long)info.timezone*60;
                     78:                        /* now fix up local daylight time */
                     79:                        if(localtime(&timbuf)->tm_isdst)
                     80:                                timbuf -= 60*60;
                     81:                }
                     82:                time(&wtmp[0].ut_time);
                     83:                if(stime(&timbuf) < 0) {
                     84:                        rc++;
                     85:                        printf("date: no permission\n");
                     86:                } else if ((wf = open("/usr/adm/wtmp", 1)) >= 0) {
                     87:                        time(&wtmp[1].ut_time);
                     88:                        lseek(wf, 0L, 2);
                     89:                        write(wf, (char *)wtmp, sizeof(wtmp));
                     90:                        close(wf);
                     91:                }
                     92:        }
                     93:        if (rc==0)
                     94:                time(&timbuf);
                     95:        if(uflag) {
                     96:                ap = asctime(gmtime(&timbuf));
                     97:                tzn = "GMT";
                     98:        } else {
                     99:                struct tm *tp;
                    100:                tp = localtime(&timbuf);
                    101:                ap = asctime(tp);
                    102:                tzn = timezone(info.timezone, tp->tm_isdst);
                    103:        }
                    104:        if(nflag) {
                    105:                /* fix up daylight saving and timezone */
                    106:                if(uflag == 0) {
                    107:                        timbuf -= (long)info.timezone*60;
                    108:                        if(localtime(&timbuf)->tm_isdst)
                    109:                                timbuf += 60*60;
                    110:                }
                    111:                printf("%ld\n", timbuf);
                    112:        } else {
                    113:                printf("%.20s", ap);
                    114:                if (tzn)
                    115:                        printf("%s", tzn);
                    116:                printf("%s", ap+19);
                    117:        }
                    118:        exit(rc);
                    119: }
                    120: 
                    121: gtime()
                    122: {
                    123:        register int i, year, month;
                    124:        int day, hour, mins, secs;
                    125:        struct tm *L;
                    126:        char x;
                    127: 
                    128:        ep=ap;
                    129:        while(*ep) ep++;
                    130:        sp=ap;
                    131:        while(sp<ep) {
                    132:                x = *sp;
                    133:                *sp++ = *--ep;
                    134:                *ep = x;
                    135:        }
                    136:        sp=ap;
                    137:        time(&timbuf);
                    138:        L=localtime(&timbuf);
                    139:        secs = gp(-1);
                    140:        if(*sp!='.') {
                    141:                mins=secs;
                    142:                secs=0;
                    143:        } else {sp++;
                    144:                mins = gp(-1);
                    145:        }
                    146:        hour = gp(-1);
                    147:        day = gp(L->tm_mday);
                    148:        month = gp(L->tm_mon+1);
                    149:        year = gp(L->tm_year);
                    150:        if(*sp)
                    151:                return(1);
                    152:        if( month<1 || month>12 ||
                    153:            day<1 || day>31 ||
                    154:            mins<0 || mins>59 ||
                    155:            secs<0 || secs>59)
                    156:                return(1);
                    157:        if (hour==24) {
                    158:                hour=0; day++;
                    159:        }
                    160:        if (hour<0 || hour>23)
                    161:                return(1);
                    162:        timbuf = 0;
                    163:        year += 1900;
                    164:        for(i=1970; i<year; i++)
                    165:                timbuf += dysize(i);
                    166:        /* Leap year */
                    167:        if (dysize(year)==366 && month >= 3)
                    168:                timbuf++;
                    169:        while(--month)
                    170:                timbuf += dmsize[month-1];
                    171:        timbuf += day-1;
                    172:        timbuf = 24*timbuf + hour;
                    173:        timbuf = 60*timbuf + mins;
                    174:        timbuf = 60*timbuf + secs;
                    175:        return(0);
                    176: 
                    177: }
                    178: 
                    179: gp(dfault)
                    180: {
                    181:        register int c, d;
                    182: 
                    183:        if(*sp==0)
                    184:                return(dfault);
                    185:        c = (*sp++)-'0';
                    186:        d = (*sp ? (*sp++)-'0' : 0);
                    187:        if(c<0 || c>9 || d<0 || d>9)
                    188:                return(-1);
                    189:        return(c+10*d);
                    190: }

unix.superglobalmegacorp.com

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