fix pr/1226: allow rdate to use adjtime(2)
This commit is contained in:
parent
8a6ad45eed
commit
1f4499519b
@ -27,7 +27,7 @@
|
||||
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
.\"
|
||||
.\" $Id: rdate.8,v 1.1 1994/11/17 17:39:54 glass Exp $
|
||||
.\" $Id: rdate.8,v 1.2 1996/02/10 18:44:54 christos Exp $
|
||||
.\"
|
||||
.Dd April 30, 1994
|
||||
.Dt RDATE 8
|
||||
@ -37,14 +37,14 @@
|
||||
.Nd set the system's date from a remote host
|
||||
.Sh SYNOPSIS
|
||||
.Nm rdate
|
||||
.Op Fl ps
|
||||
.Op Fl psa
|
||||
.Ar host
|
||||
.Sh DESCRIPTION
|
||||
.Nm Rdate
|
||||
displays and sets the local date and time from the
|
||||
host name or address given as the argument. It uses the RFC868
|
||||
protocol which is usually implemented as a built-in service of
|
||||
.Xr inetd(1).
|
||||
.Xr inetd 1
|
||||
.Pp
|
||||
Available options:
|
||||
.Pp
|
||||
@ -53,5 +53,11 @@ Available options:
|
||||
Do not set, just print the remote time
|
||||
.It Fl s
|
||||
Do not print the time.
|
||||
.It Fl a
|
||||
Use the
|
||||
.Xr adjtime 2
|
||||
call to gradually skew the local time to the
|
||||
remote time rather than just hopping.
|
||||
.Sh SEE ALSO
|
||||
.Xr inetd 1
|
||||
.Xr adjtime 2
|
||||
|
@ -27,7 +27,7 @@
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* $Id: rdate.c,v 1.1 1994/06/02 22:55:07 deraadt Exp $
|
||||
* $Id: rdate.c,v 1.2 1996/02/10 18:44:56 christos Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
@ -38,8 +38,8 @@
|
||||
* midnight January 1st 1900.
|
||||
*/
|
||||
#ifndef lint
|
||||
static char rcsid[] = "$Id: rdate.c,v 1.1 1994/06/02 22:55:07 deraadt Exp $";
|
||||
#endif /* lint */
|
||||
static char rcsid[] = "$Id: rdate.c,v 1.2 1996/02/10 18:44:56 christos Exp $";
|
||||
#endif/* lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
@ -58,23 +58,35 @@ static char rcsid[] = "$Id: rdate.c,v 1.1 1994/06/02 22:55:07 deraadt Exp $";
|
||||
#define DIFFERENCE 2208988800
|
||||
#endif
|
||||
|
||||
extern char *__progname;
|
||||
|
||||
static void
|
||||
usage()
|
||||
{
|
||||
(void) fprintf(stderr, "Usage: %s [-psa] host\n", __progname);
|
||||
(void) fprintf(stderr, " -p: just print, don't set\n");
|
||||
(void) fprintf(stderr, " -s: just set, don't print\n");
|
||||
(void) fprintf(stderr, " -a: use adjtime instead of instant change\n");
|
||||
}
|
||||
|
||||
int
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
int pr = 0, silent = 0, s;
|
||||
int slidetime = 0;
|
||||
int adjustment;
|
||||
time_t tim;
|
||||
char *hname;
|
||||
struct hostent *hp;
|
||||
struct protoent *pp, ppp;
|
||||
struct servent *sp, ssp;
|
||||
struct sockaddr_in sa;
|
||||
extern char *__progname;
|
||||
extern int optind;
|
||||
int c;
|
||||
|
||||
while ((c = getopt(argc, argv, "ps")) != -1)
|
||||
while ((c = getopt(argc, argv, "psa")) != -1)
|
||||
switch (c) {
|
||||
case 'p':
|
||||
pr++;
|
||||
@ -84,21 +96,25 @@ main(argc, argv)
|
||||
silent++;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
slidetime++;
|
||||
break;
|
||||
|
||||
default:
|
||||
goto usage;
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (argc - 1 != optind) {
|
||||
usage:
|
||||
(void) fprintf(stderr, "Usage: %s [-ps] host\n", __progname);
|
||||
return (1);
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
hname = argv[optind];
|
||||
|
||||
if ((hp = gethostbyname(hname)) == NULL) {
|
||||
fprintf(stderr, "%s: ", __progname);
|
||||
(void) fprintf(stderr, "%s: ", __progname);
|
||||
herror(hname);
|
||||
exit(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((sp = getservbyname("time", "tcp")) == NULL) {
|
||||
@ -129,13 +145,29 @@ usage:
|
||||
tim = ntohl(tim) - DIFFERENCE;
|
||||
|
||||
if (!pr) {
|
||||
struct timeval tv;
|
||||
tv.tv_sec = tim;
|
||||
tv.tv_usec = 0;
|
||||
if (settimeofday(&tv, NULL) == -1)
|
||||
err(1, "Could not set time of day");
|
||||
struct timeval tv;
|
||||
if (!slidetime) {
|
||||
tv.tv_sec = tim;
|
||||
tv.tv_usec = 0;
|
||||
if (settimeofday(&tv, NULL) == -1)
|
||||
err(1, "Could not set time of day");
|
||||
} else {
|
||||
struct timeval tv_current;
|
||||
if (gettimeofday(&tv_current, NULL) == -1)
|
||||
err(1, "Could not get local time of day");
|
||||
adjustment = tv.tv_sec = tim - tv_current.tv_sec;
|
||||
tv.tv_usec = 0;
|
||||
if (adjtime(&tv, NULL) == -1)
|
||||
err(1, "Could not adjust time of day");
|
||||
}
|
||||
}
|
||||
if (!silent)
|
||||
|
||||
if (!silent) {
|
||||
(void) fputs(ctime(&tim), stdout);
|
||||
if (slidetime)
|
||||
(void) fprintf(stdout,
|
||||
"%s: adjust local clock by %d seconds\n",
|
||||
adjustment);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user