| 1 | /*
|
|---|
| 2 | =============================================================================
|
|---|
| 3 | date.c -- date - with format capabilities
|
|---|
| 4 | Version 1 -- 1988-01-14
|
|---|
| 5 | =============================================================================
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include "types.h"
|
|---|
| 9 | #include "utmp.h"
|
|---|
| 10 | #include "stdio.h"
|
|---|
| 11 | #include "time.h"
|
|---|
| 12 |
|
|---|
| 13 | #define MONTH itoa(tim->tm_mon+1, cp,2)
|
|---|
| 14 | #define DAY itoa(tim->tm_mday, cp, 2)
|
|---|
| 15 | #define YEAR itoa(tim->tm_year, cp, 2)
|
|---|
| 16 | #define HOUR itoa(tim->tm_hour, cp, 2)
|
|---|
| 17 | #define MINUTE itoa(tim->tm_min, cp, 2)
|
|---|
| 18 | #define SECOND itoa(tim->tm_sec, cp, 2)
|
|---|
| 19 | #define JULIAN itoa(tim->tm_yday+1, cp, 3)
|
|---|
| 20 | #define WEEKDAY itoa(tim->tm_wday, cp, 1)
|
|---|
| 21 | #define MODHOUR itoa(h, cp, 2)
|
|---|
| 22 | #define dysize(A) (((A) % 4) ? 365 : 366)
|
|---|
| 23 |
|
|---|
| 24 | int dmsize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|---|
| 25 |
|
|---|
| 26 | char month[12][3] = {
|
|---|
| 27 |
|
|---|
| 28 | "Jan","Feb","Mar","Apr",
|
|---|
| 29 | "May","Jun","Jul","Aug",
|
|---|
| 30 | "Sep","Oct","Nov","Dec"
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | char days[7][3] = {
|
|---|
| 34 |
|
|---|
| 35 | "Sun","Mon","Tue","Wed",
|
|---|
| 36 | "Thu","Fri","Sat"
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | char *itoa();
|
|---|
| 40 | char *cbp;
|
|---|
| 41 | long timbuf;
|
|---|
| 42 |
|
|---|
| 43 | struct utmp wtmp[2] = {
|
|---|
| 44 |
|
|---|
| 45 | {"", "", OTIME_MSG, 0, OLD_TIME, 0, 0, 0},
|
|---|
| 46 | {"", "", NTIME_MSG, 0, NEW_TIME, 0, 0, 0}
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | /*
|
|---|
| 50 | =============================================================================
|
|---|
| 51 | gpair() -- get 2 digits
|
|---|
| 52 | =============================================================================
|
|---|
| 53 | */
|
|---|
| 54 |
|
|---|
| 55 | int
|
|---|
| 56 | gpair()
|
|---|
| 57 | {
|
|---|
| 58 | register int c, d;
|
|---|
| 59 | register char *cp;
|
|---|
| 60 |
|
|---|
| 61 | cp = cbp;
|
|---|
| 62 |
|
|---|
| 63 | if(*cp == 0)
|
|---|
| 64 | return(-1);
|
|---|
| 65 |
|
|---|
| 66 | c = (*cp++ - '0') * 10;
|
|---|
| 67 |
|
|---|
| 68 | if (c<0 || c>100)
|
|---|
| 69 | return(-1);
|
|---|
| 70 |
|
|---|
| 71 | if(*cp == 0)
|
|---|
| 72 | return(-1);
|
|---|
| 73 |
|
|---|
| 74 | if ((d = *cp++ - '0') < 0 || d > 9)
|
|---|
| 75 | return(-1);
|
|---|
| 76 |
|
|---|
| 77 | cbp = cp;
|
|---|
| 78 |
|
|---|
| 79 | return (c+d);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /* |
|---|
| 83 | */
|
|---|
| 84 |
|
|---|
| 85 | /*
|
|---|
| 86 | =============================================================================
|
|---|
| 87 | gtime() -- get time
|
|---|
| 88 | =============================================================================
|
|---|
| 89 | */
|
|---|
| 90 |
|
|---|
| 91 | int
|
|---|
| 92 | gtime()
|
|---|
| 93 | {
|
|---|
| 94 | register int i;
|
|---|
| 95 | register int y, t;
|
|---|
| 96 | int d, h, m;
|
|---|
| 97 | long nt;
|
|---|
| 98 |
|
|---|
| 99 | tzset();
|
|---|
| 100 |
|
|---|
| 101 | t = gpair();
|
|---|
| 102 |
|
|---|
| 103 | if (t < 1 OR t > 12)
|
|---|
| 104 | return(1);
|
|---|
| 105 |
|
|---|
| 106 | d = gpair();
|
|---|
| 107 |
|
|---|
| 108 | if(d < 1 OR d > 31)
|
|---|
| 109 | return(1);
|
|---|
| 110 |
|
|---|
| 111 | h = gpair();
|
|---|
| 112 |
|
|---|
| 113 | if (h EQ 24) {
|
|---|
| 114 |
|
|---|
| 115 | h = 0;
|
|---|
| 116 | d++;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | m = gpair();
|
|---|
| 120 |
|
|---|
| 121 | if (m < 0 OR m > 59)
|
|---|
| 122 | return(1);
|
|---|
| 123 |
|
|---|
| 124 | y = gpair();
|
|---|
| 125 |
|
|---|
| 126 | if (y < 0) {
|
|---|
| 127 |
|
|---|
| 128 | (void) time(&nt);
|
|---|
| 129 | y = localtime(&nt)->tm_year;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | if (*cbp EQ 'p')
|
|---|
| 133 | h += 12;
|
|---|
| 134 |
|
|---|
| 135 | if (h < 0 OR h > 23)
|
|---|
| 136 | return(1);
|
|---|
| 137 |
|
|---|
| 138 | timbuf = 0;
|
|---|
| 139 | y += 1900;
|
|---|
| 140 |
|
|---|
| 141 | for(i = 1970; i < y; i++)
|
|---|
| 142 | timbuf += dysize(i);
|
|---|
| 143 |
|
|---|
| 144 | /* Leap year */
|
|---|
| 145 |
|
|---|
| 146 | if (dysize(y) EQ 366 AND t GE 3)
|
|---|
| 147 | timbuf += 1;
|
|---|
| 148 |
|
|---|
| 149 | while (--t)
|
|---|
| 150 | timbuf += dmsize[t - 1];
|
|---|
| 151 |
|
|---|
| 152 | timbuf += (d - 1);
|
|---|
| 153 | timbuf *= 24;
|
|---|
| 154 | timbuf += h;
|
|---|
| 155 | timbuf *= 60;
|
|---|
| 156 | timbuf += m;
|
|---|
| 157 | timbuf *= 60;
|
|---|
| 158 |
|
|---|
| 159 | return(0);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /* |
|---|
| 163 | */
|
|---|
| 164 |
|
|---|
| 165 | /*
|
|---|
| 166 | =============================================================================
|
|---|
| 167 | itoa(i,ptr,dig)
|
|---|
| 168 | =============================================================================
|
|---|
| 169 | */
|
|---|
| 170 |
|
|---|
| 171 | char *
|
|---|
| 172 | itoa(i, ptr, dig)
|
|---|
| 173 | register int i;
|
|---|
| 174 | register char *ptr;
|
|---|
| 175 | register int dig;
|
|---|
| 176 | {
|
|---|
| 177 | switch(dig) {
|
|---|
| 178 |
|
|---|
| 179 | case 3:
|
|---|
| 180 | *ptr++ = i/100 + '0';
|
|---|
| 181 | i = i - i / 100 * 100;
|
|---|
| 182 |
|
|---|
| 183 | case 2:
|
|---|
| 184 | *ptr++ = i / 10 + '0';
|
|---|
| 185 |
|
|---|
| 186 | case 1:
|
|---|
| 187 | *ptr++ = i % 10 + '0';
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | return(ptr);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /* |
|---|
| 194 | */
|
|---|
| 195 |
|
|---|
| 196 | main(argc, argv)
|
|---|
| 197 | int argc;
|
|---|
| 198 | int **argv;
|
|---|
| 199 | {
|
|---|
| 200 | register char *aptr, *cp, c;
|
|---|
| 201 | int h, hflag, i, tfailed, wf;
|
|---|
| 202 | long tbuf, time(), lseek();
|
|---|
| 203 | struct tm *tim;
|
|---|
| 204 | char buf[200], *tzn;
|
|---|
| 205 |
|
|---|
| 206 | tfailed = 0;
|
|---|
| 207 |
|
|---|
| 208 | if(argc > 1) {
|
|---|
| 209 |
|
|---|
| 210 | cbp = (char *)argv[1];
|
|---|
| 211 |
|
|---|
| 212 | if(*cbp == '+') {
|
|---|
| 213 |
|
|---|
| 214 | hflag = 0;
|
|---|
| 215 |
|
|---|
| 216 | for(cp=buf;cp< &buf[200];)
|
|---|
| 217 | *cp++ = '\0';
|
|---|
| 218 |
|
|---|
| 219 | (void) time(&tbuf);
|
|---|
| 220 | tim = localtime(&tbuf);
|
|---|
| 221 | aptr = (char *)argv[1];
|
|---|
| 222 | aptr++;
|
|---|
| 223 | cp = buf;
|
|---|
| 224 |
|
|---|
| 225 | while(c = *aptr++) {
|
|---|
| 226 |
|
|---|
| 227 | if(c == '%')
|
|---|
| 228 | switch(*aptr++) {
|
|---|
| 229 |
|
|---|
| 230 | case '%':
|
|---|
| 231 | *cp++ = '%';
|
|---|
| 232 | continue;
|
|---|
| 233 |
|
|---|
| 234 | case 'n':
|
|---|
| 235 | *cp++ = '\n';
|
|---|
| 236 | continue;
|
|---|
| 237 |
|
|---|
| 238 | case 't':
|
|---|
| 239 | *cp++ = '\t';
|
|---|
| 240 | continue;
|
|---|
| 241 |
|
|---|
| 242 | case 'm' :
|
|---|
| 243 | cp = MONTH;
|
|---|
| 244 | continue;
|
|---|
| 245 |
|
|---|
| 246 | case 'd':
|
|---|
| 247 | cp = DAY;
|
|---|
| 248 | continue;
|
|---|
| 249 |
|
|---|
| 250 | case 'y' :
|
|---|
| 251 | cp = YEAR;
|
|---|
| 252 | continue;
|
|---|
| 253 |
|
|---|
| 254 | case 'D':
|
|---|
| 255 | cp = MONTH;
|
|---|
| 256 | *cp++ = '/';
|
|---|
| 257 | cp = DAY;
|
|---|
| 258 | *cp++ = '/';
|
|---|
| 259 | cp = YEAR;
|
|---|
| 260 | continue;
|
|---|
| 261 |
|
|---|
| 262 | case 'H':
|
|---|
| 263 | cp = HOUR;
|
|---|
| 264 | continue;
|
|---|
| 265 |
|
|---|
| 266 | case 'M':
|
|---|
| 267 | cp = MINUTE;
|
|---|
| 268 | continue;
|
|---|
| 269 |
|
|---|
| 270 | case 'S':
|
|---|
| 271 | cp = SECOND;
|
|---|
| 272 | continue;
|
|---|
| 273 |
|
|---|
| 274 | case 'T':
|
|---|
| 275 | cp = HOUR;
|
|---|
| 276 | *cp++ = ':';
|
|---|
| 277 | cp = MINUTE;
|
|---|
| 278 | *cp++ = ':';
|
|---|
| 279 | cp = SECOND;
|
|---|
| 280 | continue;
|
|---|
| 281 |
|
|---|
| 282 | case 'j':
|
|---|
| 283 | cp = JULIAN;
|
|---|
| 284 | continue;
|
|---|
| 285 |
|
|---|
| 286 | case 'w':
|
|---|
| 287 | cp = WEEKDAY;
|
|---|
| 288 | continue;
|
|---|
| 289 |
|
|---|
| 290 | case 'r':
|
|---|
| 291 | if((h = tim->tm_hour) >= 12)
|
|---|
| 292 | hflag++;
|
|---|
| 293 |
|
|---|
| 294 | if((h %= 12) == 0)
|
|---|
| 295 | h = 12;
|
|---|
| 296 |
|
|---|
| 297 | cp = MODHOUR;
|
|---|
| 298 | *cp++ = ':';
|
|---|
| 299 | cp = MINUTE;
|
|---|
| 300 | *cp++ = ':';
|
|---|
| 301 | cp = SECOND;
|
|---|
| 302 | *cp++ = ' ';
|
|---|
| 303 |
|
|---|
| 304 | if(hflag)
|
|---|
| 305 | *cp++ = 'P';
|
|---|
| 306 | else
|
|---|
| 307 | *cp++ = 'A';
|
|---|
| 308 |
|
|---|
| 309 | *cp++ = 'M';
|
|---|
| 310 | continue;
|
|---|
| 311 |
|
|---|
| 312 | case 'h':
|
|---|
| 313 | for(i=0; i<3; i++)
|
|---|
| 314 | *cp++ = month[tim->tm_mon][i];
|
|---|
| 315 | continue;
|
|---|
| 316 |
|
|---|
| 317 | case 'a':
|
|---|
| 318 | for(i=0; i<3; i++)
|
|---|
| 319 | *cp++ = days[tim->tm_wday][i];
|
|---|
| 320 | continue;
|
|---|
| 321 |
|
|---|
| 322 | default:
|
|---|
| 323 | (void) fprintf(stderr, "date: bad format character - %c\n", *--aptr);
|
|---|
| 324 | exit(2);
|
|---|
| 325 | } /* endsw */
|
|---|
| 326 |
|
|---|
| 327 | *cp++ = c;
|
|---|
| 328 |
|
|---|
| 329 | } /* endwh */
|
|---|
| 330 |
|
|---|
| 331 | *cp = '\n';
|
|---|
| 332 | (void) write(1,buf,(cp - &buf[0]) + 1);
|
|---|
| 333 | exit(0);
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | if(*cbp == '-') {
|
|---|
| 337 |
|
|---|
| 338 | (void) fprintf(stderr,"date: no TOY clock\n");
|
|---|
| 339 | exit(2);
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | if(gtime()) {
|
|---|
| 343 |
|
|---|
| 344 | (void) fprintf(stderr,"date: bad conversion\n");
|
|---|
| 345 | exit(2);
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | /* convert to Greenwich time, on assumption of Standard time. */
|
|---|
| 349 |
|
|---|
| 350 | timbuf += timezone;
|
|---|
| 351 |
|
|---|
| 352 | /* Now fix up to local daylight time. */
|
|---|
| 353 |
|
|---|
| 354 | if (localtime(&timbuf)->tm_isdst)
|
|---|
| 355 | timbuf += -1*60*60;
|
|---|
| 356 |
|
|---|
| 357 | (void) time(&wtmp[0].ut_time);
|
|---|
| 358 |
|
|---|
| 359 | if(stime(&timbuf) < 0) {
|
|---|
| 360 |
|
|---|
| 361 | tfailed++;
|
|---|
| 362 | (void) fprintf(stderr, "date: no permission\n");
|
|---|
| 363 |
|
|---|
| 364 | } else {
|
|---|
| 365 | (void) time(&wtmp[1].ut_time);
|
|---|
| 366 |
|
|---|
| 367 | /* Attempt to write entries to the utmp file and to the wtmp file. */
|
|---|
| 368 |
|
|---|
| 369 | pututline(&wtmp[0]) ;
|
|---|
| 370 | pututline(&wtmp[1]) ;
|
|---|
| 371 |
|
|---|
| 372 | if ((wf = open(WTMP_FILE, 1)) >= 0) {
|
|---|
| 373 |
|
|---|
| 374 | (void) lseek(wf, 0L, 2);
|
|---|
| 375 | (void) write(wf, (char *)wtmp, sizeof(wtmp));
|
|---|
| 376 | }
|
|---|
| 377 | }
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | if (tfailed==0)
|
|---|
| 381 | (void) time(&timbuf);
|
|---|
| 382 |
|
|---|
| 383 | cbp = ctime(&timbuf);
|
|---|
| 384 | (void) write(1, cbp, 20);
|
|---|
| 385 | tzn = tzname[localtime(&timbuf)->tm_isdst];
|
|---|
| 386 |
|
|---|
| 387 | if (tzn)
|
|---|
| 388 | (void) write(1, tzn, 3);
|
|---|
| 389 |
|
|---|
| 390 | (void) write(1, cbp + 19, 6);
|
|---|
| 391 | exit(tfailed ? 2 : 0);
|
|---|
| 392 | }
|
|---|