-1

I searched in various ways on the internet, but none of the results could answer my question, maybe here I will find the solution.

In python I can do this:

imp = "import os"
exec(imp)
os.system("ping x.x.x.x")

What is the equivalent of this exact function call in the C language?

Skully
  • 2,882
  • 3
  • 20
  • 31
Archangel
  • 25
  • 3
  • Simple answer: you can’t do it. – Mithridates the Great Dec 09 '21 at 04:03
  • 2
    Does this answer your question? [is it possible to make a function execute code from a string on the stack?](https://stackoverflow.com/questions/3225124/is-it-possible-to-make-a-function-execute-code-from-a-string-on-the-stack) – Skully Dec 09 '21 at 04:06
  • 1
    Your C program would have to contain or have access to a C compiler (and other parts of development environment) to do at least something similar to Python's "exec". – Michael Butscher Dec 09 '21 at 04:07
  • 1
    *"In python I can do this:"* -- OK, but what is the result of doing that? Sure, you could restrict your question to people who know both Python and C, but do you want to hamper your question like that? You should describe what you want to accomplish in words, then use the Python code as an example. Not only does this expand the base of people who can answer, but it also expands the base of people who can benefit from your question (to include people with basically the same question, but not based in Python). – JaMiT Dec 09 '21 at 05:05
  • I'm not too familiar with exec(). I see you're passing it a string here. Will what you pass to it always be a string of commands? – Gabriel Staples Dec 09 '21 at 05:14
  • Also, please provide some pseudo code in C (or pseudo C magic language you make up) you'd like to see done this way. I have some ideas. – Gabriel Staples Dec 09 '21 at 05:16
  • Also, do you need new code to be dynamically generated in C at run time? Or is compile time sufficient? Both are possible I think but at runtime means you need your code to write code and call the compiler at run time, so your code writes and compiles more code, like a little autonomous code factory. – Gabriel Staples Dec 09 '21 at 05:18
  • 1
    I don't know why people are going on about C compilers, perhaps they're confused by your roundabout way of importing `os` (why *are* you doing it that way?). But aren't you just asking for the `system()` call from `stdlib` in C here? – Grismar Dec 09 '21 at 05:18
  • @GabrielStaples, the ping command and the os header are just an example – Archangel Dec 09 '21 at 20:03

1 Answers1

1

What is the equivalent of this exact function call in the C language?

exec() in Python will attempt to execute any string as Python. system() in C or C++ (or an equivalent system() call in any other language, for that matter, like os.system() in Python) will attempt to execute any string as a system call, which is your shell language and could be Bash, Python, Perl, another C executable, or anything else for that matter.

So, there isn't an exact equivalent. But, the closest thing is probably the system() call, which can call any string as a command-line command as though you had typed it at the terminal.

The C system() call, however, is really an exact equivalent of the Python os.system() call. Generally, only scripted programming languages have the exec() call, while all or most programming languages have a system() call. C (in general, I suppose, as there are probably C interpreters out there) is a compiled language, not a scripted language.

Going further, if you need to read back the stdout or stderr output from the command you called, you need to pipe it back to your process (since it gets spawned in a new process) using a pipe as an Inter-Process Communication (IPC) mechanism. You can open an IPC pipe via a call to popen(). You'd use that in place of the system() call. See here for an example: How can I run an external program from C and parse its output?

Here's a system() call example I tested on Linux Ubuntu. You're gonna love this! It makes me laugh just looking at it. But, it's insightful and instructive nonetheless, and if you think about it, it opens up a TON of really cool possibilities.

system_call_python.c (for the latest version of this code, see system_call_python.c in my eRCaGuy_hello_world repo here):

#include <stdlib.h>  // For `system()` calls
#include <stdio.h>   // For `printf()

#define PYTHON_CODE \
    "imp = \"import os\"\n" \
    "exec(imp)\n" \
    "os.system(\"ping 127.0.0.1\")\n"

int main()
{
    system("echo '" PYTHON_CODE "' > myfile.py");
    system("python3 myfile.py");

    return 0;
}

Build and run cmd + output:

eRCaGuy_hello_world/c$ mkdir -p bin && gcc -O3 -std=c11 -save-temps=obj system_call_python.c -o bin/system_call_python && bin/system_call_python
system_call_python.c: In function ‘main’:
system_call_python.c:41:5: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
     system("echo '" PYTHON_CODE "' > myfile.py");
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                           
system_call_python.c:42:5: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
     system("python3 myfile.py");
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.082 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.086 ms

Here is what myfile.py looks like, which the C code above autogenerated (see it in my eRCaGuy_hello_world repo here):

imp = "import os"
exec(imp)
os.system("ping 127.0.0.1")

So there you have it: have C construct some program in Bash or Python, then have C call it. Alternatively you could have C construct a program in C and have it compile and then call it--a program writing a program.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Good. thanks for the response. your response may not be able to give me a solution, but it gave me a new idea. – Archangel Dec 09 '21 at 19:51