1 | /* $Id: wrapper.c 2851 2016-08-31 17:30:52Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * Wrapper program for various debugging purposes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007-2016 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
10 | * copy of this software and associated documentation files (the "Software"),
|
---|
11 | * to deal in the Software without restriction, including without limitation
|
---|
12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
13 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
14 | * Software is furnished to do so, subject to the following conditions:
|
---|
15 | *
|
---|
16 | * The above copyright notice and this permission notice shall be included
|
---|
17 | * in all copies or substantial portions of the Software.
|
---|
18 | *
|
---|
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
---|
25 | * IN THE SOFTWARE.
|
---|
26 | *
|
---|
27 | * Alternatively, the content of this file may be used under the terms of the
|
---|
28 | * GPL version 2 or later, or LGPL version 2.1 or later.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <string.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #ifdef _MSC_VER
|
---|
39 | # include <process.h>
|
---|
40 | #else
|
---|
41 | # include <unistd.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | int main(int argc, char **argv, char **envp)
|
---|
46 | {
|
---|
47 | const char *pszLogTo = getenv("WRAPPER_LOGTO");
|
---|
48 | const char *pszLogFileArgs = getenv("WRAPPER_LOGFILEARGS");
|
---|
49 | const char *pszLogEnv = getenv("WRAPPER_LOGENV");
|
---|
50 | const char *pszExec = getenv("WRAPPER_EXEC");
|
---|
51 | const char *pszSigSegv = getenv("WRAPPER_SIGSEGV");
|
---|
52 | const char *pszRetVal = getenv("WRAPPER_RETVAL");
|
---|
53 | int i;
|
---|
54 |
|
---|
55 | if (pszLogTo)
|
---|
56 | {
|
---|
57 | FILE *pLog = fopen(pszLogTo, "a");
|
---|
58 | if (pLog)
|
---|
59 | {
|
---|
60 | fprintf(pLog, "+++ %s pid=%ld +++\n", argv[0], (long)getpid());
|
---|
61 | for (i = 1; i < argc; i++)
|
---|
62 | {
|
---|
63 | fprintf(pLog, "argv[%d]: '%s'\n", i, argv[i]);
|
---|
64 | if (pszLogFileArgs)
|
---|
65 | {
|
---|
66 | FILE *pArg = fopen(argv[i], "r");
|
---|
67 | if (pArg)
|
---|
68 | {
|
---|
69 | int iLine = 0;
|
---|
70 | static char szLine[64*1024];
|
---|
71 | while (fgets(szLine, sizeof(szLine), pArg) && iLine++ < 42)
|
---|
72 | fprintf(pLog, "%2d: %s", iLine, szLine);
|
---|
73 | fclose(pArg);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | if (pszLogEnv)
|
---|
78 | for (i = 0; envp[i]; i++)
|
---|
79 | fprintf(pLog, "envp[%d]: '%s'\n", i, envp[i]);
|
---|
80 | fprintf(pLog, "--- %s pid=%ld ---\n", argv[0], (long)getpid());
|
---|
81 | fclose(pLog);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (pszSigSegv)
|
---|
86 | {
|
---|
87 | char *pchIllegal = (char *)1;
|
---|
88 | pchIllegal[0] = '\0';
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (pszExec)
|
---|
92 | {
|
---|
93 | /** @todo */
|
---|
94 | }
|
---|
95 |
|
---|
96 | return pszRetVal ? atol(pszRetVal) : 1;
|
---|
97 | }
|
---|
98 |
|
---|