[Arakhnę-Dev] [224] * Rewrite the josuuid native library to use dmidecode system command to retreive the serial number and UUID of the operating system .

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 224
Author:   galland
Date:     2011-06-03 19:31:46 +0200 (Fri, 03 Jun 2011)
Log Message:
-----------
* Rewrite the josuuid native library to use dmidecode system command to retreive the serial number and UUID of the operating system.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystemNativeWrapper.java
    trunk/arakhneVmutils/native/josuuid/linux32/pom.xml
    trunk/arakhneVmutils/native/josuuid/linux64/pom.xml
    trunk/arakhneVmutils/native/josuuid/mingw32/pom.xml
    trunk/arakhneVmutils/native/josuuid/mingw64/pom.xml
    trunk/arakhneVmutils/native/josuuid/src/main/native/OperatingSystemNativeWrapperJNI.c
    trunk/arakhneVmutils/native/josuuid/src/main/native/winos.c

Added Paths:
-----------
    trunk/arakhneVmutils/native/josuuid/src/main/native/unixos.c
    trunk/arakhneVmutils/native/josuuid/src/main/native/utils.c
    trunk/arakhneVmutils/native/josuuid/src/main/native/utils.h

Removed Paths:
-------------
    trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.c
    trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.h
    trunk/arakhneVmutils/native/josuuid/src/main/native/winos.h

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystem.java	2011-06-03 17:31:46 UTC (rev 224)
@@ -88,6 +88,10 @@
 	 */
     OTHER;
 	
+	private static final String NULL = new String();
+	private static String osSerialNumber = null;
+	private static String osUUID = null;
+	
 	/** Replies if the current OperatingSystem constant is corresponding
 	 * to the current operating system.
 	 * 
@@ -217,24 +221,60 @@
 	}
 
 	/** Get the OS serial number.
+	 * <p>
+	 * This function does not allow to run any system command with
+	 * the super-user rights, and it disable any additional GUI.
 	 * 
 	 * @return the serial number associated to the current operating system.
 	 */
 	public static String getOSSerialNumber() {
-		if (nativeWrapper!=null)
-			return nativeWrapper.getOSSerialNumber();
-		return null;
+		return getOSSerialNumber(false, false);
 	}
 
+	/** Get the OS serial number.
+	 * 
+	 * @param enableSuperUser indicates if the super-user commands are enabled or not.
+	 * @param enableGUI indicates if any additional GUI could be opened, or not.
+	 * @return the serial number associated to the current operating system.
+	 * @sine 6.1
+	 */
+	public static String getOSSerialNumber(boolean enableSuperUser, boolean enableGUI) {
+		if (osSerialNumber==null) {
+			if (nativeWrapper!=null)
+				osSerialNumber = nativeWrapper.getOSSerialNumber(enableSuperUser, enableGUI);
+			if (osSerialNumber==null)
+				osSerialNumber = NULL;
+		}
+		return osSerialNumber==NULL ? null : osSerialNumber;
+	}
+
 	/** Get the OS UUID.
+	 * <p>
+	 * This function does not allow to run any system command with
+	 * the super-user rights, and it disable any additional GUI.
 	 * 
 	 * @return an unique identifier for the current operating system.
 	 */
 	public static String getOSUUID() {
-		if (nativeWrapper!=null)
-			return nativeWrapper.getOSUUID();
-		return null;
+		return getOSUUID(false, false);
 	}
+
+	/** Get the OS UUID.
+	 * 
+	 * @param enableSuperUser indicates if the super-user commands are enabled or not.
+	 * @param enableGUI indicates if any additional GUI could be opened, or not.
+	 * @return an unique identifier for the current operating system.
+	 * @since 6.1
+	 */
+	public static String getOSUUID(boolean enableSuperUser, boolean enableGUI) {
+		if (osUUID==null) {
+			if (nativeWrapper!=null)
+				osUUID = nativeWrapper.getOSUUID(enableSuperUser, enableGUI);
+			if (osUUID==null)
+				osUUID = NULL;
+		}
+		return osUUID==NULL ? null : osUUID;
+	}
 	
 	private static final OperatingSystemNativeWrapper nativeWrapper; 
 	

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystemNativeWrapper.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystemNativeWrapper.java	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/OperatingSystemNativeWrapper.java	2011-06-03 17:31:46 UTC (rev 224)
@@ -44,14 +44,18 @@
 
 	/** Get the OS serial number.
 	 * 
+	 * @param enableSuperUser indicates if the super-user commands are enabled or not.
+	 * @param enableGUI indicates if any additional GUI could be opened, or not.
 	 * @return the serial number associated to the current operating system.
 	 */
-	public native String getOSSerialNumber();
+	public native String getOSSerialNumber(boolean enableSuperUser, boolean enableGUI);
 
 	/** Get the OS UUID.
 	 * 
+	 * @param enableSuperUser indicates if the super-user commands are enabled or not.
+	 * @param enableGUI indicates if any additional GUI could be opened, or not.
 	 * @return an unique identifier for the current operating system.
 	 */
-	public native String getOSUUID();
+	public native String getOSUUID(boolean enableSuperUser, boolean enableGUI);
 
 }

Modified: trunk/arakhneVmutils/native/josuuid/linux32/pom.xml
===================================================================
--- trunk/arakhneVmutils/native/josuuid/linux32/pom.xml	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/linux32/pom.xml	2011-06-03 17:31:46 UTC (rev 224)
@@ -38,7 +38,7 @@
         <configuration>
           <compilerProvider>generic</compilerProvider>
           <compilerStartOptions>
-            <compilerStartOption>-m32 -O3 -Wall -Werror -fmessage-length=0</compilerStartOption>
+            <compilerStartOption>-m32 -fPIC -O3 -Wall -Werror -fmessage-length=0</compilerStartOption>
           </compilerStartOptions>
           
           <javahOS>linux</javahOS>
@@ -47,7 +47,8 @@
             <source>
 	      <directory>../src/main/native</directory>
 	      <fileNames>
-                <fileName>josuuid.c</fileName>
+                <fileName>utils.c</fileName>
+                <fileName>unixos.c</fileName>
                 <fileName>OperatingSystemNativeWrapperJNI.c</fileName>
 	      </fileNames>
             </source>

Modified: trunk/arakhneVmutils/native/josuuid/linux64/pom.xml
===================================================================
--- trunk/arakhneVmutils/native/josuuid/linux64/pom.xml	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/linux64/pom.xml	2011-06-03 17:31:46 UTC (rev 224)
@@ -47,7 +47,8 @@
             <source>
 	      <directory>../src/main/native</directory>
 	      <fileNames>
-                <fileName>josuuid.c</fileName>
+                <fileName>utils.c</fileName>
+                <fileName>unixos.c</fileName>
                 <fileName>OperatingSystemNativeWrapperJNI.c</fileName>
 	      </fileNames>
             </source>

Modified: trunk/arakhneVmutils/native/josuuid/mingw32/pom.xml
===================================================================
--- trunk/arakhneVmutils/native/josuuid/mingw32/pom.xml	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/mingw32/pom.xml	2011-06-03 17:31:46 UTC (rev 224)
@@ -82,7 +82,7 @@
             <source>
 	      <directory>../src/main/native</directory>
 	      <fileNames>
-                <fileName>josuuid.c</fileName>
+                <fileName>utils.c</fileName>
                 <fileName>winos.c</fileName>
                 <fileName>OperatingSystemNativeWrapperJNI.c</fileName>
 	      </fileNames>

Modified: trunk/arakhneVmutils/native/josuuid/mingw64/pom.xml
===================================================================
--- trunk/arakhneVmutils/native/josuuid/mingw64/pom.xml	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/mingw64/pom.xml	2011-06-03 17:31:46 UTC (rev 224)
@@ -82,7 +82,7 @@
             <source>
 	      <directory>../src/main/native</directory>
 	      <fileNames>
-                <fileName>josuuid.c</fileName>
+                <fileName>utils.c</fileName>
                 <fileName>winos.c</fileName>
                 <fileName>OperatingSystemNativeWrapperJNI.c</fileName>
 	      </fileNames>

Modified: trunk/arakhneVmutils/native/josuuid/src/main/native/OperatingSystemNativeWrapperJNI.c
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/OperatingSystemNativeWrapperJNI.c	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/OperatingSystemNativeWrapperJNI.c	2011-06-03 17:31:46 UTC (rev 224)
@@ -1,7 +1,7 @@
 /* 
  * $Id$
  * 
- * Copyright (C) 2005-2010 St&eacute;phane GALLAND
+ * Copyright (C) 2005-2011 St&eacute;phane GALLAND
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -27,8 +27,13 @@
 #include <stdlib.h>
 
 #include "org_arakhne_vmutil_OperatingSystemNativeWrapper.h"
-#include "josuuid.h"
 
+/* Replies the serial number of the system */
+char* getOSSerial(int enableSuperUser, int enableGUI);
+
+/* Replies the UUID of the system */
+char* getOSUUID(int enableSuperUser, int enableGUI);
+
 /*
  * Class:     org_arakhne_vmutil_OperatingSystemNativeWrapper
  * Method:    getOSSerialNumber
@@ -36,9 +41,12 @@
  */
 JNIEXPORT jstring JNICALL
 Java_org_arakhne_vmutil_OperatingSystemNativeWrapper_getOSSerialNumber
-(JNIEnv * env, jclass clazz) {
+(JNIEnv * env, jobject instance, jboolean enableSuperUser,
+ jboolean enableGUI) {
 	jstring jSerial = NULL;
-	char* cSerial = getOSSerial();
+	char* cSerial = getOSSerial(
+						enableSuperUser==JNI_TRUE,
+						enableGUI==JNI_TRUE);
 	if (cSerial!=NULL) {
 		jSerial = (*env)->NewStringUTF (env, cSerial);
 		free(cSerial);
@@ -54,9 +62,12 @@
  */
 JNIEXPORT jstring JNICALL
 Java_org_arakhne_vmutil_OperatingSystemNativeWrapper_getOSUUID
-(JNIEnv *env, jclass clazz) {
+(JNIEnv *env, jobject instance, jboolean enableSuperUser,
+ jboolean enableGUI) {
 	jstring jUUID = NULL;
-	char* cUUID = getOSUUID();
+	char* cUUID = getOSUUID(
+						enableSuperUser==JNI_TRUE,
+						enableGUI==JNI_TRUE);
 	if (cUUID!=NULL) {
 		jUUID = (*env)->NewStringUTF (env, cUUID);
 		free(cUUID);

Deleted: trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.c
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.c	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.c	2011-06-03 17:31:46 UTC (rev 224)
@@ -1,194 +0,0 @@
-/* 
- * $Id$
- * 
- * Copyright (C) 2005-2010 St&eacute;phane GALLAND
- * 
- * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * This program is free software; you can redistribute it and/or modify
- */
- 
-#ifdef DEBUG
-# warning THE LIBRARY IS COMPILED WITH DEBUG INFORMATION
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
-#include "osmacro.h"
-
-#ifdef __WINDOWS__
-# include <windows.h>
-# include "winos.h"
-# define WINUUID_PART_COUNT 4
-static char hexCharacters[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
-#else
-# include <unistd.h>
-# define DWORD unsigned long int
-#endif
-
-#include "josuuid.h"
-
-/* Remove white spaces at the begining and at the end of a string */
-static void trim(char** text) {
-	char* t;
-	unsigned long startIdx, endIdx;
-	unsigned long len, i;
-	if ((text==NULL)||(*text==NULL)) return;
-    len = strlen(*text);
-    // Search starting character
-    for(startIdx=0; startIdx<len; startIdx++) {
-    	if (!isspace((*text)[startIdx])) break;
-    }
-    if (startIdx>=len) {
-    	free(*text);
-    	*text = strdup("");
-    	return;
-    }
-    // Search ending character
-    for(endIdx=len-1; endIdx>=0; endIdx--) {
-    	if (!isspace((*text)[endIdx])) break;
-    }
-    if (endIdx<0) {
-    	free(*text);
-    	*text = strdup("");
-    	return;
-    }
-    // Create the new string
-    t = (char*)malloc(sizeof(char)*(endIdx-startIdx+2));
-    for(i=0; startIdx<=endIdx; i++, startIdx++) {
-    	t[i] = (*text)[startIdx];
-    }
-    t[i] = '\0';
-    free(*text);
-    *text = t;
-}
-
-#ifndef __WINDOWS__
-/* Run the specified shell command and replies its standard output */
-static char* runCommand(const char* cmd) {
-	FILE* cmdOutput;
-	char* result = NULL;
-	
-	cmdOutput = popen(cmd, "r");
-	
-	if (cmdOutput!=NULL) {
-		char buffer[128];
-		unsigned long i,j, count = 0;
-		unsigned long charCount;
-		
-		charCount = fread(buffer, sizeof(char), 128, cmdOutput);
-		while (charCount>0) {
-			result = (char*)realloc(result,sizeof(char)*(count+charCount+1));
-			for(i=0, j=count; i<charCount; i++, j++) {
-				result[j] = buffer[i];
-			} 
-			count += charCount;
-			result[count] = '\0';
-			charCount = fread(buffer, sizeof(char), 128, cmdOutput);
-		}
-	
-		pclose(cmdOutput);
-	}
-	
-    return result;
-}
-#endif
-
-/* Replies the serial number of the system */
-char* getOSSerial() {
-#ifdef __WINDOWS__
-  	DWORD size = 0;
-  	BYTE* data = NULL;
-  	if (getWindowsSerial(&data, &size)) {
-		unsigned long i;
-	  	char* serial = (char*)malloc(sizeof(char)*size);
-	  	for(i=0; i<size; i++) {
-	  		serial[i] = data[i];
-	  	}
-	  	free(data);
-	  	trim(&serial);
-	  	return serial;
-  	}
-  	return NULL;
-#else
-	char* result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.serial");
-	if (result!=NULL) {
-		trim(&result);
-	}
-	else {
-		result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.system.serial");
-		if (result!=NULL) {
-			trim(&result);
-		}
-	}
-	return result;
-#endif
-}
-
-/* Replies the UUID of the system */
-char* getOSUUID() {
-#ifdef __WINDOWS__
-	DWORD size;
-	BYTE* data;
-  	if (getWindowsSerial(&data, &size)) {
-  		unsigned long i, j, k, totalSize = 2*(size-1);
-	  	char* serial = (char*)malloc(sizeof(char)*(totalSize+(totalSize/WINUUID_PART_COUNT)+2));
-  		char characterToTreat;
-  		short b0, b1;
-  		BOOL lastIsSeparator = FALSE;
-	  	
-	  	for(i=0, j=0, k=WINUUID_PART_COUNT; i<size; i++) {
-	  		characterToTreat = data[i];
-	  		if (isalnum(characterToTreat)) {
-		  		b0 = (characterToTreat & 0x0F) ^ 0x0F;
-		  		b1 = ((characterToTreat & 0xF0) >> 4) ^ 0x0F;
-				serial[j++] = hexCharacters[b0];
-				serial[j++] = hexCharacters[b1];
-				k --;
-				if (k<=0) {
-					serial[j++] = '-';
-					k = WINUUID_PART_COUNT;
-					lastIsSeparator = TRUE;
-				}
-				else {
-					lastIsSeparator = FALSE;
-				}
-	  		}
-	  	}
-	  	if (lastIsSeparator) j--;
-	  	serial[j] = '\0';
-	  	free(data);
-	  	
-	  	trim(&serial);
-	  	
-	  	return serial;
-  	}
-  	return NULL;
-#else
-	char* result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid");
-	if (result!=NULL) {
-		trim(&result);
-	}
-	else {
-		result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.system.uuid");
-		if (result!=NULL) {
-			trim(&result);
-		}
-	}
-	return result;
-#endif
-}

Deleted: trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.h
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.h	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/josuuid.h	2011-06-03 17:31:46 UTC (rev 224)
@@ -1,33 +0,0 @@
-/* 
- * $Id$
- * 
- * Copyright (C) 2005-2010 St&eacute;phane GALLAND
- * 
- * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * This program is free software; you can redistribute it and/or modify
- */
- 
-#ifndef __JOSUUID_H__
-#  define __JOSUUID_H__
-
-#  include "osmacro.h"
-
-/* Replies the serial number of the system */
-char* getOSSerial();
-
-/* Replies the UUID of the system */
-char* getOSUUID();
-
-#endif /* __JOSUUID_H__ */

Added: trunk/arakhneVmutils/native/josuuid/src/main/native/unixos.c
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/unixos.c	                        (rev 0)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/unixos.c	2011-06-03 17:31:46 UTC (rev 224)
@@ -0,0 +1,216 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2005-2011 St&eacute;phane GALLAND
+ * 
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+ 
+#ifdef DEBUG
+# warning THE LIBRARY IS COMPILED WITH DEBUG INFORMATION
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+# include <unistd.h>
+
+#include "osmacro.h"
+#include "utils.h"
+ 
+#ifndef __UNIX__
+# error You may use Unix compiler
+#endif
+
+#define DWORD unsigned long int
+#define PATH_SEPARATOR ':' 
+#define FILE_SEPARATOR '/'
+
+/* Replies if the given command is in the PATH variable */
+static int whichCommand(const char* cmd) {
+	char* pathVariable = getenv("PATH");
+	if (pathVariable!=NULL) {
+		FILE* file;
+		int i;
+		char* first;
+		char* filename;
+		int count, count2;
+#ifdef DEBUG
+		printf("PATH=%s\n", pathVariable);
+#endif
+		count = strlen(cmd);
+		i = 0;
+		first = pathVariable;
+		filename = NULL;
+		while (pathVariable[i]!='\0') {
+			if (pathVariable[i]==PATH_SEPARATOR) {
+				pathVariable[i] = '\0';
+				count2 = strlen(first);
+#ifdef DEBUG
+				printf("PATH[%d]=%s\n", i, first);
+#endif
+				filename = (char*)realloc(filename, sizeof(char)*(count+count2+2));
+				strncpy(filename, first, count2);
+				pathVariable[i] = PATH_SEPARATOR;
+				filename[count2] = FILE_SEPARATOR;
+				strncpy(filename+count2+1, cmd, count);
+				filename[count+count2+1] = '\0';
+#ifdef DEBUG
+				printf("TEST: %s\n", filename);
+#endif
+				file = fopen(filename, "rb");
+				if (file!=NULL) {
+					fclose(file);
+#ifdef DEBUG
+					printf("EXECUTABLE: %s\n", filename);
+#endif
+					if (filename!=NULL) {
+						free(filename);
+					}
+					return 1;
+				}
+				first = pathVariable + i + 1;
+			}
+			++i;
+		}
+		if (filename!=NULL) {
+			free(filename);
+		}
+#ifdef DEBUG
+		printf("PATH=%s\n", pathVariable);
+#endif
+	}
+#ifdef DEBUG
+	else {
+		printf("PATH=\n");
+	}
+#endif		
+	return 0;
+}
+
+/* Run the specified shell command and replies its standard output */
+static char* runCommand(const char* cmd) {
+	FILE* cmdOutput;
+	char* result = NULL;
+	
+	cmdOutput = popen(cmd, "r");
+	
+	if (cmdOutput!=NULL) {
+		char buffer[128];
+		unsigned long i,j, count = 0;
+		unsigned long charCount;
+		
+		charCount = fread(buffer, sizeof(char), 128, cmdOutput);
+		while (charCount>0) {
+			result = (char*)realloc(result,sizeof(char)*(count+charCount+1));
+			for(i=0, j=count; i<charCount; i++, j++) {
+				result[j] = buffer[i];
+			} 
+			count += charCount;
+			result[count] = '\0';
+			charCount = fread(buffer, sizeof(char), 128, cmdOutput);
+		}
+	
+		pclose(cmdOutput);
+	}
+	
+    return result;
+}
+
+void iddle() {
+	whichCommand("");
+	runCommand("");
+}
+
+/* Replies the serial number of the system */
+char* getOSSerial(int enableSuperUser, int enableGUI) {
+	char* result = NULL;
+	if (whichCommand("hal-get-property")) {
+		result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.serial");
+		if (result!=NULL) {
+			trim(&result);
+		}
+		else {
+			result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.system.serial");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+		return result;
+	}
+	if (result==NULL && whichCommand("dmidecode")) {
+		if (enableSuperUser && enableGUI && whichCommand("gksudo")) {
+			result = runCommand("gksudo -- dmidecode -s system-serial-number");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+		if (enableSuperUser && result==NULL && whichCommand("sudo")) {
+			result = runCommand("sudo -A -- dmidecode -s system-serial-number");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+		if (result==NULL) {
+			result = runCommand("dmidecode -s system-serial-number");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+	}
+	/* no way to obtain the serial number */
+	return result;
+}
+
+/* Replies the UUID of the system */
+char* getOSUUID(int enableSuperUser, int enableGUI) {
+	char* result = NULL;
+	if (whichCommand("hal-get-property")) {
+		result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid");
+		if (result!=NULL) {
+			trim(&result);
+		}
+		else {
+			result = runCommand("hal-get-property --udi /org/freedesktop/Hal/devices/computer --key smbios.system.uuid");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+	}
+	if (result==NULL && whichCommand("dmidecode")) {
+		if (enableSuperUser && enableGUI && whichCommand("gksudo")) {
+			result = runCommand("gksudo -- dmidecode -s system-uuid");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+		if (enableSuperUser && result==NULL && whichCommand("sudo")) {
+			result = runCommand("sudo -A -- dmidecode -s system-uuid");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+		if (result==NULL) {
+			result = runCommand("dmidecode -s system-uuid");
+			if (result!=NULL) {
+				trim(&result);
+			}
+		}
+	}
+	/* no way to obtain the UUID */
+	return result;
+}

Added: trunk/arakhneVmutils/native/josuuid/src/main/native/utils.c
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/utils.c	                        (rev 0)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/utils.c	2011-06-03 17:31:46 UTC (rev 224)
@@ -0,0 +1,66 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2005-2011 St&eacute;phane GALLAND
+ * 
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+ 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "utils.h"
+
+#ifdef DEBUG
+# warning THE LIBRARY IS COMPILED WITH DEBUG INFORMATION
+#endif
+
+/* Remove white spaces at the begining and at the end of a string */
+void trim(char** text) {
+	char* t;
+	unsigned long startIdx, endIdx;
+	unsigned long len, i;
+	if ((text==NULL)||(*text==NULL)) return;
+    len = strlen(*text);
+    // Search starting character
+    for(startIdx=0; startIdx<len; startIdx++) {
+    	if (!isspace((*text)[startIdx])) break;
+    }
+    if (startIdx>=len) {
+    	free(*text);
+    	*text = strdup("");
+    	return;
+    }
+    // Search ending character
+    for(endIdx=len-1; endIdx>=0; endIdx--) {
+    	if (!isspace((*text)[endIdx])) break;
+    }
+    if (endIdx<0) {
+    	free(*text);
+    	*text = strdup("");
+    	return;
+    }
+    // Create the new string
+    t = (char*)malloc(sizeof(char)*(endIdx-startIdx+2));
+    for(i=0; startIdx<=endIdx; i++, startIdx++) {
+    	t[i] = (*text)[startIdx];
+    }
+    t[i] = '\0';
+    free(*text);
+    *text = t;
+}

Added: trunk/arakhneVmutils/native/josuuid/src/main/native/utils.h
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/utils.h	                        (rev 0)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/utils.h	2011-06-03 17:31:46 UTC (rev 224)
@@ -0,0 +1,27 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2011 St&eacute;phane GALLAND
+ * 
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+ 
+#ifndef __UTILS_H__
+#  define __UTILS_H__
+
+void trim(char** text);
+
+#endif /* __UTILS_H__ */

Modified: trunk/arakhneVmutils/native/josuuid/src/main/native/winos.c
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/winos.c	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/winos.c	2011-06-03 17:31:46 UTC (rev 224)
@@ -1,7 +1,7 @@
 /* 
  * $Id$
  * 
- * Copyright (C) 2005-2010 St&eacute;phane GALLAND
+ * Copyright (C) 2005-2011 St&eacute;phane GALLAND
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -19,22 +19,60 @@
  * This program is free software; you can redistribute it and/or modify
  */
  
-#include "osmacro.h"
- 
 #ifdef DEBUG
 # warning THE LIBRARY IS COMPILED WITH DEBUG INFORMATION
 #endif
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "osmacro.h"
+#include "utils.h"
+ 
 #ifndef __WINDOWS__
 # error You may use Windows compiler
 #endif
 
+#  define W9XFIRST    1
+#  define W95         1
+#  define W95SP1      2
+#  define W95OSR2     3
+#  define W98         4
+#  define W98SP1      5
+#  define W98SE       6
+#  define WME         7
+#  define W9XLAST    99
+
+#  define WNT_FIRST 101
+#  define WNT351    101
+#  define WNT4      102
+#  define W2K       103
+#  define WXP       104
+#  define WNT_LAST  199
+
+#  define WCEFIRST  201
+#  define WCE       201
+#  define WCELAST   299
+
+#  ifndef VER_PLATFORM_WIN32_WINDOWS
+#    define VER_PLATFORM_WIN32_WINDOWS   1
+#  endif
+#  ifndef VER_PLATFORM_WIN32_NT
+#    define VER_PLATFORM_WIN32_NT        2
+#  endif
+#  ifndef VER_PLATFORM_WIN32_CE
+#    define VER_PLATFORM_WIN32_CE        3
+#  endif
+
 #include <windows.h>
 #include <winreg.h>
-#include "winos.h"
 
-#include <stdio.h>
+#define WINUUID_PART_COUNT 4
 
+static char hexCharacters[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
 /* Replies the windows version */
 BOOL getWindowsVersion(DWORD *version) {
   OSVERSIONINFO osinfo;
@@ -84,11 +122,6 @@
 } 
 
 /* Read the value of a registrery value */
-BOOL readRegistry(const CHAR* key, const CHAR* valueName, BYTE** data, DWORD* size) {
-	return readRegistryI(key,valueName,data,size,1);
-}
-
-/* Read the value of a registrery value */
 BOOL readRegistryI(const CHAR* key, const CHAR* valueName, BYTE** data, DWORD* size, BYTE allocationFactor) {
 	HKEY hKey = NULL;  // registry handle, kept open between calls
 	LONG ret;
@@ -136,9 +169,9 @@
 	return TRUE;
 }
 
-/* Replies the serial number of the system */
-BOOL getWindowsSerial(BYTE** serial, DWORD* serialSize) {
-	return getWindowsSerialI(serial, serialSize, 1);
+/* Read the value of a registrery value */
+BOOL readRegistry(const CHAR* key, const CHAR* valueName, BYTE** data, DWORD* size) {
+	return readRegistryI(key,valueName,data,size,1);
 }
 
 /* Replies the serial number of the system */
@@ -176,3 +209,65 @@
   	
   	return TRUE;
 }
+
+/* Replies the serial number of the system */
+BOOL getWindowsSerial(BYTE** serial, DWORD* serialSize) {
+	return getWindowsSerialI(serial, serialSize, 1);
+}
+
+/* Replies the serial number of the system */
+char* getOSSerial(int enableSuperUser, int enableGUI) {
+  	DWORD size = 0;
+  	BYTE* data = NULL;
+  	if (getWindowsSerial(&data, &size)) {
+		unsigned long i;
+	  	char* serial = (char*)malloc(sizeof(char)*size);
+	  	for(i=0; i<size; i++) {
+	  		serial[i] = data[i];
+	  	}
+	  	free(data);
+	  	trim(&serial);
+	  	return serial;
+  	}
+  	return NULL;
+}
+
+/* Replies the UUID of the system */
+char* getOSUUID(int enableSuperUser, int enableGUI) {
+	DWORD size;
+	BYTE* data;
+  	if (getWindowsSerial(&data, &size)) {
+  		unsigned long i, j, k, totalSize = 2*(size-1);
+	  	char* serial = (char*)malloc(sizeof(char)*(totalSize+(totalSize/WINUUID_PART_COUNT)+2));
+  		char characterToTreat;
+  		short b0, b1;
+  		BOOL lastIsSeparator = FALSE;
+	  	
+	  	for(i=0, j=0, k=WINUUID_PART_COUNT; i<size; i++) {
+	  		characterToTreat = data[i];
+	  		if (isalnum(characterToTreat)) {
+		  		b0 = (characterToTreat & 0x0F) ^ 0x0F;
+		  		b1 = ((characterToTreat & 0xF0) >> 4) ^ 0x0F;
+				serial[j++] = hexCharacters[b0];
+				serial[j++] = hexCharacters[b1];
+				k --;
+				if (k<=0) {
+					serial[j++] = '-';
+					k = WINUUID_PART_COUNT;
+					lastIsSeparator = TRUE;
+				}
+				else {
+					lastIsSeparator = FALSE;
+				}
+	  		}
+	  	}
+	  	if (lastIsSeparator) j--;
+	  	serial[j] = '\0';
+	  	free(data);
+	  	
+	  	trim(&serial);
+	  	
+	  	return serial;
+  	}
+  	return NULL;
+}

Deleted: trunk/arakhneVmutils/native/josuuid/src/main/native/winos.h
===================================================================
--- trunk/arakhneVmutils/native/josuuid/src/main/native/winos.h	2011-03-01 09:09:12 UTC (rev 223)
+++ trunk/arakhneVmutils/native/josuuid/src/main/native/winos.h	2011-06-03 17:31:46 UTC (rev 224)
@@ -1,75 +0,0 @@
-/* 
- * $Id$
- * 
- * Copyright (C) 2005-2010 St&eacute;phane GALLAND
- * 
- * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * This program is free software; you can redistribute it and/or modify
- */
- 
-#ifndef __WINOS_H__
-#  define __WINOS_H__
-
-#  include "osmacro.h"
-
-#  define W9XFIRST    1
-#  define W95         1
-#  define W95SP1      2
-#  define W95OSR2     3
-#  define W98         4
-#  define W98SP1      5
-#  define W98SE       6
-#  define WME         7
-#  define W9XLAST    99
-
-#  define WNT_FIRST 101
-#  define WNT351    101
-#  define WNT4      102
-#  define W2K       103
-#  define WXP       104
-#  define WNT_LAST  199
-
-#  define WCEFIRST  201
-#  define WCE       201
-#  define WCELAST   299
-
-#  ifndef VER_PLATFORM_WIN32_WINDOWS
-#    define VER_PLATFORM_WIN32_WINDOWS   1
-#  endif
-#  ifndef VER_PLATFORM_WIN32_NT
-#    define VER_PLATFORM_WIN32_NT        2
-#  endif
-#  ifndef VER_PLATFORM_WIN32_CE
-#    define VER_PLATFORM_WIN32_CE        3
-#  endif
-
-#  include <windows.h>
-
-/* Replies the windows version */
-BOOL getWindowsVersion(DWORD *version);
-
-/* Read the value of a registrery value */
-BOOL readRegistry(const CHAR* key, const CHAR* valueName, BYTE** data, DWORD* size);
-
-/* Read the value of a registrery value */
-BOOL readRegistryI(const CHAR* key, const CHAR* valueName, BYTE** data, DWORD* size, BYTE allocationFactor);
-
-/* Replies the serial number of the system */
-BOOL getWindowsSerial(BYTE** serial, DWORD* serialSize);
-
-/* Replies the serial number of the system */
-BOOL getWindowsSerialI(BYTE** serial, DWORD* serialSize, BYTE allocationFactor);
-
-#endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/