Implement --output.
authorAndre Noll <maan@systemlinux.org>
Mon, 30 Jun 2008 15:53:52 +0000 (17:53 +0200)
committerAndre Noll <maan@systemlinux.org>
Mon, 30 Jun 2008 15:53:52 +0000 (17:53 +0200)
The option was added long ago, but the implementation was missing..

error.h
select.c

diff --git a/error.h b/error.h
index 7c3f4aa..895f9f8 100644 (file)
--- a/error.h
+++ b/error.h
@@ -25,6 +25,7 @@
        _ERROR(MMAP, "mmap error") \
        _ERROR(OSL, "osl error") \
        _ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR") \
+       _ERROR(OUTPUT, "error writing output file") \
 
 
 /**
index 4795de4..0530257 100644 (file)
--- a/select.c
+++ b/select.c
@@ -111,6 +111,19 @@ static enum enum_count_unit format_count_value(enum enum_count_unit unit,
        return u;
 }
 
+static FILE *output_file;
+
+__printf_1_2 static int output(const char const *fmt, ...)
+{
+       va_list argp;
+       int ret;
+
+       va_start(argp, fmt);
+       ret = vfprintf(output_file, fmt, argp);
+       va_end(argp);
+       return ret < 0? -E_OUTPUT : 1;
+}
+
 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
 {
        char *result = NULL, *tmp;
@@ -184,10 +197,11 @@ static int user_stats_loop_function(struct osl_row *row, void *data)
                        format_count_value(select_conf.count_unit_arg, files,
                                select_conf.count_unit_arg == count_unit_arg_h,
                                formated_value);
-                       printf("\t%s%s", formated_value,
+                       ret = output("\t%s%s", formated_value,
                                (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
-                                       "\t" : "\n"
-                       );
+                                       "\t" : "\n");
+                       if (ret < 0)
+                               goto err;
                }
                if (summary)
                        usi->ui->files += files;
@@ -202,11 +216,13 @@ static int user_stats_loop_function(struct osl_row *row, void *data)
                        format_size_value(select_conf.size_unit_arg, bytes,
                                select_conf.size_unit_arg == size_unit_arg_h,
                                formated_value);
-                       printf("%s%s%s",
+                       ret = output("%s%s%s",
                                (usi->flags & USF_PRINT_FILES)? "" : "\t",
                                formated_value,
                                usi->flags & USF_PRINT_DIRNAME?  "\t" : "\n"
                        );
+                       if (ret < 0)
+                               goto err;
                }
                if (summary) {
                        usi->ui->bytes += bytes;
@@ -222,10 +238,12 @@ static int user_stats_loop_function(struct osl_row *row, void *data)
                ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
                if (ret < 0)
                        goto err;
-               printf("%s%s\n",
+               ret = output("%s%s\n",
                        (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
                        dirname);
                free(dirname);
+               if (ret < 0)
+                       goto err;
        }
        if (usi->count > 0)
                usi->count--;
@@ -258,9 +276,11 @@ static int global_stats_loop_function(struct osl_row *row, void *data)
                        format_count_value(select_conf.count_unit_arg, files,
                                select_conf.count_unit_arg == count_unit_arg_h,
                                formated_value);
-                       printf("\t%s%s", formated_value,
+                       ret = output("\t%s%s", formated_value,
                                (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
                                "\t" : "\n");
+                       if (ret < 0)
+                               goto err;
                }
                if (summary)
                        num_files += files;
@@ -275,11 +295,13 @@ static int global_stats_loop_function(struct osl_row *row, void *data)
                        format_size_value(select_conf.size_unit_arg, bytes,
                                select_conf.size_unit_arg == size_unit_arg_h,
                                formated_value);
-                       printf("%s%s%s",
+                       ret = output("%s%s%s",
                                (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
                                formated_value,
                                (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
                        );
+                       if (ret < 0)
+                               goto err;
                }
                if (summary) {
                        num_bytes += bytes;
@@ -290,10 +312,12 @@ static int global_stats_loop_function(struct osl_row *row, void *data)
                ret = get_dir_name_of_row(row, &dirname);
                if (ret < 0)
                        goto err;
-               printf("%s%s\n",
+               ret = output("%s%s\n",
                        (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
                        dirname);
                free(dirname);
+               if (ret < 0)
+                       goto err;
        }
        if (gsi->count > 0)
                gsi->count--;
@@ -329,27 +353,31 @@ static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private
        return check_loop_return(ret, *loop_ret, *loop_osl_errno);
 }
 
-static void print_global_summary(void)
+static int print_global_summary(void)
 {
        char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
                s[FORMATED_VALUE_SIZE];
        enum enum_count_unit ud, uf;
        enum enum_size_unit us;
+       int ret;
 
        if (select_conf.no_global_summary_given)
-               return;
+               return 1;
        ud = format_count_value(select_conf.count_unit_arg, num_dirs, 0, d);
        uf = format_count_value(select_conf.count_unit_arg, num_files, 0, f);
        us = format_size_value(select_conf.size_unit_arg, num_bytes, 0, s);
 
-       if (!select_conf.no_headers_given)
-               printf("Global summary "
+       if (!select_conf.no_headers_given) {
+               ret = output("Global summary "
                        "(dirs(%c)/files(%c)/size(%c))\n",
                        count_unit_abbrevs[ud],
                        count_unit_abbrevs[uf],
                        size_unit_abbrevs[us]
                );
-       printf("\t%s\t%s\t%s\n\n", d, f, s);
+               if (ret < 0)
+                       return ret;
+       }
+       return output("\t%s\t%s\t%s\n\n", d, f, s);
 }
 
 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
@@ -367,14 +395,13 @@ static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
        format_size_value(select_conf.size_unit_arg, ui->bytes,
                select_conf.size_unit_arg == size_unit_arg_h,
                formated_bytes);
-       printf("\t%s\t%u\t%s\t%s\t%s\n",
+       return output("\t%s\t%u\t%s\t%s\t%s\n",
                ui->pw_name? ui->pw_name : "?",
                (unsigned)ui->uid,
                formated_dir_count,
                formated_file_count,
                formated_bytes
        );
-       return 1;
 }
 
 static int name_comp(const void *a, const void *b)
@@ -427,16 +454,19 @@ static int (*summary_comparators[])(const void *, const void *) = {
        [user_summary_sort_arg_size] = size_comp,
 };
 
-static void print_user_summary(void)
+static int print_user_summary(void)
 {
        if (select_conf.no_user_summary_given)
-               return;
-       if (!select_conf.no_headers_given)
-               printf("User summary "
+               return 1;
+       if (!select_conf.no_headers_given) {
+               int ret = output("User summary "
                        "(pw_name/uid/dirs%s/files%s/size%s):\n",
                        count_unit_buf, count_unit_buf, size_unit_buf);
+               if (ret < 0)
+                       return ret;
+       }
        sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
-       for_each_admissible_user(print_user_summary_line, NULL);
+       return for_each_admissible_user(print_user_summary_line, NULL);
 }
 
 static int print_user_list(struct user_info *ui, __a_unused void *data)
@@ -451,21 +481,29 @@ static int print_user_list(struct user_info *ui, __a_unused void *data)
                usi.count = select_conf.limit_arg;
                usi.ui = ui;
                usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
-               if (!select_conf.no_headers_given)
-                       printf("%s (uid %u), by size%s:\n",
+               if (!select_conf.no_headers_given) {
+                       ret = output("%s (uid %u), by size%s:\n",
                                ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
                                size_unit_buf);
+                       if (ret < 0)
+                               return ret;
+               }
                ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
                        &usi.ret, &usi.osl_errno);
                if (ret < 0)
                        return ret;
-               printf("\n");
+               ret = output("\n");
+               if (ret < 0)
+                       return ret;
        }
        if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
-               if (!select_conf.no_headers_given)
-                       printf("%s (uid %u), by file count%s:\n",
+               if (!select_conf.no_headers_given) {
+                       ret = output("%s (uid %u), by file count%s:\n",
                                ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
                                count_unit_buf);
+                       if (ret < 0)
+                               return ret;
+               }
                usi.count = select_conf.limit_arg,
                usi.ui = ui;
                usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
@@ -473,7 +511,9 @@ static int print_user_list(struct user_info *ui, __a_unused void *data)
                        &usi.ret, &usi.osl_errno);
                if (ret < 0)
                        return ret;
-               printf("\n");
+               ret = output("\n");
+               if (ret < 0)
+                       return ret;
        }
        if (ula == user_list_arg_none && !select_conf.no_user_summary_given) {
                usi.count = select_conf.limit_arg;
@@ -503,26 +543,36 @@ static int print_global_lists(void)
        if (print_size_list) {
                gsi.count = select_conf.limit_arg;
                gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
-               if (!select_conf.no_headers_given)
-                       printf("By size%s:\n", size_unit_buf);
+               if (!select_conf.no_headers_given) {
+                       ret = output("By size%s:\n", size_unit_buf);
+                       if (ret < 0)
+                               return ret;
+               }
                ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
                        global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
                if (ret < 0)
                        return ret;
-               printf("\n");
+               ret = output("\n");
+               if (ret < 0)
+                       return ret;
        }
        if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
                gsi.count = select_conf.limit_arg;
                gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
                if (!print_size_list)
                        gsi.flags |= GSF_COMPUTE_SUMMARY;
-               if (!select_conf.no_headers_given)
-                       printf("By file count%s:\n", count_unit_buf);
+               if (!select_conf.no_headers_given) {
+                       ret = output("By file count%s:\n", count_unit_buf);
+                       if (ret < 0)
+                               return ret;
+               }
                ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
                        global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
                if (ret < 0)
                        return ret;
-               printf("\n");
+               ret = output("\n");
+               if (ret < 0)
+                       return ret;
        }
        if (gla == global_list_arg_none && !select_conf.no_global_summary_given) {
                /* must compute summary */
@@ -543,9 +593,15 @@ static int print_statistics(void)
        ret = print_global_lists();
        if (ret < 0)
                return ret;
-       print_global_summary();
-       print_user_lists();
-       print_user_summary();
+       ret = print_global_summary();
+       if (ret < 0)
+               return ret;
+       ret = print_user_lists();
+       if (ret < 0)
+               return ret;
+       ret = print_user_summary();
+       if (ret < 0)
+               return ret;
        return 1;
 }
 
@@ -588,6 +644,13 @@ int run_select_query(struct uid_range *admissible_uids)
 {
        int ret;
 
+       if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) {
+               output_file = fopen(select_conf.output_arg, "w");
+               if (!output_file)
+                       return -ERRNO_TO_ERROR(errno);
+       } else
+               output_file = stdout;
+
        if (select_conf.count_unit_arg != count_unit_arg_h)
                count_unit_buf[1] = count_unit_abbrevs[select_conf.count_unit_arg];
        else
@@ -599,14 +662,17 @@ int run_select_query(struct uid_range *admissible_uids)
 
        ret = open_dir_table(0);
        if (ret < 0)
-               return ret;
+               goto out;
        check_signals();
        ret = read_uid_file(admissible_uids);
        if (ret < 0)
-               return ret;
+               goto out;
        check_signals();
        ret = print_statistics();
+out:
        close_all_tables();
+       if (output_file != stdout)
+               fclose(output_file);
        return ret;
 }