*: Updated copyright to 2009 and normalized name & email.
[kbuild-mirror.git] / src / lib / wrapper.c
blob6cdb5d469feb2f8fc549ce2efba5eb7b7300f9a5
1 /* $Id$ */
2 /** @file
3 * Wrapper program for various debugging purposes.
4 */
6 /*
7 * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
9 * This file is part of kBuild.
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
26 /*******************************************************************************
27 * Header Files *
28 *******************************************************************************/
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #ifdef _MSC_VER
33 # include <process.h>
34 #else
35 # include <unistd.h>
36 #endif
38 int main(int argc, char **argv, char **envp)
40 const char *pszLogTo = getenv("WRAPPER_LOGTO");
41 const char *pszLogFileArgs = getenv("WRAPPER_LOGFILEARGS");
42 const char *pszLogEnv = getenv("WRAPPER_LOGENV");
43 const char *pszExec = getenv("WRAPPER_EXEC");
44 const char *pszSigSegv = getenv("WRAPPER_SIGSEGV");
45 const char *pszRetVal = getenv("WRAPPER_RETVAL");
46 int i;
48 if (pszLogTo)
50 FILE *pLog = fopen(pszLogTo, "a");
51 if (pLog)
53 fprintf(pLog, "+++ %s pid=%ld +++\n", argv[0], (long)getpid());
54 for (i = 1; i < argc; i++)
56 fprintf(pLog, "argv[%d]: '%s'\n", i, argv[i]);
57 if (pszLogFileArgs)
59 FILE *pArg = fopen(argv[i], "r");
60 if (pArg)
62 int iLine = 0;
63 static char szLine[64*1024];
64 while (fgets(szLine, sizeof(szLine), pArg) && iLine++ < 42)
65 fprintf(pLog, "%2d: %s", iLine, szLine);
66 fclose(pArg);
70 if (pszLogEnv)
71 for (i = 0; envp[i]; i++)
72 fprintf(pLog, "envp[%d]: '%s'\n", i, envp[i]);
73 fprintf(pLog, "--- %s pid=%ld ---\n", argv[0], (long)getpid());
74 fclose(pLog);
78 if (pszSigSegv)
80 char *pchIllegal = (char *)1;
81 pchIllegal[0] = '\0';
84 if (pszExec)
86 /** @todo */
89 return pszRetVal ? atol(pszRetVal) : 1;