如何使用C API來操作UCI


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[32];
    int i;

    ctx = uci_alloc_context();

    memset(a, 0, sizeof(a));
    printf("1234....\n");
    strcpy(a, "scws.wsn.netid");
    printf("a(L=%d) = %s\n", strlen(a), a);

執行之后似乎strcpy只能拷貝.之前的幾個字符,將uci_alloc_context調到strcpy之后就可以了,再把它調回來又不再出現這樣的問題了!很奇怪。

 

這樣應用可以:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM