Add error handing for bad enivronment and bad parameter

This commit is contained in:
Aaron committed 2015-08-03 10:48:46 -07:00
1 parent fb5279ad1e
commit 7a85d035c0
5 files changed
+30 -22

No files matched your search

+20 -1
View File
@@ -1,10 +1,28 @@
#include "getcomputername.h"
#include <unistd.h>
#include <errno.h>
#include <string>
#include <langinfo.h>
BOOL GetComputerName(LPTSTR name, LPDWORD len)
BOOL GetComputerNameW(LPTSTR name, LPDWORD len)
{
const std::string utf8 = "UTF-8";
errno = 0;
// Check parameters
if (!name || !len) {
errno = ERROR_INVALID_PARAMETER;
return 0;
}
// Select locale from environment
setlocale(LC_ALL, "");
// Check that locale is UTF-8
if (nl_langinfo(CODESET) != utf8) {
errno = ERROR_BAD_ENVIRONMENT;
return 0;
}
size_t len2 = *len;
int host = gethostname(name, len2);
if(host == 0)
@@ -17,3 +35,4 @@ BOOL GetComputerName(LPTSTR name, LPDWORD len)
return FALSE;
}
}