Blame msvc/missing.c

Packit Service b0a153
/*
Packit Service b0a153
 * Source file for missing WinCE functionality
Packit Service b0a153
 * Copyright © 2012 RealVNC Ltd.
Packit Service b0a153
 *
Packit Service b0a153
 * This library is free software; you can redistribute it and/or
Packit Service b0a153
 * modify it under the terms of the GNU Lesser General Public
Packit Service b0a153
 * License as published by the Free Software Foundation; either
Packit Service b0a153
 * version 2.1 of the License, or (at your option) any later version.
Packit Service b0a153
 *
Packit Service b0a153
 * This library is distributed in the hope that it will be useful,
Packit Service b0a153
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b0a153
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service b0a153
 * Lesser General Public License for more details.
Packit Service b0a153
 *
Packit Service b0a153
 * You should have received a copy of the GNU Lesser General Public
Packit Service b0a153
 * License along with this library; if not, write to the Free Software
Packit Service b0a153
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit Service b0a153
 */
Packit Service b0a153
Packit Service b0a153
#include "missing.h"
Packit Service b0a153
Packit Service b0a153
#include <config.h>
Packit Service b0a153
#include <libusbi.h>
Packit Service b0a153
Packit Service b0a153
#include <windows.h>
Packit Service b0a153
Packit Service b0a153
// The registry path to store environment variables
Packit Service b0a153
#define ENVIRONMENT_REG_PATH _T("Software\\libusb\\environment")
Packit Service b0a153
Packit Service b0a153
/* Workaround getenv not being available on WinCE.
Packit Service b0a153
 * Instead look in HKLM\Software\libusb\environment */
Packit Service b0a153
char *getenv(const char *name)
Packit Service b0a153
{
Packit Service b0a153
	static char value[MAX_PATH];
Packit Service b0a153
	TCHAR wValue[MAX_PATH];
Packit Service b0a153
	WCHAR wName[MAX_PATH];
Packit Service b0a153
	DWORD dwType, dwData;
Packit Service b0a153
	HKEY hkey;
Packit Service b0a153
	LONG rc;
Packit Service b0a153
Packit Service b0a153
	if (!name)
Packit Service b0a153
		return NULL;
Packit Service b0a153
Packit Service b0a153
	if (MultiByteToWideChar(CP_UTF8, 0, name, -1, wName, MAX_PATH) <= 0) {
Packit Service b0a153
		usbi_dbg("Failed to convert environment variable name to wide string");
Packit Service b0a153
		return NULL;
Packit Service b0a153
	}
Packit Service b0a153
	wName[MAX_PATH - 1] = 0; // Be sure it's NUL terminated
Packit Service b0a153
Packit Service b0a153
	rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ENVIRONMENT_REG_PATH, 0, KEY_QUERY_VALUE, &hkey);
Packit Service b0a153
	if (rc != ERROR_SUCCESS) {
Packit Service b0a153
		usbi_dbg("Failed to open registry key for getenv with error %d", rc);
Packit Service b0a153
		return NULL;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	// Attempt to read the key
Packit Service b0a153
	dwData = sizeof(wValue);
Packit Service b0a153
	rc = RegQueryValueEx(hkey, wName, NULL, &dwType,
Packit Service b0a153
		(LPBYTE)&wValue, &dwData);
Packit Service b0a153
	RegCloseKey(hkey);
Packit Service b0a153
	if (rc != ERROR_SUCCESS) {
Packit Service b0a153
		usbi_dbg("Failed to read registry key value for getenv with error %d", rc);
Packit Service b0a153
		return NULL;
Packit Service b0a153
	}
Packit Service b0a153
	if (dwType != REG_SZ) {
Packit Service b0a153
		usbi_dbg("Registry value was of type %d instead of REG_SZ", dwType);
Packit Service b0a153
		return NULL;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	// Success in reading the key, convert from WCHAR to char
Packit Service b0a153
	if (WideCharToMultiByte(CP_UTF8, 0,
Packit Service b0a153
			wValue, dwData / sizeof(*wValue),
Packit Service b0a153
			value, MAX_PATH,
Packit Service b0a153
			NULL, NULL) <= 0) {
Packit Service b0a153
		usbi_dbg("Failed to convert environment variable value to narrow string");
Packit Service b0a153
		return NULL;
Packit Service b0a153
	}
Packit Service b0a153
	value[MAX_PATH - 1] = 0; // Be sure it's NUL terminated
Packit Service b0a153
	return value;
Packit Service b0a153
}