upload tizen2.0 source
[framework/uifw/xorg/lib/libsm.git] / src / sm_genid.c
old mode 100755 (executable)
new mode 100644 (file)
index 2cd6d26..c6731d8
@@ -1,6 +1,6 @@
 /*
 
-Copyright © 2002 Sun Microsystems, Inc.  All rights reserved.
+Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
 
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
@@ -53,15 +53,15 @@ in this Software without prior written authorization from The Open Group.
  */
 
 #ifdef WIN32
-#define _WILLWINSOCK_
+# define _WILLWINSOCK_
 #endif
 #ifdef HAVE_CONFIG_H
-#include <config.h>
+# include <config.h>
 #endif
 #include <X11/SM/SMlib.h>
 #include "SMlibint.h"
 #ifdef XTHREADS
-#include <X11/Xthreads.h>
+# include <X11/Xthreads.h>
 #endif
 #include <stdio.h>
 
@@ -70,39 +70,35 @@ in this Software without prior written authorization from The Open Group.
 
 #ifndef WIN32
 
-#if defined(TCPCONN) || defined(STREAMSCONN)
-#ifndef Lynx
-#include <sys/socket.h>
-#else
-#include <socket.h>
-#endif
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#define XOS_USE_NO_LOCKING
-#define X_INCLUDE_NETDB_H
-#include <X11/Xos_r.h>
-#endif
+# if defined(TCPCONN) || defined(STREAMSCONN)
+#  include <sys/socket.h>
+#  include <netinet/in.h>
+#  include <arpa/inet.h>
+#  define XOS_USE_NO_LOCKING
+#  define X_INCLUDE_NETDB_H
+#  include <X11/Xos_r.h>
+# endif
 
 #else /* WIN32 */
 
-#include <X11/Xwinsock.h>
-#include <X11/Xw32defs.h>
-#define X_INCLUDE_NETDB_H
-#define XOS_USE_MTSAFE_NETDBAPI
-#include <X11/Xos_r.h>
+# include <X11/Xwinsock.h>
+# include <X11/Xw32defs.h>
+# define X_INCLUDE_NETDB_H
+# define XOS_USE_MTSAFE_NETDBAPI
+# include <X11/Xos_r.h>
 
 #endif /* WIN32 */
 
 #ifdef MNX_TCPCONN
-#include <net/gen/netdb.h>
+# include <net/gen/netdb.h>
 
-#define TCPCONN
+# define TCPCONN
 #endif
 
 #if defined(HAVE_UUID_CREATE)
-#include <uuid.h>
+# include <uuid.h>
 #elif defined(HAVE_LIBUUID)
-#include <uuid/uuid.h>
+# include <uuid/uuid.h>
 #endif
 
 \f
@@ -139,45 +135,45 @@ SmsGenerateClientID(SmsConn smsConn)
     temp[1] = '\0';
     uuid_unparse_lower(uuid, &temp[1]);
 
-    if ((id = malloc (strlen (temp) + 1)) != NULL)
-       strcpy (id, temp);
+    id = strdup (temp);
 
     return id;
 #else
-#if defined(TCPCONN) || defined(STREAMSCONN)
+# if defined(TCPCONN) || defined(STREAMSCONN)
     static const char hex[] = "0123456789abcdef";
     char hostname[256];
     char address[64], *addr_ptr = address;
     char temp[256];
     char *id;
     static int sequence = 0;
-
-    if (gethostname (hostname, sizeof (hostname)))
-       return (NULL);
-
-    {
     char* inet_addr;
-    char temp[4], *ptr1, *ptr2;
+    char *ptr1;
     unsigned char decimal[4];
     int i, len;
     struct in_addr *haddr = NULL;
-#if defined(IPv6) && defined(AF_INET6)
+#  if defined(IPv6) && defined(AF_INET6)
     struct addrinfo *ai, *first_ai;
+#  endif
+
+    if (gethostname (hostname, sizeof (hostname)))
+       return (NULL);
+
+#  if defined(IPv6) && defined(AF_INET6)
     if (getaddrinfo(hostname,NULL,NULL,&ai) != 0)
        return NULL;
 
     for (first_ai = ai; ai != NULL; ai = ai->ai_next) {
-       if ( (ai->ai_family == AF_INET) || (ai->ai_family == AF_INET6) ) 
+       if ( (ai->ai_family == AF_INET) || (ai->ai_family == AF_INET6) )
            break;
     }
     if (ai == NULL) {
        freeaddrinfo(first_ai);
        return NULL;
-    } 
+    }
 
     if (ai->ai_family == AF_INET6) {
        unsigned char *cp = (unsigned char *) &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr.s6_addr;
-       
+
        *addr_ptr++ = '6';      /* IPv6 address code */
 
        for (i = 0 ; i < 16 ; i++) {
@@ -189,51 +185,52 @@ SmsGenerateClientID(SmsConn smsConn)
 
     } else { /* Fall through to IPv4 address handling */
        haddr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
-#else
-#ifdef XTHREADS_NEEDS_BYNAMEPARAMS
-    _Xgethostbynameparams hparams;
-#endif
-    struct hostent *hostp;
-
-    if ((hostp = _XGethostbyname (hostname,hparams)) != NULL)
-       haddr = (struct in_addr *)(hostp->h_addr);
-    else
-       return NULL;
-#endif
-
-    inet_addr = inet_ntoa (*haddr);
-    for (i = 0, ptr1 = inet_addr; i < 3; i++)
-    {
-       ptr2 = strchr (ptr1, '.');
-       len = ptr2 - ptr1;
-       if (!ptr2 || len > 3) {
-#if defined(IPv6) && defined(AF_INET6)
-           freeaddrinfo(first_ai);
-#endif
-           return (NULL);
+#  else
+#   ifdef XTHREADS_NEEDS_BYNAMEPARAMS
+       _Xgethostbynameparams hparams;
+#   endif
+       struct hostent *hostp;
+
+       if ((hostp = _XGethostbyname (hostname,hparams)) != NULL)
+           haddr = (struct in_addr *)(hostp->h_addr);
+       else
+           return NULL;
+#  endif
+
+       inet_addr = inet_ntoa (*haddr);
+       for (i = 0, ptr1 = inet_addr; i < 3; i++)
+       {
+           char temp4[4];
+           char *ptr2 = strchr (ptr1, '.');
+
+           len = ptr2 - ptr1;
+           if (!ptr2 || len > 3) {
+#  if defined(IPv6) && defined(AF_INET6)
+               freeaddrinfo(first_ai);
+#  endif
+               return (NULL);
+           }
+           strncpy (temp4, ptr1, len);
+           temp4[len] = '\0';
+           decimal[i] = atoi (temp4);
+           ptr1 = ptr2 + 1;
        }
-       strncpy (temp, ptr1, len);
-       temp[len] = '\0';
-       decimal[i] = atoi (temp);
-       ptr1 = ptr2 + 1;
-    }
 
-    decimal[3] = atoi (ptr1);
+       decimal[3] = atoi (ptr1);
 
-    *addr_ptr++ = '1';
+       *addr_ptr++ = '1';
 
-    for (i = 0; i < 4; i++) {
-        *addr_ptr++ = hex[decimal[i] >> 4];
-        *addr_ptr++ = hex[decimal[i] & 0x0f];
-    }
+       for (i = 0; i < 4; i++) {
+           *addr_ptr++ = hex[decimal[i] >> 4];
+           *addr_ptr++ = hex[decimal[i] & 0x0f];
+       }
 
-    *addr_ptr++ = '\0';
+       *addr_ptr++ = '\0';
 
-#if defined(IPv6) && defined(AF_INET6)
+#  if defined(IPv6) && defined(AF_INET6)
     }
     freeaddrinfo(first_ai);
-#endif
-    }
+#  endif
 
     sprintf (temp, "1%s%.13ld%.10ld%.4d", address, (long)time((Time_t*)0),
             (long)getpid(), sequence);
@@ -241,12 +238,11 @@ SmsGenerateClientID(SmsConn smsConn)
     if (++sequence > 9999)
        sequence = 0;
 
-    if ((id = malloc (strlen (temp) + 1)) != NULL)
-       strcpy (id, temp);
+    id = strdup (temp);
 
     return (id);
-#else
+# else
     return (NULL);
-#endif
+# endif
 #endif
 }