Debian Codegraph @

sal/qa/osl/process/osl_process_child.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #if defined(_WIN32) // Windows
21 # define WIN32_LEAN_AND_MEAN
22 # include <windows.h>
23 #else
24 # include <unistd.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <fstream>
29 #include <string.h>
30
31
32 #ifdef UNX
33 #if defined( MACOSX )
34 # include <crt_externs.h>
35 # define environ (*_NSGetEnviron())
36 # else
37 extern char** environ;
38 # endif
39 #endif
40
41 #ifdef _WIN32
42 # define SLEEP(t) (Sleep((t)*1000))
43 #else
44 # define SLEEP(t) (sleep((t)))
45 #endif
46
47 static void wait_for_seconds(char* time)
48 {
49 SLEEP(atoi(time));
50 }
51
52 #ifdef _WIN32
53
54 static void w_to_a(LPCWSTR strW, LPSTR strA, DWORD size)
55 {
56 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, size, nullptr, nullptr);
57 }
58
59 static void dump_env(char* file_path)
60 {
61 LPWSTR env = GetEnvironmentStringsW();
62 LPWSTR p = env;
63
64 std::ofstream file(file_path);
65
66 char buffer[32767];
67 while (size_t l = wcslen(p))
68 {
69 w_to_a(p, buffer, sizeof(buffer));
70 file << buffer << '\0';
71 p += l + 1;
72 }
73 FreeEnvironmentStringsW(env);
74 }
75 #else
76 static void dump_env(char* file_path)
77 {
78 std::ofstream file(file_path);
79 for (int i = 0; environ[i] != nullptr; ++i)
80 file << environ[i] << '\0';
81 }
82 #endif
83
84 int main(int argc, char* argv[])
85 {
86 if (argc > 2)
87 {
88 if (strcmp("-join", argv[1]) == 0)
89 {
90 // coverity[tainted_data] - this is a build-time only test tool
91 wait_for_seconds(argv[2]);
92 }
93 else if (strcmp("-env", argv[1]) == 0)
94 dump_env(argv[2]);
95 }
96
97 return 0;
98 }
99
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */