Force UTF-16 output.
[nssm.git] / nssm.cpp
index 32f1142..8dd264a 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -13,16 +13,20 @@ int str_equiv(const TCHAR *a, const TCHAR *b) {
 }\r
 \r
 /* Convert a string to a number. */\r
-int str_number(const TCHAR *string, unsigned long *number) {\r
+int str_number(const TCHAR *string, unsigned long *number, TCHAR **bogus) {\r
   if (! string) return 1;\r
 \r
-  TCHAR *bogus;\r
-  *number = _tcstoul(string, &bogus, 0);\r
-  if (*bogus) return 2;\r
+  *number = _tcstoul(string, bogus, 0);\r
+  if (**bogus) return 2;\r
 \r
   return 0;\r
 }\r
 \r
+int str_number(const TCHAR *string, unsigned long *number) {\r
+  TCHAR *bogus;\r
+  return str_number(string, number, &bogus);\r
+}\r
+\r
 /* Remove basename of a path. */\r
 void strip_basename(TCHAR *buffer) {\r
   size_t len = _tcslen(buffer);\r
@@ -71,9 +75,25 @@ static void check_console() {
   FreeConsole();\r
 }\r
 \r
+int num_cpus() {\r
+  DWORD_PTR i, affinity, system_affinity;\r
+  if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64;\r
+  for (i = 0; system_affinity & (1LL << i); i++);\r
+  return (int) i;\r
+}\r
+\r
 int _tmain(int argc, TCHAR **argv) {\r
   check_console();\r
 \r
+#ifdef UNICODE\r
+  /*\r
+    Ensure we write in UTF-16 mode, so that non-ASCII characters don't get\r
+    mangled.  If we were compiled in ANSI mode it won't work.\r
+   */\r
+  _setmode(_fileno(stdout), _O_U16TEXT);\r
+  _setmode(_fileno(stderr), _O_U16TEXT);\r
+#endif\r
+\r
   /* Remember if we are admin */\r
   check_admin();\r
 \r