Commits

Jason Perkins committed 98dca8e

Pull 25: Add Unix support to os.getversion() (wfgleper)

Comments (0)

Files changed (3)

 * Patch 3441850: Use debug.traceback() as error handler (Konstantin Tokarev)
 * Patch 3462994: Make flag values case-insensitive (Konstantin Tokarev)
 * Patch 3466877: Removed -flat_namespace from Mac OS X flags (Konstantin Tokarev)
+* Pull 25: Add Unix support to os.getversion() (wfgleper)
 
 
 -------

src/host/os_getversion.c

 /**
  * \file   os_getversioninfo.c
  * \brief  Retrieve operating system version information.
- * \author Copyright (c) 2011 Jason Perkins and the Premake project
+ * \author Copyright (c) 2011-2012 Jason Perkins and the Premake project
  */
 
 #include "premake.h"
+#include <stdlib.h>
 
 struct OsVersionInfo
 {
 	int minorversion;
 	int revision;
 	const char* description;
-} ;
+	int isalloc;
+};
 
 static void getversion(struct OsVersionInfo* info);
 
+
 int os_getversion(lua_State* L)
 {
-	struct OsVersionInfo info;
+	struct OsVersionInfo info = {0};
 	getversion(&info);
 
 	lua_newtable(L);
 	lua_pushstring(L, info.description);
 	lua_settable(L, -3);
 
+	if (info.isalloc) {
+		free((void*)info.description);
+	}
+
 	return 1;
 }
 
 
 /*************************************************************/
 
+#elif defined(PLATFORM_BSD) || defined(PLATFORM_LINUX) || defined(PLATFORM_SOLARIS)
+
+#include <string.h>
+#include <sys/utsname.h>
+
+void getversion(struct OsVersionInfo* info)
+{
+	struct utsname u;
+	char* ver;
+
+	info->majorversion = 0;
+	info->minorversion = 0;
+	info->revision = 0;
+
+	if (uname(&u))
+	{
+		// error
+		info->description = PLATFORM_STRING;
+		return;
+	}
+
+#if __GLIBC__
+	// When using glibc, info->description gets set to u.sysname,
+	// but it isn't passed out of this function, so we need to copy 
+	// the string.
+	info->description = malloc(strlen(u.sysname) + 1);
+	strcpy((char*)info->description, u.sysname);
+	info->isalloc = 1;
+#else
+	info->description = u.sysname;
+#endif
+
+	if ((ver = strtok(u.release, ".-")) != NULL)
+	{
+		info->majorversion = atoi(ver);
+		// continue parsing from the previous position
+		if ((ver = strtok(NULL, ".-")) != NULL)
+		{
+			info->minorversion = atoi(ver);
+			if ((ver = strtok(NULL, ".-")) != NULL)
+				info->revision = atoi(ver);
+		}
+	}
+}
+
+/*************************************************************/
+
 #else
 
 void getversion(struct OsVersionInfo* info)

src/host/premake.h

 #if defined(__linux__)
 #define PLATFORM_LINUX    (1)
 #define PLATFORM_STRING   "linux"
-#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
 #define PLATFORM_BSD      (1)
 #define PLATFORM_STRING   "bsd"
 #elif defined(__APPLE__) && defined(__MACH__)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.