1 /**
2  * \file   os_rmdir.c
3  * \brief  Remove a subdirectory.
4  * \author Copyright (c) 2002-2013 Jason Perkins and the Premake project
5  */
6 
7 #include "premake.h"
8 
9 #if PLATFORM_WINDOWS
10 
os_remove(lua_State * L)11 int os_remove(lua_State* L)
12 {
13 	const char* filename = luaL_checkstring(L, 1);
14 
15 	wchar_t wide_path[PATH_MAX];
16 	if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, wide_path, PATH_MAX) == 0)
17 	{
18 		lua_pushstring(L, "unable to encode path");
19 		return lua_error(L);
20 	}
21 
22 	if (DeleteFileW(wide_path))
23 	{
24 		lua_pushboolean(L, 1);
25 		return 1;
26 	}
27 	else
28 	{
29 		DWORD err = GetLastError();
30 
31 		char unicodeErr[512];
32 
33 		LPWSTR messageBuffer = NULL;
34 		if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR) &messageBuffer, 0, NULL) != 0)
35 		{
36 			if (WideCharToMultiByte(CP_UTF8, 0, messageBuffer, -1, unicodeErr, sizeof(unicodeErr), NULL, NULL) == 0)
37 				strcpy(unicodeErr, "failed to translate error message");
38 
39 			LocalFree(messageBuffer);
40 		}
41 		else
42 			strcpy(unicodeErr, "failed to get error message");
43 
44 		lua_pushnil(L);
45 		lua_pushfstring(L, "%s: %s", filename, unicodeErr);
46 		lua_pushinteger(L, err);
47 		return 3;
48 	}
49 }
50 
51 #endif
52