Drop test cases for deprecated ucimap-example.
[project/uci.git] / util.c
diff --git a/util.c b/util.c
index 7cad0d1..ce7d849 100644 (file)
--- a/util.c
+++ b/util.c
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <libgen.h>
 
 #include "uci.h"
 #include "uci_internal.h"
@@ -177,7 +178,7 @@ __private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
  * note: when opening for write and seeking to the beginning of
  * the stream, truncate the file
  */
-__private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
+__private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create)
 {
        struct stat statbuf;
        FILE *file = NULL;
@@ -189,11 +190,15 @@ __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, i
 
        if (create) {
                flags |= O_CREAT;
-               name = basename(filename);
+               if (origfilename) {
+                       name = basename((char *) origfilename);
+               } else {
+                       name = basename((char *) filename);
+               }
                if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
                        UCI_THROW(ctx, UCI_ERR_MEM);
                } else {
-                       if (stat(filename2,&statbuf) == 0)
+                       if (stat(filename2, &statbuf) == 0)
                                mode = statbuf.st_mode;
 
                        free(filename2);
@@ -201,7 +206,7 @@ __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, i
        }
 
        if (!write && ((stat(filename, &statbuf) < 0) ||
-               ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
+               ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
        }