home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / slurp / part02 / time.c < prev   
Encoding:
C/C++ Source or Header  |  1993-03-12  |  1.5 KB  |  72 lines

  1. /*
  2.  * time - obtain the time from the remote server
  3.  *
  4.  * Copyright (C) 1992/93 Stephen Hebditch. All rights reserved.
  5.  * TQM Communications, BCM Box 225, London, WC1N 3XX.
  6.  * steveh@orbital.demon.co.uk  +44 836 825962
  7.  *
  8.  * See README for more information and disclaimers
  9.  *
  10.  * Obtain the current time from the remote server in standard unix time
  11.  * format for use with the next NEWNEWS. If the client is unable to
  12.  * connect to the time server or the read fails then the error is
  13.  * reported and the program is exited.
  14.  *
  15.  * $Id: time.c,v 1.5 1993/03/01 18:09:12 root Exp $
  16.  *
  17.  * $Log: time.c,v $
  18.  * Revision 1.5  1993/03/01  18:09:12  root
  19.  * Made the epoch constant an unsigned long.
  20.  *
  21.  * Revision 1.4  1993/02/14  15:10:01  root
  22.  * No changes.
  23.  *
  24.  * Revision 1.0  1992/08/92
  25.  * Initial coding.
  26.  *
  27.  */
  28.  
  29. #include "slurp.h"
  30.  
  31. #include <unistd.h>
  32. #include <netinet/in.h>
  33.  
  34.  
  35.     time_t
  36. server_time (char *hostname)
  37.     {
  38.     int server, ret;
  39.     u_long timebuf;
  40.  
  41. /* First open the socket */
  42.  
  43.     if ((server = tcp_open (hostname, "time")) < 0)
  44.         return ((time_t) 0);
  45.  
  46.     ret = read (server, &timebuf, 4);
  47.  
  48. /* Close the socket and check we got 4 bytes */
  49.  
  50.     (void) close (server);
  51.  
  52.     if (ret != 4)
  53.         {
  54.         log_ret ("server_time: Read error on time server socket");
  55.         return ((time_t) 0);
  56.         }
  57.  
  58. /* Convert byte order if needed */
  59.  
  60.     timebuf = ntohl (timebuf);
  61.  
  62.     if (debug_flag)
  63.         (void) fprintf (stderr, "time is currently %ld at server %s\n",
  64.                         timebuf, hostname);
  65.  
  66. /* Convert the time from seconds since 1900 to seconds since 1970 */
  67.  
  68.     return ((time_t) (timebuf - 2208988800UL));
  69.     }
  70.  
  71. /* END-OF-FILE */
  72.