]> pilppa.org Git - uci.git/blobdiff - parse.c
better config file handling
[uci.git] / parse.c
diff --git a/parse.c b/parse.c
index 9dd7126f3325a01a361174f492590ff07725a0af..5fc46618208fb3c3bbee2da5400b53cb8ce56861 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -16,6 +16,7 @@
  * This file contains the code for parsing uci config files
  */
 
+#include <sys/stat.h>
 #include <ctype.h>
 
 #define LINEBUF        128
@@ -267,9 +268,11 @@ static void uci_parse_line(struct uci_context *ctx)
        }
 }
 
-int uci_parse(struct uci_context *ctx, const char *name)
+int uci_load(struct uci_context *ctx, const char *name)
 {
        struct uci_parse_context *pctx;
+       struct stat statbuf;
+       char *filename;
 
        UCI_HANDLE_ERR(ctx);
        UCI_ASSERT(ctx, name != NULL);
@@ -280,11 +283,26 @@ int uci_parse(struct uci_context *ctx, const char *name)
        pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
        ctx->pctx = pctx;
 
-       /* TODO: use /etc/config/ */
-       pctx->file = fopen(name, "r");
-       if (!pctx->file)
+       switch (name[0]) {
+       case '.':
+       case '/':
+               /* absolute/relative path outside of /etc/config */
+               filename = (char *) name;
+               break;
+       default:
+               filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
+               sprintf(filename, UCI_CONFDIR "/%s", name);
+               break;
+       }
+
+       if ((stat(filename, &statbuf) < 0) ||
+               ((statbuf.st_mode &  S_IFMT) != S_IFREG))
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
 
+       pctx->file = fopen(filename, "r");
+       if (!pctx->file)
+               UCI_THROW(ctx, UCI_ERR_IO);
+
        pctx->cfg = uci_alloc_file(ctx, name);
 
        while (!feof(pctx->file)) {