home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume18 / localtime3 / part07 < prev    next >
Text File  |  1989-04-19  |  28KB  |  1,201 lines

  1. Subject:  v18i117:  Table-driven ctime/time/localtime/date package, Part07/07
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by:  ado@ncifcrf.gov
  7. Posting-number: Volume 18, Issue 117
  8. Archive-name: localtime3/part07
  9.  
  10. : To unbundle, sh this file
  11. echo file 'date.c' >&2
  12. cat >'date.c' <<'End of date.c'
  13. #ifndef lint
  14. #ifndef NOID
  15. static char    elsieid[] = "@(#)date.c    7.1";
  16. /*
  17. ** Modified from the UCB version whose SCCS ID appears below.
  18. */
  19. #endif /* !defined NOID */
  20. #endif /* !defined lint */
  21.  
  22. /*
  23.  * Copyright (c) 1985, 1987, 1988 The Regents of the University of California.
  24.  * All rights reserved.
  25.  *
  26.  * Redistribution and use in source and binary forms are permitted
  27.  * provided that the above copyright notice and this paragraph are
  28.  * duplicated in all such forms and that any documentation,
  29.  * advertising materials, and other materials related to such
  30.  * distribution and use acknowledge that the software was developed
  31.  * by the University of California, Berkeley.  The name of the
  32.  * University may not be used to endorse or promote products derived
  33.  * from this software without specific prior written permission.
  34.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  35.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  36.  * WARRANTIES OF MERCHANT[A]BILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  37.  */
  38.  
  39. #ifndef lint
  40. char copyright[] =
  41. "@(#) Copyright (c) 1985, 1987, 1988 The Regents of the University of California.\n\
  42.  All rights reserved.\n";
  43. #endif /* not lint */
  44.  
  45. #ifndef lint
  46. static char sccsid[] = "@(#)date.c    4.23 (Berkeley) 9/20/88";
  47. #endif /* not lint */
  48.  
  49. #ifndef USG
  50. #include "sys/time.h"    /* for DST_NONE */
  51. #endif /* !defined USG */
  52. #include "private.h"
  53. #include "utmp.h"    /* for OLD_TIME (or its absence) */
  54.  
  55. /*
  56. ** The two things date knows about time are. . .
  57. */
  58.  
  59. #ifndef TM_YEAR_BASE
  60. #define TM_YEAR_BASE    1900
  61. #endif /* !defined TM_YEAR_BASE */
  62.  
  63. #ifndef SECSPERMIN
  64. #define SECSPERMIN    60
  65. #endif /* !defined SECSPERMIN */
  66.  
  67. extern double        atof();
  68. extern char **        environ;
  69. extern char *        getlogin();
  70. extern int        logwtmp();
  71. extern time_t        mktime();
  72. extern char *        optarg;
  73. extern int        optind;
  74. extern char *        strchr();
  75. extern time_t        time();
  76. extern char *        tzname[2];
  77.  
  78. static int        retval = EXIT_SUCCESS;
  79.  
  80. static void        checkfinal();
  81. static int        comptm();
  82. static time_t        convert();
  83. static void        display();
  84. static void        dogmt();
  85. static void        errensure();
  86. static void        iffy();
  87. static char *        nondigit();
  88. static void        oops();
  89. static void        reset();
  90. static void        timeout();
  91. static void        usage();
  92. static void        wildinput();
  93.  
  94. int
  95. main(argc, argv)
  96. int    argc;
  97. char *    argv[];
  98. {
  99.     register char *        format;
  100.     register char *        value;
  101.     register char *        cp;
  102.     register int        ch;
  103.     register int        dousg;
  104.     register int        aflag = 0;
  105.     register int        dflag = 0;
  106.     register int        nflag = 0;
  107.     register int        tflag = 0;
  108.     register int        minuteswest;
  109.     register int        dsttime;
  110.     register float        adjust;
  111.     time_t            now;
  112.     time_t            t;
  113.  
  114.     (void) time(&now);
  115.     format = value = NULL;
  116.     while ((ch = getopt(argc, argv, "und:t:a:")) != EOF) {
  117.         switch (ch) {
  118.         default:
  119.             usage();
  120.         case 'u':        /* do it in GMT */
  121.         case 'c':
  122.             dogmt();
  123.             break;
  124.         case 'n':        /* don't set network */
  125.             nflag = 1;
  126.             break;
  127.         case 'd':        /* daylight savings time */
  128.             if (dflag) {
  129.                 (void) fprintf(stderr,
  130.                     "date: error: multiple -d's used");
  131.                 usage();
  132.             }
  133.             dflag = 1;
  134.             cp = optarg;
  135.             dsttime = atoi(cp);
  136.             if (*cp == '\0' || *nondigit(cp) != '\0')
  137.                 wildinput("-t value", optarg,
  138.                     "must be a non-negative number");
  139.             break;
  140.         case 't':        /* minutes west of GMT */
  141.             if (tflag) {
  142.                 (void) fprintf(stderr,
  143.                     "date: error: multiple -t's used");
  144.                 usage();
  145.             }
  146.             tflag = 1;
  147.             cp = optarg;
  148.             minuteswest = atoi(cp);
  149.             if (*cp == '+' || *cp == '-')
  150.                 ++cp;
  151.             if (*cp == '\0' || *nondigit(cp) != '\0')
  152.                 wildinput("-d value", optarg,
  153.                     "must be a number");
  154.             break;
  155.         case 'a':        /* adjustment */
  156.             if (aflag) {
  157.                 (void) fprintf(stderr,
  158.                     "date: error: multiple -a's used");
  159.                 usage();
  160.             }
  161.             aflag = 1;
  162.             cp = optarg;
  163.             adjust = atof(cp);
  164.             if (*cp == '+' || *cp == '-')
  165.                 ++cp;
  166.             if (*cp == '\0' || strcmp(cp, ".") == 0)
  167.                 wildinput("-a value", optarg,
  168.                     "must be a number");
  169.             cp = nondigit(cp);
  170.             if (*cp == '.')
  171.                 ++cp;
  172.             if (*nondigit(cp) != '\0')
  173.                 wildinput("-a value", optarg,
  174.                     "must be a number");
  175.             break;
  176.         }
  177.     }
  178.     while (optind < argc) {
  179.         cp = argv[optind++];
  180.         if (*cp == '+')
  181.             if (format == NULL)
  182.                 format = cp + 1;
  183.             else {
  184.                 (void) fprintf(stderr,
  185. "date: error: multiple formats in command line\n");
  186.                 usage();
  187.             }
  188.         else    if (value == NULL)
  189.                 value = cp;
  190.             else {
  191.                 (void) fprintf(stderr,
  192. "date: error: multiple values in command line\n");
  193.                 usage();
  194.             }
  195.     }
  196.     if (value != NULL) {
  197.         /*
  198.         ** This order ensures that "reasonable" twelve-digit inputs
  199.         ** (such as 120203042006) won't be misinterpreted
  200.         ** even if time_t's range all the way back to the thirteenth
  201.         ** century.  Do not change the order.
  202.         */
  203.         t = convert(value, (dousg = TRUE), now);
  204.         if (t == -1)
  205.             t = convert(value, (dousg = FALSE), now);
  206.         if (t == -1) {
  207.             /*
  208.             ** Out of range values,
  209.             ** or time that falls in a DST transition hole?
  210.             */
  211.             if ((cp = strchr(value, '.')) != NULL) {
  212.                 /*
  213.                 ** Ensure that the failure of
  214.                 **    TZ=US/Eastern date 8712312359.60
  215.                 ** doesn't get misdiagnosed.  (It was
  216.                 **    TZ=US/Eastern date 8712311859.60
  217.                 ** when the leap second was inserted.)
  218.                 ** The normal check won't work since
  219.                 ** the given time is valid in GMT.
  220.                 */
  221.                 if (atoi(cp + 1) >= SECSPERMIN)
  222.                     wildinput("time", value,
  223.                         "out of range seconds given");
  224.             }
  225.             dogmt();
  226.             t = convert(value, FALSE, now);
  227.             if (t == -1)
  228.                 t = convert(value, TRUE, now);
  229.             wildinput("time", value,
  230.                 (t == -1) ?
  231.                 "out of range value given" :
  232.                 "time skipped when clock springs forward");
  233.         }
  234.     }
  235.     /*
  236.     ** Entire command line has now been checked.
  237.     */
  238.     if (aflag) {
  239. #ifdef DST_NONE
  240.         struct timeval    tv;
  241.  
  242.         tv.tv_sec = (int) adjust;
  243.         tv.tv_usec = (int) ((adjust - tv.tv_sec) * 1000000);
  244.         if (adjtime(&tv, (struct timeval *) NULL) != 0)
  245.             oops("date: error: adjtime");
  246. #else /* !defined DST_NONE */
  247.         reset((time_t) (now + adjust), nflag);
  248. #endif /* !defined DST_NONE */
  249.         /*
  250.         ** Sun silently ignores everything else; we follow suit.
  251.         */
  252.         (void) exit(retval);
  253.     }
  254.     if (dflag || tflag) {
  255. #ifdef DST_NONE
  256.         struct timezone    tz;
  257.  
  258.         if (!dflag || !tflag)
  259.             if (gettimeofday((struct timeval *) NULL, &tz) != 0)
  260.                 oops("date: error: gettimeofday");
  261.         if (dflag)
  262.             tz.tz_dsttime = dsttime;
  263.         if (tflag)
  264.             tz.tz_minuteswest = minuteswest;
  265.         if (settimeofday((struct timeval *) NULL, &tz) != 0)
  266.             oops("date: error: settimeofday");
  267. #else /* !defined DST_NONE */
  268.         (void) fprintf(stderr,
  269. "date: warning: kernel doesn't keep -d/-t information, option ignored\n");
  270. #endif /* !defined DST_NONE */
  271.     }
  272.  
  273.     if (value == NULL)
  274.         display(format);
  275.  
  276.     reset(t, nflag);
  277.  
  278.     checkfinal(value, dousg, t, now);
  279.  
  280. #ifdef EBUG
  281.     {
  282.         struct tm    tm;
  283.  
  284.         tm = *localtime(&t);
  285.         timeout(stdout, "%c\n", &tm);
  286.         (void) exit(retval);
  287.     }
  288. #endif /* defined EBUG */
  289.  
  290.     display(format);
  291.  
  292.     /* gcc -Wall pacifier */
  293.     for ( ; ; )
  294.         ;
  295. }
  296.  
  297. static void
  298. dogmt()
  299. {
  300.     register char **    saveenv;
  301.     static char *        fakeenv[] = { "TZ=GMT0", NULL };
  302.  
  303.     saveenv = environ;
  304.     environ = fakeenv;
  305.     tzset();
  306.     environ = saveenv;
  307. }
  308.  
  309. #ifdef OLD_TIME
  310.  
  311. /*
  312. ** We assume we're on a System-V-based system,
  313. ** should use stime,
  314. ** should write System-V-format utmp entries,
  315. ** and don't have network notification to worry about.
  316. */
  317.  
  318. #include "fcntl.h"    /* for O_WRONLY, O_APPEND */
  319.  
  320. /*ARGSUSED*/
  321. static void
  322. reset(newt, nflag)
  323. time_t    newt;
  324. int    nflag;
  325. {
  326.     register int        fid;
  327.     time_t            oldt;
  328.     static struct {
  329.         struct utmp    before;
  330.         struct utmp    after;
  331.     } s;
  332.  
  333.     /*
  334.     ** Wouldn't it be great if stime returned the old time?
  335.     */
  336.     (void) time(&oldt);
  337.     if (stime(&newt) != 0)
  338.         oops("date: error: stime");
  339.     s.before.ut_type = OLD_TIME;
  340.     s.before.ut_time = oldt;
  341.     (void) strcpy(s.before.ut_line, OTIME_MSG);
  342.     s.after.ut_type = NEW_TIME;
  343.     s.after.ut_time = newt;
  344.     (void) strcpy(s.after.ut_line, NTIME_MSG);
  345.     fid = open(WTMP_FILE, O_WRONLY | O_APPEND);
  346.     if (fid < 0)
  347.         oops("date: error: log file open");
  348.     if (write(fid, (char *) &s, sizeof s) != sizeof s)
  349.         oops("date: error: log file write");
  350.     if (close(fid) != 0)
  351.         oops("date: error: log file close");
  352.     pututline(&s.before);
  353.     pututline(&s.after);
  354. }
  355.  
  356. #else /* !defined OLD_TIME */
  357.  
  358. /*
  359. ** We assume we're on a BSD-based system,
  360. ** should use settimeofday,
  361. ** should write BSD-format utmp entries (using logwtmp),
  362. ** and may get to worry about network notification.
  363. ** The "time name" changes between 4.3-tahoe and 4.4;
  364. ** we include sys/param.h to determine which we should use.
  365. */
  366.  
  367. #ifndef TIME_NAME
  368. #include "sys/param.h"
  369. #ifdef BSD4_4
  370. #define TIME_NAME    "date"
  371. #else /* !defined BSD4_4 */
  372. #define TIME_NAME    ""
  373. #endif /* !defined BSD4_4 */
  374. #endif /* !defined TIME_NAME */
  375.  
  376. #include "syslog.h"
  377. #include "sys/socket.h"
  378. #include "netinet/in.h"
  379. #include "netdb.h"
  380. #define TSPTYPES
  381. #include "protocols/timed.h"
  382.  
  383. #ifndef TSP_SETDATE
  384. /*ARGSUSED*/
  385. #endif /* !defined TSP_SETDATE */
  386. static void
  387. reset(newt, nflag)
  388. time_t    newt;
  389. int    nflag;
  390. {
  391.     register char *        username;
  392.     static struct timeval    tv;    /* static so tv_usec is 0 */
  393.  
  394. #ifdef EBUG
  395.     return;
  396. #endif /* defined EBUG */
  397.     username = getlogin();
  398.     if (username == NULL || *username == '\0') /* single-user or no tty */
  399.         username = "root";
  400.     tv.tv_sec = newt;
  401. #ifdef TSP_SETDATE
  402.     if (nflag || !netsettime(tv))
  403. #endif /* defined TSP_SETDATE */
  404.     {
  405.         /*
  406.         ** "old" entry is always written, for compatibility.
  407.         */
  408.         logwtmp("|", TIME_NAME, "");
  409.         if (settimeofday(&tv, (struct timezone *) NULL) == 0) {
  410.             logwtmp("{", TIME_NAME, "");    /* } */
  411.             syslog(LOG_AUTH | LOG_NOTICE, "date set by %s",
  412.                 username);
  413.         } else     oops("date: error: settimeofday");
  414.     }
  415. }
  416.  
  417. #endif /* !defined OLD_TIME */
  418.  
  419. static void
  420. wildinput(item, value, reason)
  421. char *    item;
  422. char *    value;
  423. char *    reason;
  424. {
  425.     (void) fprintf(stderr, "date: error: bad command line %s \"%s\", %s\n",
  426.         item, value, reason);
  427.     usage();
  428. }
  429.  
  430. static void
  431. errensure()
  432. {
  433.     if (retval == EXIT_SUCCESS)
  434.         retval = EXIT_FAILURE;
  435. }
  436.  
  437. static char *
  438. nondigit(cp)
  439. register char *    cp;
  440. {
  441.     while (isdigit(*cp))
  442.         ++cp;
  443.     return cp;
  444. }
  445.  
  446. static void
  447. usage()
  448. {
  449.     (void) fprintf(stderr, "date: usage is date ");
  450.     (void) fprintf(stderr, "[-u] ");
  451.     (void) fprintf(stderr, "[-c] ");
  452.     (void) fprintf(stderr, "[-n] ");
  453.     (void) fprintf(stderr, "[-d dst] ");
  454.     (void) fprintf(stderr, "[-t min-west] ");
  455.     (void) fprintf(stderr, "[-a sss.fff] ");
  456.     (void) fprintf(stderr, "[[yyyy]mmddhhmm[yyyy][.ss]] ");
  457.     (void) fprintf(stderr, "[+format]\n");
  458.     errensure();
  459.     (void) exit(retval);
  460. }
  461.  
  462. static void
  463. oops(string)
  464. char *    string;
  465. {
  466.     (void) perror(string);
  467.     errensure();
  468.     display((char *) NULL);
  469. }
  470.  
  471. static void
  472. display(format)
  473. char *    format;
  474. {
  475.     struct tm    tm;
  476.     time_t        now;
  477.  
  478.     (void) time(&now);
  479.     tm = *localtime(&now);
  480.     if (format == NULL) {
  481.         timeout(stdout, "%a %b ", &tm);
  482.         (void) printf("%2d ", tm.tm_mday);
  483.         timeout(stdout, "%X %Z %Y", &tm);
  484.     } else    timeout(stdout, format, &tm);
  485.     (void) putchar('\n');
  486.     (void) fflush(stdout);
  487.     (void) fflush(stderr);
  488.     if (ferror(stdout) || ferror(stderr)) {
  489.         (void) fprintf(stderr, "date: error: couldn't write results\n");
  490.         errensure();
  491.     }
  492.     (void) exit(retval);
  493. }
  494.  
  495. extern size_t    strftime();
  496.  
  497. #define INCR    1024
  498.  
  499. static void
  500. timeout(fp, format, tmp)
  501. FILE *        fp;
  502. char *        format;
  503. struct tm *    tmp;
  504. {
  505.     char *    cp;
  506.     size_t    result, size;
  507.  
  508.     if (*format == '\0')
  509.         return;
  510.     size = INCR;
  511.     cp = malloc((alloc_size_t) size);
  512.     for ( ; ; ) {
  513.         if (cp == NULL) {
  514.             (void) fprintf(stderr,
  515.                 "date: error: can't get memory\n");
  516.             errensure();
  517.             (void) exit(retval);
  518.         }
  519.         result = strftime(cp, size, format, tmp);
  520.         if (result != 0)
  521.             break;
  522.         size += INCR;
  523.         cp = realloc(cp, (alloc_size_t) size);
  524.     }
  525.     (void) fwrite(cp, 1, result, fp);
  526.     free(cp);
  527. }
  528.  
  529. static int
  530. comptm(atmp, btmp)
  531. register struct tm * atmp;
  532. register struct tm * btmp;
  533. {
  534.     register int    result;
  535.  
  536.     if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
  537.         (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
  538.         (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
  539.         (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
  540.         (result = (atmp->tm_min - btmp->tm_min)) == 0)
  541.             result = atmp->tm_sec - btmp->tm_sec;
  542.     return result;
  543. }
  544.  
  545. /*
  546. ** convert --
  547. **    convert user's input into a time_t.
  548. */
  549.  
  550. #define    ATOI2(ar)    (ar[0] - '0') * 10 + (ar[1] - '0'); ar += 2;
  551.  
  552. static time_t
  553. convert(value, dousg, t)
  554. register char *    value;
  555. int        dousg;
  556. time_t        t;
  557. {
  558.     register char *    cp;
  559.     register char *    dotp;
  560.     register int    cent, year_in_cent, month, hour, day, mins, secs;
  561.     struct tm    tm, outtm;
  562.     time_t        outt;
  563.  
  564.     tm = *localtime(&t);
  565.     cent = (tm.tm_year + TM_YEAR_BASE) / 100;
  566.     year_in_cent = (tm.tm_year + TM_YEAR_BASE) - cent * 100;
  567.     month = tm.tm_mon + 1;
  568.     day = tm.tm_mday;
  569.     hour = tm.tm_hour;
  570.     mins = tm.tm_min;
  571.     secs = 0;
  572.  
  573.     dotp = strchr(value, '.');
  574.     for (cp = value; *cp != '\0'; ++cp)
  575.         if (!isdigit(*cp) && cp != dotp)
  576.             wildinput("time", value, "contains a nondigit");
  577.  
  578.     if (dotp == NULL)
  579.         dotp = strchr(value, '\0');
  580.     else {
  581.         cp = dotp + 1;
  582.         if (strlen(cp) != 2)
  583.             wildinput("time", value,
  584.                 "seconds part is not two digits");
  585.         secs = ATOI2(cp);
  586.     }
  587.  
  588.     cp = value;
  589.     switch (dotp - cp) {
  590.         default:
  591.             wildinput("time", value, "main part is wrong length");
  592.         case 12:
  593.             if (!dousg) {
  594.                 cent = ATOI2(cp);
  595.                 year_in_cent = ATOI2(cp);
  596.             }
  597.             month = ATOI2(cp);
  598.             day = ATOI2(cp);
  599.             hour = ATOI2(cp);
  600.             mins = ATOI2(cp);
  601.             if (dousg) {
  602.                 cent = ATOI2(cp);
  603.                 year_in_cent = ATOI2(cp);
  604.             }
  605.             break;
  606.         case 8:    /* mmddhhmm */
  607.             month = ATOI2(cp);
  608.             /* fall through to. . . */
  609.         case 6:    /* ddhhmm */
  610.             day = ATOI2(cp);
  611.             /* fall through to. . . */
  612.         case 4:    /* hhmm */
  613.             hour = ATOI2(cp);
  614.             mins = ATOI2(cp);
  615.             break;
  616.         case 10:
  617.             if (!dousg) {
  618.                 year_in_cent = ATOI2(cp);
  619.             }
  620.             month = ATOI2(cp);
  621.             day = ATOI2(cp);
  622.             hour = ATOI2(cp);
  623.             mins = ATOI2(cp);
  624.             if (dousg) {
  625.                 year_in_cent = ATOI2(cp);
  626.             }
  627.             break;
  628.     }
  629.  
  630.     tm.tm_year = cent * 100 + year_in_cent - TM_YEAR_BASE;
  631.     tm.tm_mon = month - 1;
  632.     tm.tm_mday = day;
  633.     tm.tm_hour = hour;
  634.     tm.tm_min = mins;
  635.     tm.tm_sec = secs;
  636.     tm.tm_isdst = -1;
  637.     outtm = tm;
  638.     outt = mktime(&outtm);
  639.     return (comptm(&tm, &outtm) == 0) ? outt : -1;
  640. }
  641.  
  642. /*
  643. ** Code from here on out is either as provided by UCB
  644. ** or is only called just before the program exits.
  645. */
  646.  
  647. /*
  648. ** Check for iffy input.
  649. */
  650.  
  651. static void
  652. checkfinal(value, didusg, t, oldnow)
  653. char *    value;
  654. int    didusg;
  655. time_t    t;
  656. time_t    oldnow;
  657. {
  658.     time_t        othert;
  659.     struct tm    tm;
  660.     struct tm    othertm;
  661.     register int    pass;
  662.     register long    offset;
  663.  
  664.     /*
  665.     ** See if there's both a USG and a BSD interpretation.
  666.     */
  667.     othert = convert(value, !didusg, oldnow);
  668.     if (othert != -1 && othert != t)
  669.         iffy(t, othert, value, "year could be at start or end");
  670.     /*
  671.     ** See if there's both a DST and a STD version.
  672.     */
  673.     tm = *localtime(&t);
  674.     othertm = tm;
  675.     othertm.tm_isdst = !tm.tm_isdst;
  676.     othert = mktime(&othertm);
  677.     if (othert != -1 && othertm.tm_isdst != tm.tm_isdst &&
  678.         comptm(&tm, &othertm) == 0)
  679.             iffy(t, othert, value,
  680.                 "both standard and summer time versions exist");
  681. /*
  682. ** Final check.
  683. **
  684. ** If a jurisdiction shifts time *without* shifting whether time is
  685. ** summer or standard (as Hawaii, the United Kingdom, and Saudi Arabia
  686. ** have done), routine checks for iffy times may not work.
  687. ** So we perform this final check, deferring it until after the time has
  688. ** been set--it may take a while, and we don't want to introduce an unnecessary
  689. ** lag between the time the user enters their command and the time that
  690. ** stime/settimeofday is called.
  691. **
  692. ** We just check nearby times to see if any of them have the same representation
  693. ** as the time that convert returned.  We work our way out from the center
  694. ** for quick response in solar time situations.  We only handle common cases--
  695. ** offsets of at most a minute, and offsets of exact numbers of minutes
  696. ** and at most an hour.
  697. */
  698.     for (offset = 1; offset <= 60; ++offset)
  699.         for (pass = 1; pass <= 4; ++pass) {
  700.             if (pass == 1)
  701.                 othert = t + offset;
  702.             else if (pass == 2)
  703.                 othert = t - offset;
  704.             else if (pass == 3)
  705.                 othert = t + 60 * offset;
  706.             else    othert = t - 60 * offset;
  707.             othertm = *localtime(&othert);
  708.             if (comptm(&tm, &othertm) == 0)
  709.                 iffy(t, othert, value,
  710.                     "multiple matching times exist");
  711.         }
  712. }
  713.  
  714. static void
  715. iffy(thist, thatt, value, reason)
  716. time_t    thist;
  717. time_t    thatt;
  718. char *    value;
  719. char *    reason;
  720. {
  721.     struct tm    tm;
  722.  
  723.     (void) fprintf(stderr, "date: warning: ambiguous time \"%s\", %s.\n",
  724.         value, reason);
  725.     tm = *gmtime(&thist);
  726.     (void) fprintf(stderr, "Time was set as if you used\n");
  727.     /*
  728.     ** Avoid running afoul of SCCS!
  729.     */
  730.     timeout(stderr, "\tdate -u ", &tm);
  731.     timeout(stderr, "%m%d%H", &tm);
  732.     timeout(stderr, "%M", &tm);
  733.     timeout(stderr, "%Y.%S\n", &tm);
  734.     tm = *localtime(&thist);
  735.     timeout(stderr, "to get %c", &tm);
  736.     (void) fprintf(stderr, " (%s time)",
  737.         tm.tm_isdst ? "summer" : "standard");
  738.     (void) fprintf(stderr, ".  Use\n");
  739.     tm = *gmtime(&thatt);
  740.     timeout(stderr, "\tdate -u ", &tm);
  741.     timeout(stderr, "%m%d%H", &tm);
  742.     timeout(stderr, "%M", &tm);
  743.     timeout(stderr, "%Y.%S\n", &tm);
  744.     tm = *localtime(&thatt);
  745.     timeout(stderr, "to get %c", &tm);
  746.     (void) fprintf(stderr, " (%s time)",
  747.         tm.tm_isdst ? "summer" : "standard");
  748.     (void) fprintf(stderr, ".\n");
  749.     errensure();
  750.     (void) exit(retval);
  751. }
  752.  
  753. #ifdef TSP_SETDATE
  754. #define    WAITACK        2    /* seconds */
  755. #define    WAITDATEACK    5    /* seconds */
  756.  
  757. extern    int errno;
  758. /*
  759.  * Set the date in the machines controlled by timedaemons
  760.  * by communicating the new date to the local timedaemon. 
  761.  * If the timedaemon is in the master state, it performs the
  762.  * correction on all slaves.  If it is in the slave state, it
  763.  * notifies the master that a correction is needed.
  764.  * Returns 1 on success, 0 on failure.
  765.  */
  766. netsettime(ntv)
  767.     struct timeval ntv;
  768. {
  769.     int s, length, port, timed_ack, found, err;
  770.     long waittime;
  771.     fd_set ready;
  772.     char hostname[MAXHOSTNAMELEN];
  773.     struct timeval tout;
  774.     struct servent *sp;
  775.     struct tsp msg;
  776.     struct sockaddr_in sin, dest, from;
  777.  
  778.     sp = getservbyname("timed", "udp");
  779.     if (sp == 0) {
  780.         fputs("udp/timed: unknown service\n", stderr);
  781.         retval = 2;
  782.         return (0);
  783.     }
  784.     dest.sin_port = sp->s_port;
  785.     dest.sin_family = AF_INET;
  786.     dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
  787.     s = socket(AF_INET, SOCK_DGRAM, 0);
  788.     if (s < 0) {
  789.         if (errno != EPROTONOSUPPORT)
  790.             perror("date: socket");
  791.         goto bad;
  792.     }
  793.     bzero((char *)&sin, sizeof (sin));
  794.     sin.sin_family = AF_INET;
  795.     for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
  796.         sin.sin_port = htons((u_short)port);
  797.         if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
  798.             break;
  799.         if (errno != EADDRINUSE) {
  800.             if (errno != EADDRNOTAVAIL)
  801.                 perror("date: bind");
  802.             goto bad;
  803.         }
  804.     }
  805.     if (port == IPPORT_RESERVED / 2) {
  806.         fputs("date: All ports in use\n", stderr);
  807.         goto bad;
  808.     }
  809.     msg.tsp_type = TSP_SETDATE;
  810.     msg.tsp_vers = TSPVERSION;
  811.     if (gethostname(hostname, sizeof (hostname))) {
  812.         perror("gethostname");
  813.         goto bad;
  814.     }
  815.     (void) strncpy(msg.tsp_name, hostname, sizeof (hostname));
  816.     msg.tsp_seq = htons((u_short)0);
  817.     msg.tsp_time.tv_sec = htonl((u_long)ntv.tv_sec);
  818.     msg.tsp_time.tv_usec = htonl((u_long)ntv.tv_usec);
  819.     length = sizeof (struct sockaddr_in);
  820.     if (connect(s, &dest, length) < 0) {
  821.         perror("date: connect");
  822.         goto bad;
  823.     }
  824.     if (send(s, (char *)&msg, sizeof (struct tsp), 0) < 0) {
  825.         if (errno != ECONNREFUSED)
  826.             perror("date: send");
  827.         goto bad;
  828.     }
  829.     timed_ack = -1;
  830.     waittime = WAITACK;
  831. loop:
  832.     tout.tv_sec = waittime;
  833.     tout.tv_usec = 0;
  834.     FD_ZERO(&ready);
  835.     FD_SET(s, &ready);
  836.     found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
  837.     length = sizeof(err);
  838.     if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *)&err, &length) == 0
  839.         && err) {
  840.         errno = err;
  841.         if (errno != ECONNREFUSED)
  842.             perror("date: send (delayed error)");
  843.         goto bad;
  844.     }
  845.     if (found > 0 && FD_ISSET(s, &ready)) {
  846.         length = sizeof (struct sockaddr_in);
  847.         if (recvfrom(s, (char *)&msg, sizeof (struct tsp), 0, &from,
  848.             &length) < 0) {
  849.             if (errno != ECONNREFUSED)
  850.                 perror("date: recvfrom");
  851.             goto bad;
  852.         }
  853.         msg.tsp_seq = ntohs(msg.tsp_seq);
  854.         msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
  855.         msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
  856.         switch (msg.tsp_type) {
  857.  
  858.         case TSP_ACK:
  859.             timed_ack = TSP_ACK;
  860.             waittime = WAITDATEACK;
  861.             goto loop;
  862.  
  863.         case TSP_DATEACK:
  864.             (void)close(s);
  865.             return (1);
  866.  
  867.         default:
  868.             fprintf(stderr,
  869.                 "date: Wrong ack received from timed: %s\n", 
  870.                 tsptype[msg.tsp_type]);
  871.             timed_ack = -1;
  872.             break;
  873.         }
  874.     }
  875.     if (timed_ack == -1)
  876.         fputs("date: Can't reach time daemon, time set locally.\n",
  877.             stderr);
  878. bad:
  879.     (void)close(s);
  880.     retval = 2;
  881.     return (0);
  882. }
  883. #endif /* defined TSP_SETDATE */
  884. End of date.c
  885. echo file 'logwtmp.c' >&2
  886. cat >'logwtmp.c' <<'End of logwtmp.c'
  887. #ifndef lint
  888. #ifndef NOID
  889. static char    elsieid[] = "@(#)logwtmp.c    7.1";
  890. /* As received from UCB, with include file reordering and OLD_TIME condition. */
  891. #endif /* !defined NOID */
  892. #endif /* !defined lint */
  893.  
  894. /*
  895.  * Copyright (c) 1988 The Regents of the University of California.
  896.  * All rights reserved.
  897.  *
  898.  * Redistribution and use in source and binary forms are permitted
  899.  * provided that the above copyright notice and this paragraph are
  900.  * duplicated in all such forms and that any documentation,
  901.  * advertising materials, and other materials related to such
  902.  * distribution and use acknowledge that the software was developed
  903.  * by the University of California, Berkeley.  The name of the
  904.  * University may not be used to endorse or promote products derived
  905.  * from this software without specific prior written permission.
  906.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  907.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  908.  * WARRANTIES OF MERCHANT[A]BILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  909.  */
  910.  
  911. #ifndef lint
  912. #ifdef LIBC_SCCS
  913. static char sccsid[] = "@(#)logwtmp.c    5.2 (Berkeley) 9/20/88";
  914. #endif /* defined LIBC_SCCS */
  915. #endif /* !defined lint */
  916.  
  917. #include <sys/types.h>
  918. #include <utmp.h>
  919.  
  920. #ifndef OLD_TIME
  921.  
  922. #include <sys/file.h>
  923. #include <sys/time.h>
  924. #include <sys/stat.h>
  925.  
  926. #define    WTMPFILE    "/usr/adm/wtmp"
  927.  
  928. logwtmp(line, name, host)
  929.     char *line, *name, *host;
  930. {
  931.     struct utmp ut;
  932.     struct stat buf;
  933.     int fd;
  934.     time_t time();
  935.     char *strncpy();
  936.  
  937.     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
  938.         return;
  939.     if (!fstat(fd, &buf)) {
  940.         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
  941.         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
  942.         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
  943.         (void)time(&ut.ut_time);
  944.         if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
  945.             sizeof(struct utmp))
  946.             (void)ftruncate(fd, buf.st_size);
  947.     }
  948.     (void)close(fd);
  949. }
  950.  
  951. #endif /* !defined OLD_TIME */
  952. End of logwtmp.c
  953. echo file 'strftime.c' >&2
  954. cat >'strftime.c' <<'End of strftime.c'
  955. #ifndef lint
  956. #ifndef NOID
  957. static char    elsieid[] = "@(#)strftime.c    7.1";
  958. /*
  959. ** Based on the UCB version whose ID appears below.
  960. ** This is ANSIish only when time is treated identically in all locales and
  961. ** when "multibyte character == plain character".
  962. */
  963. #endif /* !defined NOID */
  964. #endif /* !defined lint */
  965. /*
  966.  * Copyright (c) 1989 The Regents of the University of California.
  967.  * All rights reserved.
  968.  *
  969.  * Redistribution and use in source and binary forms are permitted
  970.  * provided that the above copyright notice and this paragraph are
  971.  * duplicated in all such forms and that any documentation,
  972.  * advertising materials, and other materials related to such
  973.  * distribution and use acknowledge that the software was developed
  974.  * by the University of California, Berkeley.  The name of the
  975.  * University may not be used to endorse or promote products derived
  976.  * from this software without specific prior written permission.
  977.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  978.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  979.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  980.  */
  981.  
  982. #if defined(LIBC_SCCS) && !defined(lint)
  983. static char sccsid[] = "@(#)strftime.c    5.4 (Berkeley) 3/14/89";
  984. #endif /* LIBC_SCCS and not lint */
  985.  
  986. #include "sys/types.h"
  987. #include "time.h"
  988. #include "tzfile.h"
  989.  
  990. static char *afmt[] = {
  991.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  992. };
  993. static char *Afmt[] = {
  994.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  995.     "Saturday",
  996. };
  997. static char *bfmt[] = {
  998.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  999.     "Oct", "Nov", "Dec",
  1000. };
  1001. static char *Bfmt[] = {
  1002.     "January", "February", "March", "April", "May", "June", "July",
  1003.     "August", "September", "October", "November", "December",
  1004. };
  1005.  
  1006. static size_t gsize;
  1007. static char *pt;
  1008.  
  1009. static void _add();
  1010. static void _conv();
  1011. static void _fmt();
  1012.  
  1013. size_t
  1014. strftime(s, maxsize, format, t)
  1015.     char *s;
  1016.     char *format;
  1017.     size_t maxsize;
  1018.     struct tm *t;
  1019. {
  1020.     pt = s;
  1021.     gsize = maxsize;
  1022.     _fmt(format, t);
  1023.     if (gsize <= 0)
  1024.         return(0);
  1025.     *pt = '\0';
  1026.     return(maxsize - gsize);
  1027. }
  1028.  
  1029. static void
  1030. _fmt(format, t)
  1031.     register char *format;
  1032.     struct tm *t;
  1033. {
  1034.     for (; *format; ++format) {
  1035.         if (*format == '%')
  1036.             switch(*++format) {
  1037.             case '\0':
  1038.                 --format;
  1039.                 break;
  1040.             case 'A':
  1041.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  1042.                     _add("?");
  1043.                 else    _add(Afmt[t->tm_wday]);
  1044.                 continue;
  1045.             case 'a':
  1046.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  1047.                     _add("?");
  1048.                 else    _add(afmt[t->tm_wday]);
  1049.                 continue;
  1050.             case 'B':
  1051.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  1052.                     _add("?");
  1053.                 else    _add(Bfmt[t->tm_mon]);
  1054.                 continue;
  1055.             case 'b':
  1056.             case 'h':
  1057.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  1058.                     _add("?");
  1059.                 else    _add(bfmt[t->tm_mon]);
  1060.                 continue;
  1061.             case 'c':
  1062.                 _fmt("%D %X", t);
  1063.                 continue;
  1064.             case 'C':
  1065.                 _fmt("%a %b %e %X %Y", t);
  1066.                 continue;
  1067.             case 'D':
  1068.             case 'x':
  1069.                 _fmt("%m/%d/%y", t);
  1070.                 continue;
  1071.             case 'd':
  1072.                 _conv(t->tm_mday, 2, '0');
  1073.                 continue;
  1074.             case 'e':
  1075.                 _conv(t->tm_mday, 2, ' ');
  1076.                 continue;
  1077.             case 'H':
  1078.                 _conv(t->tm_hour, 2, '0');
  1079.                 continue;
  1080.             case 'I':
  1081.                 _conv(t->tm_hour % 12 ?
  1082.                     t->tm_hour % 12 : 12, 2, '0');
  1083.                 continue;
  1084.             case 'j':
  1085.                 _conv(t->tm_yday + 1, 3, '0');
  1086.                 continue;
  1087.             case 'k':
  1088.                 _conv(t->tm_hour % 12 ?
  1089.                     t->tm_hour % 12 : 12, 2, ' ');
  1090.                 continue;
  1091. #ifdef KITCHEN_SINK
  1092.             case 'K':
  1093.                 _add("kitchen sink");
  1094.                 continue;
  1095. #endif /* defined KITCHEN_SINK */
  1096.             case 'l':
  1097.                 _conv(t->tm_hour, 2, ' ');
  1098.                 continue;
  1099.             case 'M':
  1100.                 _conv(t->tm_min, 2, '0');
  1101.                 continue;
  1102.             case 'm':
  1103.                 _conv(t->tm_mon + 1, 2, '0');
  1104.                 continue;
  1105.             case 'n':
  1106.                 _add("\n");
  1107.                 continue;
  1108.             case 'p':
  1109.                 _add(t->tm_hour >= 12 ? "PM" : "AM");
  1110.                 continue;
  1111.             case 'R':
  1112.                 _fmt("%H:%M", t);
  1113.                 continue;
  1114.             case 'r':
  1115.                 _fmt("%I:%M:%S %p", t);
  1116.                 continue;
  1117.             case 'S':
  1118.                 _conv(t->tm_sec, 2, '0');
  1119.                 continue;
  1120.             case 'T':
  1121.             case 'X':
  1122.                 _fmt("%H:%M:%S", t);
  1123.                 continue;
  1124.             case 't':
  1125.                 _add("\t");
  1126.                 continue;
  1127.             case 'U':
  1128.                 _conv((t->tm_yday + 7 - t->tm_wday) / 7,
  1129.                     2, '0');
  1130.                 continue;
  1131.             case 'W':
  1132.                 _conv((t->tm_yday + 7 -
  1133.                     (t->tm_wday ? (t->tm_wday - 1) : 6))
  1134.                     / 7, 2, '0');
  1135.                 continue;
  1136.             case 'w':
  1137.                 _conv(t->tm_wday, 1, '0');
  1138.                 continue;
  1139.             case 'y':
  1140.                 _conv((t->tm_year + TM_YEAR_BASE) % 100,
  1141.                     2, '0');
  1142.                 continue;
  1143.             case 'Y':
  1144.                 _conv(t->tm_year + TM_YEAR_BASE, 4, '0');
  1145.                 continue;
  1146.             case 'Z':
  1147. #ifdef TM_ZONE
  1148.                 if (t->TM_ZONE)
  1149.                     _add(t->TM_ZONE);
  1150.                 else
  1151. #endif /* defined TM_ZONE */
  1152.                 if (t->tm_isdst == 0 || t->tm_isdst == 1) {
  1153.                     extern char *    tzname[2];
  1154.  
  1155.                     _add(tzname[t->tm_isdst]);
  1156.                 } else    _add("?");
  1157.                 continue;
  1158.             case '%':
  1159.             /*
  1160.              * X311J/88-090 (4.12.3.5): if conversion char is
  1161.              * undefined, behavior is undefined.  Print out the
  1162.              * character itself as printf(3) also does.
  1163.              */
  1164.             default:
  1165.                 break;
  1166.         }
  1167.         if (gsize <= 0)
  1168.             return;
  1169.         *pt++ = *format;
  1170.         --gsize;
  1171.     }
  1172. }
  1173.  
  1174. static void
  1175. _conv(n, digits, fill)
  1176.     int n, digits, fill;
  1177. {
  1178.     static char buf[10];
  1179.     register char *p;
  1180.  
  1181.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  1182.         *p-- = n % 10 + '0';
  1183.     while (p > buf && digits-- > 0)
  1184.         *p-- = fill;
  1185.     _add(++p);
  1186. }
  1187.  
  1188. static void
  1189. _add(str)
  1190.     register char *str;
  1191. {
  1192.     for (;; ++pt, --gsize) {
  1193.         if (gsize <= 0)
  1194.             return;
  1195.         if (!(*pt = *str++))
  1196.             return;
  1197.     }
  1198. }
  1199. End of strftime.c
  1200. exit
  1201.