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;
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;
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;
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--;
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,
(gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
"\t" : "\n");
+ if (ret < 0)
+ goto err;
}
if (summary)
num_files += files;
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",
(gsi->flags & GSF_PRINT_FILES)? "" : "\t",
formated_value,
(gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
);
+ if (ret < 0)
+ goto err;
}
if (summary) {
num_bytes += bytes;
ret = get_dir_name_of_row(row, &dirname);
if (ret < 0)
goto err;
- printf("%s%s\n",
+ ret = output("%s%s\n",
(gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
dirname);
free(dirname);
+ if (ret < 0)
+ goto err;
}
if (gsi->count > 0)
gsi->count--;
return check_loop_return(ret, *loop_ret, *loop_osl_errno);
}
-static void print_global_summary(void)
+static int print_global_summary(void)
{
char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
s[FORMATED_VALUE_SIZE];
enum enum_count_unit ud, uf;
enum enum_size_unit us;
+ int ret;
if (select_conf.no_global_summary_given)
- return;
+ return 1;
ud = format_count_value(select_conf.count_unit_arg, num_dirs, 0, d);
uf = format_count_value(select_conf.count_unit_arg, num_files, 0, f);
us = format_size_value(select_conf.size_unit_arg, num_bytes, 0, s);
- if (!select_conf.no_headers_given)
- printf("Global summary "
+ if (!select_conf.no_headers_given) {
+ ret = output("Global summary "
"(dirs(%c)/files(%c)/size(%c))\n",
count_unit_abbrevs[ud],
count_unit_abbrevs[uf],
size_unit_abbrevs[us]
);
- printf("\t%s\t%s\t%s\n\n", d, f, s);
+ if (ret < 0)
+ return ret;
+ }
+ return output("\t%s\t%s\t%s\n\n", d, f, s);
}
static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
format_size_value(select_conf.size_unit_arg, ui->bytes,
select_conf.size_unit_arg == size_unit_arg_h,
formated_bytes);
- printf("\t%s\t%u\t%s\t%s\t%s\n",
+ 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
);
- return 1;
}
static int name_comp(const void *a, const void *b)
[user_summary_sort_arg_size] = size_comp,
};
-static void print_user_summary(void)
+static int print_user_summary(void)
{
if (select_conf.no_user_summary_given)
- return;
- if (!select_conf.no_headers_given)
- printf("User summary "
+ 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);
+ if (ret < 0)
+ return ret;
+ }
sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
- for_each_admissible_user(print_user_summary_line, NULL);
+ return for_each_admissible_user(print_user_summary_line, NULL);
}
static int print_user_list(struct user_info *ui, __a_unused void *data)
usi.count = select_conf.limit_arg;
usi.ui = ui;
usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
- if (!select_conf.no_headers_given)
- printf("%s (uid %u), by size%s:\n",
+ 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;
- printf("\n");
+ 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)
- printf("%s (uid %u), by file count%s:\n",
+ 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;
&usi.ret, &usi.osl_errno);
if (ret < 0)
return ret;
- printf("\n");
+ 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;
if (print_size_list) {
gsi.count = select_conf.limit_arg;
gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
- if (!select_conf.no_headers_given)
- printf("By size%s:\n", size_unit_buf);
+ if (!select_conf.no_headers_given) {
+ ret = output("By size%s:\n", size_unit_buf);
+ if (ret < 0)
+ return ret;
+ }
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");
+ ret = output("\n");
+ if (ret < 0)
+ return ret;
}
if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
gsi.count = select_conf.limit_arg;
gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
if (!print_size_list)
gsi.flags |= GSF_COMPUTE_SUMMARY;
- if (!select_conf.no_headers_given)
- printf("By file count%s:\n", count_unit_buf);
+ if (!select_conf.no_headers_given) {
+ ret = output("By file count%s:\n", count_unit_buf);
+ if (ret < 0)
+ return ret;
+ }
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");
+ ret = output("\n");
+ if (ret < 0)
+ return ret;
}
if (gla == global_list_arg_none && !select_conf.no_global_summary_given) {
/* must compute summary */
ret = print_global_lists();
if (ret < 0)
return ret;
- print_global_summary();
- print_user_lists();
- print_user_summary();
+ ret = print_global_summary();
+ if (ret < 0)
+ return ret;
+ ret = print_user_lists();
+ if (ret < 0)
+ return ret;
+ ret = print_user_summary();
+ if (ret < 0)
+ return ret;
return 1;
}
{
int ret;
+ 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
ret = open_dir_table(0);
if (ret < 0)
- return ret;
+ goto out;
check_signals();
ret = read_uid_file(admissible_uids);
if (ret < 0)
- return ret;
+ goto out;
check_signals();
ret = print_statistics();
+out:
close_all_tables();
+ if (output_file != stdout)
+ fclose(output_file);
return ret;
}