0

how can i specify arguments to a python script from C program , where this arguments must be passed while the calling python script inside the c program

This C code is able to run python script successfully ,but how can i pass arguments as well that can be accepted by python script?

    #include <stdio.h>
    #include <string.h>
    #include <python2.7/Python.h>
    #include <getopt.h>

      int main (int argc, char * argv[])
      {
        char command[50] = "python2.7  /alok/analyze.py";
        system(command);return(0);
      }
aAAAAAAa
  • 135
  • 1
  • 2
  • 9
  • did just add the arguments after `/alok/analyze.py` not work? – 12431234123412341234123 Mar 25 '17 at 11:58
  • I dont think you need the header `Python.h`, you do not use any python functions, you just call system() with a string, which then just use fork()-exec()-wait() which also have nothing to do with Python, and you program do not need to know anything about Python. – 12431234123412341234123 Mar 25 '17 at 12:04
  • Then is your error not in this call, `system()` will just run `execl("/bin/sh","-c",command,NULL);` – 12431234123412341234123 Mar 25 '17 at 12:11
  • You know that you with this call just pass the string `"-dargv[1]"` to it? If you want to use a argv-argument you must copy it to make a single string for `system()` or use `fork()` and one of the `exec()`-functions. – 12431234123412341234123 Mar 25 '17 at 12:13
  • You can not just ignore double quotes. you will need to reserve enough buffer space (you must make sure this buffer is bigger than both strings together or abort it if it is too long) and copy both strings in this buffer. So that you have one string with the complete argument for `system()` – 12431234123412341234123 Mar 25 '17 at 12:20
  • First, check if the program have a valid argument (argc>=2) , then use `strlen()` to determine the length of both strings (const string and from `argv[1]`) then creat a buffer which can hold both strings, copy both strings to this buffer and then use `system()` – 12431234123412341234123 Mar 25 '17 at 12:40
  • a other way is to use the Python API, in this case you would need to use the header `Python.h` but not `system()`, see: http://stackoverflow.com/questions/12142174/run-a-python-script-with-arguments – 12431234123412341234123 Mar 25 '17 at 13:29
  • Please learn programming C before you us it for such things. You should know C better when you plane to us it, because in C you can do things really badly and still work on some machines but not on others (C is not like Python). First, you must free the content of `combined` before you overwrite `combined` and then do not use this address anymore. And you can not have 2 variables with the same name in the same scope. – 12431234123412341234123 Mar 28 '17 at 11:05

1 Answers1

1

From the commends, i saw that your real problem is, how to make a string from 2 given strings.

What you can do is: Write a function that concatenate 2 strings in to one. For that you need to get the length of both strings, then add this lengths (also add 1 for the '\0'-Byte and check for overflows), then use malloc() to reserve buffer space for the new string and copy both strings to this buffer.

You can do it like this (do not just use this, it is not very well testet, and the error handling is not great):

void die(const char *msg)
  {
    fprintf(stderr,"[ERROR] %s\n",msg);
    exit(EXIT_FAILURE);
  }


char *catString(const char *a, const char *b)
  {
    //calculate the buffer length we need
    size_t lena = strlen(a);
    size_t lenb = strlen(b);
    size_t lenTot = lena+lenb+1; //need 1 extra for the end-of-string '\0'
    if(lenTot<lena) //check for overflow
      {
        die("size_t overflow");
      }
    //reseve memory
    char *buffer = malloc(lenTot);
    if(!buffer) //check if malloc fail
      {
        die("malloc fail");
      }
    strcpy(buffer,a); //copy string a to the buffer
    strcpy(&buffer[lena],b);//copy string b to the buffer
    return buffer;
  }

After this you can use this function to create the string you need from your static string "python2.7 ./myScript " and argv[1]

int main(int argc, char **argv)
  {
    //without a argument we should not call the python script
    if(argc<2)
      {
        die("need at least one argument");
      }
    //make one string to call system()
    char *combined = catString("python2.7 ./myScript ",argv[1]);
    printf("DEBUG complete string is '%s'\n",combined);
    int i = system(combined);
    //we must free the buffer after use it or we generate memory leaks
    free(combined);
    if(i<0)
      {
        die("system()-call failed");
      }
    printf("DEBUG returned from system()-call\n");
    return EXIT_SUCCESS;
  }

You need the extra space in "python2.7 ./myScript ", without you would get "python2.7 ./myScriptArgumentToMain".

And with this your caller can execute any code he like because we do not escape argv[1], so a call to your program with yourProgram "argumentToPython ; badProgram argumentToBadProgram" will execute badProgram also which you do not want (in the most cases)