Delete trailing whitespace.
[freeradius.git] / src / lib / missing.c
index f95e107..9c0c580 100644 (file)
@@ -4,22 +4,30 @@
  *
  * Version:    $Id$
  *
+ *   This library is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU Lesser General Public
+ *   License as published by the Free Software Foundation; either
+ *   version 2.1 of the License, or (at your option) any later version.
+ *
+ *   This library is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ *   Lesser General Public License for more details.
+ *
+ *   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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ *
+ * Copyright 2000,2006  The FreeRADIUS server project
  */
 
-static const char rcsid[] = "$Id$";
+#include       <freeradius-devel/ident.h>
+RCSID("$Id$")
 
-#include       "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       "missing.h"
-
 #ifndef HAVE_CRYPT
 char *crypt(char *key, char *salt)
 {
@@ -84,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 (strlen(h) + 1 > len)
-               return -1;
-       strcpy(name, h);
-       return 0;
-}
-#endif
-
 #ifndef HAVE_STRSEP
 /*
  *     Get next token from string *stringp, where tokens are
@@ -151,10 +146,90 @@ strsep(char **stringp, const char *delim)
  *     Even if localtime is NOT re-entrant, this function will
  *     lower the possibility of race conditions.
  */
-struct tm *localtime_r(const time_t *clock, struct tm *result)
+struct tm *localtime_r(const time_t *l_clock, struct tm *result)
+{
+  memcpy(result, localtime(l_clock), sizeof(*result));
+
+  return result;
+}
+#endif
+
+#ifndef HAVE_CTIME_R
+/*
+ *     We use ctime_r() by default in the server.
+ *
+ *     For systems which do NOT have ctime_r(), we make the
+ *     assumption that ctime() is re-entrant, and returns a
+ *     per-thread data structure.
+ *
+ *     Even if ctime is NOT re-entrant, this function will
+ *     lower the possibility of race conditions.
+ */
+char *ctime_r(const time_t *l_clock, char *l_buf)
+{
+  strcpy(l_buf, ctime(l_clock));
+
+  return l_buf;
+}
+#endif
+
+#ifndef HAVE_GMTIME_R
+/*
+ *     We use gmtime_r() by default in the server.
+ *
+ *     For systems which do NOT have gmtime_r(), we make the
+ *     assumption that gmtime() is re-entrant, and returns a
+ *     per-thread data structure.
+ *
+ *     Even if gmtime is NOT re-entrant, this function will
+ *     lower the possibility of race conditions.
+ */
+struct tm *gmtime_r(const time_t *l_clock, struct tm *result)
 {
-  *result = localtime(clock);
+  memcpy(result, gmtime(l_clock), sizeof(*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