Search code examples
pythonbashgets

Passing "gets" c language function input parameter from an inline python code



How can I pass output of a Python script to gets function of a c program ? My c program code is below :

#include <stdio.h>
int main() 
{
   char name[64];
   printf("%p\n", name);
   fflush(stdout);
   puts("What's your name?");
   fflush(stdout);
   gets(name);
   printf("Hello, %s!\n", name);
   return 0;
}

What I want to do is something like below:

$./a.out "$(python -c 'print "A"*1000')"

Thanks a lot.


Solution

  • To send data from the stdout of one command into the stdin of another command, you need a "pipe":

    python -c 'print "A"*1000' | ./a.out
    

    I assume that the buffer overrun here is deliberate, so I'll leave out the lecture about the unsafety of gets.

    Normally, a command-line utility will acquire its input from the argument array (argv in the parameters to main), which usually avoids the need for copying the data and thus any risk if a buffer overrun.