srcanamdw/codescanner/pyinstaller/source/linux/main.c
author noe\swadi
Thu, 18 Feb 2010 12:29:02 +0530
changeset 1 22878952f6e2
permissions -rw-r--r--
Committing the CodeScanner Core tool This component has been moved from the StaticAnaApps package. BUG : 5889 (http://developer.symbian.org/webbugs/show_bug.cgi?id=5889).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     1
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     2
 * Bootloader for a packed executable.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     3
 * Copyright (C) 2005, Giovanni Bajo
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     4
 * Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     5
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     7
 * modify it under the terms of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     8
 * as published by the Free Software Foundation; either version 2
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     9
 * of the License, or (at your option) any later version.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    10
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    11
 * In addition to the permissions in the GNU General Public License, the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    12
 * authors give you unlimited permission to link or embed the compiled
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    13
 * version of this file into combinations with other programs, and to
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    14
 * distribute those combinations without any restriction coming from the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    15
 * use of this file. (The General Public License restrictions do apply in
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    16
 * other respects; for example, they cover modification of the file, and
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    17
 * distribution when not linked into a combine executable.)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    18
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    19
 * This program is distributed in the hope that it will be useful,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    22
 * GNU General Public License for more details.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    23
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    24
 * You should have received a copy of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    25
 * along with this program; if not, write to the Free Software
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    27
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    28
#include "launch.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    29
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    30
#ifdef FREEZE_EXCEPTIONS
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    31
extern unsigned char M_exceptions[];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    32
static struct _frozen _PyImport_FrozenModules[] = {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    33
    {"exceptions", M_exceptions, EXCEPTIONS_LEN},
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    34
    {0, 0, 0}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    35
};
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    36
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    37
int main(int argc, char* argv[])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    38
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    39
    char thisfile[_MAX_PATH];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    40
    char homepath[_MAX_PATH];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    41
    char magic_envvar[_MAX_PATH + 12];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    42
    char ldlib_envvar[_MAX_PATH * 4 + 12];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    43
    char archivefile[_MAX_PATH + 5];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    44
    char *oldldlib;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    45
    TOC *ptoc = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    46
    int rc = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    47
    char *workpath = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    48
    /* atexit(cleanUp); */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    49
#ifdef FREEZE_EXCEPTIONS
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    50
    PyImport_FrozenModules = _PyImport_FrozenModules;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    51
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    52
    // fill in thisfile
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    53
#ifdef __CYGWIN__
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    54
    if (strncasecmp(&argv[0][strlen(argv[0])-4], ".exe", 4)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    55
        strcpy(thisfile, argv[0]);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    56
        strcat(thisfile, ".exe");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    57
        Py_SetProgramName(thisfile);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    58
    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    59
    else 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    60
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    61
        Py_SetProgramName(argv[0]);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    62
    strcpy(thisfile, Py_GetProgramFullPath());
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    63
    VS(thisfile);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    64
    VS(" is thisfile\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    65
    
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    66
    workpath = getenv( "_MEIPASS2" );
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    67
    VS(workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    68
    VS(" is _MEIPASS2 (workpath)\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    69
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    70
    // fill in here (directory of thisfile)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    71
    strcpy(homepath, Py_GetPrefix());
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    72
    strcat(homepath, "/");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    73
    VS(homepath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    74
    VS(" is homepath\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    75
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    76
    if (init(homepath, &thisfile[strlen(homepath)], workpath)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    77
        /* no pkg there, so try the nonelf configuration */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    78
        strcpy(archivefile, thisfile);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    79
        strcat(archivefile, ".pkg");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    80
        if (init(homepath, &archivefile[strlen(homepath)], workpath)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    81
            FATALERROR("Cannot open self ");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    82
            FATALERROR(thisfile);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    83
            FATALERROR(" or archive ");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    84
            FATALERROR(archivefile);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    85
            FATALERROR("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    86
            return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    87
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    88
    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    89
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    90
    if (workpath) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    91
        // we're the "child" process
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    92
        VS("Already have a workpath - running!\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    93
        rc = doIt(argc, argv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    94
        if (strcmp(workpath, homepath)!=0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    95
            clear(workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    96
    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    97
    else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    98
        if (extractBinaries(&workpath)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    99
            VS("Error extracting binaries\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   100
            return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   101
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   102
        if (workpath == NULL) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   103
            /* now look for the "force LD_LIBRARY" flag */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   104
            ptoc = getFirstTocEntry();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   105
            while (ptoc) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   106
                if ((ptoc->typcd == 'o') && (ptoc->name[0] == 'f'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   107
                    workpath = homepath;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   108
                    ptoc = getNextTocEntry(ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   109
                }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   110
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   111
        if (workpath) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   112
            VS("Executing self as child with ");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   113
            // run the "child" process, then clean up
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   114
            strcpy(magic_envvar, "_MEIPASS2=");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   115
            strcat(magic_envvar, workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   116
            putenv(magic_envvar);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   117
            // now LD_LIBRARY_PATH
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   118
            strcpy(ldlib_envvar, "LD_LIBRARY_PATH=");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   119
            strcat(ldlib_envvar, workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   120
            ldlib_envvar[strlen(ldlib_envvar)-1] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   121
            oldldlib = getenv("LD_LIBRARY_PATH");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   122
            if (oldldlib) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   123
                strcat(ldlib_envvar, ":");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   124
                strcat(ldlib_envvar, oldldlib);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   125
            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   126
            putenv(ldlib_envvar);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   127
            VS(ldlib_envvar);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   128
            VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   129
            rc = execvp(thisfile, argv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   130
            VS("Back to parent...\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   131
        }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   132
        else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   133
            // no "child" process necessary
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   134
            rc = doIt(argc, argv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   135
    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   136
    return rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   137
}