util: Don't cast returns from malloc()
[openafs.git] / src / util / hostparse.c
index 86361f6..9caf422 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
-#ifdef UKERNEL
-#include "afs/sysincludes.h"
-#include "afs/afsutil.h"
-#include <netdb.h>
-#include <ctype.h>
-#else /* UKERNEL */
-#include <stdio.h>
-#include <sys/types.h>
-#include <stdlib.h>
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <direct.h>
 #else
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <netdb.h>
 #include <ctype.h>
 #endif
-#include <string.h>
-#include <errno.h>
-#include "afsutil.h"
-#endif /* UKERNEL */
 
+#include "afsutil.h"
 
 /* also parse a.b.c.d addresses */
 struct hostent *
-hostutil_GetHostByName(register char *ahost)
+hostutil_GetHostByName(char *ahost)
 {
-    register int tc;
+    int tc;
     static struct hostent thostent;
     static char *addrp[2];
     static char addr[4];
-    register char *ptr = ahost;
+    char *ptr = ahost;
     afs_uint32 tval, numeric = 0;
     int dots = 0;
 
@@ -133,10 +117,10 @@ hostutil_GetNameByINet(afs_uint32 addr)
     return tbuffer;
 }
 
-/* the parameter is a pointer to a buffer containing a string of 
-** bytes of the form 
+/* the parameter is a pointer to a buffer containing a string of
+** bytes of the form
 ** w.x.y.z     # machineName
-** returns the network interface in network byte order 
+** returns the network interface in network byte order
 */
 afs_uint32
 extractAddr(char *line, int maxSize)
@@ -233,20 +217,7 @@ extractAddr(char *line, int maxSize)
     return val;
 }
 
-/* 
-** converts a 4byte IP address into a static string (e.g. w.x.y.z) 
-** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
-** causes a memory fault. 
-*/
-char *
-afs_inet_ntoa(afs_uint32 addr)
-{
-    struct in_addr temp;
-    temp.s_addr = addr;
-    return (char *)inet_ntoa(temp);
-}
-
-/* same as above, but to a non-static buffer, must be freed by called */
+/* same as inet_ntoa, but to a non-static buffer, must be freed by called */
 char *
 afs_inet_ntoa_r(afs_uint32 addr, char *buf)
 {
@@ -273,11 +244,11 @@ gettmpdir(void)
 
     if (saveTmpDir == NULL) {
        /* initialize global temporary directory string */
-       char *dirp = (char *)malloc(MAX_PATH);
+       char *dirp = malloc(MAX_PATH+1);
        int freeDirp = 1;
 
        if (dirp != NULL) {
-           DWORD pathLen = GetTempPath(MAX_PATH, dirp);
+           DWORD pathLen = GetTempPath(MAX_PATH+1, dirp);
 
            if (pathLen == 0 || pathLen > MAX_PATH) {
                /* can't get tmp path; get cur work dir */
@@ -299,7 +270,7 @@ gettmpdir(void)
                }
            }
        }
-       /* dirp != NULL */
+
        if (dirp != NULL) {
            FilepathNormalize(dirp);
        } else {
@@ -308,28 +279,12 @@ gettmpdir(void)
            freeDirp = 0;
        }
 
-       /* atomically initialize shared buffer pointer IF still null */
-
-#if 0
-       if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
-           /* shared buffer pointer already initialized by another thread */
-           if (freeDirp) {
-               free(dirp);
-           }
-       }                       /* interlock xchng */
-#endif
-
-       /* Above is what we really want to do, but Windows 95 does not have
-        * InterlockedCompareExchange().  So we just atomically swap
-        * the buffer pointer values but we do NOT deallocate the
-        * previously installed buffer, if any, in case it is in use.
-        */
-#ifdef _WIN64
-       InterlockedExchange64((LONGLONG)(INT_PTR)&saveTmpDir, (LONGLONG) dirp);
-#else
-       InterlockedExchange((LONG) & saveTmpDir, (LONG) dirp);
-#endif
-
+        /* atomically initialize shared buffer pointer IF still null */
+        if (InterlockedCompareExchangePointer(&saveTmpDir, dirp, NULL) != NULL) {
+            /* shared buffer pointer already initialized by another thread */
+            if (freeDirp)
+                free(dirp);
+        }
     }
     /* if (!saveTmpDir) */
     tmpdirp = saveTmpDir;