Introduce select-mode and use pretty formating for the global lists.
[adu.git] / select.c
index efe2c3c55a1ea18fb2629243ddf5b5c61aed6dca..7b3338376cd977cf316d639377279be15fa11faa 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"
 
+/** 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] = "( )";
+#define GLOBAL_LIST_ATOMS \
+       ATOM(size, SIZE) \
+       ATOM(files, COUNT) \
+       ATOM(dirname, STRING) \
 
-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
+
+#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
+
+#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 {
+
+/* these get filled in by the select command. */
+static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
+
+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 {
@@ -69,7 +120,6 @@ static const uint64_t count_unit_divisors[] = {
 
 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)
 {
@@ -104,39 +154,55 @@ 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;
        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,30 +210,14 @@ 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));
-       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);
-       if (ret < 0)
-               goto out;
-       *name = make_message("%s/%s", prefix, this_dir);
-       free(prefix);
-       ret = 1;
-out:
-       free(this_dir);
-       return ret;
+       return get_dir_name_by_number((uint64_t *)obj.data, name);
 }
+
 static int user_stats_loop_function(struct osl_row *row, void *data)
 {
        struct user_stats_info *usi = data;
@@ -187,13 +237,14 @@ static int user_stats_loop_function(struct osl_row *row, void *data)
                        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,
+                       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;
@@ -205,14 +256,16 @@ static int user_stats_loop_function(struct osl_row *row, void *data)
                        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,
+                       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;
@@ -228,10 +281,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--;
@@ -242,74 +297,6 @@ err:
        return -1;
 }
 
-static int global_stats_loop_function(struct osl_row *row, void *data)
-{
-       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;
-
-       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--;
-       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)
 {
        if (ret >= 0)
@@ -335,127 +322,284 @@ 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(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;
-
-       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
-       );
+       int ret;
+       char *buf;
+       union atom_value values[] = {
+               [gsa_dirs] = {.num_value = (long long unsigned)num_dirs},
+               [gsa_files] = {.num_value =  (long long unsigned)num_files},
+               [gsa_size] = {.num_value =  (long long unsigned)num_bytes}
+       };
 
+       if (select_conf.no_global_summary_given)
+               return 1;
+       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)
 {
-       char formated_dir_count[FORMATED_VALUE_SIZE],
-               formated_file_count[FORMATED_VALUE_SIZE],
-               formated_bytes[FORMATED_VALUE_SIZE ];
-
-       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
-       );
-       return 1;
+       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 = format_items(fi, values);
+       int 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 void print_user_summary(void)
+static int size_comp(const void *a, const void *b)
 {
-       printf("User summary "
-               "(pw_name/uid/dirs%s/files%s/size%s):\n",
-               count_unit_buf, count_unit_buf, size_unit_buf);
-       for_each_admissible_user(print_user_summary_line, NULL);
+       return NUM_COMPARE(((struct user_info *)a)->bytes,
+               ((struct user_info *)b)->bytes);
 }
 
-static int print_user_stat(struct user_info *ui, __a_unused void *data)
+/*
+ * 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 int print_user_summary(struct format_info *fi)
+{
+       if (select_conf.no_user_summary_given)
+               return 1;
+       if (!select_conf.no_headers_given) {
+               int ret = output("User summary\n");
+               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 print_user_list(struct user_info *ui, __a_unused void *data)
 {
        int ret;
-       struct user_stats_info usi = {
-               .count = conf.limit_arg,
-               .ui = ui
-       };
+       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) {
+                       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;
+               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) {
+                       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;
+               ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
+                       &usi.ret, &usi.osl_errno);
+               if (ret < 0)
+                       return ret;
+               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;
+               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;
+}
 
-       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);
+static int print_user_lists(void)
+{
+       return for_each_admissible_user(print_user_list, NULL);
+}
+
+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;
-       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);
+       *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;
-       printf("\n");
+       *num_bytes = *(uint64_t *)obj.data;
        return 1;
 }
 
-static int print_user_stats(void)
+static int global_list_loop_function(struct osl_row *row, void *data)
 {
-       return for_each_admissible_user(print_user_stat, 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_statistics(void)
+static int print_global_list(struct format_info *fi)
 {
        int ret;
-       struct global_stats_info gsi = {
-               .count = conf.limit_arg,
-               .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
+       enum dir_table_columns sort_column = DT_BYTES;
+       struct global_list_info gli = {
+               .fi = fi,
+               .count = select_conf.limit_arg
        };
 
-       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);
-       if (ret < 0)
-               return ret;
-       printf("\n");
-       print_global_summary();
-       print_user_stats();
-       print_user_summary();
-       return 1;
+       if (!select_conf.no_headers_given) {
+               ret = output("Global list\n");
+               if (ret < 0)
+                       return ret;
+       }
+       if (select_conf.sort_arg == sort_arg_files)
+               sort_column = DT_FILES;
+       return adu_loop_reverse(dir_table, sort_column, &gli,
+               global_list_loop_function, &gli.ret, &gli.osl_errno);
 }
 
-static int read_uid_file(void)
+static int print_statistics(struct select_format_info *sli)
+{
+       int ret;
+
+       switch (select_conf.select_mode_arg) {
+               case select_mode_arg_global_list:
+                       ret = print_global_list(sli->global_list_fi);
+                       free_format_info(sli->global_list_fi);
+                       return ret;
+               case select_mode_arg_global_summary:
+                       ret = print_global_summary(sli->global_summary_fi);
+                       free_format_info(sli->global_summary_fi);
+                       return ret;
+               case select_mode_arg_user_list:
+                       ret = print_user_lists();
+                       return ret;
+               case select_mode_arg_user_summary:
+                       ret = print_user_summary(sli->user_summary_fi);
+                       free_format_info(sli->user_summary_fi);
+                       return ret;
+       };
+       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;
@@ -481,7 +625,7 @@ static int read_uid_file(void)
        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;
        }
@@ -490,28 +634,117 @@ out:
        return ret;
 }
 
-int com_select(void)
+int run_select_query(struct uid_range *admissible_uids,
+               struct select_format_info *sfi)
 {
        int ret;
 
-       if (conf.count_unit_arg != count_unit_arg_h)
-               count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
+       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
                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];
+       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';
 
        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(sfi);
+out:
        close_all_tables();
+       if (output_file != stdout)
+               fclose(output_file);
+       return ret;
+}
+
+/* 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 select_format_info *sfi)
+{
+       int ret;
+       const char **line;
+
+       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;
+
+       }
+       ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
+       if (ret < 0)
+               return ret;
+       ret = parse_format_string(select_conf.user_summary_format_arg,
+               user_summary_atoms, &sfi->user_summary_fi);
+       if (ret < 0)
+               return ret;
+       ret = parse_format_string(select_conf.global_summary_format_arg,
+               global_summary_atoms, &sfi->global_summary_fi);
+       if (ret < 0)
+               goto global_summary_err;
+       ret = parse_format_string(select_conf.global_list_format_arg,
+               global_list_atoms, &sfi->global_list_fi);
+       if (ret < 0)
+               goto global_list_err;
+       return 1;
+global_list_err:
+       free_format_info(sfi->global_summary_fi);
+global_summary_err:
+       free_format_info(sfi->user_summary_fi);
        return ret;
+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;
+       struct select_format_info sfi;
+       int ret;
+       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, &sfi);
+       if (ret <= 0) /* do not run query if help was given */
+               return ret;
+       return run_select_query(admissible_uids, &sfi);
 }