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

Defined functions

gp defined in line 158; used 6 times
gtime defined in line 100; used 1 times
  • in line 60
main defined in line 44; never used

Defined variables

ap defined in line 11; used 8 times
dmsize defined in line 15; used 1 times
ep defined in line 11; used 6 times
sp defined in line 11; used 12 times
timbuf defined in line 10; used 20 times
uflag defined in line 12; used 3 times
wtmp defined in line 36; used 4 times

Defined macros

MENLO defined in line 32; used 1 times
  • in line 33
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 909
Valid CSS Valid XHTML 1.0 Strict