如何使用C API来操作UCI

时间:2024-01-19 19:24:50

https://forum.openwrt.org/viewtopic.php?pid=183335#p183335

Compiling UCI as stand alone with an example using the C API
1. Compiling UCI as stand alone

cd ~
git clone git://nbd.name/uci.git ~/uci
cd ~/uci
cmake -DBUILD_LUA=off .
make install DESTDIR=$HOME

2. Example code using the UCI C API
~/uci/main.c

#include <stdio.h>
#include <string.h>
#include <uci.h>
#include <stdlib.h> int main (int argc, char **argv)
{
struct uci_context *c;
struct uci_ptr p;
char *a = strdup ("wireless.@wifi-iface[0].ssid"); c = uci_alloc_context ();
if (uci_lookup_ptr (c, &p, a, true) != UCI_OK)
{
uci_perror (c, "XXX");
return 1;
} printf("%s\n", p.o->v.string);
uci_free_context (c);
free (a);
return 0;
}

3. Compile the example

cc -I$HOME/usr/include -L$HOME/usr/lib main.c -luci -o uci-test

4. Run the compiled example binary

export LD_LIBRARY_PATH=$HOME/usr/lib
./uci-test
XXX: Entry not found

heloc

使用过程中发现一个比较奇怪的问题,

static int config_file_read(struct _options *opt)
{
struct uci_context *ctx;
struct uci_ptr ptr;
char a[];
int i; ctx = uci_alloc_context(); memset(a, , sizeof(a));
printf("1234....\n");
strcpy(a, "scws.wsn.netid");
printf("a(L=%d) = %s\n", strlen(a), a);

执行之后似乎strcpy只能拷贝.之前的几个字符,将uci_alloc_context调到strcpy之后就可以了,再把它调回来又不再出现这样的问题了!很奇怪。

这样应用可以:

如何使用C API来操作UCI