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