/soc/2012/sanket/statscollector-2.x.y: 02514442728f: Refactor co...

Sanket Agarwal sanket at soc.pidgin.im
Tue Jul 10 00:36:23 EDT 2012


Changeset: 02514442728f9f9843b164d1b2d249d90d21ad20
Author:	 Sanket Agarwal <sanket at soc.pidgin.im>
Date:	 2012-05-24 09:59 +0000
Branch:	 soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/02514442728f

Description:

Refactor code for detecting POSIX

POSIX can be detected by presence of unistd.h and checking if
_POSIX_VERSION is defined. I have henced removed the components where I
was checking __APPLE__ and __LINUX__ etc separately (wherever possible).

Second change is to use ``uname -s'' as far as possible. This ensures that
we are using dynamic-run time information to detect platforms, which is a
good thing if cross-compiling of binaries is done!

diffstat:

 libpurple/plugins/statscollector.c |  91 ++++++++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 33 deletions(-)

diffs (202 lines):

diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
--- a/libpurple/plugins/statscollector.c
+++ b/libpurple/plugins/statscollector.c
@@ -18,14 +18,25 @@
 
 #ifdef _WIN32
 #include<windows.h>
+#endif
 
-#elif defined __APPLE__
+#ifdef __APPLE__
 #include<CoreServices/CoreServices.h>
 #include<sys/types.h>
 #include<sys/sysctl.h>
-#include<sys/utsname.h>
-#include<dlfcn.h>
+#endif
 
+/* POSIX compliance is an issue that I have looked into some detail now
+ * It seems like presence of unistd.h and _POSIX_VERSION being defined
+ * confirms the presence of POSIX compliance. For this ofcourse we need
+ * to ensure that unistd.h is defined. As it is *already* a part of
+ * internal.h, we could directly go ahead with the testing if the
+ * system is POSIX or not
+ */
+
+#ifdef _POSIX_VERSION
+# include <sys/utsname.h>
+# include <dlfcn.h>
 #endif
 
 #define STATS_MAJOR_VERSION 1
@@ -133,20 +144,26 @@
   int bit_size=-1;
   xmlnode *bit_size_xml;
 #ifdef _WIN32
+
   BOOL bIsWow64;
   typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
   LPFN_ISWOW64PROCESS fnIsWow64Process;
-#elif (defined __USE_POSIX) || (defined __APPLE__) /* What to use for APPLE */
+
+#elif defined _POSIX_VERSION
+
   struct utsname os_utsname;
   char *m_name;
+
 #endif
 
   bit_size_xml = xmlnode_new("os-bit");
 
 #ifdef _WIN64
+
   bit_size = 64;
 
 #elif defined _WIN32
+
   /* Check if we are running as wow64 process */
   bIsWow64 = FALSE;
   fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress( \
@@ -162,7 +179,8 @@
   if(bIsWow64) bit_size= 64;
   else bit_size = 32;
 
-#elif (defined __USE_POSIX) || (defined __APPLE__)
+#elif _POSIX_VERSION
+
   /* Use uname to find kernel architecture and infer bit-size */
   uname(&os_utsname);
   m_name = os_utsname.machine;
@@ -175,6 +193,7 @@
       || !g_strcmp0(m_name, "amd64") || !g_strcmp0(m_name, "ppc64")) \
     bit_size = 64;
   else bit_size = 32;
+
 #endif
 
   xmlnode_insert_data(bit_size_xml, g_strdup_printf("%d", bit_size), -1);
@@ -190,23 +209,20 @@
 
   xmlnode *arch_xml;
   char *arch_str;
-#if (defined __USE_POSIX) || (defined __APPLE__)
-  struct utsname os_utsname;
-#elif defined _WIN32
+
+#ifdef _WIN32
   SYSTEM_INFO sys_info;
   typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
   typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
   PGNSI pGNSI;
+#elif defined _POSIX_VERSION
+  struct utsname os_utsname;
 #endif
 
   arch_xml = xmlnode_new("arch");
 
 
-#if (defined __USE_POSIX) || (defined __APPLE__)
-  uname(&os_utsname);
-  arch_str = os_utsname.machine;
-
-#elif defined _WIN32
+#ifdef _WIN32
   pGNSI = (PGNSI) GetProcAddress(
     GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
 
@@ -228,6 +244,9 @@
     default:
       arch_str = "UNKNOWN";
   }
+#elif defined _POSIX_VERSION
+  uname(&os_utsname);
+  arch_str = os_utsname.machine;
 #endif
 
   xmlnode_set_attrib(arch_xml, "id", arch_str);
@@ -245,15 +264,14 @@
 
   xmlnode *os_name_xml;
   char *name_attrib;
+
 #ifdef _WIN32
+
   xmlnode *major_version, *minor_version;
   OSVERSIONINFO osvi;
 
-#elif defined __USE_POSIX
-  xmlnode *os_release, *os_version;
-  struct utsname os_utsname;
+#elif defined __APPLE__
 
-#elif defined __APPLE__
   int mib[2];
   size_t length;
   SInt32 major_version,minor_version,bug_fix_version;
@@ -275,17 +293,22 @@
   sdl_library = dlopen("/System/Library/Frameworks/CoreServices.framework/CoreServices", RTLD_LAZY); // we assume standard location for framework
   gestalt_ext = dlsym(sdl_library, "Gestalt");
 
+#elif defined _POSIX_VERSION
+
+  xmlnode *os_release, *os_version;
+  struct utsname os_utsname;
+  uname(&os_utsname);
+
 #endif
 
-
   os_name_xml = xmlnode_new("os-info");
 
 #ifdef _WIN32
   name_attrib = "windows";
-#elif defined __linux__
-  name_attrib = "linux";
-#elif defined __APPLE__
-  name_attrib = "apple";
+#elif defined _POSIX_VERSION
+  name_attrib = os_utsname.sysname;
+#else
+  name_attrib = "unknown";
 #endif
 
   xmlnode_set_attrib(os_name_xml, "id", name_attrib);
@@ -306,16 +329,6 @@
   xmlnode_insert_child(os_name_xml, major_version);
   xmlnode_insert_child(os_name_xml, minor_version);
 
-#elif defined __USE_POSIX
-  uname(&os_utsname);
-  os_release = xmlnode_new("release");
-  os_version = xmlnode_new("version");
-  xmlnode_insert_data(os_release, os_utsname.release, -1);
-  xmlnode_insert_data(os_version, os_utsname.version, -1);
-
-  xmlnode_insert_child(os_name_xml, os_release);
-  xmlnode_insert_child(os_name_xml, os_version);
-
 #elif defined __APPLE__
 
   major_version_xml = xmlnode_new("major-version");
@@ -390,7 +403,19 @@
 
   }
 
-#endif /* APPLE block */
+ /* APPLE block ends */
+
+#elif defined _POSIX_VERSION
+
+  os_release = xmlnode_new("release");
+  os_version = xmlnode_new("version");
+  xmlnode_insert_data(os_release, os_utsname.release, -1);
+  xmlnode_insert_data(os_version, os_utsname.version, -1);
+
+  xmlnode_insert_child(os_name_xml, os_release);
+  xmlnode_insert_child(os_name_xml, os_version);
+
+#endif
 
   return os_name_xml;
 



More information about the Commits mailing list