Getting real argv[0] in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart D. Gathman

    Getting real argv[0] in python

    I need to be able to obtain the real argv[0] (not the script name).
    The application is writing a CUPS backend in python. For some reason,
    CUPS decided to pass the URI as argv[0] (perhaps to ensure that CUPS
    will only ever run on Unix, since CUPS stands for Common *Unix*
    Printing System). The only solution I can think of is to write a C
    wrapper that inserts the original argv[0] before execing python with
    the script.

    Is there already a way to do this that I have missed?

    --
    Stuart D. Gathman <stuart@bmsi.co m>
    Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
    "Confutatis maledictis, flamis acribus addictis" - background song for
    a Microsoft sponsored "Where do you want to go from here?" commercial.

  • Stuart D. Gathman

    #2
    Re: Getting real argv[0] in python

    On Tue, 28 Oct 2003 13:52:26 -0500, Stuart D. Gathman wrote:
    [color=blue]
    > I need to be able to obtain the real argv[0] (not the script name).
    > The application is writing a CUPS backend in python. For some reason,
    > CUPS decided to pass the URI as argv[0] (perhaps to ensure that CUPS
    > will only ever run on Unix, since CUPS stands for Common *Unix*
    > Printing System). The only solution I can think of is to write a C
    > wrapper that inserts the original argv[0] before execing python with
    > the script.
    >
    > Is there already a way to do this that I have missed?[/color]

    Here is the simple C wrapper I am using:

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>

    static const char scriptdir[] = "/usr/lib/cups/python/";

    int main(int argc,char **argv) {
    char *nargv[10];
    char script[256];
    const char *p;
    int i;
    int slen,len;
    if (argc > 7) {
    fputs("Usage: pycups uri ...",stderr);
    return 1;
    }

    p = strchr(argv[0],':');
    if (p)
    len = p - argv[0];
    else
    len = strlen(argv[0]);
    slen = strlen(scriptdi r);
    strcpy(script,s criptdir);
    if (len + slen + 4 > sizeof script)
    len = sizeof script - slen - 4;
    strncat(script, argv[0],len);
    script[slen + len] = 0;
    strcat(script," .py");

    nargv[0] = "python2";
    nargv[1] = script;
    for (i = 0; i < argc; ++i)
    nargv[i+2] = argv[i];
    nargv[i+2] = 0;
    execvp("python2 ",nargv);
    perror("exec");
    return 1;
    }

    --
    Stuart D. Gathman <stuart@bmsi.co m>
    Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
    "Confutatis maledictis, flamis acribus addictis" - background song for
    a Microsoft sponsored "Where do you want to go from here?" commercial.

    Comment

    Working...