Browse code

Move get system directory to a separate function

Only refactoring to reduce code-duplication, no functional changes.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <1538510474-27602-1-git-send-email-selva.nair@gmail.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg17518.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Selva Nair authored on 2018/10/03 05:01:12
Showing 3 changed files
... ...
@@ -25,7 +25,7 @@
25 25
 #include "validate.h"
26 26
 
27 27
 LPCTSTR service_instance = TEXT("");
28
-
28
+static wchar_t win_sys_path[MAX_PATH];
29 29
 
30 30
 /*
31 31
  * These are necessary due to certain buggy implementations of (v)snprintf,
... ...
@@ -285,3 +285,17 @@ utf8to16(const char *utf8)
285 285
     MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, n);
286 286
     return utf16;
287 287
 }
288
+
289
+const wchar_t *
290
+get_win_sys_path(void)
291
+{
292
+    const wchar_t *default_sys_path = L"C:\\Windows\\system32";
293
+
294
+    if (!GetSystemDirectoryW(win_sys_path, _countof(win_sys_path)))
295
+    {
296
+        wcsncpy(win_sys_path, default_sys_path, _countof(win_sys_path));
297
+        win_sys_path[_countof(win_sys_path) - 1] = L'\0';
298
+    }
299
+
300
+    return win_sys_path;
301
+}
... ...
@@ -933,11 +933,10 @@ RegisterDNS(LPVOID unused)
933 933
 {
934 934
     DWORD err;
935 935
     DWORD i;
936
-    WCHAR sys_path[MAX_PATH];
937 936
     DWORD timeout = RDNS_TIMEOUT * 1000; /* in milliseconds */
938 937
 
939
-    /* default path of ipconfig command */
940
-    WCHAR ipcfg[MAX_PATH] = L"C:\\Windows\\system32\\ipconfig.exe";
938
+    /* path of ipconfig command */
939
+    WCHAR ipcfg[MAX_PATH];
941 940
 
942 941
     struct
943 942
     {
... ...
@@ -952,11 +951,8 @@ RegisterDNS(LPVOID unused)
952 952
 
953 953
     HANDLE wait_handles[2] = {rdns_semaphore, exit_event};
954 954
 
955
-    if (GetSystemDirectory(sys_path, MAX_PATH))
956
-    {
957
-        swprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe");
958
-        ipcfg[MAX_PATH-1] = L'\0';
959
-    }
955
+    swprintf(ipcfg, _countof(ipcfg), L"%s\\%s", get_win_sys_path(), L"ipconfig.exe");
956
+    ipcfg[_countof(ipcfg) - 1] = L'\0';
960 957
 
961 958
     if (WaitForMultipleObjects(2, wait_handles, FALSE, timeout) == WAIT_OBJECT_0)
962 959
     {
... ...
@@ -1034,15 +1030,8 @@ netsh_dns_cmd(const wchar_t *action, const wchar_t *proto, const wchar_t *if_nam
1034 1034
     }
1035 1035
 
1036 1036
     /* Path of netsh */
1037
-    int n = GetSystemDirectory(argv0, MAX_PATH);
1038
-    if (n > 0 && n < MAX_PATH) /* got system directory */
1039
-    {
1040
-        wcsncat(argv0, L"\\netsh.exe", MAX_PATH - n - 1);
1041
-    }
1042
-    else
1043
-    {
1044
-        wcsncpy(argv0, L"C:\\Windows\\system32\\netsh.exe", MAX_PATH);
1045
-    }
1037
+    swprintf(argv0, _countof(argv0), L"%s\\%s", get_win_sys_path(), L"netsh.exe");
1038
+    argv0[_countof(argv0) - 1] = L'\0';
1046 1039
 
1047 1040
     /* cmd template:
1048 1041
      * netsh interface $proto $action dns $if_name $addr [validate=no]
... ...
@@ -97,4 +97,7 @@ DWORD MsgToEventLog(DWORD flags, LPCTSTR lpszMsg, ...);
97 97
 /* Convert a utf8 string to utf16. Caller should free the result */
98 98
 wchar_t *utf8to16(const char *utf8);
99 99
 
100
+/* return windows system directory as a pointer to a static string */
101
+const wchar_t *get_win_sys_path(void);
102
+
100 103
 #endif /* ifndef _SERVICE_H */