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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 //########################################
28 // includes
29 
30 #if ( defined WNT )                     // Windows
31 #include <tools/prewin.h>
32 #   define UNICODE
33 #   define _UNICODE
34 #	define WIN32_LEAN_AND_MEAN
35 // #	include <windows.h>
36 #   include <tchar.h>
37 #include <tools/postwin.h>
38 #else
39 #	include <unistd.h>
40 #endif
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <iostream>
45 #include <fstream>
46 
47 #include <rtl/ustring.hxx>
48 
49 //########################################
50 // defines
51 
52 #ifdef WNT
53 #   define SLEEP(t) (Sleep((t)*1000))
54 #else
55 #   define SLEEP(t) (sleep((t)))
56 #endif
57 
58 //########################################
59 void wait_for_seconds(char* time)
60 {
61     SLEEP(atoi(time));
62 }
63 
64 //########################################
65 
66 #ifdef WNT
67 //########################################
68 void w_to_a(LPCTSTR _strW, LPSTR strA, DWORD size)
69 {
70     LPCWSTR strW = reinterpret_cast<LPCWSTR>(_strW);
71     WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, size, NULL, NULL);
72 }
73 //########################################
74     void dump_env(char* file_path)
75     {
76         LPTSTR env = reinterpret_cast<LPTSTR>(
77             GetEnvironmentStrings());
78         LPTSTR p   = env;
79 
80         std::ofstream file(file_path);
81 
82         char buffer[32767];
83         while (size_t l = _tcslen(reinterpret_cast<wchar_t*>(p)))
84         {
85             w_to_a(p, buffer, sizeof(buffer));
86             file << buffer << std::endl;
87             p += l + 1;
88         }
89         FreeEnvironmentStrings(env);
90     }
91 #else
92     extern char** environ;
93 
94     void dump_env(char* file_path)
95     {
96         std::ofstream file(file_path);
97         for (int i = 0; NULL != environ[i]; i++)
98             file << environ[i] << std::endl;
99     }
100 #endif
101 
102 //########################################
103 int main(int argc, char* argv[])
104 {
105     rtl::OUString s;
106 
107     //t_print("Parameter: ");
108     printf("child process Parameter: ");
109     for (int i = 1; i < argc; i++)
110         printf("%s ", argv[i]);
111     printf("\n");
112 
113     if (argc > 2)
114     {
115         if (0 == strcmp("-join", argv[1]))
116         {
117             wait_for_seconds(argv[2]);
118         }
119         else if (0 == strcmp("-env", argv[1]))
120         {
121             dump_env(argv[2]);
122         }
123     }
124 
125     return (0);
126 }
127 
128