I came up with another solution myself. The general idea is to create a shell script that can config IP address in the system init directory. Here's the code:
#include <stdio>
#define MAXBUF 100
int main(int argc, char** argv)
{
FILE* f;
char content[MAXBUF];
f = fopen("/etc/init.d/configip", "w+");
strcat("#!/bin/sh\n", content);
strcat("ifconfig ", content);
strcat(argv[1], content);
strcat(" ", content);
strcat(argv[2], content);
strcat(" up", content);
fwrite(content, 1, strlen(content) + 1, f);
fclose(f);
return 0;
}
When this program was executed with arguments like "192.168.0.1 255.255.255.0", it will generate a shell script in etc/init.d
:
#!/bin/sh
ifconfig 192.168.0.1 255.255.255.0 up
The script will be loaded every time Linux boots up.