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);
      }
8
  • did just add the arguments after /alok/analyze.py not work? Commented Mar 25, 2017 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. Commented Mar 25, 2017 at 12:04
  • Then is your error not in this call, system() will just run execl("/bin/sh","-c",command,NULL); Commented Mar 25, 2017 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. Commented Mar 25, 2017 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() Commented Mar 25, 2017 at 12:20

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.