X-Git-Url: http://git.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=util.c;h=09f18171cede5399a020610050b9f202dd476ee7;hp=3725ec19a6d49780394be6bafb0299bf96ec959b;hb=f8f9decc545a7e9fc7b4c92534f4bf2b1db4a4f0;hpb=e4516d01a7d2b0a5a8def7b5791c7d4032138287 diff --git a/util.c b/util.c index 3725ec1..09f1817 100644 --- a/util.c +++ b/util.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "uci.h" #include "uci_internal.h" @@ -88,9 +89,12 @@ bool uci_validate_text(const char *str) { while (*str) { unsigned char c = *str; - if ((c == '\r') || (c == '\n') || - ((c < 32) && (c != '\t'))) + if (((c < 32) && + (c != '\t') && + (c != '\n') && + (c != '\r'))) { return false; + } str++; } return true; @@ -160,12 +164,12 @@ error: } -__private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason) +__private void uci_parse_error(struct uci_context *ctx, char *reason) { struct uci_parse_context *pctx = ctx->pctx; pctx->reason = reason; - pctx->byte = pos - pctx->buf; + pctx->byte = pctx_pos(pctx); UCI_THROW(ctx, UCI_ERR_PARSE); } @@ -177,22 +181,39 @@ __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; int fd, ret; - int mode = (write ? O_RDWR : O_RDONLY); + int flags = (write ? O_RDWR : O_RDONLY); + mode_t mode = UCI_FILEMODE; + char *name = NULL; + char *filename2 = NULL; + + if (create) { + flags |= O_CREAT; + 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) + mode = statbuf.st_mode; - if (create) - mode |= O_CREAT; + free(filename2); + } + } 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); } - fd = open(filename, mode, UCI_FILEMODE); + fd = open(filename, flags, mode); if (fd < 0) goto error;