Add options for selecting what to print.
authorAndre Noll <maan@systemlinux.org>
Sun, 1 Jun 2008 22:55:36 +0000 (00:55 +0200)
committerAndre Noll <maan@systemlinux.org>
Sun, 1 Jun 2008 22:55:36 +0000 (00:55 +0200)
adu.ggo
select.c

diff --git a/adu.ggo b/adu.ggo
index 2a6af08..bfdaf5c 100644 (file)
--- a/adu.ggo
+++ b/adu.ggo
@@ -182,6 +182,7 @@ enum typestr="format"
 values="h","b","k","m","g","t"
 default="h"
 optional
+dependon="select"
 details="
        Print sizes in the given unit: human-readable, bytes,
        kilobytes (2^10), megabytes (2^20), gigabytes (2^30), terabytes
@@ -195,6 +196,7 @@ enum typestr="format"
 values="h","n","k","m","g","t"
 default="h"
 optional
+dependon="select"
 details="
        Print the number of files/directories in the given unit:
        human-readable, number, number/10^3, number/10^6, number/10^12,
@@ -202,6 +204,58 @@ details="
        format.
 "
 
+option "no-headers" -
+#~~~~~~~~~~~~~~~~~~~~
+"supress descriptions for listings/tables"
+flag off
+dependon="select"
+details="
+       This is mostly useful to feed the output of adu to scripts.
+"
+
+option "global-list" -
+#~~~~~~~~~~~~~~~~~~~~~
+"how to print global directory listings"
+enum typestr="which"
+values="size","file_count","both","none"
+default="both"
+optional
+dependon="select"
+details="
+       By default adu prints two global directory listings: The
+       first prints the directory names ordered by the sum of the
+       sizes of the contained files while the second listing prints
+       them sorted by the number of files. This option can be used
+       to print only one or neither of these two listings.
+"
+
+option "no-global-summary" -
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~
+"do not print the summary line"
+flag off
+dependon="select"
+
+option "user-list" -
+#~~~~~~~~~~~~~~~~~~~
+"how to print per-user directory listings"
+enum typestr="which"
+values="size","file_count","both","none"
+default="both"
+optional
+dependon="select"
+details="
+       Similar to the global directory listings mentioned above,
+       adu can print two directory listings per user. This option
+       controls which of the these should be printed.
+"
+
+option "no-user-summary" -
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~
+"do not print the user summary table"
+flag off
+dependon="select"
+
+
 option "user-summary-sort" -
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~
 "how to sort the user-summary"
@@ -209,6 +263,7 @@ enum typestr="col_spec"
 values="name","uid","dir_count","file_count","size"
 default="size"
 optional
+dependon="select"
 details="
        It is enough to specify the first letter of the column specifier,
        e.g. \"--user-summary-sort f\" sorts by file count.
index 515bbc4..59755a6 100644 (file)
--- a/select.c
+++ b/select.c
@@ -349,19 +349,20 @@ static void print_global_summary(void)
        enum enum_count_unit ud, uf;
        enum enum_size_unit us;
 
+       if (conf.no_global_summary_given)
+               return;
        ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d);
        uf = format_count_value(conf.count_unit_arg, num_files, 0, f);
        us = format_size_value(conf.size_unit_arg, num_bytes, 0, s);
 
-       printf("Global summary "
-               "(dirs(%c)/files(%c)/size(%c))\n"
-               "\t%s\t%s\t%s\n\n",
-               count_unit_abbrevs[ud],
-               count_unit_abbrevs[uf],
-               size_unit_abbrevs[us],
-               d, f, s
-       );
-
+       if (!conf.no_headers_given)
+               printf("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);
 }
 
 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
@@ -441,74 +442,122 @@ static int (*summary_comparators[])(const void *, const void *) = {
 
 static void print_user_summary(void)
 {
-       printf("User summary "
-               "(pw_name/uid/dirs%s/files%s/size%s):\n",
-               count_unit_buf, count_unit_buf, size_unit_buf);
+       if (conf.no_user_summary_given)
+               return;
+       if (!conf.no_headers_given)
+               printf("User summary "
+                       "(pw_name/uid/dirs%s/files%s/size%s):\n",
+                       count_unit_buf, count_unit_buf, size_unit_buf);
        sort_hash_table(summary_comparators[conf.user_summary_sort_arg]);
        for_each_admissible_user(print_user_summary_line, NULL);
 }
 
-static int print_user_stat(struct user_info *ui, __a_unused void *data)
+static int print_user_list(struct user_info *ui, __a_unused void *data)
 {
        int ret;
-       struct user_stats_info usi = {
-               .count = conf.limit_arg,
-               .ui = ui
-       };
-
-       usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
-       printf("%s (uid %u), by size%s:\n",
-               ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
-               size_unit_buf);
-       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%s (uid %u), by file count%s:\n",
-               ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
-               count_unit_buf);
-       usi.count = conf.limit_arg,
-       usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
-       ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
-               &usi.ret, &usi.osl_errno);
-       if (ret < 0)
-               return ret;
-       printf("\n");
+       struct user_stats_info usi;
+       enum enum_user_list ula = conf.user_list_arg;
+       int print_size_list = (ula == user_list_arg_size
+               || ula == user_list_arg_both);
+
+       if (print_size_list) {
+               usi.count = conf.limit_arg;
+               usi.ui = ui;
+               usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
+               if (!conf.no_headers_given)
+                       printf("%s (uid %u), by size%s:\n",
+                               ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
+                               size_unit_buf);
+               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");
+       }
+       if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
+               if (!conf.no_headers_given)
+                       printf("%s (uid %u), by file count%s:\n",
+                               ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
+                               count_unit_buf);
+               usi.count = conf.limit_arg,
+               usi.ui = ui;
+               usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
+               ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
+                       &usi.ret, &usi.osl_errno);
+               if (ret < 0)
+                       return ret;
+               printf("\n");
+       }
+       if (ula == user_list_arg_none && !conf.no_user_summary_given) {
+               usi.count = conf.limit_arg;
+               usi.ui = ui;
+               usi.flags = USF_COMPUTE_SUMMARY;
+               ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
+                       &usi.ret, &usi.osl_errno);
+               if (ret < 0)
+                       return ret;
+       }
        return 1;
 }
 
-static int print_user_stats(void)
+static int print_user_lists(void)
+{
+       return for_each_admissible_user(print_user_list, NULL);
+}
+
+static int print_global_lists(void)
 {
-       return for_each_admissible_user(print_user_stat, NULL);
+       struct global_stats_info gsi;
+       int ret;
+       enum enum_global_list gla = conf.global_list_arg;
+       int print_size_list = (gla == global_list_arg_size
+               || gla == global_list_arg_both);
+
+       if (print_size_list) {
+               gsi.count = conf.limit_arg;
+               gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
+               if (!conf.no_headers_given)
+                       printf("By size%s:\n", size_unit_buf);
+               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");
+       }
+       if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
+               gsi.count = conf.limit_arg;
+               gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
+               if (!print_size_list)
+                       gsi.flags |= GSF_COMPUTE_SUMMARY;
+               if (!conf.no_headers_given)
+                       printf("By file count%s:\n", count_unit_buf);
+               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");
+       }
+       if (gla == global_list_arg_none && !conf.no_global_summary_given) {
+               /* must compute summary */
+               gsi.count = conf.limit_arg;
+               gsi.flags = GSF_COMPUTE_SUMMARY;
+               ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
+                       global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
+               if (ret < 0)
+                       return ret;
+       }
+       return 1;
 }
 
 static int print_statistics(void)
 {
        int ret;
-       struct global_stats_info gsi = {
-               .count = conf.limit_arg,
-               .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
-       };
-
-       printf("By size%s:\n",
-               size_unit_buf);
-       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");
-
-       gsi.count = conf.limit_arg;
-       gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
-       printf("By file count%s:\n",
-               count_unit_buf);
-       ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
-               global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
+
+       ret = print_global_lists();
        if (ret < 0)
                return ret;
-       printf("\n");
        print_global_summary();
-       print_user_stats();
+       print_user_lists();
        print_user_summary();
        return 1;
 }