4cb18d231fcd3d1d3b743b3b47c28f4f7e184f6d
[project/uci.git] / file.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
5  * this program is free software; you can redistribute it and/or modify
6  * it under the terms of the gnu lesser general public license version 2.1
7  * as published by the free software foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 /*
16  * This file contains the code for parsing uci config files
17  */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <stdbool.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <ctype.h>
26
27 #define LINEBUF 32
28 #define LINEBUF_MAX     4096
29
30 static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
31 {
32         struct uci_parse_context *pctx = ctx->pctx;
33
34         pctx->reason = reason;
35         pctx->byte = pos - pctx->buf;
36         UCI_THROW(ctx, UCI_ERR_PARSE);
37 }
38
39 /*
40  * Fetch a new line from the input stream and resize buffer if necessary
41  */
42 static void uci_getln(struct uci_context *ctx, int offset)
43 {
44         struct uci_parse_context *pctx = ctx->pctx;
45         char *p;
46         int ofs;
47
48         if (pctx->buf == NULL) {
49                 pctx->buf = uci_malloc(ctx, LINEBUF);
50                 pctx->bufsz = LINEBUF;
51         }
52
53         ofs = offset;
54         do {
55                 p = &pctx->buf[ofs];
56                 p[ofs] = 0;
57
58                 p = fgets(p, pctx->bufsz - ofs, pctx->file);
59                 if (!p || !*p)
60                         return;
61
62                 ofs += strlen(p);
63                 if (pctx->buf[ofs - 1] == '\n') {
64                         pctx->line++;
65                         pctx->buf[ofs - 1] = 0;
66                         return;
67                 }
68
69                 if (pctx->bufsz > LINEBUF_MAX/2)
70                         uci_parse_error(ctx, p, "line too long");
71
72                 pctx->bufsz *= 2;
73                 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
74         } while (1);
75 }
76
77 /*
78  * Clean up all extra memory used by the parser and exporter
79  */
80 static void uci_file_cleanup(struct uci_context *ctx)
81 {
82         struct uci_parse_context *pctx;
83
84         if (ctx->buf) {
85                 free(ctx->buf);
86                 ctx->buf = NULL;
87                 ctx->bufsz = 0;
88         }
89
90         pctx = ctx->pctx;
91         if (!pctx)
92                 return;
93
94         ctx->pctx = NULL;
95         if (pctx->package)
96                 uci_free_package(&pctx->package);
97
98         if (pctx->buf)
99                 free(pctx->buf);
100
101         free(pctx);
102 }
103
104 /* 
105  * parse a character escaped by '\'
106  * returns true if the escaped character is to be parsed
107  * returns false if the escaped character is to be ignored
108  */
109 static inline bool parse_backslash(struct uci_context *ctx, char **str)
110 {
111         /* skip backslash */
112         *str += 1;
113
114         /* undecoded backslash at the end of line, fetch the next line */
115         if (!**str) {
116                 *str += 1;
117                 uci_getln(ctx, *str - ctx->pctx->buf);
118                 return false;
119         }
120
121         /* FIXME: decode escaped char, necessary? */
122         return true;
123 }
124
125 /*
126  * move the string pointer forward until a non-whitespace character or
127  * EOL is reached
128  */
129 static void skip_whitespace(struct uci_context *ctx, char **str)
130 {
131 restart:
132         while (**str && isspace(**str))
133                 *str += 1;
134
135         if (**str == '\\') {
136                 if (!parse_backslash(ctx, str))
137                         goto restart;
138         }
139 }
140
141 static inline void addc(char **dest, char **src)
142 {
143         **dest = **src;
144         *dest += 1;
145         *src += 1;
146 }
147
148 /*
149  * parse a double quoted string argument from the command line
150  */
151 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
152 {
153         char c;
154
155         /* skip quote character */
156         *str += 1;
157
158         while ((c = **str)) {
159                 switch(c) {
160                 case '"':
161                         **target = 0;
162                         *str += 1;
163                         return;
164                 case '\\':
165                         if (!parse_backslash(ctx, str))
166                                 continue;
167                         /* fall through */
168                 default:
169                         addc(target, str);
170                         break;
171                 }
172         }
173         uci_parse_error(ctx, *str, "unterminated \"");
174 }
175
176 /*
177  * parse a single quoted string argument from the command line
178  */
179 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
180 {
181         char c;
182         /* skip quote character */
183         *str += 1;
184
185         while ((c = **str)) {
186                 switch(c) {
187                 case '\'':
188                         **target = 0;
189                         *str += 1;
190                         return;
191                 default:
192                         addc(target, str);
193                 }
194         }
195         uci_parse_error(ctx, *str, "unterminated '");
196 }
197
198 /*
199  * parse a string from the command line and detect the quoting style
200  */
201 static void parse_str(struct uci_context *ctx, char **str, char **target)
202 {
203         do {
204                 switch(**str) {
205                 case '\'':
206                         parse_single_quote(ctx, str, target);
207                         break;
208                 case '"':
209                         parse_double_quote(ctx, str, target);
210                         break;
211                 case 0:
212                         goto done;
213                 case '\\':
214                         if (!parse_backslash(ctx, str))
215                                 continue;
216                         /* fall through */
217                 default:
218                         addc(target, str);
219                         break;
220                 }
221         } while (**str && !isspace(**str));
222 done:
223
224         /* 
225          * if the string was unquoted and we've stopped at a whitespace
226          * character, skip to the next one, because the whitespace will
227          * be overwritten by a null byte here
228          */
229         if (**str)
230                 *str += 1;
231
232         /* terminate the parsed string */
233         **target = 0;
234 }
235
236 /*
237  * extract the next argument from the command line
238  */
239 static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
240 {
241         char *val;
242         char *ptr;
243
244         val = ptr = *str;
245         skip_whitespace(ctx, str);
246         parse_str(ctx, str, &ptr);
247         if (!*val) {
248                 if (required)
249                         uci_parse_error(ctx, *str, "insufficient arguments");
250                 goto done;
251         }
252
253         if (name && !uci_validate_name(val))
254                 uci_parse_error(ctx, val, "invalid character in field");
255
256 done:
257         return val;
258 }
259
260 /*
261  * verify that the end of the line or command is reached.
262  * throw an error if extra arguments are given on the command line
263  */
264 static void assert_eol(struct uci_context *ctx, char **str)
265 {
266         char *tmp;
267
268         tmp = next_arg(ctx, str, false, false);
269         if (tmp && *tmp)
270                 uci_parse_error(ctx, *str, "too many arguments");
271 }
272
273 /* 
274  * switch to a different config, either triggered by uci_load, or by a
275  * 'package <...>' statement in the import file
276  */
277 static void uci_switch_config(struct uci_context *ctx)
278 {
279         struct uci_parse_context *pctx;
280         struct uci_element *e;
281         const char *name;
282
283         pctx = ctx->pctx;
284         name = pctx->name;
285
286         /* add the last config to main config file list */
287         if (pctx->package) {
288                 uci_list_add(&ctx->root, &pctx->package->e.list);
289
290                 pctx->package = NULL;
291                 pctx->section = NULL;
292         }
293
294         if (!name)
295                 return;
296
297         /* 
298          * if an older config under the same name exists, unload it
299          * ignore errors here, e.g. if the config was not found
300          */
301         e = uci_lookup_list(ctx, &ctx->root, name);
302         if (e)
303                 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
304         pctx->package = uci_alloc_package(ctx, name);
305 }
306
307 /*
308  * parse the 'package' uci command (next config package)
309  */
310 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
311 {
312         char *name = NULL;
313
314         /* command string null-terminated by strtok */
315         *str += strlen(*str) + 1;
316
317         name = next_arg(ctx, str, true, true);
318         assert_eol(ctx, str);
319         if (single)
320                 return;
321
322         ctx->pctx->name = name;
323         uci_switch_config(ctx);
324 }
325
326 /*
327  * parse the 'config' uci command (open a section)
328  */
329 static void uci_parse_config(struct uci_context *ctx, char **str)
330 {
331         char *name = NULL;
332         char *type = NULL;
333
334         if (!ctx->pctx->package) {
335                 if (!ctx->pctx->name)
336                         uci_parse_error(ctx, *str, "attempting to import a file without a package name");
337
338                 uci_switch_config(ctx);
339         }
340
341         /* command string null-terminated by strtok */
342         *str += strlen(*str) + 1;
343
344         type = next_arg(ctx, str, true, true);
345         name = next_arg(ctx, str, false, true);
346         assert_eol(ctx, str);
347         ctx->pctx->section = uci_alloc_section(ctx->pctx->package, type, name);
348 }
349
350 /*
351  * parse the 'option' uci command (open a value)
352  */
353 static void uci_parse_option(struct uci_context *ctx, char **str)
354 {
355         char *name = NULL;
356         char *value = NULL;
357
358         if (!ctx->pctx->section)
359                 uci_parse_error(ctx, *str, "option command found before the first section");
360
361         /* command string null-terminated by strtok */
362         *str += strlen(*str) + 1;
363
364         name = next_arg(ctx, str, true, true);
365         value = next_arg(ctx, str, true, false);
366         assert_eol(ctx, str);
367         uci_alloc_option(ctx->pctx->section, name, value);
368 }
369
370
371 /*
372  * parse a complete input line, split up combined commands by ';'
373  */
374 static void uci_parse_line(struct uci_context *ctx, bool single)
375 {
376         struct uci_parse_context *pctx = ctx->pctx;
377         char *word, *brk = NULL;
378
379         for (word = strtok_r(pctx->buf, ";", &brk);
380                  word;
381                  word = strtok_r(NULL, ";", &brk)) {
382
383                 char *pbrk = NULL;
384                 word = strtok_r(word, " \t", &pbrk);
385
386                 switch(word[0]) {
387                         case 'p':
388                                 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
389                                         uci_parse_package(ctx, &word, single);
390                                 break;
391                         case 'c':
392                                 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
393                                         uci_parse_config(ctx, &word);
394                                 break;
395                         case 'o':
396                                 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
397                                         uci_parse_option(ctx, &word);
398                                 break;
399                         default:
400                                 uci_parse_error(ctx, word, "unterminated command");
401                                 break;
402                 }
403         }
404 }
405
406 /* max number of characters that escaping adds to the string */
407 #define UCI_QUOTE_ESCAPE        "'\\''"
408
409 /*
410  * escape an uci string for export
411  */
412 static char *uci_escape(struct uci_context *ctx, char *str)
413 {
414         char *s, *p;
415         int pos = 0;
416
417         if (!ctx->buf) {
418                 ctx->bufsz = LINEBUF;
419                 ctx->buf = malloc(LINEBUF);
420         }
421
422         s = str;
423         p = strchr(str, '\'');
424         if (!p)
425                 return str;
426
427         do {
428                 int len = p - s;
429                 if (len > 0) {
430                         if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
431                                 ctx->bufsz *= 2;
432                                 ctx->buf = realloc(ctx->buf, ctx->bufsz);
433                                 if (!ctx->buf)
434                                         UCI_THROW(ctx, UCI_ERR_MEM);
435                         }
436                         memcpy(&ctx->buf[pos], s, len);
437                         pos += len;
438                 }
439                 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
440                 pos += sizeof(UCI_QUOTE_ESCAPE);
441                 s = p + 1;
442         } while ((p = strchr(s, '\'')));
443
444         return ctx->buf;
445 }
446
447
448 /*
449  * export a single config package to a file stream
450  */
451 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
452 {
453         struct uci_context *ctx = p->ctx;
454         struct uci_element *s, *o;
455
456         if (header)
457                 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
458         uci_foreach_element(&p->sections, s) {
459                 struct uci_section *sec = uci_to_section(s);
460                 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
461                 fprintf(stream, " '%s'\n", uci_escape(ctx, sec->e.name));
462                 uci_foreach_element(&sec->options, o) {
463                         struct uci_option *opt = uci_to_option(o);
464                         fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
465                         fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
466                 }
467         }
468         fprintf(stream, "\n");
469 }
470
471 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
472 {
473         struct uci_element *e;
474
475         UCI_HANDLE_ERR(ctx);
476         UCI_ASSERT(ctx, stream != NULL);
477
478         if (package)
479                 uci_export_package(package, stream, header);
480         else {
481                 uci_foreach_element(&ctx->root, e) {
482                         uci_export_package(uci_to_package(e), stream, header);
483                 }
484         }
485
486         return 0;
487 }
488
489 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
490 {
491         struct uci_parse_context *pctx;
492
493         /* make sure no memory from previous parse attempts is leaked */
494         uci_file_cleanup(ctx);
495
496         pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
497         ctx->pctx = pctx;
498         pctx->file = stream;
499
500         /*
501          * If 'name' was supplied, assume that the supplied stream does not contain
502          * the appropriate 'package <name>' string to specify the config name
503          * NB: the config file can still override the package name
504          */
505         if (name)
506                 pctx->name = name;
507
508         while (!feof(pctx->file)) {
509                 uci_getln(ctx, 0);
510                 UCI_TRAP_SAVE(ctx, error);
511                 if (pctx->buf[0])
512                         uci_parse_line(ctx, single);
513                 UCI_TRAP_RESTORE(ctx);
514                 continue;
515 error:
516                 if (ctx->flags & UCI_FLAG_PERROR)
517                         uci_perror(ctx, NULL);
518                 if ((ctx->errno != UCI_ERR_PARSE) ||
519                         (ctx->flags & UCI_FLAG_STRICT))
520                         UCI_THROW(ctx, ctx->errno);
521         }
522
523         if (package)
524                 *package = pctx->package;
525
526         pctx->name = NULL;
527         uci_switch_config(ctx);
528
529         /* no error happened, we can get rid of the parser context now */
530         uci_file_cleanup(ctx);
531
532         return 0;
533 }
534
535 /*
536  * open a stream and go to the right position
537  *
538  * note: when opening for write and seeking to the beginning of
539  * the stream, truncate the file
540  */
541 static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
542 {
543         struct stat statbuf;
544         FILE *file = NULL;
545         int fd, ret;
546         int mode = (write ? O_RDWR : O_RDONLY);
547
548         if (create)
549                 mode |= O_CREAT;
550
551         if (!write && ((stat(filename, &statbuf) < 0) ||
552                 ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
553                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
554         }
555
556         fd = open(filename, mode, UCI_FILEMODE);
557         if (fd <= 0)
558                 goto error;
559
560         if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
561                 goto error;
562
563         ret = lseek(fd, 0, pos);
564
565         if (ret < 0)
566                 goto error;
567
568         file = fdopen(fd, (write ? "w+" : "r"));
569         if (file)
570                 goto done;
571
572 error:
573         UCI_THROW(ctx, UCI_ERR_IO);
574 done:
575         return file;
576 }
577
578 static void uci_close_stream(FILE *stream)
579 {
580         int fd;
581
582         if (!stream)
583                 return;
584
585         fd = fileno(stream);
586         flock(fd, LOCK_UN);
587         fclose(stream);
588 }
589
590 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
591 {
592         bool delete = false;
593         char *package = NULL;
594         char *section = NULL;
595         char *option = NULL;
596         char *value = NULL;
597
598         if (buf[0] == '-') {
599                 delete = true;
600                 buf++;
601         }
602
603         UCI_INTERNAL(uci_parse_tuple, ctx, buf, &package, &section, &option, &value);
604         if (!package || !section || (!delete && !value))
605                 goto error;
606         if (strcmp(package, p->e.name) != 0)
607                 goto error;
608         if (!uci_validate_name(section))
609                 goto error;
610         if (option && !uci_validate_name(option))
611                 goto error;
612
613         if (delete)
614                 UCI_INTERNAL(uci_del, ctx, p, section, option);
615         else
616                 UCI_INTERNAL(uci_set, ctx, p, section, option, value);
617
618         return;
619 error:
620         UCI_THROW(ctx, UCI_ERR_PARSE);
621 }
622
623 static void uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
624 {
625         struct uci_parse_context *pctx;
626
627         /* make sure no memory from previous parse attempts is leaked */
628         uci_file_cleanup(ctx);
629
630         pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
631         ctx->pctx = pctx;
632         pctx->file = stream;
633
634         while (!feof(pctx->file)) {
635                 uci_getln(ctx, 0);
636                 if (!pctx->buf[0])
637                         continue;
638
639                 /*
640                  * ignore parse errors in single lines, we want to preserve as much
641                  * history as possible
642                  */
643                 UCI_TRAP_SAVE(ctx, error);
644                 uci_parse_history_line(ctx, p, pctx->buf);
645                 UCI_TRAP_RESTORE(ctx);
646 error:
647                 continue;
648         }
649
650         /* no error happened, we can get rid of the parser context now */
651         uci_file_cleanup(ctx);
652 }
653
654 static void uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
655 {
656         char *filename = NULL;
657         FILE *f = NULL;
658
659         if (!p->confdir)
660                 return;
661         if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
662                 UCI_THROW(ctx, UCI_ERR_MEM);
663
664         UCI_TRAP_SAVE(ctx, done);
665         f = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
666         uci_parse_history(ctx, f, p);
667         UCI_TRAP_RESTORE(ctx);
668
669 done:
670         if (flush && f) {
671                 rewind(f);
672                 ftruncate(fileno(f), 0);
673         }
674         if (filename)
675                 free(filename);
676         uci_close_stream(f);
677         ctx->errno = 0;
678 }
679
680
681 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
682 {
683         char *filename;
684         bool confdir;
685         FILE *file = NULL;
686
687         UCI_HANDLE_ERR(ctx);
688         UCI_ASSERT(ctx, name != NULL);
689
690         switch (name[0]) {
691         case '.':
692                 /* relative path outside of /etc/config */
693                 if (name[1] != '/')
694                         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
695                 /* fall through */
696         case '/':
697                 /* absolute path outside of /etc/config */
698                 filename = uci_strdup(ctx, name);
699                 name = strrchr(name, '/') + 1;
700                 confdir = false;
701                 break;
702         default:
703                 /* config in /etc/config */
704                 if (strchr(name, '/'))
705                         UCI_THROW(ctx, UCI_ERR_INVAL);
706                 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
707                 sprintf(filename, UCI_CONFDIR "/%s", name);
708                 confdir = true;
709                 break;
710         }
711
712         file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
713         ctx->errno = 0;
714         UCI_TRAP_SAVE(ctx, done);
715         uci_import(ctx, file, name, package, true);
716         UCI_TRAP_RESTORE(ctx);
717
718         if (*package) {
719                 (*package)->path = filename;
720                 (*package)->confdir = confdir;
721                 uci_load_history(ctx, *package, false);
722         }
723
724 done:
725         uci_close_stream(file);
726         return ctx->errno;
727 }
728
729 int uci_save(struct uci_context *ctx, struct uci_package *p)
730 {
731         FILE *f = NULL;
732         char *filename = NULL;
733         struct uci_element *e, *tmp;
734
735         UCI_HANDLE_ERR(ctx);
736         UCI_ASSERT(ctx, p != NULL);
737
738         /* 
739          * if the config file was outside of the /etc/config path,
740          * don't save the history to a file, update the real file
741          * directly.
742          * does not modify the uci_package pointer
743          */
744         if (!p->confdir)
745                 return uci_commit(ctx, &p);
746
747         if (uci_list_empty(&p->history))
748                 return 0;
749
750         if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
751                 UCI_THROW(ctx, UCI_ERR_MEM);
752
753         ctx->errno = 0;
754         UCI_TRAP_SAVE(ctx, done);
755         f = uci_open_stream(ctx, filename, SEEK_END, true, true);
756         UCI_TRAP_RESTORE(ctx);
757
758         uci_foreach_element_safe(&p->history, tmp, e) {
759                 struct uci_history *h = uci_to_history(e);
760
761                 if (h->cmd == UCI_CMD_REMOVE)
762                         fprintf(f, "-");
763
764                 fprintf(f, "%s.%s", p->e.name, h->section);
765                 if (e->name)
766                         fprintf(f, ".%s", e->name);
767
768                 if (h->cmd == UCI_CMD_REMOVE)
769                         fprintf(f, "\n");
770                 else
771                         fprintf(f, "=%s\n", h->value);
772                 uci_free_history(h);
773         }
774
775 done:
776         uci_close_stream(f);
777         if (filename)
778                 free(filename);
779         if (ctx->errno)
780                 UCI_THROW(ctx, ctx->errno);
781
782         return 0;
783 }
784
785 int uci_commit(struct uci_context *ctx, struct uci_package **package)
786 {
787         struct uci_package *p;
788         FILE *f = NULL;
789         char *name = NULL;
790         char *path = NULL;
791
792         UCI_HANDLE_ERR(ctx);
793         UCI_ASSERT(ctx, package != NULL);
794         p = *package;
795
796         UCI_ASSERT(ctx, p != NULL);
797         UCI_ASSERT(ctx, p->path != NULL);
798
799         /* open the config file for writing now, so that it is locked */
800         f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
801
802         /* flush unsaved changes and reload from history file */
803         UCI_TRAP_SAVE(ctx, done);
804         if (p->confdir) {
805                 name = uci_strdup(ctx, p->e.name);
806                 path = uci_strdup(ctx, p->path);
807                 if (!uci_list_empty(&p->history))
808                         UCI_INTERNAL(uci_save, ctx, p);
809                 uci_free_package(&p);
810                 uci_file_cleanup(ctx);
811                 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
812
813                 p->path = path;
814                 p->confdir = true;
815                 *package = p;
816
817                 /* freed together with the uci_package */
818                 path = NULL;
819
820                 /* check for updated history, just in case */
821                 uci_load_history(ctx, p, true);
822         }
823
824         rewind(f);
825         ftruncate(fileno(f), 0);
826
827         uci_export(ctx, f, p, false);
828         UCI_TRAP_RESTORE(ctx);
829
830 done:
831         if (name)
832                 free(name);
833         if (path)
834                 free(path);
835         uci_close_stream(f);
836         if (ctx->errno)
837                 UCI_THROW(ctx, ctx->errno);
838
839         return 0;
840 }
841
842
843 /* 
844  * This function returns the filename by returning the string
845  * after the last '/' character. By checking for a non-'\0'
846  * character afterwards, directories are ignored (glob marks
847  * those with a trailing '/'
848  */
849 static inline char *get_filename(char *path)
850 {
851         char *p;
852
853         p = strrchr(path, '/');
854         p++;
855         if (!*p)
856                 return NULL;
857         return p;
858 }
859
860 int uci_list_configs(struct uci_context *ctx, char ***list)
861 {
862         char **configs;
863         glob_t globbuf;
864         int size, i;
865         char *buf;
866
867         UCI_HANDLE_ERR(ctx);
868
869         if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
870                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
871
872         size = sizeof(char *) * (globbuf.gl_pathc + 1);
873         for(i = 0; i < globbuf.gl_pathc; i++) {
874                 char *p;
875
876                 p = get_filename(globbuf.gl_pathv[i]);
877                 if (!p)
878                         continue;
879
880                 size += strlen(p) + 1;
881         }
882
883         configs = uci_malloc(ctx, size);
884         buf = (char *) &configs[globbuf.gl_pathc + 1];
885         for(i = 0; i < globbuf.gl_pathc; i++) {
886                 char *p;
887
888                 p = get_filename(globbuf.gl_pathv[i]);
889                 if (!p)
890                         continue;
891
892                 configs[i] = buf;
893                 strcpy(buf, p);
894                 buf += strlen(buf) + 1;
895         }
896         *list = configs;
897
898         return 0;
899 }
900