Add format string doku, simplify format string handling.
[adu.git] / select.c
index b394676..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 "error.h"
 #include "portable_io.h"
 
-/** The decimal representation of an uint64_t never exceeds that size. */
-#define FORMATED_VALUE_SIZE 25
+/* global list */
+#define GLOBAL_LIST_ATOMS \
+       ATOM(size, SIZE) \
+       ATOM(files, COUNT) \
+       ATOM(dirname, STRING) \
 
-/* 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,
+#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 const char size_unit_abbrevs[] = " BKMGT";
-static const char count_unit_abbrevs[] = "  kmgt";
+static FILE *output_file;
 
-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)
@@ -109,34 +135,37 @@ static int get_dir_name_by_number(uint64_t *dirnum, char **name)
        char *result = NULL, *tmp;
        struct osl_row *row;
        uint64_t val = *dirnum;
-       struct osl_object obj = {.data = &val, .size = sizeof(val)};
+       struct osl_object obj;
        int ret;
+       char *pfx;
 
 again:
+       obj.data = &val;
+       obj.size = sizeof(val);
        ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
        if (ret < 0)
                goto out;
-       ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
-       if (ret < 0)
-               goto out;
-       if (result) {
-               tmp = make_message("%s/%s", (char *)obj.data, result);
-               free(result);
-               result = tmp;
-       } else
-               result = adu_strdup((char *)obj.data);
        ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
        if (ret < 0)
                goto out;
        val = *(uint64_t *)obj.data;
+       ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
+       if (ret < 0)
+               goto out;
+       pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data :  ".";
+       tmp = make_message("%s/%s", pfx, result? result : "");
+       free(result);
+       result = tmp;
        if (val)
                goto again;
 out:
        if (ret < 0) {
                free(result);
                *name = NULL;
-       } else
+       } else {
+               assert(result);
                *name = result;
+       }
        return ret;
 }
 
@@ -144,170 +173,68 @@ static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
 {
        struct osl_object obj;
        int ret;
-       char *this_dir, *prefix = NULL;
 
        *name = NULL;
-       ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
+       ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
        if (ret < 0)
                return ret;
-       this_dir = adu_strdup((char *)obj.data);
-       ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
+       return get_dir_name_by_number((uint64_t *)obj.data, name);
+}
+
+static int get_dir_name_of_user_row(struct osl_row *user_table_row,
+               struct user_info *ui, char **dirname)
+{
+       struct osl_object obj;
+       int ret = osl(osl_get_object(ui->table, user_table_row,
+               UT_DIR_NUM, &obj));
+
        if (ret < 0)
-               goto out;
-       if (!*(uint64_t *)obj.data) {
-               *name = this_dir;
-               return 1;
-       }
-       ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix);
+               return ret;
+       return get_dir_name_by_number((uint64_t *)obj.data, dirname);
+}
+
+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)
-               goto out;
-       *name = make_message("%s/%s", prefix, this_dir);
-       free(prefix);
-       ret = 1;
-out:
-       free(this_dir);
-       return ret;
+               return ret;
+       *num_files = *(uint64_t *)obj.data;
+       return 1;
 }
-static int user_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 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, row, UT_FILES, &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(conf.count_unit_arg, files,
-                               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(conf.size_unit_arg, bytes,
-                               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;
+       *num_files = *(uint64_t *)obj.data;
+       return 1;
+}
 
-       }
-       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_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;
-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_bytes(struct osl_row *row, struct user_info *ui,
+               uint64_t *num_bytes)
 {
-       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_BYTES, &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(conf.count_unit_arg, files,
-                               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(conf.size_unit_arg, bytes,
-                               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_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)
@@ -335,131 +262,332 @@ 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)
 {
-       char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
-               s[FORMATED_VALUE_SIZE];
-       enum enum_count_unit ud, uf;
-       enum enum_size_unit us;
-
-       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
-       );
+       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 void print_id_stats(void)
+static int print_global_summary(struct format_info *fi)
 {
-       struct user_info *ui;
+       int ret;
+       char *buf;
+       struct global_summary_info gsi = {.num_dirs = 0};
 
-       printf("User summary "
-               "(pw_name/uid/dirs%s/files%s/size%s):\n",
-               count_unit_buf, count_unit_buf, size_unit_buf);
-       FOR_EACH_USER(ui) {
-               char formated_dir_count[FORMATED_VALUE_SIZE],
-                       formated_file_count[FORMATED_VALUE_SIZE],
-                       formated_bytes[FORMATED_VALUE_SIZE ];
-               if (!ui_used(ui) || !ui_admissible(ui))
-                       continue;
-               format_count_value(conf.count_unit_arg, ui->dirs,
-                       conf.count_unit_arg == count_unit_arg_h,
-                       formated_dir_count);
-               format_count_value(conf.count_unit_arg, ui->files,
-                       conf.count_unit_arg == count_unit_arg_h,
-                       formated_file_count);
-               format_size_value(conf.size_unit_arg, ui->bytes,
-                       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
-               );
+       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_stats(void)
+static int user_summary_loop_function(struct osl_row *row, void *data)
 {
-       struct user_info *ui;
+       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;
 
-       FOR_EACH_USER(ui) {
-               struct user_stats_info usi = {
-                       .count = conf.limit_arg,
-                       .ui = ui
-               };
-               if (!ui_used(ui) || !ui_admissible(ui))
-                       continue;
-               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);
+       buf = format_items(fi, values);
+       ret = output("%s", buf);
+       free(buf);
+       return ret;
+}
+
+static int name_comp(const void *a, const void *b)
+{
+       char *x = ((struct user_info *)a)->pw_name;
+       char *y = ((struct user_info *)b)->pw_name;
+
+       if (!x)
+               return 1;
+       if (!y)
+               return -1;
+       return strcmp(x, y);
+}
+
+static int uid_comp(const void *a, const void *b)
+{
+       return -NUM_COMPARE(((struct user_info *)a)->uid,
+               ((struct user_info *)b)->uid);
+}
+
+static int dir_count_comp(const void *a, const void *b)
+{
+       return NUM_COMPARE(((struct user_info *)a)->dirs,
+               ((struct user_info *)b)->dirs);
+}
+
+static int file_count_comp(const void *a, const void *b)
+{
+       return NUM_COMPARE(((struct user_info *)a)->files,
+               ((struct user_info *)b)->files);
+}
+
+static int size_comp(const void *a, const void *b)
+{
+       return NUM_COMPARE(((struct user_info *)a)->bytes,
+               ((struct user_info *)b)->bytes);
+}
+
+static int print_user_summary(struct format_info *fi)
+{
+       /*
+        * 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;
-               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);
+       }
+       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]);
+       return for_each_admissible_user(print_user_summary_line, fi);
+}
+
+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;
+       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;
-               printf("\n");
        }
-       return 1;
+       return adu_loop_reverse(ui->table, sort_column, &uli, user_list_loop_function,
+               &uli.ret, &uli.osl_errno);
 }
 
-static int print_statistics(void)
+static int global_list_loop_function(struct osl_row *row, void *data)
 {
-       int ret;
-       struct global_stats_info gsi = {
-               .count = conf.limit_arg,
-               .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
+       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;
 
-       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);
+       check_signals();
+       ret = -E_LOOP_COMPLETE;
+       if (!gli->count)
+               goto err;
+
+       ret = get_num_files_of_row(row, &num_files);
        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);
+               goto err;
+       values[gla_files].num_value = (long long unsigned)num_files;
+
+       ret = get_num_bytes_of_row(row, &num_bytes);
        if (ret < 0)
-               return ret;
-       printf("\n");
-       print_global_summary();
-       print_user_stats();
-       print_id_stats();
-       return 1;
+               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 read_uid_file(void)
+static int print_global_list(struct format_info *fi)
+{
+       int ret;
+       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;
+       }
+       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(struct format_info *fi)
+{
+       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)
 {
        size_t size;
        uint32_t n;
        char *filename = get_uid_list_name(), *map;
        int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
+       unsigned bits;
 
        if (ret < 0) {
                INFO_LOG("failed to map %s\n", filename);
@@ -469,14 +597,17 @@ static int read_uid_file(void)
        num_uids = size / 4;
        INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
        free(filename);
-       /* hash table size should be a power of two and larger than the number of uids */
-       uid_hash_table_size = 4;
-       while (uid_hash_table_size < num_uids)
-               uid_hash_table_size *= 2;
-       create_hash_table();
+       /*
+        * Compute number of hash table bits. The hash table size must be a
+        * power of two and larger than the number of uids.
+        */
+       bits = 2;
+       while (1 << bits < num_uids)
+               bits++;
+       create_hash_table(bits);
        for (n = 0; n < num_uids; n++) {
                uint32_t uid = read_u32(map + n * sizeof(uid));
-               ret = search_uid(uid, OPEN_USER_TABLE, NULL);
+               ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
                if (ret < 0)
                        goto out;
        }
@@ -485,28 +616,119 @@ out:
        return ret;
 }
 
-int com_select(void)
+int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
 {
        int ret;
 
-       if (conf.count_unit_arg != count_unit_arg_h)
-               count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
-       else
-               count_unit_buf[0] = '\0';
-       if (conf.size_unit_arg != size_unit_arg_h)
-               size_unit_buf[1] = size_unit_abbrevs[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();
+       ret = open_dir_table(0);
        if (ret < 0)
-               return ret;
+               goto out;
        check_signals();
-       ret = read_uid_file();
+       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;
+}
+
+#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;
+       const char **line;
+       char *fmt = NULL;
+       struct atom *atoms;
+
+       if (conf.select_options_given) {
+               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;
+               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 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;
 }