/** \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"
/** Global bytes count. */
static uint64_t num_bytes;
+
/** The decimal representation of an uint64_t never exceeds that size. */
#define FORMATED_VALUE_SIZE 25
+#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
+
/* these get filled in by the select command. */
static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
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(select_conf.count_unit_arg, ui->dirs,
- select_conf.count_unit_arg == count_unit_arg_h,
- formated_dir_count);
- format_count_value(select_conf.count_unit_arg, ui->files,
- select_conf.count_unit_arg == count_unit_arg_h,
- formated_file_count);
- format_size_value(select_conf.size_unit_arg, ui->bytes,
- select_conf.size_unit_arg == size_unit_arg_h,
- formated_bytes);
- return output("\t%s\t%u\t%s\t%s\t%s\n",
- ui->pw_name? ui->pw_name : "?",
- (unsigned)ui->uid,
- formated_dir_count,
- formated_file_count,
- formated_bytes
- );
+ struct 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);
+ return output("%s", buf);
}
static int name_comp(const void *a, const void *b)
[user_summary_sort_arg_size] = size_comp,
};
-static int print_user_summary(void)
+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 "
- "(pw_name/uid/dirs%s/files%s/size%s):\n",
- count_unit_buf, count_unit_buf, size_unit_buf);
+ 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, NULL);
+ return for_each_admissible_user(print_user_summary_line, fi);
}
static int print_user_list(struct user_info *ui, __a_unused void *data)
return 1;
}
-static int print_statistics(void)
+static int print_statistics(struct select_format_info *sli)
{
int ret;
ret = print_user_lists();
if (ret < 0)
return ret;
- ret = print_user_summary();
+ ret = print_user_summary(sli->user_summary_fi);
if (ret < 0)
return ret;
return 1;
return ret;
}
-int run_select_query(struct uid_range *admissible_uids)
+int run_select_query(struct uid_range *admissible_uids,
+ struct select_format_info *sfi)
{
int ret;
if (ret < 0)
goto out;
check_signals();
- ret = print_statistics();
+ ret = print_statistics(sfi);
out:
close_all_tables();
if (output_file != stdout)
/* 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 uid_range **admissible_uids, struct select_format_info *sfi)
{
int ret;
const char **line;
- if (select_cmdline_parser_string_ext(string, &select_conf, "select",
- params))
- return -E_SYNTAX;
+ 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;
- if (!select_conf.help_given && !select_conf.detailed_help_given)
- return 1;
-
+ ret = parse_format_string(select_conf.user_summary_format_arg,
+ user_summary_atoms, &sfi->user_summary_fi);
+ if (ret < 0)
+ return ret;
+ return 1;
+help:
line = select_conf.detailed_help_given?
select_args_info_detailed_help : select_args_info_help;
if (!output_file)
int com_select(void)
{
struct uid_range *admissible_uids = NULL;
-
- if (conf.select_options_given) {
- int ret;
- struct select_cmdline_parser_params params = {
- .override = 1,
- .initialize = 1,
- .check_required = 1,
- .check_ambiguity = 1,
- .print_errors = 1
- };
- ret = parse_select_options(conf.select_options_arg, ¶ms,
- &admissible_uids);
- if (ret <= 0) /* do not run query if help was given */
- return ret;
- }
- return run_select_query(admissible_uids);
+ 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, ¶ms,
+ &admissible_uids, &sfi);
+ if (ret <= 0) /* do not run query if help was given */
+ return ret;
+ return run_select_query(admissible_uids, &sfi);
}