static char sccsid
[] = "@(#)date.c 4.8 (Berkeley) %G%";
* Date - print and set date
* Modified by Riccardo Gusella to work with timed
#include <protocols/timed.h>
#define WTMP "/usr/adm/wtmp"
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static char *usage
= "usage: date [-u] [yymmddhhmm[.ss]]\n";
char *username
, *getlogin();
#define WAITACK 10 /* seconds */
#define WAITDATEACK 5 /* seconds */
struct hostent
*hp
, *gethostbyname();
struct sockaddr_in sin
, dest
, from
;
int bytenetorder(), bytehostorder();
(void) gettimeofday(&tv
, &tz
);
if (argc
> 1 && strcmp(argv
[1], "-u") == 0) {
printf("You are not superuser: date not set\n");
printf("Cannot record your name: date not set\n");
syslog(LOG_ERR
, "date: set by %s", username
);
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((long *)&tv
.tv_sec
)->tm_isdst
)
(void) open("/etc/timed", O_WRONLY
, 01700);
if (settimeofday(&tv
, (struct timezone
*)0) < 0) {
if ((wf
= open(WTMP
, 1)) >= 0) {
(void) time((long *)&wtmp
[1].ut_time
);
(void) lseek(wf
, (off_t
)0L, 2);
(void) write(wf
, (char *)wtmp
, sizeof(wtmp
));
* Here we 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.
srvp
= getservbyname("timed", "udp");
fprintf(stderr
, "udp/timed: unknown service\n");
dest
.sin_port
= srvp
->s_port
;
dest
.sin_family
= AF_INET
;
sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
perror("opening socket");
sin
.sin_family
= AF_INET
;
for (port
= IPPORT_RESERVED
- 1; port
> IPPORT_RESERVED
/ 2; port
--) {
sin
.sin_port
= htons((u_short
)port
);
if (bind(sock
, (struct sockaddr
*)&sin
, sizeof (sin
)) >= 0)
if (errno
!= EADDRINUSE
&& errno
!= EADDRNOTAVAIL
) {
if (port
== IPPORT_RESERVED
/ 2) {
fprintf(stderr
, "socket: All ports in use\n");
(void) gethostname(hostname
,sizeof(hostname
));
hp
= gethostbyname(hostname
);
bcopy(hp
->h_addr
, &dest
.sin_addr
.s_addr
, hp
->h_length
);
(void) strcpy(msg
.tsp_name
, hostname
);
(void) gettimeofday(&now
, (struct timezone
*)0);
length
= sizeof(struct sockaddr_in
);
if (sendto(sock
, (char *)&msg
, sizeof(struct tsp
), 0,
found
= select(20, &ready
, (int *)0, (int *)0, &tout
);
length
= sizeof(struct sockaddr_in
);
if (recvfrom(sock
, (char *)&msg
, sizeof(struct tsp
), 0,
tout
.tv_sec
= WAITDATEACK
;
printf("Wrong ack received: %s\n",
if (timed_ack
== TSP_ACK
) {
printf("Network date being set\n");
printf("Communication problems\n");
(void) gettimeofday(&tv
, (struct timezone
*)0);
ap
= asctime(gmtime((long *)&tv
.tv_sec
));
tp
= localtime((long *)&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((long *)&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)
t1
->tv_sec
-= t2
->tv_sec
;
t1
->tv_usec
-= t2
->tv_usec
;
if (t1
->tv_usec
>= 1000000) {
ptr
->tsp_seq
= htons((u_short
)ptr
->tsp_seq
);
ptr
->tsp_time
.tv_sec
= htonl((u_long
)ptr
->tsp_time
.tv_sec
);
ptr
->tsp_time
.tv_usec
= htonl((u_long
)ptr
->tsp_time
.tv_usec
);
ptr
->tsp_seq
= ntohs((u_short
)ptr
->tsp_seq
);
ptr
->tsp_time
.tv_sec
= ntohl((u_long
)ptr
->tsp_time
.tv_sec
);
ptr
->tsp_time
.tv_usec
= ntohl((u_long
)ptr
->tsp_time
.tv_usec
);