Add format string doku, simplify format string handling.
[adu.git] / select.c
index 4795de4..949fd92 100644 (file)
--- a/select.c
+++ b/select.c
@@ -7,6 +7,7 @@
 /** \file select.c The select mode of adu. */
 
 #include <dirent.h> /* readdir() */
+#include "format.h"
 #include "adu.h"
 #include "gcc-compat.h"
 #include "cmdline.h"
 #include "string.h"
 #include "error.h"
 #include "portable_io.h"
-#include "select.cmdline.h"
-
-/** Global dir count. */
-static uint64_t num_dirs;
-/** Global files count. */
-static uint64_t num_files;
-/** Global bytes count. */
-static uint64_t num_bytes;
-
-/** The decimal representation of an uint64_t never exceeds that size. */
-#define FORMATED_VALUE_SIZE 25
-
-/* these get filled in by the select command. */
-static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
-
-enum global_stats_flags {
-       GSF_PRINT_DIRNAME = 1,
-       GSF_PRINT_BYTES = 2,
-       GSF_PRINT_FILES = 4,
-       GSF_COMPUTE_SUMMARY = 8,
+
+/* global list */
+#define GLOBAL_LIST_ATOMS \
+       ATOM(size, SIZE) \
+       ATOM(files, COUNT) \
+       ATOM(dirname, STRING) \
+
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom global_list_atoms[] = {
+       GLOBAL_LIST_ATOMS
+       {.name = NULL}
+};
+#undef ATOM
+#define ATOM(x, y) gla_ ## x,
+enum global_list_atoms {GLOBAL_LIST_ATOMS};
+#undef ATOM
+
+/* global summary */
+#define GLOBAL_SUMMARY_ATOMS \
+       ATOM(dirs, COUNT) \
+       ATOM(files, COUNT) \
+       ATOM(size, SIZE)
+
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom global_summary_atoms[] = {
+       GLOBAL_SUMMARY_ATOMS
+       {.name = NULL}
+};
+#undef ATOM
+#define ATOM(x, y) gsa_ ## x,
+enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
+#undef ATOM
+
+/* user list */
+#define USER_LIST_ATOMS \
+       ATOM(pw_name, STRING) \
+       ATOM(uid, ID) \
+       ATOM(size, SIZE) \
+       ATOM(files, COUNT) \
+       ATOM(dirname, STRING) \
+
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom user_list_atoms[] = {
+       USER_LIST_ATOMS
+       {.name = NULL}
 };
+#undef ATOM
+#define ATOM(x, y) ula_ ## x,
+enum user_list_atoms {USER_LIST_ATOMS};
+#undef ATOM
+
+/* user summary */
+#define USER_SUMMARY_ATOMS \
+       ATOM(pw_name, STRING) \
+       ATOM(uid, ID) \
+       ATOM(dirs, COUNT) \
+       ATOM(files, COUNT) \
+       ATOM(size, SIZE)
+
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom user_summary_atoms[] = {
+       USER_SUMMARY_ATOMS
+       {.name = NULL}
+};
+#undef ATOM
+#define ATOM(x, y) usa_ ## x,
+enum user_summary_atoms {USER_SUMMARY_ATOMS};
+#undef ATOM
 
-struct global_stats_info {
+struct global_list_info {
        uint32_t count;
        int ret;
        int osl_errno;
-       enum global_stats_flags flags;
+       struct format_info *fi;
 };
 
-enum user_stats_flags {
-       USF_PRINT_DIRNAME = 1,
-       USF_PRINT_BYTES = 2,
-       USF_PRINT_FILES = 4,
-       USF_COMPUTE_SUMMARY = 8,
+struct global_summary_info {
+       /** Global dir count. */
+       uint64_t num_dirs;
+       /** Global files count. */
+       uint64_t num_files;
+       /** Global bytes count. */
+       uint64_t num_bytes;
+       int ret;
+       int osl_errno;
 };
 
-struct user_stats_info {
+struct user_list_info {
        uint32_t count;
-       enum user_stats_flags flags;
+       struct user_info *ui;
+       struct format_info *fi;
        int ret;
        int osl_errno;
-       struct user_info *ui;
 };
 
-static const uint64_t size_unit_divisors[] = {
-       [size_unit_arg_b] = 1ULL,
-       [size_unit_arg_k] = 1024ULL,
-       [size_unit_arg_m] = 1024ULL * 1024ULL,
-       [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
-       [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
+struct user_summary_info {
+       struct user_info *ui;
+       int ret;
+       int osl_errno;
 };
 
-static const uint64_t count_unit_divisors[] = {
 
-       [count_unit_arg_n] = 1ULL,
-       [count_unit_arg_k] = 1000ULL,
-       [count_unit_arg_m] = 1000ULL * 1000ULL,
-       [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
-       [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
-};
+static FILE *output_file;
 
-static const char size_unit_abbrevs[] = " BKMGT";
-static const char count_unit_abbrevs[] = "  kmgt";
-static enum enum_size_unit format_size_value(enum enum_size_unit unit,
-               uint64_t value, int print_unit, char *result)
+__printf_1_2 static int output(const char const *fmt, ...)
 {
-       enum enum_size_unit u = unit;
-       char unit_buf[2] = "\0\0";
-
-       if (unit == size_unit_arg_h) /* human readable */
-               for (u = size_unit_arg_b; u < size_unit_arg_t &&
-                               value > size_unit_divisors[u + 1]; u++)
-                       ; /* nothing */
-       if (print_unit)
-               unit_buf[0] = size_unit_abbrevs[u];
-       sprintf(result, "%llu%s",
-               (long long unsigned)value / size_unit_divisors[u], unit_buf);
-       return u;
-}
+       va_list argp;
+       int ret;
 
-static enum enum_count_unit format_count_value(enum enum_count_unit unit,
-               uint64_t value, int print_unit, char *result)
-{
-       enum enum_count_unit u = unit;
-       char unit_buf[2] = "\0\0";
-
-       if (unit == count_unit_arg_h) /* human readable */
-               for (u = count_unit_arg_n; u < count_unit_arg_t &&
-                               value > count_unit_divisors[u + 1]; u++)
-                       ; /* nothing */
-       if (print_unit)
-               unit_buf[0] = count_unit_abbrevs[u];
-       sprintf(result, "%llu%s",
-               (long long unsigned)value / count_unit_divisors[u], unit_buf);
-       return u;
+       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)
@@ -162,146 +181,60 @@ static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
        return get_dir_name_by_number((uint64_t *)obj.data, name);
 }
 
-static int user_stats_loop_function(struct osl_row *row, void *data)
+static int get_dir_name_of_user_row(struct osl_row *user_table_row,
+               struct user_info *ui, char **dirname)
 {
-       struct user_stats_info *usi = data;
        struct osl_object obj;
-       int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
-       char formated_value[FORMATED_VALUE_SIZE];
+       int ret = osl(osl_get_object(ui->table, user_table_row,
+               UT_DIR_NUM, &obj));
 
-       check_signals();
-       if (!usi->count && !summary) {
-               ret = -E_LOOP_COMPLETE;
-               goto err;
-       }
-       if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
-               uint64_t files;
-               ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
-               if (ret < 0)
-                       goto err;
-               files = *(uint64_t *)obj.data;
-               if (usi->count && (usi->flags & USF_PRINT_FILES)) {
-                       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,
-                               (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
-                                       "\t" : "\n"
-                       );
-               }
-               if (summary)
-                       usi->ui->files += files;
-       }
-       if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
-               uint64_t bytes;
-               ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
-               if (ret < 0)
-                       goto err;
-               bytes = *(uint64_t *)obj.data;
-               if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
-                       format_size_value(select_conf.size_unit_arg, bytes,
-                               select_conf.size_unit_arg == size_unit_arg_h,
-                               formated_value);
-                       printf("%s%s%s",
-                               (usi->flags & USF_PRINT_FILES)? "" : "\t",
-                               formated_value,
-                               usi->flags & USF_PRINT_DIRNAME?  "\t" : "\n"
-                       );
-               }
-               if (summary) {
-                       usi->ui->bytes += bytes;
-                       usi->ui->dirs++;
-               }
+       if (ret < 0)
+               return ret;
+       return get_dir_name_by_number((uint64_t *)obj.data, dirname);
+}
 
-       }
-       if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
-               char *dirname;
-               ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
-               if (ret < 0)
-                       goto err;
-               ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
-               if (ret < 0)
-                       goto err;
-               printf("%s%s\n",
-                       (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
-                       dirname);
-               free(dirname);
-       }
-       if (usi->count > 0)
-               usi->count--;
+static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
+{
+       struct osl_object obj;
+       int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
+       if (ret < 0)
+               return ret;
+       *num_files = *(uint64_t *)obj.data;
        return 1;
-err:
-       usi->ret = ret;
-       usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
-       return -1;
 }
 
-static int global_stats_loop_function(struct osl_row *row, void *data)
+static int get_num_user_files(struct osl_row *row, struct user_info *ui,
+               uint64_t *num_files)
 {
-       struct global_stats_info *gsi = data;
        struct osl_object obj;
-       char *dirname, formated_value[FORMATED_VALUE_SIZE];
-       int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
+       int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
 
-       check_signals();
-       if (!gsi->count && !summary) {
-               ret = -E_LOOP_COMPLETE;
-               goto err;
-       }
-       if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
-               uint64_t files;
-               ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
-               if (ret < 0)
-                       goto err;
-               files = *(uint64_t *)obj.data;
-               if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
-                       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,
-                               (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
-                               "\t" : "\n");
-               }
-               if (summary)
-                       num_files += files;
-       }
-       if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
-               uint64_t bytes;
-               ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
-               if (ret < 0)
-                       goto err;
-               bytes = *(uint64_t *)obj.data;
-               if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
-                       format_size_value(select_conf.size_unit_arg, bytes,
-                               select_conf.size_unit_arg == size_unit_arg_h,
-                               formated_value);
-                       printf("%s%s%s",
-                               (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
-                               formated_value,
-                               (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
-                       );
-               }
-               if (summary) {
-                       num_bytes += bytes;
-                       num_dirs++;
-               }
-       }
-       if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
-               ret = get_dir_name_of_row(row, &dirname);
-               if (ret < 0)
-                       goto err;
-               printf("%s%s\n",
-                       (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
-                       dirname);
-               free(dirname);
-       }
-       if (gsi->count > 0)
-               gsi->count--;
+       if (ret < 0)
+               return ret;
+       *num_files = *(uint64_t *)obj.data;
+       return 1;
+}
+
+static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
+{
+       struct osl_object obj;
+       int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
+       if (ret < 0)
+               return ret;
+       *num_bytes = *(uint64_t *)obj.data;
+       return 1;
+}
+
+static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
+               uint64_t *num_bytes)
+{
+       struct osl_object obj;
+       int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
+
+       if (ret < 0)
+               return ret;
+       *num_bytes = *(uint64_t *)obj.data;
        return 1;
-err:
-       gsi->ret = ret;
-       gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
-       return -1;
 }
 
 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
@@ -329,52 +262,107 @@ 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 global_summary_loop_function(struct osl_row *row, void *data)
+{
+       struct global_summary_info *gsi = data;
+       int ret;
+       uint64_t num;
+
+       ret = get_num_files_of_row(row, &num);
+       if (ret < 0)
+               goto err;
+       gsi->num_files += num;
+
+       ret = get_num_bytes_of_row(row, &num);
+       if (ret < 0)
+               goto err;
+       gsi->num_bytes += num;
+       gsi->num_dirs++;
+       return 1;
+err:
+       gsi->ret = ret;
+       gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
+       return -1;
+}
+
+static int print_global_summary(struct format_info *fi)
 {
-       char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
-               s[FORMATED_VALUE_SIZE];
-       enum enum_count_unit ud, uf;
-       enum enum_size_unit us;
-
-       if (select_conf.no_global_summary_given)
-               return;
-       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 "
-                       "(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);
+       int ret;
+       char *buf;
+       struct global_summary_info gsi = {.num_dirs = 0};
+
+       union atom_value values[] = {
+               [gsa_dirs] = {.num_value = 0ULL},
+               [gsa_files] = {.num_value =  0ULL},
+               [gsa_size] = {.num_value =  0ULL}
+       };
+
+       ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
+               global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
+       if (ret < 0)
+               return ret;
+       values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
+       values[gsa_files].num_value = (long long unsigned)gsi.num_files;
+       values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
+       if (!select_conf.no_headers_given) {
+               ret = output("Global summary\n");
+               if (ret < 0)
+                       return ret;
+       }
+       buf = format_items(fi, values);
+       ret = output("%s", buf);
+       free(buf);
+       return ret;
 }
 
-static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
+static int user_summary_loop_function(struct osl_row *row, void *data)
 {
-       char formated_dir_count[FORMATED_VALUE_SIZE],
-               formated_file_count[FORMATED_VALUE_SIZE],
-               formated_bytes[FORMATED_VALUE_SIZE ];
-
-       format_count_value(select_conf.count_unit_arg, ui->dirs,
-               select_conf.count_unit_arg == count_unit_arg_h,
-               formated_dir_count);
-       format_count_value(select_conf.count_unit_arg, ui->files,
-               select_conf.count_unit_arg == count_unit_arg_h,
-               formated_file_count);
-       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",
-               ui->pw_name? ui->pw_name : "?",
-               (unsigned)ui->uid,
-               formated_dir_count,
-               formated_file_count,
-               formated_bytes
-       );
+       struct user_summary_info *usi = data;
+       uint64_t num;
+       int ret;
+
+       ret = get_num_user_files(row, usi->ui, &num);
+       if (ret < 0)
+               goto err;
+       usi->ui->files += num;
+       ret = get_num_user_bytes(row, usi->ui, &num);
+       if (ret < 0)
+               goto err;
+       usi->ui->bytes += num;
+       usi->ui->dirs++;
        return 1;
+err:
+       usi->ret = ret;
+       usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
+       return -1;
+}
+
+static int compute_user_summary(struct user_info *ui, __a_unused void *data)
+{
+       struct user_summary_info usi = {.ui = ui};
+
+       return adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
+               &usi.ret, &usi.osl_errno);
+}
+
+static int print_user_summary_line(struct user_info *ui, void *data)
+{
+       struct format_info *fi = data;
+       union atom_value values[] = {
+               [usa_pw_name] = {.string_value = ui->pw_name?
+                       ui->pw_name : "?"},
+               [usa_uid] = {.num_value = (long long unsigned)ui->uid},
+               [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
+               [usa_files] = {.num_value = (long long unsigned)ui->files},
+               [usa_size] = {.num_value =  (long long unsigned)ui->bytes}
+       };
+       char *buf;
+       int ret;
+
+       buf = format_items(fi, values);
+       ret = output("%s", buf);
+       free(buf);
+       return ret;
 }
 
 static int name_comp(const void *a, const void *b)
@@ -413,140 +401,184 @@ static int size_comp(const void *a, const void *b)
                ((struct user_info *)b)->bytes);
 }
 
-/*
- * The comparators for sorting the user summary.
- *
- * This is an array of pointers to functions taking two constant void *
- * pointers and returning an int.
- */
-static int (*summary_comparators[])(const void *, const void *) = {
-       [user_summary_sort_arg_name] = name_comp,
-       [user_summary_sort_arg_uid] = uid_comp,
-       [user_summary_sort_arg_dir_count] = dir_count_comp,
-       [user_summary_sort_arg_file_count] = file_count_comp,
-       [user_summary_sort_arg_size] = size_comp,
-};
-
-static void print_user_summary(void)
+static int print_user_summary(struct format_info *fi)
 {
-       if (select_conf.no_user_summary_given)
-               return;
-       if (!select_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);
+       /*
+        * The comparators for sorting the user summary.
+        *
+        * This is an array of pointers to functions taking two constant void *
+        * pointers and returning an int.
+        */
+       static int (*summary_comparators[])(const void *, const void *) = {
+               [user_summary_sort_arg_name] = name_comp,
+               [user_summary_sort_arg_uid] = uid_comp,
+               [user_summary_sort_arg_dir_count] = dir_count_comp,
+               [user_summary_sort_arg_file_count] = file_count_comp,
+               [user_summary_sort_arg_size] = size_comp,
+       };
+
+       if (!select_conf.no_headers_given) {
+               int ret = output("User summary\n");
+               if (ret < 0)
+                       return ret;
+       }
+       int ret = for_each_admissible_user(compute_user_summary, fi);
+       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, fi);
 }
 
-static int print_user_list(struct user_info *ui, __a_unused void *data)
+static int user_list_loop_function(struct osl_row *row, void *data)
 {
+       struct user_list_info *uli = data;
+       union atom_value values[] = {
+               [ula_pw_name] = {.string_value = uli->ui->pw_name?
+                       uli->ui->pw_name : "?"},
+               [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
+               [ula_files] = {.num_value = 0ULL},
+               [ula_size] = {.num_value =  0ULL},
+               [ula_dirname] = {.string_value = NULL}
+       };
+       uint64_t num;
        int ret;
-       struct user_stats_info usi;
-       enum enum_user_list ula = select_conf.user_list_arg;
-       int print_size_list = (ula == user_list_arg_size
-               || ula == user_list_arg_both);
-
-       if (print_size_list) {
-               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",
-                               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 (!select_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 = select_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 && !select_conf.no_user_summary_given) {
-               usi.count = select_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);
+       char *dirname, *buf;
+
+       check_signals();
+       ret = -E_LOOP_COMPLETE;
+       if (!uli->count)
+               goto err;
+
+       ret = get_num_user_files(row, uli->ui, &num);
+       if (ret < 0)
+               goto err;
+       values[ula_files].num_value = num;
+
+       ret = get_num_user_bytes(row, uli->ui, &num);
+       if (ret < 0)
+               goto err;
+       values[ula_size].num_value = num;
+
+       ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
+       if (ret < 0)
+               goto err;
+       values[ula_dirname].string_value = dirname;
+
+       buf = format_items(uli->fi, values);
+       free(dirname);
+       ret = output("%s", buf);
+       free(buf);
+       uli->count--;
+       return ret;
+err:
+       uli->ret = ret;
+       uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
+       return -1;
+}
+
+static int print_user_list(struct user_info *ui, void *data)
+{
+       struct format_info *fi = data;
+       int ret;
+       enum user_table_columns sort_column = UT_BYTES;
+       struct user_list_info uli = {
+               .ui = ui,
+               .fi = fi,
+               .count = select_conf.limit_arg
+       };
+
+       if (select_conf.list_sort_arg == list_sort_arg_file_count)
+               sort_column = UT_FILES;
+
+       if (!select_conf.no_headers_given) {
+               ret = output("%s (uid %u)\n",
+                       ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid);
                if (ret < 0)
                        return ret;
        }
-       return 1;
+       return adu_loop_reverse(ui->table, sort_column, &uli, user_list_loop_function,
+               &uli.ret, &uli.osl_errno);
 }
 
-static int print_user_lists(void)
+static int global_list_loop_function(struct osl_row *row, void *data)
 {
-       return for_each_admissible_user(print_user_list, NULL);
+       struct global_list_info *gli = data;
+       union atom_value values[] = {
+               [gla_size] = {.num_value = 0ULL},
+               [gla_files] = {.num_value =  0ULL},
+               [gla_dirname] = {.string_value = NULL}
+       };
+       uint64_t num_files, num_bytes;
+       char *dirname, *buf;
+       int ret;
+
+       check_signals();
+       ret = -E_LOOP_COMPLETE;
+       if (!gli->count)
+               goto err;
+
+       ret = get_num_files_of_row(row, &num_files);
+       if (ret < 0)
+               goto err;
+       values[gla_files].num_value = (long long unsigned)num_files;
+
+       ret = get_num_bytes_of_row(row, &num_bytes);
+       if (ret < 0)
+               goto err;
+       values[gla_size].num_value = (long long unsigned)num_bytes;
+
+       ret = get_dir_name_of_row(row, &dirname);
+       if (ret < 0)
+               goto err;
+       values[gla_dirname].string_value = dirname;
+
+       buf = format_items(gli->fi, values);
+       free(dirname);
+       ret = output("%s", buf);
+       free(buf);
+       if (gli->count > 0)
+               gli->count--;
+       return ret;
+err:
+       gli->ret = ret;
+       gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
+       return -1;
 }
 
-static int print_global_lists(void)
+static int print_global_list(struct format_info *fi)
 {
-       struct global_stats_info gsi;
        int ret;
-       enum enum_global_list gla = select_conf.global_list_arg;
-       int print_size_list = (gla == global_list_arg_size
-               || gla == global_list_arg_both);
-
-       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);
-               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 = 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);
-               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 && !select_conf.no_global_summary_given) {
-               /* must compute summary */
-               gsi.count = select_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);
+       enum dir_table_columns sort_column = DT_BYTES;
+       struct global_list_info gli = {
+               .fi = fi,
+               .count = select_conf.limit_arg
+       };
+
+       if (!select_conf.no_headers_given) {
+               ret = output("Global list\n");
                if (ret < 0)
                        return ret;
        }
-       return 1;
+       if (select_conf.list_sort_arg == list_sort_arg_file_count)
+               sort_column = DT_FILES;
+       return adu_loop_reverse(dir_table, sort_column, &gli,
+               global_list_loop_function, &gli.ret, &gli.osl_errno);
 }
 
-static int print_statistics(void)
+static int print_statistics(struct format_info *fi)
 {
-       int ret;
-
-       ret = print_global_lists();
-       if (ret < 0)
-               return ret;
-       print_global_summary();
-       print_user_lists();
-       print_user_summary();
-       return 1;
+       switch (select_conf.select_mode_arg) {
+               case select_mode_arg_global_list:
+                       return print_global_list(fi);
+               case select_mode_arg_global_summary:
+                       return print_global_summary(fi);
+               case select_mode_arg_user_list:
+                       return for_each_admissible_user(print_user_list, fi);
+               case select_mode_arg_user_summary:
+                       return print_user_summary(fi);
+       };
+       ERROR_LOG("bad select mode\n");
+       return ERRNO_TO_ERROR(-EINVAL);
 }
 
 static int read_uid_file(struct uid_range *admissible_uids)
@@ -584,52 +616,119 @@ out:
        return ret;
 }
 
-int run_select_query(struct uid_range *admissible_uids)
+int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
 {
        int ret;
 
-       if (select_conf.count_unit_arg != count_unit_arg_h)
-               count_unit_buf[1] = count_unit_abbrevs[select_conf.count_unit_arg];
-       else
-               count_unit_buf[0] = '\0';
-       if (select_conf.size_unit_arg != size_unit_arg_h)
-               size_unit_buf[1] = size_unit_abbrevs[select_conf.size_unit_arg];
-       else
-               size_unit_buf[0] = '\0';
+       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;
 
        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();
+       ret = print_statistics(fi);
+out:
        close_all_tables();
+       if (output_file != stdout)
+               fclose(output_file);
        return ret;
 }
 
-int com_select(void)
+#define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
+#define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
+#define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
+#define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
+
+/* return: < 0: error, >0: OK, == 0: help given */
+int parse_select_options(char *string, struct select_cmdline_parser_params *params,
+               struct uid_range **admissible_uids, struct format_info **fi)
 {
        int ret;
-       struct uid_range *admissible_uids = NULL;
+       const char **line;
+       char *fmt = NULL;
+       struct atom *atoms;
 
        if (conf.select_options_given) {
-               struct select_cmdline_parser_params params = {
-                       .override = 1,
-                       .initialize = 1,
-                       .check_required = 1,
-                       .check_ambiguity = 1,
-                       .print_errors = 1
-               };
-
-               if (select_cmdline_parser_string_ext(conf.select_options_arg,
-                       &select_conf, "select", &params))
+               int argc;
+               char **argv;
+
+               ret = create_argv(string, &argv);
+               if (ret < 0)
+                       return ret;
+               argc = ret;
+               ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
+               free_argv(argv);
+               if (ret)
                        return -E_SYNTAX;
-               ret = parse_uid_arg(select_conf.uid_arg, &admissible_uids);
+               if (select_conf.help_given || select_conf.detailed_help_given)
+                       goto help;
+               fmt = select_conf.format_arg;
+
+       }
+       ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
+       if (ret < 0)
+               return ret;
+
+       switch (select_conf.select_mode_arg) {
+               case select_mode_arg_global_list:
+                       if (!fmt)
+                               fmt = GLOBAL_LIST_DFLT_FMT;
+                       atoms = global_list_atoms;
+               case select_mode_arg_global_summary:
+                       if (!fmt)
+                               fmt = GLOBAL_SUMMARY_DFLT_FMT;
+                       atoms = global_summary_atoms;
+               case select_mode_arg_user_list:
+                       if (!fmt)
+                               fmt = USER_LIST_DFLT_FMT;
+                       atoms = user_list_atoms;
+               case select_mode_arg_user_summary:
+                       if (!fmt)
+                               fmt = USER_SUMMARY_DFLT_FMT;
+                       atoms = user_summary_atoms;
+       };
+       return parse_format_string(fmt, atoms, fi);
+help:
+       line = select_conf.detailed_help_given?
+               select_args_info_detailed_help : select_args_info_help;
+       if (!output_file)
+               output_file = stdout;
+       for (; *line; line++) {
+               ret = output("%s\n", *line);
                if (ret < 0)
                        return ret;
        }
-       return run_select_query(admissible_uids);
+       return 0;
+}
+
+int com_select(void)
+{
+       struct uid_range *admissible_uids = NULL;
+       int ret;
+       struct format_info *fi;
+       struct select_cmdline_parser_params params = {
+               .override = 1,
+               .initialize = 1,
+               .check_required = 1,
+               .check_ambiguity = 1,
+               .print_errors = 1
+       };
+
+       select_cmdline_parser_init(&select_conf);
+       ret = parse_select_options(conf.select_options_arg, &params,
+               &admissible_uids, &fi);
+       if (ret <= 0) /* do not run query if help was given */
+               return ret;
+       ret = run_select_query(admissible_uids, fi);
+       free_format_info(fi);
+       return ret;
 }