Delete trailing whitespace.
[freeradius.git] / src / lib / missing.c
index 06425bd..9c0c580 100644 (file)
  *
  *   You should have received a copy of the GNU Lesser General Public
  *   License along with this library; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
- * Copyright 2000  The FreeRADIUS server project
+ * Copyright 2000,2006  The FreeRADIUS server project
  */
 
-static const char rcsid[] = "$Id$";
+#include       <freeradius-devel/ident.h>
+RCSID("$Id$")
 
-#include       <freeradius-devel/autoconf.h>
+#include       <freeradius-devel/libradius.h>
 
-#include       <stdio.h>
-#include       <stdlib.h>
-#include       <sys/types.h>
-#include       <sys/socket.h>
-#include       <netinet/in.h>
-#include       <arpa/inet.h>
 #include       <ctype.h>
 
-#include       <freeradius-devel/missing.h>
-#include       <freeradius-devel/libradius.h>
-
 #ifndef HAVE_CRYPT
 char *crypt(char *key, char *salt)
 {
@@ -100,19 +92,6 @@ int inet_aton(char *cp, struct in_addr *inp)
 }
 #endif
 
-#ifndef HAVE_GETHOSTNAME
-int gethostname(char *name, int len)
-{
-       char            *h;
-
-       h = getenv("HOSTNAME");
-       if (!h || (strlen(h) + 1 > len))
-               return -1;
-       strcpy(name, h);
-       return 0;
-}
-#endif
-
 #ifndef HAVE_STRSEP
 /*
  *     Get next token from string *stringp, where tokens are
@@ -212,3 +191,45 @@ struct tm *gmtime_r(const time_t *l_clock, struct tm *result)
   return result;
 }
 #endif
+
+#ifndef HAVE_GETTIMEOFDAY
+#ifdef WIN32
+/*
+ * Number of micro-seconds between the beginning of the Windows epoch
+ * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
+ *
+ * This assumes all Win32 compilers have 64-bit support.
+ */
+#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
+#define DELTA_EPOCH_IN_USEC  11644473600000000Ui64
+#else
+#define DELTA_EPOCH_IN_USEC  11644473600000000ULL
+#endif
+
+static uint64_t filetime_to_unix_epoch (const FILETIME *ft)
+{
+       uint64_t res = (uint64_t) ft->dwHighDateTime << 32;
+
+       res |= ft->dwLowDateTime;
+       res /= 10;                   /* from 100 nano-sec periods to usec */
+       res -= DELTA_EPOCH_IN_USEC;  /* from Win epoch to Unix epoch */
+       return (res);
+}
+
+int gettimeofday (struct timeval *tv, UNUSED void *tz)
+{
+       FILETIME  ft;
+       uint64_t tim;
+
+       if (!tv) {
+               errno = EINVAL;
+               return (-1);
+       }
+        GetSystemTimeAsFileTime (&ft);
+        tim = filetime_to_unix_epoch (&ft);
+        tv->tv_sec  = (long) (tim / 1000000L);
+        tv->tv_usec = (long) (tim % 1000000L);
+        return (0);
+}
+#endif
+#endif