X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=util.c;h=12aec9bd3596103741b564198cd969e97207a165;hp=812a3a3a64b296ad335ff0919102eef4d2ece59f;hb=5ad59ad412a784c5c478e31d1701ce39568ffd58;hpb=51f2ea3271762cebf1e3b54a6e87297f2cab0194 diff --git a/util.c b/util.c index 812a3a3..12aec9b 100644 --- a/util.c +++ b/util.c @@ -69,18 +69,22 @@ __private char *uci_strdup(struct uci_context *ctx, const char *str) * for names, only alphanum and _ is allowed (shell compatibility) * for types, we allow more characters */ -__private bool uci_validate_str(const char *str, bool name) +__private bool uci_validate_str(const char *str, bool name, bool package) { if (!*str) return false; - while (*str) { + for (; *str; str++) { unsigned char c = *str; - if (!isalnum(c) && c != '_') { - if (name || (c < 33) || (c > 126)) - return false; - } - str++; + + if (isalnum(c) || c == '_') + continue; + + if (c == '-' && package) + continue; + + if (name || (c < 33) || (c > 126)) + return false; } return true; } @@ -89,9 +93,10 @@ 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; @@ -161,12 +166,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); } @@ -178,7 +183,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; @@ -190,11 +195,15 @@ __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, i if (create) { flags |= O_CREAT; - name = basename((char *) 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); @@ -202,7 +211,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); }