Files
amiga-tz/date.c
T

955 lines
22 KiB
C
Raw Normal View History

1989-03-05 17:32:10 -05:00
#ifndef lint
#ifndef NOID
static char elsieid[] = "%W%";
/*
1993-11-22 12:41:31 -05:00
** Modified from the UCB version with the SCCS ID appearing below.
1989-03-05 17:32:10 -05:00
*/
#endif /* !defined NOID */
#endif /* !defined lint */
1989-03-05 17:23:15 -05:00
/*
* Copyright (c) 1985, 1987, 1988 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1989-03-22 20:08:39 -05:00
* WARRANTIES OF MERCHANT[A]BILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1989-03-05 17:23:15 -05:00
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1985, 1987, 1988 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)date.c 4.23 (Berkeley) 9/20/88";
#endif /* not lint */
1989-03-21 11:37:12 -05:00
#include "private.h"
1995-01-03 11:08:21 -05:00
#if HAVE_ADJTIME || HAVE_SETTIMEOFDAY
#include "sys/time.h" /* for struct timeval, struct timezone */
#endif /* HAVE_ADJTIME || HAVE_SETTIMEOFDAY */
#include "locale.h"
#include "utmp.h" /* for OLD_TIME (or its absence) */
1996-01-10 16:44:46 -05:00
#if HAVE_UTMPX_H
#include "utmpx.h"
#endif
1989-03-05 17:23:15 -05:00
2001-10-09 13:31:19 -04:00
#ifndef OTIME_MSG
#define OTIME_MSG "old time"
#endif
#ifndef NTIME_MSG
#define NTIME_MSG "new time"
#endif
1989-03-22 20:08:39 -05:00
/*
** The two things date knows about time are. . .
*/
#ifndef TM_YEAR_BASE
#define TM_YEAR_BASE 1900
#endif /* !defined TM_YEAR_BASE */
#ifndef SECSPERMIN
#define SECSPERMIN 60
#endif /* !defined SECSPERMIN */
1989-03-11 11:56:53 -05:00
extern double atof();
1989-03-07 21:51:45 -05:00
extern char ** environ;
extern char * getlogin();
1989-03-07 22:57:41 -05:00
extern time_t mktime();
1989-03-07 21:51:45 -05:00
extern char * optarg;
1989-03-07 19:58:19 -05:00
extern int optind;
1989-03-10 23:29:33 -05:00
extern char * strchr();
1989-03-07 21:51:45 -05:00
extern time_t time();
extern char * tzname[2];
1989-03-05 18:27:10 -05:00
1989-03-07 21:51:45 -05:00
static int retval = EXIT_SUCCESS;
1989-03-07 19:58:19 -05:00
1994-06-04 17:05:46 -04:00
static void checkfinal P((const char *, int, time_t, time_t));
static int comptm P((const struct tm *, const struct tm *));
static time_t convert P((const char *, int, time_t));
static void display P((const char *));
static void dogmt P((void));
static void errensure P((void));
static void iffy P((time_t, time_t, const char *, const char *));
int main P((int, char**));
static const char * nondigit P((const char *));
static void oops P((const char *));
static void reset P((time_t, int));
static void timeout P((FILE *, const char *, const struct tm *));
static void usage P((void));
1995-01-03 11:33:56 -05:00
static void wildinput P((const char *, const char *,
const char *));
1989-03-07 19:58:19 -05:00
1989-03-06 04:26:22 -05:00
int
main(argc, argv)
1994-06-04 17:05:46 -04:00
const int argc;
char * argv[];
1989-03-06 04:26:22 -05:00
{
1994-06-04 17:05:46 -04:00
register const char * format;
register const char * value;
register const char * cp;
1989-03-07 21:51:45 -05:00
register int ch;
1989-03-11 11:56:53 -05:00
register int dousg;
register int aflag = 0;
register int dflag = 0;
register int nflag = 0;
register int tflag = 0;
register int minuteswest;
register int dsttime;
1995-01-02 12:13:13 -05:00
register double adjust;
1989-03-10 23:29:33 -05:00
time_t now;
1989-03-07 21:51:45 -05:00
time_t t;
1989-03-06 04:26:22 -05:00
1994-06-04 17:05:46 -04:00
INITIALIZE(dousg);
INITIALIZE(minuteswest);
INITIALIZE(dsttime);
INITIALIZE(adjust);
INITIALIZE(t);
2004-11-08 10:19:01 -05:00
#ifdef LC_ALL
(void) setlocale(LC_ALL, "");
2004-11-08 10:48:47 -05:00
#endif /* defined(LC_ALL) */
2004-11-08 10:19:01 -05:00
#if HAVE_GETTEXT
1996-02-21 15:47:21 -05:00
#ifdef TZ_DOMAINDIR
(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
#endif /* defined(TEXTDOMAINDIR) */
(void) textdomain(TZ_DOMAIN);
2004-11-08 10:19:01 -05:00
#endif /* HAVE_GETTEXT */
1989-03-07 14:14:29 -05:00
(void) time(&now);
1989-03-06 05:10:58 -05:00
format = value = NULL;
1997-01-20 19:02:23 -05:00
while ((ch = getopt(argc, argv, "ucnd:t:a:")) != EOF && ch != -1) {
1989-03-06 04:26:22 -05:00
switch (ch) {
1989-03-07 21:51:45 -05:00
default:
usage();
1997-12-29 09:31:36 -05:00
case 'u': /* do it in UTC */
1989-04-11 10:25:52 -04:00
case 'c':
1989-03-11 11:56:53 -05:00
dogmt();
1989-03-07 21:51:45 -05:00
break;
case 'n': /* don't set network */
nflag = 1;
break;
2001-03-13 12:32:17 -05:00
case 'd': /* daylight saving time */
1989-03-07 21:51:45 -05:00
if (dflag) {
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: error: multiple -d's used"));
1989-03-07 21:51:45 -05:00
usage();
}
dflag = 1;
1989-03-10 22:05:54 -05:00
cp = optarg;
1989-03-11 11:56:53 -05:00
dsttime = atoi(cp);
if (*cp == '\0' || *nondigit(cp) != '\0')
1996-02-21 15:47:21 -05:00
wildinput(_("-t value"), optarg,
_("must be a non-negative number"));
1989-03-07 21:51:45 -05:00
break;
1997-12-29 09:31:36 -05:00
case 't': /* minutes west of UTC */
1989-03-07 21:51:45 -05:00
if (tflag) {
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: error: multiple -t's used"));
1989-03-07 21:51:45 -05:00
usage();
}
tflag = 1;
1989-03-10 22:05:54 -05:00
cp = optarg;
1989-03-11 11:56:53 -05:00
minuteswest = atoi(cp);
1989-03-10 22:05:54 -05:00
if (*cp == '+' || *cp == '-')
++cp;
if (*cp == '\0' || *nondigit(cp) != '\0')
1996-02-21 15:47:21 -05:00
wildinput(_("-d value"), optarg,
_("must be a number"));
1989-03-07 21:51:45 -05:00
break;
1989-03-09 13:20:49 -05:00
case 'a': /* adjustment */
if (aflag) {
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: error: multiple -a's used"));
1989-03-09 13:20:49 -05:00
usage();
}
aflag = 1;
1989-03-10 22:05:54 -05:00
cp = optarg;
1989-03-11 11:56:53 -05:00
adjust = atof(cp);
if (*cp == '+' || *cp == '-')
++cp;
if (*cp == '\0' || strcmp(cp, ".") == 0)
1996-02-21 15:47:21 -05:00
wildinput(_("-a value"), optarg,
_("must be a number"));
1989-03-11 11:56:53 -05:00
cp = nondigit(cp);
if (*cp == '.')
++cp;
if (*nondigit(cp) != '\0')
1996-02-21 15:47:21 -05:00
wildinput(_("-a value"), optarg,
_("must be a number"));
1989-03-09 13:20:49 -05:00
break;
1989-03-06 18:59:18 -05:00
}
1989-03-07 21:51:45 -05:00
}
1989-03-06 18:59:18 -05:00
while (optind < argc) {
cp = argv[optind++];
if (*cp == '+')
if (format == NULL)
format = cp + 1;
1989-03-07 21:51:45 -05:00
else {
1989-03-30 22:02:45 -05:00
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: error: multiple formats in command line\n"));
1989-03-07 21:51:45 -05:00
usage();
}
1989-03-06 18:59:18 -05:00
else if (value == NULL)
value = cp;
1989-03-07 21:51:45 -05:00
else {
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: error: multiple values in command line\n"));
1989-03-07 21:51:45 -05:00
usage();
}
1989-03-05 17:23:15 -05:00
}
1989-03-07 21:51:45 -05:00
if (value != NULL) {
1989-04-05 03:41:05 -04:00
/*
** This order ensures that "reasonable" twelve-digit inputs
** (such as 120203042006) won't be misinterpreted
** even if time_t's range all the way back to the thirteenth
** century. Do not change the order.
*/
t = convert(value, (dousg = TRUE), now);
1989-03-07 21:51:45 -05:00
if (t == -1)
1989-04-05 03:41:05 -04:00
t = convert(value, (dousg = FALSE), now);
1989-03-10 22:58:42 -05:00
if (t == -1) {
/*
1989-03-11 11:56:53 -05:00
** Out of range values,
** or time that falls in a DST transition hole?
1989-03-10 22:58:42 -05:00
*/
1989-03-11 21:07:04 -05:00
if ((cp = strchr(value, '.')) != NULL) {
/*
** Ensure that the failure of
1995-10-30 09:54:02 -05:00
** TZ=America/New_York date 8712312359.60
1989-03-11 21:07:04 -05:00
** doesn't get misdiagnosed. (It was
1995-10-30 09:54:02 -05:00
** TZ=America/New_York date 8712311859.60
1989-03-11 21:07:04 -05:00
** when the leap second was inserted.)
** The normal check won't work since
1997-12-29 09:31:36 -05:00
** the given time is valid in UTC.
1989-03-11 21:07:04 -05:00
*/
1989-03-22 20:08:39 -05:00
if (atoi(cp + 1) >= SECSPERMIN)
1996-02-21 15:47:21 -05:00
wildinput(_("time"), value,
_("out of range seconds given"));
1989-03-11 21:07:04 -05:00
}
1989-03-11 11:56:53 -05:00
dogmt();
t = convert(value, FALSE, now);
if (t == -1)
t = convert(value, TRUE, now);
1996-02-21 15:47:21 -05:00
wildinput(_("time"), value,
1989-03-11 11:56:53 -05:00
(t == -1) ?
1996-02-21 15:47:21 -05:00
_("out of range value given") :
_("time skipped when clock springs forward"));
1989-03-10 22:58:42 -05:00
}
1989-03-07 21:51:45 -05:00
}
/*
** Entire command line has now been checked.
*/
1989-03-10 20:45:11 -05:00
if (aflag) {
1995-01-03 11:08:21 -05:00
#if HAVE_ADJTIME
1989-03-11 11:56:53 -05:00
struct timeval tv;
tv.tv_sec = (int) adjust;
tv.tv_usec = (int) ((adjust - tv.tv_sec) * 1000000);
if (adjtime(&tv, (struct timeval *) NULL) != 0)
1996-02-21 15:47:21 -05:00
oops("adjtime");
1995-01-03 11:12:36 -05:00
#endif /* HAVE_ADJTIME */
#if !HAVE_ADJTIME
1989-03-11 11:56:53 -05:00
reset((time_t) (now + adjust), nflag);
1995-01-03 11:18:56 -05:00
#endif /* !HAVE_ADJTIME */
1989-03-10 20:45:11 -05:00
/*
** Sun silently ignores everything else; we follow suit.
*/
(void) exit(retval);
}
if (dflag || tflag) {
1995-01-03 11:08:21 -05:00
#if HAVE_SETTIMEOFDAY == 2
1989-03-11 11:56:53 -05:00
struct timezone tz;
if (!dflag || !tflag)
1989-03-11 11:56:53 -05:00
if (gettimeofday((struct timeval *) NULL, &tz) != 0)
1996-02-21 15:47:21 -05:00
oops("gettimeofday");
if (dflag)
1989-03-11 11:56:53 -05:00
tz.tz_dsttime = dsttime;
if (tflag)
1989-03-11 11:56:53 -05:00
tz.tz_minuteswest = minuteswest;
if (settimeofday((struct timeval *) NULL, &tz) != 0)
1996-02-21 15:47:21 -05:00
oops("settimeofday");
1995-01-03 11:12:36 -05:00
#endif /* HAVE_SETTIMEOFDAY == 2 */
#if HAVE_SETTIMEOFDAY != 2
1989-03-11 11:56:53 -05:00
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: warning: kernel doesn't keep -d/-t information, option ignored\n"));
1995-01-03 11:08:21 -05:00
#endif /* HAVE_SETTIMEOFDAY != 2 */
}
1989-03-11 11:56:53 -05:00
1989-03-06 18:59:18 -05:00
if (value == NULL)
display(format);
1989-03-11 11:56:53 -05:00
reset(t, nflag);
checkfinal(value, dousg, t, now);
1989-03-11 21:07:04 -05:00
#ifdef EBUG
{
struct tm tm;
tm = *localtime(&t);
timeout(stdout, "%c\n", &tm);
(void) exit(retval);
}
#endif /* defined EBUG */
1989-03-11 11:56:53 -05:00
display(format);
1989-03-11 21:07:04 -05:00
1989-03-11 11:56:53 -05:00
/* gcc -Wall pacifier */
for ( ; ; )
1994-04-09 17:19:09 -04:00
continue;
1989-03-11 11:56:53 -05:00
}
static void
1994-12-08 23:48:04 -05:00
dogmt()
1989-03-11 11:56:53 -05:00
{
1994-12-08 23:48:04 -05:00
static char ** fakeenv;
1989-03-11 11:56:53 -05:00
1994-12-08 23:48:04 -05:00
if (fakeenv == NULL) {
1995-01-06 16:04:10 -05:00
register int from;
register int to;
register int n;
static char tzegmt0[] = "TZ=GMT0";
1994-12-08 23:48:04 -05:00
for (n = 0; environ[n] != NULL; ++n)
continue;
1995-01-06 17:11:11 -05:00
fakeenv = (char **) malloc((size_t) (n + 2) * sizeof *fakeenv);
1994-12-08 23:48:04 -05:00
if (fakeenv == NULL) {
1996-02-21 15:47:21 -05:00
(void) perror(_("Memory exhausted"));
1994-12-08 23:48:04 -05:00
errensure();
(void) exit(retval);
}
to = 0;
1995-01-06 16:04:10 -05:00
fakeenv[to++] = tzegmt0;
1994-12-08 23:48:04 -05:00
for (from = 1; environ[from] != NULL; ++from)
if (strncmp(environ[from], "TZ=", 3) != 0)
fakeenv[to++] = environ[from];
fakeenv[to] = NULL;
environ = fakeenv;
}
1989-03-11 11:56:53 -05:00
}
1989-03-22 21:06:43 -05:00
#ifdef OLD_TIME
/*
** We assume we're on a System-V-based system,
** should use stime,
** should write System-V-format utmp entries,
** and don't have network notification to worry about.
*/
1989-04-10 20:23:10 -04:00
#include "fcntl.h" /* for O_WRONLY, O_APPEND */
1989-03-22 21:06:43 -05:00
/*ARGSUSED*/
static void
reset(newt, nflag)
1994-06-04 17:05:46 -04:00
const time_t newt;
const int nflag;
1989-03-22 21:06:43 -05:00
{
register int fid;
time_t oldt;
static struct {
struct utmp before;
struct utmp after;
} s;
1996-01-10 16:44:46 -05:00
#if HAVE_UTMPX_H
static struct {
struct utmpx before;
struct utmpx after;
} sx;
#endif
1989-03-22 21:06:43 -05:00
/*
** Wouldn't it be great if stime returned the old time?
*/
(void) time(&oldt);
if (stime(&newt) != 0)
1996-02-21 15:47:21 -05:00
oops("stime");
1989-03-22 21:06:43 -05:00
s.before.ut_type = OLD_TIME;
s.before.ut_time = oldt;
(void) strcpy(s.before.ut_line, OTIME_MSG);
s.after.ut_type = NEW_TIME;
s.after.ut_time = newt;
(void) strcpy(s.after.ut_line, NTIME_MSG);
fid = open(WTMP_FILE, O_WRONLY | O_APPEND);
if (fid < 0)
1996-02-21 15:47:21 -05:00
oops(_("log file open"));
1989-03-22 21:06:43 -05:00
if (write(fid, (char *) &s, sizeof s) != sizeof s)
1996-02-21 15:47:21 -05:00
oops(_("log file write"));
1989-03-22 21:06:43 -05:00
if (close(fid) != 0)
1996-02-21 15:47:21 -05:00
oops(_("log file close"));
1996-01-10 16:44:46 -05:00
#if !HAVE_UTMPX_H
1989-03-22 21:06:43 -05:00
pututline(&s.before);
pututline(&s.after);
1996-01-10 16:44:46 -05:00
#endif /* !HAVE_UTMPX_H */
#if HAVE_UTMPX_H
sx.before.ut_type = OLD_TIME;
sx.before.ut_tv.tv_sec = oldt;
(void) strcpy(sx.before.ut_line, OTIME_MSG);
sx.after.ut_type = NEW_TIME;
sx.after.ut_tv.tv_sec = newt;
(void) strcpy(sx.after.ut_line, NTIME_MSG);
#if !SUPPRESS_WTMPX_FILE_UPDATE
/* In Solaris 2.5 (and presumably other systems),
`date' does not update /var/adm/wtmpx.
This must be a bug. If you'd like to reproduce the bug,
define SUPPRESS_WTMPX_FILE_UPDATE to be nonzero. */
fid = open(WTMPX_FILE, O_WRONLY | O_APPEND);
if (fid < 0)
1996-02-21 15:47:21 -05:00
oops(_("log file open"));
1996-01-10 16:44:46 -05:00
if (write(fid, (char *) &sx, sizeof sx) != sizeof sx)
1996-02-21 15:47:21 -05:00
oops(_("log file write"));
1996-01-10 16:44:46 -05:00
if (close(fid) != 0)
1996-02-21 15:47:21 -05:00
oops(_("log file close"));
1996-01-10 16:44:46 -05:00
#endif /* !SUPPRESS_WTMPX_FILE_UPDATE */
pututxline(&sx.before);
pututxline(&sx.after);
#endif /* HAVE_UTMPX_H */
1989-03-22 21:06:43 -05:00
}
1993-11-22 12:58:06 -05:00
#endif /* defined OLD_TIME */
#ifndef OLD_TIME
1989-03-22 21:06:43 -05:00
/*
** We assume we're on a BSD-based system,
** should use settimeofday,
** should write BSD-format utmp entries (using logwtmp),
** and may get to worry about network notification.
** The "time name" changes between 4.3-tahoe and 4.4;
** we include sys/param.h to determine which we should use.
*/
#ifndef TIME_NAME
#include "sys/param.h"
#ifdef BSD4_4
#define TIME_NAME "date"
1993-11-22 12:58:06 -05:00
#endif /* defined BSD4_4 */
#ifndef BSD4_4
1989-03-22 21:06:43 -05:00
#define TIME_NAME ""
#endif /* !defined BSD4_4 */
#endif /* !defined TIME_NAME */
#include "syslog.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "netdb.h"
#define TSPTYPES
#include "protocols/timed.h"
1996-01-10 16:44:46 -05:00
extern int logwtmp();
1995-01-03 11:08:21 -05:00
#if HAVE_SETTIMEOFDAY == 1
#define settimeofday(t, tz) (settimeofday)(t)
1995-01-03 11:26:22 -05:00
#endif /* HAVE_SETTIMEOFDAY == 1 */
1995-01-03 11:08:21 -05:00
1989-03-11 21:07:04 -05:00
#ifndef TSP_SETDATE
/*ARGSUSED*/
#endif /* !defined TSP_SETDATE */
1989-03-11 11:56:53 -05:00
static void
1989-03-22 21:06:43 -05:00
reset(newt, nflag)
1994-06-04 17:05:46 -04:00
const time_t newt;
const int nflag;
1989-03-11 11:56:53 -05:00
{
1994-06-04 17:05:46 -04:00
register const char * username;
1989-03-11 11:56:53 -05:00
static struct timeval tv; /* static so tv_usec is 0 */
#ifdef EBUG
return;
1989-03-22 21:06:43 -05:00
#endif /* defined EBUG */
1989-03-07 21:51:45 -05:00
username = getlogin();
if (username == NULL || *username == '\0') /* single-user or no tty */
username = "root";
1989-03-22 21:06:43 -05:00
tv.tv_sec = newt;
1989-03-10 12:02:03 -05:00
#ifdef TSP_SETDATE
1989-03-10 19:00:37 -05:00
if (nflag || !netsettime(tv))
1989-03-09 13:20:49 -05:00
#endif /* defined TSP_SETDATE */
1989-03-10 19:00:37 -05:00
{
1989-03-22 21:06:43 -05:00
/*
1989-04-07 17:31:22 -04:00
** "old" entry is always written, for compatibility.
1989-03-22 21:06:43 -05:00
*/
logwtmp("|", TIME_NAME, "");
1989-03-10 19:00:37 -05:00
if (settimeofday(&tv, (struct timezone *) NULL) == 0) {
1989-03-22 21:06:43 -05:00
logwtmp("{", TIME_NAME, ""); /* } */
1996-02-21 15:47:21 -05:00
syslog(LOG_AUTH | LOG_NOTICE, _("date set by %s"),
1989-03-10 19:00:37 -05:00
username);
1996-02-21 15:47:21 -05:00
} else oops("settimeofday");
1989-03-10 19:00:37 -05:00
}
1989-03-06 04:26:22 -05:00
}
1989-03-05 17:23:15 -05:00
1989-03-22 21:06:43 -05:00
#endif /* !defined OLD_TIME */
1989-03-10 22:05:54 -05:00
static void
wildinput(item, value, reason)
1994-06-04 17:05:46 -04:00
const char * const item;
const char * const value;
const char * const reason;
1989-03-10 22:05:54 -05:00
{
1996-02-21 15:47:21 -05:00
(void) fprintf(stderr,
_("date: error: bad command line %s \"%s\", %s\n"),
1989-03-10 22:05:54 -05:00
item, value, reason);
usage();
}
1989-03-10 19:07:58 -05:00
static void
1994-06-07 16:05:13 -04:00
errensure P((void))
1989-03-10 19:07:58 -05:00
{
if (retval == EXIT_SUCCESS)
retval = EXIT_FAILURE;
}
1994-06-04 17:05:46 -04:00
static const char *
1989-03-09 13:20:49 -05:00
nondigit(cp)
1994-06-04 17:05:46 -04:00
register const char * cp;
1989-03-09 13:20:49 -05:00
{
1995-10-30 10:19:59 -05:00
while (is_digit(*cp))
1989-03-09 13:20:49 -05:00
++cp;
return cp;
}
1989-03-07 21:51:45 -05:00
static void
1994-06-07 16:05:13 -04:00
usage P((void))
1989-03-06 04:26:22 -05:00
{
1996-02-21 15:47:21 -05:00
(void) fprintf(stderr, _("date: usage is date [-u] [-c] [-n] [-d dst] \
[-t min-west] [-a sss.fff] [[yyyy]mmddhhmm[yyyy][.ss]] [+format]\n"));
1989-03-10 19:07:58 -05:00
errensure();
1989-03-14 14:42:05 -05:00
(void) exit(retval);
1989-03-06 04:26:22 -05:00
}
1989-03-08 18:42:17 -05:00
static void
oops(string)
1994-06-04 17:05:46 -04:00
const char * const string;
1989-03-08 18:42:17 -05:00
{
1996-02-21 15:47:21 -05:00
int e = errno;
(void) fprintf(stderr, _("date: error: "));
errno = e;
1989-03-08 18:42:17 -05:00
(void) perror(string);
1989-03-10 19:07:58 -05:00
errensure();
1989-03-08 18:42:17 -05:00
display((char *) NULL);
}
1989-03-06 04:26:22 -05:00
static void
1989-03-06 05:10:58 -05:00
display(format)
1994-06-04 17:05:46 -04:00
const char * const format;
1989-03-06 04:26:22 -05:00
{
1989-03-07 22:57:41 -05:00
struct tm tm;
1989-03-10 23:29:33 -05:00
time_t now;
1989-03-06 04:26:22 -05:00
1989-03-08 18:42:17 -05:00
(void) time(&now);
1989-03-07 14:14:29 -05:00
tm = *localtime(&now);
1995-01-02 12:13:58 -05:00
timeout(stdout, format ? format : "%+", &tm);
1989-03-06 18:59:18 -05:00
(void) putchar('\n');
1989-03-07 14:14:29 -05:00
(void) fflush(stdout);
(void) fflush(stderr);
if (ferror(stdout) || ferror(stderr)) {
1993-11-22 12:58:06 -05:00
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: error: couldn't write results\n"));
1989-03-10 19:07:58 -05:00
errensure();
1989-03-07 14:14:29 -05:00
}
1989-03-06 04:26:22 -05:00
(void) exit(retval);
}
1989-03-10 19:28:00 -05:00
extern size_t strftime();
1989-03-11 21:07:04 -05:00
#define INCR 1024
1989-03-10 19:28:00 -05:00
static void
timeout(fp, format, tmp)
1994-06-04 17:05:46 -04:00
FILE * const fp;
const char * const format;
const struct tm * const tmp;
1989-03-10 19:28:00 -05:00
{
1989-03-11 21:07:04 -05:00
char * cp;
1994-06-04 17:05:46 -04:00
size_t result;
size_t size;
1989-03-10 19:28:00 -05:00
if (*format == '\0')
return;
1989-03-11 21:07:04 -05:00
size = INCR;
1995-01-06 17:11:11 -05:00
cp = malloc((size_t) size);
1989-03-11 21:07:04 -05:00
for ( ; ; ) {
if (cp == NULL) {
(void) fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: error: can't get memory\n"));
1989-03-11 21:07:04 -05:00
errensure();
(void) exit(retval);
}
2000-04-20 15:36:18 -04:00
cp[0] = '\1';
1989-03-11 21:07:04 -05:00
result = strftime(cp, size, format, tmp);
2000-04-20 15:36:18 -04:00
if (result != 0 || cp[0] == '\0')
1989-03-11 21:07:04 -05:00
break;
size += INCR;
1995-01-06 17:11:11 -05:00
cp = realloc(cp, (size_t) size);
1989-03-10 19:28:00 -05:00
}
1989-03-12 07:36:40 -05:00
(void) fwrite(cp, 1, result, fp);
1989-03-11 21:07:04 -05:00
free(cp);
1989-03-10 19:28:00 -05:00
}
1989-03-10 22:58:42 -05:00
static int
comptm(atmp, btmp)
1994-06-04 17:05:46 -04:00
register const struct tm * const atmp;
register const struct tm * const btmp;
1989-03-10 22:58:42 -05:00
{
register int result;
if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
(result = (atmp->tm_min - btmp->tm_min)) == 0)
result = atmp->tm_sec - btmp->tm_sec;
return result;
}
1989-03-07 14:14:29 -05:00
/*
1989-03-10 22:58:42 -05:00
** convert --
1989-03-07 22:57:44 -05:00
** convert user's input into a time_t.
*/
1989-03-06 04:26:22 -05:00
1993-11-22 12:39:02 -05:00
#define ATOI2(ar) (ar[0] - '0') * 10 + (ar[1] - '0'); ar += 2;
1989-03-05 17:23:15 -05:00
1989-03-07 14:14:29 -05:00
static time_t
1989-03-11 11:56:53 -05:00
convert(value, dousg, t)
1994-06-04 17:05:46 -04:00
register const char * const value;
const int dousg;
const time_t t;
1989-03-05 17:23:15 -05:00
{
1994-06-04 17:05:46 -04:00
register const char * cp;
1995-01-02 12:14:52 -05:00
register const char * dotp;
1989-03-11 11:56:53 -05:00
register int cent, year_in_cent, month, hour, day, mins, secs;
1989-03-10 22:58:42 -05:00
struct tm tm, outtm;
time_t outt;
1989-03-05 17:23:15 -05:00
1989-03-10 22:58:42 -05:00
tm = *localtime(&t);
2004-11-08 10:48:47 -05:00
#define DIVISOR 100
year_in_cent = tm.tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
cent = tm.tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
year_in_cent / DIVISOR;
year_in_cent %= DIVISOR;
if (year_in_cent < 0) {
year_in_cent += DIVISOR;
--cent;
}
1989-03-11 11:56:53 -05:00
month = tm.tm_mon + 1;
day = tm.tm_mday;
hour = tm.tm_hour;
mins = tm.tm_min;
1989-03-11 21:07:04 -05:00
secs = 0;
1989-03-11 11:56:53 -05:00
1989-03-11 21:07:04 -05:00
dotp = strchr(value, '.');
for (cp = value; *cp != '\0'; ++cp)
1995-10-30 10:19:59 -05:00
if (!is_digit(*cp) && cp != dotp)
1996-02-21 15:47:21 -05:00
wildinput(_("time"), value, _("contains a nondigit"));
1989-03-11 21:07:04 -05:00
if (dotp == NULL)
dotp = strchr(value, '\0');
else {
cp = dotp + 1;
1989-03-10 23:29:33 -05:00
if (strlen(cp) != 2)
1996-02-21 15:47:21 -05:00
wildinput(_("time"), value,
_("seconds part is not two digits"));
1989-03-11 11:56:53 -05:00
secs = ATOI2(cp);
1989-03-05 17:23:15 -05:00
}
1989-03-11 11:56:53 -05:00
cp = value;
1989-03-11 21:07:04 -05:00
switch (dotp - cp) {
1989-03-07 14:14:29 -05:00
default:
2005-01-31 14:06:04 -05:00
wildinput(_("time"), value,
_("main part is wrong length"));
1989-04-05 03:37:47 -04:00
case 12:
if (!dousg) {
cent = ATOI2(cp);
year_in_cent = ATOI2(cp);
}
1989-04-04 16:13:08 -04:00
month = ATOI2(cp);
day = ATOI2(cp);
hour = ATOI2(cp);
mins = ATOI2(cp);
1989-04-05 03:37:47 -04:00
if (dousg) {
cent = ATOI2(cp);
year_in_cent = ATOI2(cp);
}
1989-04-04 16:13:08 -04:00
break;
1989-03-10 23:29:33 -05:00
case 8: /* mmddhhmm */
1989-03-11 11:56:53 -05:00
month = ATOI2(cp);
1989-03-10 23:29:33 -05:00
/* fall through to. . . */
case 6: /* ddhhmm */
1989-03-11 11:56:53 -05:00
day = ATOI2(cp);
1989-03-10 23:29:33 -05:00
/* fall through to. . . */
case 4: /* hhmm */
1989-03-11 11:56:53 -05:00
hour = ATOI2(cp);
mins = ATOI2(cp);
1989-03-10 22:58:42 -05:00
break;
1989-03-11 11:56:53 -05:00
case 10:
if (!dousg) {
year_in_cent = ATOI2(cp);
}
month = ATOI2(cp);
day = ATOI2(cp);
hour = ATOI2(cp);
mins = ATOI2(cp);
1989-03-10 22:58:42 -05:00
if (dousg) {
1989-03-11 11:56:53 -05:00
year_in_cent = ATOI2(cp);
1989-03-10 22:58:42 -05:00
}
break;
1989-03-07 14:14:29 -05:00
}
1989-03-11 11:56:53 -05:00
tm.tm_year = cent * 100 + year_in_cent - TM_YEAR_BASE;
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_hour = hour;
tm.tm_min = mins;
tm.tm_sec = secs;
1989-03-10 23:29:33 -05:00
tm.tm_isdst = -1;
1989-03-10 22:58:42 -05:00
outtm = tm;
outt = mktime(&outtm);
return (comptm(&tm, &outtm) == 0) ? outt : -1;
1989-03-05 17:23:15 -05:00
}
1989-03-11 21:07:04 -05:00
/*
1994-06-04 17:09:44 -04:00
** Code from here on out is either based on code provided by UCB
1989-03-11 21:07:04 -05:00
** or is only called just before the program exits.
*/
/*
** Check for iffy input.
*/
static void
checkfinal(value, didusg, t, oldnow)
1994-06-04 17:05:46 -04:00
const char * const value;
const int didusg;
const time_t t;
const time_t oldnow;
1989-03-11 21:07:04 -05:00
{
time_t othert;
struct tm tm;
struct tm othertm;
register int pass;
register long offset;
1989-03-30 22:02:45 -05:00
1989-03-11 21:07:04 -05:00
/*
** See if there's both a USG and a BSD interpretation.
*/
othert = convert(value, !didusg, oldnow);
if (othert != -1 && othert != t)
1996-02-21 15:47:21 -05:00
iffy(t, othert, value, _("year could be at start or end"));
1989-03-11 21:07:04 -05:00
/*
** See if there's both a DST and a STD version.
*/
tm = *localtime(&t);
othertm = tm;
othertm.tm_isdst = !tm.tm_isdst;
othert = mktime(&othertm);
if (othert != -1 && othertm.tm_isdst != tm.tm_isdst &&
comptm(&tm, &othertm) == 0)
iffy(t, othert, value,
1996-02-21 15:47:21 -05:00
_("both standard and summer time versions exist"));
1989-03-11 21:07:04 -05:00
/*
** Final check.
**
** If a jurisdiction shifts time *without* shifting whether time is
** summer or standard (as Hawaii, the United Kingdom, and Saudi Arabia
** have done), routine checks for iffy times may not work.
** So we perform this final check, deferring it until after the time has
** been set--it may take a while, and we don't want to introduce an unnecessary
** lag between the time the user enters their command and the time that
** stime/settimeofday is called.
1989-03-30 22:02:45 -05:00
**
1993-11-22 12:58:06 -05:00
** We just check nearby times to see if any have the same representation
1989-03-11 21:07:04 -05:00
** as the time that convert returned. We work our way out from the center
** for quick response in solar time situations. We only handle common cases--
** offsets of at most a minute, and offsets of exact numbers of minutes
** and at most an hour.
*/
for (offset = 1; offset <= 60; ++offset)
for (pass = 1; pass <= 4; ++pass) {
if (pass == 1)
othert = t + offset;
else if (pass == 2)
othert = t - offset;
else if (pass == 3)
othert = t + 60 * offset;
else othert = t - 60 * offset;
othertm = *localtime(&othert);
if (comptm(&tm, &othertm) == 0)
iffy(t, othert, value,
1996-02-21 15:47:21 -05:00
_("multiple matching times exist"));
1989-03-11 21:07:04 -05:00
}
}
static void
iffy(thist, thatt, value, reason)
1994-06-04 17:05:46 -04:00
const time_t thist;
const time_t thatt;
const char * const value;
const char * const reason;
1989-03-11 21:07:04 -05:00
{
struct tm tm;
1996-02-21 15:47:21 -05:00
(void) fprintf(stderr, _("date: warning: ambiguous time \"%s\", %s.\n"),
1989-03-11 21:07:04 -05:00
value, reason);
tm = *gmtime(&thist);
/*
** Avoid running afoul of SCCS!
*/
1996-02-21 15:47:21 -05:00
timeout(stderr, _("Time was set as if you used\n\tdate -u %m%d%H\
%M\
%Y.%S\n"), &tm);
1989-03-11 21:07:04 -05:00
tm = *localtime(&thist);
1996-02-21 15:47:21 -05:00
timeout(stderr, _("to get %c"), &tm);
(void) fprintf(stderr, _(" (%s). Use\n"),
tm.tm_isdst ? _("summer time") : _("standard time"));
1989-03-11 21:07:04 -05:00
tm = *gmtime(&thatt);
1996-02-21 15:47:21 -05:00
timeout(stderr, _("\tdate -u %m%d%H\
%M\
%Y.%S\n"), &tm);
1989-03-11 21:07:04 -05:00
tm = *localtime(&thatt);
1996-02-21 15:47:21 -05:00
timeout(stderr, _("to get %c"), &tm);
(void) fprintf(stderr, _(" (%s).\n"),
tm.tm_isdst ? _("summer time") : _("standard time"));
1989-03-11 21:07:04 -05:00
errensure();
(void) exit(retval);
}
1989-03-09 13:20:49 -05:00
#ifdef TSP_SETDATE
1993-11-22 12:39:02 -05:00
#define WAITACK 2 /* seconds */
#define WAITDATEACK 5 /* seconds */
1989-03-05 17:23:15 -05:00
/*
* Set the date in the machines controlled by timedaemons
1993-11-22 12:34:55 -05:00
* by communicating the new date to the local timedaemon.
1989-03-05 17:23:15 -05:00
* If the timedaemon is in the master state, it performs the
* correction on all slaves. If it is in the slave state, it
* notifies the master that a correction is needed.
* Returns 1 on success, 0 on failure.
*/
netsettime(ntv)
struct timeval ntv;
{
int s, length, port, timed_ack, found, err;
long waittime;
fd_set ready;
char hostname[MAXHOSTNAMELEN];
struct timeval tout;
struct servent *sp;
struct tsp msg;
struct sockaddr_in sin, dest, from;
sp = getservbyname("timed", "udp");
if (sp == 0) {
1996-02-21 15:47:21 -05:00
fputs(_("udp/timed: unknown service\n"), stderr);
1989-03-05 17:23:15 -05:00
retval = 2;
return (0);
}
dest.sin_port = sp->s_port;
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
if (errno != EPROTONOSUPPORT)
perror("date: socket");
goto bad;
}
bzero((char *)&sin, sizeof (sin));
sin.sin_family = AF_INET;
for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
sin.sin_port = htons((u_short)port);
if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
break;
if (errno != EADDRINUSE) {
if (errno != EADDRNOTAVAIL)
perror("date: bind");
goto bad;
}
}
if (port == IPPORT_RESERVED / 2) {
1996-02-21 15:47:21 -05:00
fputs(_("date: All ports in use\n"), stderr);
1989-03-05 17:23:15 -05:00
goto bad;
}
msg.tsp_type = TSP_SETDATE;
msg.tsp_vers = TSPVERSION;
if (gethostname(hostname, sizeof (hostname))) {
1989-03-11 22:35:14 -05:00
perror("gethostname");
1989-03-05 17:23:15 -05:00
goto bad;
}
(void) strncpy(msg.tsp_name, hostname, sizeof (hostname));
msg.tsp_seq = htons((u_short)0);
msg.tsp_time.tv_sec = htonl((u_long)ntv.tv_sec);
msg.tsp_time.tv_usec = htonl((u_long)ntv.tv_usec);
length = sizeof (struct sockaddr_in);
if (connect(s, &dest, length) < 0) {
perror("date: connect");
goto bad;
}
if (send(s, (char *)&msg, sizeof (struct tsp), 0) < 0) {
if (errno != ECONNREFUSED)
perror("date: send");
goto bad;
}
timed_ack = -1;
waittime = WAITACK;
loop:
tout.tv_sec = waittime;
tout.tv_usec = 0;
FD_ZERO(&ready);
FD_SET(s, &ready);
found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
2001-06-22 13:31:26 -04:00
length = sizeof err;
1989-03-05 17:23:15 -05:00
if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *)&err, &length) == 0
&& err) {
errno = err;
if (errno != ECONNREFUSED)
1996-02-21 15:47:21 -05:00
perror(_("date: send (delayed error)"));
1989-03-05 17:23:15 -05:00
goto bad;
}
if (found > 0 && FD_ISSET(s, &ready)) {
length = sizeof (struct sockaddr_in);
if (recvfrom(s, (char *)&msg, sizeof (struct tsp), 0, &from,
&length) < 0) {
if (errno != ECONNREFUSED)
perror("date: recvfrom");
goto bad;
}
msg.tsp_seq = ntohs(msg.tsp_seq);
msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
switch (msg.tsp_type) {
case TSP_ACK:
timed_ack = TSP_ACK;
waittime = WAITDATEACK;
goto loop;
case TSP_DATEACK:
(void)close(s);
return (1);
default:
1989-03-11 22:35:14 -05:00
fprintf(stderr,
1996-02-21 15:47:21 -05:00
_("date: Wrong ack received from timed: %s\n"),
1993-11-22 12:58:06 -05:00
tsptype[msg.tsp_type]);
1989-03-05 17:23:15 -05:00
timed_ack = -1;
break;
}
}
if (timed_ack == -1)
1996-02-21 15:47:21 -05:00
fputs(_("date: Can't reach time daemon, time set locally.\n"),
1993-11-22 12:58:06 -05:00
stderr);
1989-03-05 17:23:15 -05:00
bad:
(void)close(s);
retval = 2;
return (0);
}
1989-03-09 13:20:49 -05:00
#endif /* defined TSP_SETDATE */