* Copyright (c) 1985 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1985 Regents of the University of California.\n\
static char sccsid
[] = "@(#)date.c 4.19 (Berkeley) %G%";
* Date - print and set date
#define WTMP "/usr/adm/wtmp"
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static char *usage
= "usage: date [-n] [-u] [yymmddhhmm[.ss]]\n";
char *strcpy(), *strncpy();
char *username
, *getlogin();
openlog("date", LOG_ODELAY
, LOG_AUTH
);
(void) gettimeofday(&tv
, &tz
);
while (argc
> 1 && argv
[1][0] == '-') {
switch ((int)argv
[1][0]) {
fprintf(stderr
, "You are not superuser: date not set\n");
if (username
== NULL
|| *username
== '\0') /* single-user or no tty */
wtmp
[0].ut_time
= tv
.tv_sec
;
/* convert to GMT assuming local time */
tv
.tv_sec
+= (long)tz
.tz_minuteswest
*60;
/* now fix up local daylight time */
if (localtime((time_t *)&tv
.tv_sec
)->tm_isdst
)
if (nflag
|| !settime(tv
)) {
if (settimeofday(&tv
, (struct timezone
*)0) < 0) {
if ((wf
= open(WTMP
, O_WRONLY
|O_APPEND
)) >= 0) {
(void) time((time_t *)&wtmp
[1].ut_time
);
(void) write(wf
, (char *)wtmp
, sizeof(wtmp
));
syslog(LOG_NOTICE
, "set by %s", username
);
(void) gettimeofday(&tv
, (struct timezone
*)0);
ap
= asctime(gmtime((time_t *)&tv
.tv_sec
));
tp
= localtime((time_t *)&tv
.tv_sec
);
tzn
= timezone(tz
.tz_minuteswest
, tp
->tm_isdst
);
register int i
, year
, month
;
int day
, hour
, mins
, secs
;
(void) gettimeofday(&tv
, (struct timezone
*)0);
L
= localtime((time_t *)&tv
.tv_sec
);
if (month
< 1 || month
> 12 ||
if (hour
< 0 || hour
> 23)
for (i
= 1970; i
< year
; i
++)
if (dysize(year
) == 366 && month
>= 3)
tv
.tv_sec
+= dmsize
[month
-1];
tv
.tv_sec
= 24*tv
.tv_sec
+ hour
;
tv
.tv_sec
= 60*tv
.tv_sec
+ mins
;
tv
.tv_sec
= 60*tv
.tv_sec
+ secs
;
d
= (*sp
? (*sp
++) - '0' : 0);
if (c
< 0 || c
> 9 || d
< 0 || d
> 9)
#include <protocols/timed.h>
#define WAITACK 2 /* seconds */
#define WAITDATEACK 5 /* seconds */
* Set the date in the machines controlled by timedaemons
* by communicating the new date to the local timedaemon.
* 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.
int s
, length
, port
, timed_ack
, found
, err
;
char hostname
[MAXHOSTNAMELEN
];
struct sockaddr_in sin
, dest
, from
;
sp
= getservbyname("timed", "udp");
fprintf(stderr
, "udp/timed: unknown service\n");
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 (errno
!= EPROTONOSUPPORT
)
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)
if (errno
!= EADDRINUSE
) {
if (errno
!= EADDRNOTAVAIL
)
if (port
== IPPORT_RESERVED
/ 2) {
fprintf(stderr
, "date: All ports in use\n");
msg
.tsp_type
= TSP_SETDATE
;
msg
.tsp_vers
= TSPVERSION
;
(void) gethostname(hostname
, sizeof (hostname
));
(void) strncpy(msg
.tsp_name
, hostname
, sizeof (hostname
));
msg
.tsp_seq
= htons((u_short
)0);
msg
.tsp_time
.tv_sec
= htonl((u_long
)tv
.tv_sec
);
msg
.tsp_time
.tv_usec
= htonl((u_long
)tv
.tv_usec
);
length
= sizeof (struct sockaddr_in
);
if (connect(s
, &dest
, length
) < 0) {
if (send(s
, (char *)&msg
, sizeof (struct tsp
), 0) < 0) {
if (errno
!= ECONNREFUSED
)
found
= select(FD_SETSIZE
, &ready
, (fd_set
*)0, (fd_set
*)0, &tout
);
if (getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *)&err
, &length
) == 0
if (errno
!= ECONNREFUSED
)
perror("date: send (delayed error)");
if (found
> 0 && FD_ISSET(s
, &ready
)) {
length
= sizeof (struct sockaddr_in
);
if (recvfrom(s
, (char *)&msg
, sizeof (struct tsp
), 0, &from
,
if (errno
!= ECONNREFUSED
)
perror("date: recvfrom");
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
);
"date: Wrong ack received from timed: %s\n",
"date: Can't reach time daemon, time set locally.\n");