简体   繁体   中英

C program for executing 'echo' on a target with no /bin directory

Let us say I cross-compile this code for ARM from a host Linux system:

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>

int main (void) {

   return execl ("/bin/echo", "echo", NULL);

}

I got a binary echoFromC

The problem is on my target where this binary would run, has no /bin, /usr etc. So it will not execute the command.

So I want that the final binary should contain every thing required to excute "echo". It shouild not look for /bin/echo (since it is not there).

How do I do this?

Actually I have to implement:

bash> echo 240 > /sys/class/gpio/export
bash> echo out > /sys/class/gpio/gpio240/direction
bash> echo 1 > /sys/class/gpio/gpio240/value

in ac program

You could implement those echo functions in C, for example to implement echo 240 > /sys/class/gpio/export , you could do something like

FILE *fh;

fh = open("/sys/class/gpio/export", "w+");
if (fh == NULL) { /* fopen failed */ }

fwrite("240\n", 1, 4, fh);

fclose(fh);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM