Add format string doku, simplify format string handling.
[adu.git] / select.c
index 7b33383..949fd92 100644 (file)
--- a/select.c
+++ b/select.c
 #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
-
+/* global list */
 #define GLOBAL_LIST_ATOMS \
        ATOM(size, SIZE) \
        ATOM(files, COUNT) \
@@ -42,6 +32,7 @@ struct atom global_list_atoms[] = {
 enum global_list_atoms {GLOBAL_LIST_ATOMS};
 #undef ATOM
 
+/* global summary */
 #define GLOBAL_SUMMARY_ATOMS \
        ATOM(dirs, COUNT) \
        ATOM(files, COUNT) \
@@ -57,6 +48,25 @@ struct atom global_summary_atoms[] = {
 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) \
@@ -74,11 +84,6 @@ struct atom user_summary_atoms[] = {
 enum user_summary_atoms {USER_SUMMARY_ATOMS};
 #undef ATOM
 
-
-
-/* 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;
@@ -86,73 +91,31 @@ struct global_list_info {
        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,
 };
 
-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,
+struct user_summary_info {
+       struct user_info *ui;
+       int ret;
+       int osl_errno;
 };
 
-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)
-{
-       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;
-}
-
-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;
-}
 
 static FILE *output_file;
 
@@ -218,83 +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);
-                       ret = output("\t%s%s", formated_value,
-                               (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
-                                       "\t" : "\n");
-                       if (ret < 0)
-                               goto err;
-               }
-               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);
-                       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;
-                       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;
-               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--;
+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;
+}
+
+static int get_num_user_files(struct osl_row *row, struct user_info *ui,
+               uint64_t *num_files)
+{
+       struct osl_object obj;
+       int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
+
+       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:
-       usi->ret = ret;
-       usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
-       return -1;
 }
 
 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
@@ -322,18 +262,48 @@ 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 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)
 {
        int ret;
        char *buf;
+       struct global_summary_info gsi = {.num_dirs = 0};
+
        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}
+               [gsa_dirs] = {.num_value = 0ULL},
+               [gsa_files] = {.num_value =  0ULL},
+               [gsa_size] = {.num_value =  0ULL}
        };
 
-       if (select_conf.no_global_summary_given)
-               return 1;
+       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)
@@ -345,7 +315,37 @@ static int print_global_summary(struct format_info *fi)
        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)
+{
+       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[] = {
@@ -353,12 +353,14 @@ static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
                        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_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);
+       char *buf;
+       int ret;
 
+       buf = format_items(fi, values);
+       ret = output("%s", buf);
        free(buf);
        return ret;
 }
@@ -399,114 +401,103 @@ 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 int print_user_summary(struct format_info *fi)
 {
-       if (select_conf.no_user_summary_given)
-               return 1;
+       /*
+        * 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]);
        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) {
-                       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;
-}
+       char *dirname, *buf;
 
-static int print_user_lists(void)
-{
-       return for_each_admissible_user(print_user_list, NULL);
-}
+       check_signals();
+       ret = -E_LOOP_COMPLETE;
+       if (!uli->count)
+               goto err;
 
-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));
+       ret = get_num_user_files(row, uli->ui, &num);
        if (ret < 0)
-               return ret;
-       *num_files = *(uint64_t *)obj.data;
-       return 1;
+               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 get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
+static int print_user_list(struct user_info *ui, void *data)
 {
-       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;
+       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 adu_loop_reverse(ui->table, sort_column, &uli, user_list_loop_function,
+               &uli.ret, &uli.osl_errno);
 }
 
 static int global_list_loop_function(struct osl_row *row, void *data)
@@ -568,32 +559,23 @@ static int print_global_list(struct format_info *fi)
                if (ret < 0)
                        return ret;
        }
-       if (select_conf.sort_arg == sort_arg_files)
+       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 select_format_info *sli)
+static int print_statistics(struct format_info *fi)
 {
-       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;
+                       return print_global_list(fi);
                case select_mode_arg_global_summary:
-                       ret = print_global_summary(sli->global_summary_fi);
-                       free_format_info(sli->global_summary_fi);
-                       return ret;
+                       return print_global_summary(fi);
                case select_mode_arg_user_list:
-                       ret = print_user_lists();
-                       return ret;
+                       return for_each_admissible_user(print_user_list, fi);
                case select_mode_arg_user_summary:
-                       ret = print_user_summary(sli->user_summary_fi);
-                       free_format_info(sli->user_summary_fi);
-                       return ret;
+                       return print_user_summary(fi);
        };
        ERROR_LOG("bad select mode\n");
        return ERRNO_TO_ERROR(-EINVAL);
@@ -634,8 +616,7 @@ out:
        return ret;
 }
 
-int run_select_query(struct uid_range *admissible_uids,
-               struct select_format_info *sfi)
+int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
 {
        int ret;
 
@@ -646,15 +627,6 @@ int run_select_query(struct uid_range *admissible_uids,
        } 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 (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)
                goto out;
@@ -663,7 +635,7 @@ int run_select_query(struct uid_range *admissible_uids,
        if (ret < 0)
                goto out;
        check_signals();
-       ret = print_statistics(sfi);
+       ret = print_statistics(fi);
 out:
        close_all_tables();
        if (output_file != stdout)
@@ -671,12 +643,19 @@ out:
        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 select_format_info *sfi)
+               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;
@@ -692,29 +671,32 @@ int parse_select_options(char *string, struct select_cmdline_parser_params *para
                        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;
-       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;
+
+       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;
@@ -731,8 +713,8 @@ help:
 int com_select(void)
 {
        struct uid_range *admissible_uids = NULL;
-       struct select_format_info sfi;
        int ret;
+       struct format_info *fi;
        struct select_cmdline_parser_params params = {
                .override = 1,
                .initialize = 1,
@@ -743,8 +725,10 @@ int com_select(void)
 
        select_cmdline_parser_init(&select_conf);
        ret = parse_select_options(conf.select_options_arg, &params,
-               &admissible_uids, &sfi);
+               &admissible_uids, &fi);
        if (ret <= 0) /* do not run query if help was given */
                return ret;
-       return run_select_query(admissible_uids, &sfi);
+       ret = run_select_query(admissible_uids, fi);
+       free_format_info(fi);
+       return ret;
 }