1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #ifndef INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
25 #define INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
26
27 #include "sal/config.h"
28
29 #include <cstddef>
30
31 #include <tchar.h>
32
33 #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
34 #define MY_STRING(s) (s), MY_LENGTH(s)
35
36 namespace desktop_win32 {
37
commandLineAppend(WCHAR * buffer,WCHAR const * text,std::size_t length)38 inline WCHAR * commandLineAppend(
39 WCHAR * buffer, WCHAR const * text, std::size_t length)
40 {
41 wcsncpy(buffer, text, length + 1); // trailing null
42 return buffer + length;
43 }
44
commandLineAppend(WCHAR * buffer,WCHAR const * text)45 inline WCHAR * commandLineAppend(WCHAR * buffer, WCHAR const * text) {
46 return commandLineAppend(buffer, text, wcslen(text));
47 }
48
commandLineAppendEncoded(WCHAR * buffer,WCHAR const * text)49 inline WCHAR * commandLineAppendEncoded(WCHAR * buffer, WCHAR const * text) {
50 std::size_t n = 0;
51 for (;;) {
52 WCHAR c = *text++;
53 if (c == L'\0') {
54 break;
55 } else if (c == L'$') {
56 buffer = commandLineAppend(buffer, MY_STRING(L"\\$"));
57 n = 0;
58 } else if (c == L'\\') {
59 buffer = commandLineAppend(buffer, MY_STRING(L"\\\\"));
60 n += 2;
61 } else {
62 *buffer++ = c;
63 n = 0;
64 }
65 }
66 // The command line will continue with a double quote, so double any
67 // preceding backslashes as required by Windows:
68 for (std::size_t i = 0; i < n; ++i) {
69 *buffer++ = L'\\';
70 }
71 *buffer = L'\0';
72 return buffer;
73 }
74
75 // Set the PATH environment variable in the current (loader) process, so that a
76 // following CreateProcess has the necessary environment:
77 //
78 // @param binPath
79 // Must point to an array of size at least MAX_PATH. Is filled with the null
80 // terminated full path to the "bin" file corresponding to the current
81 // executable.
82 //
83 // @param iniDirectory
84 // Must point to an array of size at least MAX_PATH. Is filled with the null
85 // terminated full directory path (ending in "\") to the "ini" file
86 // corresponding to the current executable.
87 void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory);
88
89 }
90
91 #endif
92