lotza work

SCCS-file: date.c
SCCS-SID: 1.5
This commit is contained in:
Arthur David Olson
2012-07-18 03:01:54 -04:00
committed by Paul Eggert
parent 352a1bc6df
commit 6da4c4050f
+196 -95
View File
@@ -3,8 +3,6 @@
static char elsieid[] = "%W%"; static char elsieid[] = "%W%";
/* /*
** UCB's date.c, plus BSD conditionalizing, pluse changes to use "mktime". ** UCB's date.c, plus BSD conditionalizing, pluse changes to use "mktime".
** plus BSD conditionalizing,
** plus changes to use "mktime".
*/ */
#endif /* !defined NOID */ #endif /* !defined NOID */
#endif /* !defined lint */ #endif /* !defined lint */
@@ -50,11 +48,22 @@ static char sccsid[] = "@(#)date.c 4.23 (Berkeley) 9/20/88";
#include <ctype.h> #include <ctype.h>
#include <strings.h> #include <strings.h>
#define ATOI2(ar) (ar[0] - '0') * 10 + (ar[1] - '0'); ar += 2; /*
** TO DO: provide a way to disambiguate 1:30 a.m. on the day you "fall back"?
** also: provide a way to disambiguate between multiple noons
** in Riyadh, both of which are standard time.
*/
extern int optind;
#ifndef USG
static struct timeval tv; static struct timeval tv;
static int retval; static int retval;
static void display();
int
main(argc, argv) main(argc, argv)
int argc; int argc;
char **argv; char **argv;
@@ -63,11 +72,11 @@ main(argc, argv)
extern char *optarg; extern char *optarg;
struct timezone tz; struct timezone tz;
char *ap, *tzn; char *ap, *tzn;
int ch, uflag, nflag; int ch, nflag;
char *username, *getlogin(); char *username, *getlogin();
time_t time(); time_t time();
nflag = uflag = 0; nflag = 0;
tz.tz_dsttime = tz.tz_minuteswest = 0; tz.tz_dsttime = tz.tz_minuteswest = 0;
while ((ch = getopt(argc, argv, "d:nut:")) != EOF) while ((ch = getopt(argc, argv, "d:nut:")) != EOF)
switch((char)ch) { switch((char)ch) {
@@ -78,7 +87,7 @@ main(argc, argv)
nflag = 1; nflag = 1;
break; break;
case 'u': /* do it in GMT */ case 'u': /* do it in GMT */
uflag = 1; setgmt();
break; break;
case 't': /* minutes west of GMT */ case 't': /* minutes west of GMT */
/* error check; we can't allow "PST" */ /* error check; we can't allow "PST" */
@@ -103,7 +112,7 @@ main(argc, argv)
settimeofday((struct timeval *)NULL, &tz)) { settimeofday((struct timeval *)NULL, &tz)) {
perror("settimeofday"); perror("settimeofday");
retval = 1; retval = 1;
goto display; display();
} }
if (gettimeofday(&tv, &tz)) { if (gettimeofday(&tv, &tz)) {
@@ -112,34 +121,21 @@ main(argc, argv)
} }
if (!argc) if (!argc)
goto display; display();
if (uflag) { tv.tv_sec = gtime(*argv, (time_t) tv.tv_sec, -1);
/* if (tv.tv_sec == -1) {
** Take measures to ensure that mktime uses GMT.
*/
}
if (gtime(*argv)) {
usage(); usage();
retval = 1; retval = 1;
goto display; display();
}
if (uflag) {
/*
** Undo measures to ensure that mktime uses GMT.
*/
} }
#ifdef BSD
if (nflag || !netsettime(tv)) { if (nflag || !netsettime(tv)) {
#else
{
#endif /* !defined BSD */
logwtmp("|", "date", ""); logwtmp("|", "date", "");
if (settimeofday(&tv, (struct timezone *)NULL)) { if (settimeofday(&tv, (struct timezone *)NULL)) {
perror("settimeofday"); perror("settimeofday");
retval = 1; retval = 1;
goto display; display();
} }
logwtmp("{", "date", ""); logwtmp("{", "date", "");
} }
@@ -149,91 +145,198 @@ main(argc, argv)
username = "root"; username = "root";
syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", username); syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", username);
display: display();
if (gettimeofday(&tv, (struct timezone *)NULL)) { for ( ; ; )
perror("gettimeofday"); ;
}
usage()
{
fputs("usage: date [-nu] [-d dst] [-t minutes_west] [yymmddhhmm[.ss]]\n", stderr);
}
#else /* defined USG */
int
main(argc, argv)
int argc;
char **argv;
{
int ch;
time_t time();
time_t t;
while ((ch = getopt(argc, argv, "u")) != EOF)
switch (ch) {
case 'u':
setgmt():
break;
default:
usage();
exit(1);
}
if (argc == optind)
display();
else if ((argc - optind) != 1) {
usage();
exit(1); exit(1);
} }
if (uflag) { (void) time(&t);
ap = asctime(gmtime((time_t *)&tv.tv_sec)); t = gtime(argv[optind], t, -1);
tzn = "GMT"; if (t == -1) {
usage();
retval = 1;
} else {
logwtmp("|", "date", "");
if (stime(&t) == 0)
logwtmp("{", "date", "");
else {
perror("stime");
retval = 1;
}
} }
else { display();
struct tm * tp; }
extern char * tzname[2];
tp = localtime((time_t *)&tv.tv_sec); usage()
ap = asctime(tp); {
tzn = tzname[tp->tm_isdst]; (void) fprintf(stderr, "date: usage is date [-u] yymmddhhmm[.ss]\n");
} exit(1);
printf("%.20s%s%s", ap, tzn, ap + 19); }
exit(retval);
#endif /* defined USG */
static void
display()
{
register struct tm * tp;
register char * cp;
time_t t;
extern char * tzname[2];
(void) time(&t);
tp = localtime(&t);
cp = asctime(tp);
(void) printf("%.20s%s%s", cp, tzname[tp->tm_isdst], cp + 19);
(void) exit(retval);
for ( ; ; )
;
}
setgmt()
{
register char ** saveenv;
extern char ** environ;
char * fakeenv[2];
fakeenv[0] = "TZ=GMT0";
fakeenv[1] = NULL;
saveenv = environ;
environ = fakeenv;
tzset();
environ = saveenv;
} }
/* /*
* gtime -- * gtime --
* convert user's time into number of seconds * convert user's input into a time_t.
* STILL TO DO: COPE WITH DST/STD. * Track the BSD behavior of treating
* 8903062415
* as March 7, 1989, 12:15 a.m. rather than
* March 6, 1989, 12:15 a.m.
*/ */
static
gtime(ap) #define PAIR(cp) ((*(cp) - '0') * 10 + *((cp) + 1) - '0')
register char *ap;
static time_t
gtime(ap, t, isdst)
register char * ap;
time_t t; /* time to use for filling in unspecified information */
int isdst;
{ {
register char * C; register char * cp;
time_t t; struct tm intm, outtm;
struct tm in, out; time_t outt;
int saw24;
int okay;
in = *localtime((time_t *)&tv.tv_sec); if (isdst < 0) {
in.tm_sec = 0; time_t thist, thatt;
for (C = ap; *C; ++C) {
if (*C == '.') { /* seconds provided */ thist = gtime(ap, t, 0);
if (strlen(C) != 3) thatt = gtime(ap, t, 1);
return(1); if (thist == -1)
if (!isdigit(C[1]) || !isdigit(C[2])) if (thatt == -1)
return(1); return -1;
in.tm_sec = (C[1] - '0') * 10 + (C[2] - '0'); else return thatt;
break; else if (thatt == -1)
} return thist;
if (!isdigit(*C)) else {
return(-1); (void) fprintf(stderr, "ambiguous time\n");
return -1;
}
} }
for (cp = ap; *cp != '\0'; ++cp)
switch ((int)(C - ap)) { /* length */ if (!isdigit(*cp) && *cp != '.')
return -1;
intm = *localtime(&t);
intm.tm_sec = 0;
saw24 = 0;
cp = ap;
switch (strlen(cp)) {
case 13: /* yymmddhhmm.ss */
if (cp[10] != '.')
return -1;
intm.tm_sec = PAIR(&cp[11]);
case 10: /* yymmddhhmm */ case 10: /* yymmddhhmm */
in.tm_year = ATOI2(ap); intm.tm_year = PAIR(cp);
if (in.tm_year < 70) /* epoch year */ cp += 2;
in.tm_year += 100;
case 8: /* mmddhhmm */ case 8: /* mmddhhmm */
in.tm_mon = ATOI2(ap); intm.tm_mon = PAIR(cp);
--in.tm_mon; cp += 2;
--intm.tm_mon;
case 6: /* ddhhmm */ case 6: /* ddhhmm */
in.tm_mday = ATOI2(ap); intm.tm_mday = PAIR(cp);
cp += 2;
case 4: /* hhmm */ case 4: /* hhmm */
in.tm_hour = ATOI2(ap); intm.tm_hour = PAIR(cp);
in.tm_min = ATOI2(ap); cp += 2;
if (intm.tm_hour == 24) {
saw24 = 1;
intm.tm_hour = 0;
++intm.tm_mday;
}
intm.tm_min = PAIR(cp);
break; break;
default: default:
return(1); return -1;
} }
intm.tm_isdst = isdst;
out = in; outtm = intm;
tv.tv_sec = mktime(&out); outt = mktime(&outtm);
if (tv.tv_sec == -1 || if (outtm.tm_isdst != intm.tm_isdst ||
out.tm_year != in.tm_year || outtm.tm_sec != intm.tm_sec ||
out.tm_mon != in.tm_mon || outtm.tm_min != intm.tm_min ||
out.tm_hour != in.tm_hour || outtm.tm_hour != intm.tm_hour)
out.tm_min != in.tm_min || okay = 0;
out.tm_sec != in.tm_sec) { else if (outtm.tm_mday == intm.tm_mday &&
/* outtm.tm_mon == intm.tm_mon &&
** Old code allows hour == 24; outtm.tm_year == intm.tm_year)
** check for it here. okay = 1;
*/ else if (!saw24)
return 1; okay = 0;
} else if (outtm.tm_mday == intm.tm_mday + 1)
return 0; okay = outtm.tm_mon == intm.tm_mon &&
outtm.tm_year == intm.tm_year;
else if (outtm.tm_mday != 1)
okay = 0;
else if (outtm.tm_mon == intm.tm_mon + 1)
okay = outtm.tm_year == intm.tm_year;
else if (outtm.tm_mon != 0)
okay = 0;
else okay = outtm.tm_year == intm.tm_year + 1;
return okay ? outt : -1;
} }
#ifdef BSD
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
@@ -255,6 +358,9 @@ extern int errno;
netsettime(ntv) netsettime(ntv)
struct timeval ntv; struct timeval ntv;
{ {
#ifndef TSP_SETDATE
return 0;
#else /* defined TSP_SETDATE */
int s, length, port, timed_ack, found, err; int s, length, port, timed_ack, found, err;
long waittime; long waittime;
fd_set ready; fd_set ready;
@@ -368,10 +474,5 @@ bad:
(void)close(s); (void)close(s);
retval = 2; retval = 2;
return (0); return (0);
} #endif /* defined TSP_SETDATE */
#endif /* defined BSD */
usage()
{
fputs("usage: date [-nu] [-d dst] [-t minutes_west] [yymmddhhmm[.ss]]\n", stderr);
} }