Subject: rdate source
To: None <current-users@sun-lamp.cs.berkeley.edu>
From: Christos Zoulas <christos@deshaw.com>
List: current-users
Date: 04/30/1994 14:45:04
Someone asked for it on the net, so here is a copy.

christos

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  rdate.c rdate.1
# Wrapped by christos@cs2 on Sat Apr 30 14:44:13 1994
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'rdate.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'rdate.c'\"
else
echo shar: Extracting \"'rdate.c'\" \(2577 characters\)
sed "s/^X//" >'rdate.c' <<'END_OF_FILE'
X/* $Header: /home/hyperion/mu/christos/src/mine/sys/rdate/RCS/rdate.c,v 1.3 1991/05/30 22:13:23 christos Exp $ */
X/*
X * rdate.c: Set the date from the specified host
X *
X * 	Uses the rfc868 time protocol at socket 37.
X *	Time is returned as the number of seconds since
X *	midnight January 1st 1900.
X */
X#ifndef lint
Xstatic char rcsid[] = "$Id: rdate.c,v 1.3 1991/05/30 22:13:23 christos Exp $";
X#endif /* lint */
X
X#include <sys/types.h>
X#include <sys/param.h>
X#include <stdio.h>
X#include <ctype.h>
X#include <string.h>
X#include <sys/time.h>
X#include <sys/socket.h>
X#include <netdb.h>
X#include <netinet/in.h>
X
X/* seconds from midnight Jan 1900 - 1970 */
X#if __STDC__
X# define DIFFERENCE 2208988800UL
X#else
X# define DIFFERENCE 2208988800
X#endif
X
Xint
Xmain(argc, argv)
X    int argc;
X    char *argv[];
X{
X    int pr = 0, silent = 0, s;
X    time_t tim;
X    char *hname;
X    struct hostent *hp;
X    struct protoent *pp, ppp;
X    struct servent  *sp, ssp;
X    struct sockaddr_in sa;
X    extern char* __progname;
X    extern int optind;
X    int c;
X
X    while ((c = getopt(argc, argv, "ps")) != -1)
X	switch (c) {
X	case 'p':
X	    pr++;
X	    break;
X
X	case 's':
X	    silent++;
X	    break;
X
X	default:
X	    goto usage;
X	}
X
X    if (argc - 1 != optind) {
Xusage:
X	(void) fprintf(stderr, "Usage: %s [-ps] <host>.\n", __progname);
X	return(1);
X    }
X
X    hname = argv[optind];
X
X    if (isdigit(hname[0])) {
X	if ((hp = gethostbyaddr(hname, sizeof(struct sockaddr_in),
X			        AF_INET)) == NULL)
X	    err(1, "Unknown host address %s", hname);
X    }
X    else {
X	if ((hp = gethostbyname(hname)) == NULL)
X	    err(1, "Unknown host name %s", hname);
X    }
X
X    if ((sp = getservbyname("time", "tcp")) == NULL) {
X	sp = &ssp;
X	sp->s_port = 37;
X	sp->s_proto = "tcp";
X    }
X
X    if ((pp = getprotobyname(sp->s_proto)) == NULL) {
X	pp = &ppp;
X	pp->p_proto = 6;
X    }
X
X    if ((s = socket(AF_INET, SOCK_STREAM, pp->p_proto)) == -1)
X	err(1, "Could not create socket");
X
X    sa.sin_family = AF_INET;
X    sa.sin_port = sp->s_port;
X
X    memcpy(&(sa.sin_addr.s_addr), hp->h_addr, hp->h_length);
X    memset(sa.sin_zero, 0, sizeof(sa.sin_zero));
X
X    if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) == -1)
X	err(1, "Could not connect socket");
X
X    if (read(s, &tim, sizeof(time_t)) != sizeof(time_t))
X	err(1, "Could not read data");
X
X    (void) close(s);
X    tim = ntohl(tim) - DIFFERENCE;
X
X    if (!pr) {
X	struct timeval tv;
X	tv.tv_sec = tim;
X	tv.tv_usec = 0;
X	if (settimeofday(&tv, NULL) == -1)
X	    err(1, "Could not set time of day");
X    }
X
X    if (!silent)
X	(void) fputs(ctime(&tim), stdout);
X    return 0;
X}
END_OF_FILE
if test 2577 -ne `wc -c <'rdate.c'`; then
    echo shar: \"'rdate.c'\" unpacked with wrong size!
fi
# end of 'rdate.c'
fi
if test -f 'rdate.1' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'rdate.1'\"
else
echo shar: Extracting \"'rdate.1'\" \(514 characters\)
sed "s/^X//" >'rdate.1' <<'END_OF_FILE'
X.Dd April 30, 1994
X.Dt RDATE 1
X.Os
X.Sh NAME
X.Nm rdate
X.Nd set the system's date from a remote host
X.Sh SYNOPSIS
X.Nm rdate
X.Op Fl ps
X.Ar host 
X.Sh DESCRIPTION
X.Nm Rdate
Xdisplays and sets the local date and time from the
Xhost name or address given as the argument. It uses the RFC868
Xprotocol which is usually implemented as a built-in service of
X.Xr inetd(1).
X.Pp
XAvailable options:
X.Pp
X.Bl -tag -width indent
X.It Fl p
XDo not set, just print the remote time
X.It Fl s
XDo not print the time.
X.Sh SEE ALSO
X.Xr inetd 1
END_OF_FILE
if test 514 -ne `wc -c <'rdate.1'`; then
    echo shar: \"'rdate.1'\" unpacked with wrong size!
fi
# end of 'rdate.1'
fi
echo shar: End of shell archive.
exit 0

------------------------------------------------------------------------------