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

unix.superglobalmegacorp.com

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