Output improvements.
authorAndre Noll <maan@systemlinux.org>
Sat, 8 Nov 2008 17:21:13 +0000 (18:21 +0100)
committerAndre Noll <maan@systemlinux.org>
Sat, 8 Nov 2008 17:21:13 +0000 (18:21 +0100)
- Implement appending to an output file.
- Do not override the output file by default
- Implement piping the output into a command.

error.h
select.c

diff --git a/error.h b/error.h
index 00fbbc867dd4d488c4886283c391950d4fcb7f34..87d3fe2820ff16f80fcd0e6718344d7bd107bf4e 100644 (file)
--- a/error.h
+++ b/error.h
@@ -32,6 +32,7 @@
        _ERROR(UNIT, "no unit allowed here") \
        _ERROR(BAD_UNIT, "invalid unit specifier") \
        _ERROR(BAD_ATOM, "invalid atom") \
        _ERROR(UNIT, "no unit allowed here") \
        _ERROR(BAD_UNIT, "invalid unit specifier") \
        _ERROR(BAD_ATOM, "invalid atom") \
+       _ERROR(BAD_OUTPUT_ARG, "invalid name for output") \
 
 
 /**
 
 
 /**
index e66b2dd41a28127dcada1a8b3cbe587ea5d464a6..152e4b98667c69fe68d5a0a53fb53b2f90b677ab 100644 (file)
--- a/select.c
+++ b/select.c
@@ -586,17 +586,102 @@ static int print_statistics(struct format_info *fi)
        return -ERRNO_TO_ERROR(-EINVAL);
 }
 
        return -ERRNO_TO_ERROR(-EINVAL);
 }
 
-int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
+static int open_pipe(char *path)
 {
 {
-       int ret;
+       int p[2], ret, argc;
+       char **argv;
 
 
-       if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) {
-               output_file = fopen(select_conf.output_arg, "w");
+       ret = pipe(p);
+       if (ret < 0)
+               return ERRNO_TO_ERROR(errno);
+       ret = fork();
+       if (ret < 0)
+               return ERRNO_TO_ERROR(errno);
+       if (ret) { /* parent */
+               DEBUG_LOG("created process %d\n", ret);
+               close(p[0]);
+               output_file = fdopen(p[1], "w");
                if (!output_file)
                if (!output_file)
-                       return -ERRNO_TO_ERROR(errno);
-       } else
-               output_file = stdout;
+                       return ERRNO_TO_ERROR(errno);
+               return 1;
+       }
+       close(p[1]);
+       if (p[0] != STDIN_FILENO)
+               dup2(p[0], STDIN_FILENO);
+       DEBUG_LOG("executing %s\n", path);
+       argc = split_args(path, &argv, " \t");
+       execvp(argv[0], argv);
+       ERROR_LOG("error executing %s: %s\n", path,
+               adu_strerror(ERRNO_TO_ERROR(errno)));
+       _exit(EXIT_FAILURE);
+}
+
+static int open_output_stream(void)
+{
+       char *p;
+       int ret, flags = O_WRONLY | O_CREAT;
+
+       if (!select_conf.output_given)
+               goto use_stdout;
+       p = select_conf.output_arg;
+       switch (p[0]) {
+       case '\0': /* empty string */
+               goto bad_output_arg;
+       case '-':
+               if (!p[1]) /* "-" means stdout */
+                       goto use_stdout;
+               /* string starting with a dash */
+               flags |= O_EXCL;
+               goto open_file;
+       case '>':
+               if (!p[1]) /* ">" is invalid */
+                       goto bad_output_arg;
+               if (p[1] != '>') {
+                       p++;
+                       flags |= O_TRUNC;
+                       goto open_file;
+               }
+               /* string starting with ">>" */
+               if (!p[2]) /* ">>" is invalid */
+                       goto bad_output_arg;
+               flags |= O_APPEND;
+               p += 2;
+               goto open_file;
+       case '|':
+               if (!p[1]) /* "|" is invalid */
+                       goto bad_output_arg;
+               p++;
+               return open_pipe(p);
+       default: /* args starts with no magic character */
+               flags |= O_EXCL;
+               goto open_file;
+       }
+use_stdout:
+       output_file = stdout;
+       return 1;
+bad_output_arg:
+       output_file = NULL;
+       return -E_BAD_OUTPUT_ARG;
+open_file:
+       /*
+        * glibc's 'x' mode to fopen is not portable, so use open() and
+        * fdopen().
+        */
+       ret = open(p, flags, 0644);
+       if (ret < 0)
+               return -ERRNO_TO_ERROR(errno);
+       output_file = fdopen(ret, "w");
+       if (!output_file)
+               return -ERRNO_TO_ERROR(errno);
+       return 1;
+}
 
 
+int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
+{
+       int ret = open_output_stream();
+
+       if (ret < 0)
+               goto out;
        ret = open_dir_table(0);
        if (ret < 0)
                goto out;
        ret = open_dir_table(0);
        if (ret < 0)
                goto out;
@@ -608,8 +693,10 @@ int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
        ret = print_statistics(fi);
 out:
        close_all_tables();
        ret = print_statistics(fi);
 out:
        close_all_tables();
-       if (output_file != stdout)
+       if (output_file && output_file != stdout) {
                fclose(output_file);
                fclose(output_file);
+               output_file = NULL;
+       }
        return ret;
 }
 
        return ret;
 }