1 /**
2  * \file   os_uuid.c
3  * \brief  Create a new UUID.
4  * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
5  */
6 
7 #include "premake.h"
8 
9 
os_uuid(lua_State * L)10 int os_uuid(lua_State* L)
11 {
12 	unsigned char bytes[16];
13 	char uuid[38];
14 
15 #if PLATFORM_WINDOWS
16 
17 	static int (__stdcall *CoCreateGuid)(char*) = NULL;
18 	if (CoCreateGuid == NULL)
19 	{
20 		HMODULE hOleDll = LoadLibrary("OLE32.DLL");
21 		CoCreateGuid = (int(__stdcall*)(char*))GetProcAddress(hOleDll, "CoCreateGuid");
22 	}
23 	CoCreateGuid((char*)bytes);
24 
25 #else
26 	int result;
27 
28 	/* not sure how to get a UUID here, so I fake it */
29 	FILE* rnd = fopen("/dev/urandom", "rb");
30 	result = fread(bytes, 16, 1, rnd);
31 	fclose(rnd);
32 	if (!result)
33 		return 0;
34 #endif
35 
36 	sprintf(uuid, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
37 		bytes[0], bytes[1], bytes[2], bytes[3],
38 		bytes[4], bytes[5],
39 		bytes[6], bytes[7],
40 		bytes[8], bytes[9],
41 		bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
42 
43 	lua_pushstring(L, uuid);
44 	return 1;
45 }
46