uci: properly close input before exit
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 2ee1e36..e1e0a66 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -286,7 +286,7 @@ static int package_cmd(int cmd, char *tuple)
 {
        struct uci_element *e = NULL;
        struct uci_ptr ptr;
-       int ret = 0;
+       int ret = 1;
 
        if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
                cli_perror();
@@ -299,21 +299,25 @@ static int package_cmd(int cmd, char *tuple)
                uci_show_changes(ptr.p);
                break;
        case CMD_COMMIT:
-               if (flags & CLI_FLAG_NOCOMMIT)
-                       return 0;
+               if (flags & CLI_FLAG_NOCOMMIT) {
+                       ret = 0;
+                       goto out;
+               }
                if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
                        cli_perror();
-                       ret = 1;
+                       goto out;
                }
                break;
        case CMD_EXPORT:
-               uci_export(ctx, stdout, ptr.p, true);
+               if (uci_export(ctx, stdout, ptr.p, true) != UCI_OK) {
+                       goto out;
+               }
                break;
        case CMD_SHOW:
                if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
                        ctx->err = UCI_ERR_NOTFOUND;
                        cli_perror();
-                       ret = 1;
+                       goto out;
                }
                switch(e->type) {
                        case UCI_TYPE_PACKAGE:
@@ -327,11 +331,14 @@ static int package_cmd(int cmd, char *tuple)
                                break;
                        default:
                                /* should not happen */
-                               return 1;
+                               goto out;
                }
                break;
        }
 
+       ret = 0;
+
+out:
        if (ptr.p)
                uci_unload(ctx, ptr.p);
        return ret;
@@ -680,6 +687,7 @@ int main(int argc, char **argv)
                                break;
                        case 'f':
                                if (input != stdin) {
+                                       fclose(input);
                                        perror("uci");
                                        return 1;
                                }