+ return 0;
+
+error:
+ memset(ptr, 0, sizeof(struct uci_ptr));
+ UCI_THROW(ctx, UCI_ERR_PARSE);
+}
+
+
+__private void uci_parse_error(struct uci_context *ctx, char *reason)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+
+ pctx->reason = reason;
+ pctx->byte = pctx_pos(pctx);
+ UCI_THROW(ctx, UCI_ERR_PARSE);
+}
+
+
+
+/*
+ * open a stream and go to the right position
+ *
+ * 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, const char *origfilename, int pos, bool write, bool create)
+{
+ struct stat statbuf;
+ FILE *file = NULL;
+ int fd, ret;
+ 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;
+
+ free(filename2);
+ }
+ }
+
+ if (!write && ((stat(filename, &statbuf) < 0) ||
+ ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
+ UCI_THROW(ctx, UCI_ERR_NOTFOUND);