I want to execute a terminal command of Linux in a C program. Currently I am using system()
function but I want to use any other as the system()
function is banned as per MISRA.
For example, how can I replace
system("hwclock --systohc --utc");
First you can use fork()
to create a child process,then in the child process,you can call exec()
to execute command what you want.
There is a simple example:
$ chmod u+x command.sh
$ cat command.sh
#!/usr/bin/env bash
ls -l
************* test.c ****************
#include<unistd.h>
int main(void)
{
execl("./command.sh","command.sh",(char*)0);
return 0;
}
You can make use of fork()
and then look up for exec()
family of functions.
Alternatively, you may want to have a look at popen()
also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With