]> git.tuebingen.mpg.de Git - adu.git/blob - select.c
adu.c: Add a comment describing the user table.
[adu.git] / select.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file select.c The select mode of adu. */
8
9 #include <dirent.h> /* readdir() */
10 #include "format.h"
11 #include "adu.h"
12 #include "gcc-compat.h"
13 #include "cmdline.h"
14 #include "fd.h"
15 #include "string.h"
16 #include "error.h"
17 #include "portable_io.h"
18
19 /** Global dir count. */
20 static uint64_t num_dirs;
21 /** Global files count. */
22 static uint64_t num_files;
23 /** Global bytes count. */
24 static uint64_t num_bytes;
25
26
27 /** The decimal representation of an uint64_t never exceeds that size. */
28 #define FORMATED_VALUE_SIZE 25
29
30 #define GLOBAL_SUMMARY_ATOMS \
31         ATOM(dirs, COUNT) \
32         ATOM(files, COUNT) \
33         ATOM(size, SIZE)
34
35 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
36 struct atom global_summary_atoms[] = {
37         GLOBAL_SUMMARY_ATOMS
38         {.name = NULL}
39 };
40 #undef ATOM
41 #define ATOM(x, y) gsa_ ## x,
42 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
43 #undef ATOM
44
45 #define USER_SUMMARY_ATOMS \
46         ATOM(pw_name, STRING) \
47         ATOM(uid, ID) \
48         ATOM(dirs, COUNT) \
49         ATOM(files, COUNT) \
50         ATOM(size, SIZE)
51
52 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
53 struct atom user_summary_atoms[] = {
54         USER_SUMMARY_ATOMS
55         {.name = NULL}
56 };
57 #undef ATOM
58 #define ATOM(x, y) usa_ ## x,
59 enum user_summary_atoms {USER_SUMMARY_ATOMS};
60 #undef ATOM
61
62
63 /* these get filled in by the select command. */
64 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
65
66 enum global_stats_flags {
67         GSF_PRINT_DIRNAME = 1,
68         GSF_PRINT_BYTES = 2,
69         GSF_PRINT_FILES = 4,
70         GSF_COMPUTE_SUMMARY = 8,
71 };
72
73 struct global_stats_info {
74         uint32_t count;
75         int ret;
76         int osl_errno;
77         enum global_stats_flags flags;
78 };
79
80 enum user_stats_flags {
81         USF_PRINT_DIRNAME = 1,
82         USF_PRINT_BYTES = 2,
83         USF_PRINT_FILES = 4,
84         USF_COMPUTE_SUMMARY = 8,
85 };
86
87 struct user_stats_info {
88         uint32_t count;
89         enum user_stats_flags flags;
90         int ret;
91         int osl_errno;
92         struct user_info *ui;
93 };
94
95 static const uint64_t size_unit_divisors[] = {
96         [size_unit_arg_b] = 1ULL,
97         [size_unit_arg_k] = 1024ULL,
98         [size_unit_arg_m] = 1024ULL * 1024ULL,
99         [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
100         [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
101 };
102
103 static const uint64_t count_unit_divisors[] = {
104
105         [count_unit_arg_n] = 1ULL,
106         [count_unit_arg_k] = 1000ULL,
107         [count_unit_arg_m] = 1000ULL * 1000ULL,
108         [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
109         [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
110 };
111
112 static const char size_unit_abbrevs[] = " BKMGT";
113 static const char count_unit_abbrevs[] = "  kmgt";
114 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
115                 uint64_t value, int print_unit, char *result)
116 {
117         enum enum_size_unit u = unit;
118         char unit_buf[2] = "\0\0";
119
120         if (unit == size_unit_arg_h) /* human readable */
121                 for (u = size_unit_arg_b; u < size_unit_arg_t &&
122                                 value > size_unit_divisors[u + 1]; u++)
123                         ; /* nothing */
124         if (print_unit)
125                 unit_buf[0] = size_unit_abbrevs[u];
126         sprintf(result, "%llu%s",
127                 (long long unsigned)value / size_unit_divisors[u], unit_buf);
128         return u;
129 }
130
131 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
132                 uint64_t value, int print_unit, char *result)
133 {
134         enum enum_count_unit u = unit;
135         char unit_buf[2] = "\0\0";
136
137         if (unit == count_unit_arg_h) /* human readable */
138                 for (u = count_unit_arg_n; u < count_unit_arg_t &&
139                                 value > count_unit_divisors[u + 1]; u++)
140                         ; /* nothing */
141         if (print_unit)
142                 unit_buf[0] = count_unit_abbrevs[u];
143         sprintf(result, "%llu%s",
144                 (long long unsigned)value / count_unit_divisors[u], unit_buf);
145         return u;
146 }
147
148 static FILE *output_file;
149
150 __printf_1_2 static int output(const char const *fmt, ...)
151 {
152         va_list argp;
153         int ret;
154
155         va_start(argp, fmt);
156         ret = vfprintf(output_file, fmt, argp);
157         va_end(argp);
158         return ret < 0? -E_OUTPUT : 1;
159 }
160
161 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
162 {
163         char *result = NULL, *tmp;
164         struct osl_row *row;
165         uint64_t val = *dirnum;
166         struct osl_object obj;
167         int ret;
168         char *pfx;
169
170 again:
171         obj.data = &val;
172         obj.size = sizeof(val);
173         ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
174         if (ret < 0)
175                 goto out;
176         ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
177         if (ret < 0)
178                 goto out;
179         val = *(uint64_t *)obj.data;
180         ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
181         if (ret < 0)
182                 goto out;
183         pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data :  ".";
184         tmp = make_message("%s/%s", pfx, result? result : "");
185         free(result);
186         result = tmp;
187         if (val)
188                 goto again;
189 out:
190         if (ret < 0) {
191                 free(result);
192                 *name = NULL;
193         } else {
194                 assert(result);
195                 *name = result;
196         }
197         return ret;
198 }
199
200 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
201 {
202         struct osl_object obj;
203         int ret;
204
205         *name = NULL;
206         ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
207         if (ret < 0)
208                 return ret;
209         return get_dir_name_by_number((uint64_t *)obj.data, name);
210 }
211
212 static int user_stats_loop_function(struct osl_row *row, void *data)
213 {
214         struct user_stats_info *usi = data;
215         struct osl_object obj;
216         int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
217         char formated_value[FORMATED_VALUE_SIZE];
218
219         check_signals();
220         if (!usi->count && !summary) {
221                 ret = -E_LOOP_COMPLETE;
222                 goto err;
223         }
224         if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
225                 uint64_t files;
226                 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
227                 if (ret < 0)
228                         goto err;
229                 files = *(uint64_t *)obj.data;
230                 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
231                         format_count_value(select_conf.count_unit_arg, files,
232                                 select_conf.count_unit_arg == count_unit_arg_h,
233                                 formated_value);
234                         ret = output("\t%s%s", formated_value,
235                                 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
236                                         "\t" : "\n");
237                         if (ret < 0)
238                                 goto err;
239                 }
240                 if (summary)
241                         usi->ui->files += files;
242         }
243         if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
244                 uint64_t bytes;
245                 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
246                 if (ret < 0)
247                         goto err;
248                 bytes = *(uint64_t *)obj.data;
249                 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
250                         format_size_value(select_conf.size_unit_arg, bytes,
251                                 select_conf.size_unit_arg == size_unit_arg_h,
252                                 formated_value);
253                         ret = output("%s%s%s",
254                                 (usi->flags & USF_PRINT_FILES)? "" : "\t",
255                                 formated_value,
256                                 usi->flags & USF_PRINT_DIRNAME?  "\t" : "\n"
257                         );
258                         if (ret < 0)
259                                 goto err;
260                 }
261                 if (summary) {
262                         usi->ui->bytes += bytes;
263                         usi->ui->dirs++;
264                 }
265
266         }
267         if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
268                 char *dirname;
269                 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
270                 if (ret < 0)
271                         goto err;
272                 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
273                 if (ret < 0)
274                         goto err;
275                 ret = output("%s%s\n",
276                         (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
277                         dirname);
278                 free(dirname);
279                 if (ret < 0)
280                         goto err;
281         }
282         if (usi->count > 0)
283                 usi->count--;
284         return 1;
285 err:
286         usi->ret = ret;
287         usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
288         return -1;
289 }
290
291 static int global_stats_loop_function(struct osl_row *row, void *data)
292 {
293         struct global_stats_info *gsi = data;
294         struct osl_object obj;
295         char *dirname, formated_value[FORMATED_VALUE_SIZE];
296         int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
297
298         check_signals();
299         if (!gsi->count && !summary) {
300                 ret = -E_LOOP_COMPLETE;
301                 goto err;
302         }
303         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
304                 uint64_t files;
305                 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
306                 if (ret < 0)
307                         goto err;
308                 files = *(uint64_t *)obj.data;
309                 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
310                         format_count_value(select_conf.count_unit_arg, files,
311                                 select_conf.count_unit_arg == count_unit_arg_h,
312                                 formated_value);
313                         ret = output("\t%s%s", formated_value,
314                                 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
315                                 "\t" : "\n");
316                         if (ret < 0)
317                                 goto err;
318                 }
319                 if (summary)
320                         num_files += files;
321         }
322         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
323                 uint64_t bytes;
324                 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
325                 if (ret < 0)
326                         goto err;
327                 bytes = *(uint64_t *)obj.data;
328                 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
329                         format_size_value(select_conf.size_unit_arg, bytes,
330                                 select_conf.size_unit_arg == size_unit_arg_h,
331                                 formated_value);
332                         ret = output("%s%s%s",
333                                 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
334                                 formated_value,
335                                 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
336                         );
337                         if (ret < 0)
338                                 goto err;
339                 }
340                 if (summary) {
341                         num_bytes += bytes;
342                         num_dirs++;
343                 }
344         }
345         if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
346                 ret = get_dir_name_of_row(row, &dirname);
347                 if (ret < 0)
348                         goto err;
349                 ret = output("%s%s\n",
350                         (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
351                         dirname);
352                 free(dirname);
353                 if (ret < 0)
354                         goto err;
355         }
356         if (gsi->count > 0)
357                 gsi->count--;
358         return 1;
359 err:
360         gsi->ret = ret;
361         gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
362         return -1;
363 }
364
365 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
366 {
367         if (ret >= 0)
368                 return ret;
369         assert(ret == -E_OSL);
370         if (osl_errno != E_OSL_LOOP)
371                 /* error not caused by loop function returning negative. */
372                 return ret;
373         assert(loop_ret < 0);
374         if (loop_ret == -E_LOOP_COMPLETE) /* no error */
375                 return 1;
376         if (loop_ret == -E_OSL) { /* osl error in loop function */
377                 assert(loop_osl_errno);
378                 osl_errno = loop_osl_errno;
379         }
380         return loop_ret;
381 }
382
383 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
384                 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
385 {
386         int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
387         return check_loop_return(ret, *loop_ret, *loop_osl_errno);
388 }
389
390 static int print_global_summary(struct format_info *fi)
391 {
392         int ret;
393         char *buf;
394         union atom_value values[] = {
395                 [gsa_dirs] = {.num_value = (long long unsigned)num_dirs},
396                 [gsa_files] = {.num_value =  (long long unsigned)num_files},
397                 [gsa_size] = {.num_value =  (long long unsigned)num_bytes}
398         };
399
400         if (select_conf.no_global_summary_given)
401                 return 1;
402         if (!select_conf.no_headers_given) {
403                 ret = output("Global summary\n");
404                 if (ret < 0)
405                         return ret;
406         }
407         buf = format_items(fi, values);
408         ret = output("%s", buf);
409         free(buf);
410         return ret;
411 }
412
413 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
414 {
415         struct format_info *fi = data;
416         union atom_value values[] = {
417                 [usa_pw_name] = {.string_value = ui->pw_name?
418                         ui->pw_name : "?"},
419                 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
420                 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
421                 [usa_files] = {.num_value =  (long long unsigned)ui->files},
422                 [usa_size] = {.num_value =  (long long unsigned)ui->bytes}
423         };
424         char *buf = format_items(fi, values);
425         int ret = output("%s", buf);
426
427         free(buf);
428         return ret;
429 }
430
431 static int name_comp(const void *a, const void *b)
432 {
433         char *x = ((struct user_info *)a)->pw_name;
434         char *y = ((struct user_info *)b)->pw_name;
435
436         if (!x)
437                 return 1;
438         if (!y)
439                 return -1;
440         return strcmp(x, y);
441 }
442
443 static int uid_comp(const void *a, const void *b)
444 {
445         return -NUM_COMPARE(((struct user_info *)a)->uid,
446                 ((struct user_info *)b)->uid);
447 }
448
449 static int dir_count_comp(const void *a, const void *b)
450 {
451         return NUM_COMPARE(((struct user_info *)a)->dirs,
452                 ((struct user_info *)b)->dirs);
453 }
454
455 static int file_count_comp(const void *a, const void *b)
456 {
457         return NUM_COMPARE(((struct user_info *)a)->files,
458                 ((struct user_info *)b)->files);
459 }
460
461 static int size_comp(const void *a, const void *b)
462 {
463         return NUM_COMPARE(((struct user_info *)a)->bytes,
464                 ((struct user_info *)b)->bytes);
465 }
466
467 /*
468  * The comparators for sorting the user summary.
469  *
470  * This is an array of pointers to functions taking two constant void *
471  * pointers and returning an int.
472  */
473 static int (*summary_comparators[])(const void *, const void *) = {
474         [user_summary_sort_arg_name] = name_comp,
475         [user_summary_sort_arg_uid] = uid_comp,
476         [user_summary_sort_arg_dir_count] = dir_count_comp,
477         [user_summary_sort_arg_file_count] = file_count_comp,
478         [user_summary_sort_arg_size] = size_comp,
479 };
480
481 static int print_user_summary(struct format_info *fi)
482 {
483         if (select_conf.no_user_summary_given)
484                 return 1;
485         if (!select_conf.no_headers_given) {
486                 int ret = output("User summary\n");
487                 if (ret < 0)
488                         return ret;
489         }
490         sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
491         return for_each_admissible_user(print_user_summary_line, fi);
492 }
493
494 static int print_user_list(struct user_info *ui, __a_unused void *data)
495 {
496         int ret;
497         struct user_stats_info usi;
498         enum enum_user_list ula = select_conf.user_list_arg;
499         int print_size_list = (ula == user_list_arg_size
500                 || ula == user_list_arg_both);
501
502         if (print_size_list) {
503                 usi.count = select_conf.limit_arg;
504                 usi.ui = ui;
505                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
506                 if (!select_conf.no_headers_given) {
507                         ret = output("%s (uid %u), by size%s:\n",
508                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
509                                 size_unit_buf);
510                         if (ret < 0)
511                                 return ret;
512                 }
513                 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
514                         &usi.ret, &usi.osl_errno);
515                 if (ret < 0)
516                         return ret;
517                 ret = output("\n");
518                 if (ret < 0)
519                         return ret;
520         }
521         if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
522                 if (!select_conf.no_headers_given) {
523                         ret = output("%s (uid %u), by file count%s:\n",
524                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
525                                 count_unit_buf);
526                         if (ret < 0)
527                                 return ret;
528                 }
529                 usi.count = select_conf.limit_arg,
530                 usi.ui = ui;
531                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
532                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
533                         &usi.ret, &usi.osl_errno);
534                 if (ret < 0)
535                         return ret;
536                 ret = output("\n");
537                 if (ret < 0)
538                         return ret;
539         }
540         if (ula == user_list_arg_none && !select_conf.no_user_summary_given) {
541                 usi.count = select_conf.limit_arg;
542                 usi.ui = ui;
543                 usi.flags = USF_COMPUTE_SUMMARY;
544                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
545                         &usi.ret, &usi.osl_errno);
546                 if (ret < 0)
547                         return ret;
548         }
549         return 1;
550 }
551
552 static int print_user_lists(void)
553 {
554         return for_each_admissible_user(print_user_list, NULL);
555 }
556
557 static int print_global_lists(void)
558 {
559         struct global_stats_info gsi;
560         int ret;
561         enum enum_global_list gla = select_conf.global_list_arg;
562         int print_size_list = (gla == global_list_arg_size
563                 || gla == global_list_arg_both);
564
565         if (print_size_list) {
566                 gsi.count = select_conf.limit_arg;
567                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
568                 if (!select_conf.no_headers_given) {
569                         ret = output("By size%s:\n", size_unit_buf);
570                         if (ret < 0)
571                                 return ret;
572                 }
573                 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
574                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
575                 if (ret < 0)
576                         return ret;
577                 ret = output("\n");
578                 if (ret < 0)
579                         return ret;
580         }
581         if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
582                 gsi.count = select_conf.limit_arg;
583                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
584                 if (!print_size_list)
585                         gsi.flags |= GSF_COMPUTE_SUMMARY;
586                 if (!select_conf.no_headers_given) {
587                         ret = output("By file count%s:\n", count_unit_buf);
588                         if (ret < 0)
589                                 return ret;
590                 }
591                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
592                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
593                 if (ret < 0)
594                         return ret;
595                 ret = output("\n");
596                 if (ret < 0)
597                         return ret;
598         }
599         if (gla == global_list_arg_none && !select_conf.no_global_summary_given) {
600                 /* must compute summary */
601                 gsi.count = select_conf.limit_arg;
602                 gsi.flags = GSF_COMPUTE_SUMMARY;
603                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
604                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
605                 if (ret < 0)
606                         return ret;
607         }
608         return 1;
609 }
610
611 static int print_statistics(struct select_format_info *sli)
612 {
613         int ret;
614
615         ret = print_global_lists();
616         if (ret < 0)
617                 return ret;
618         ret = print_global_summary(sli->global_summary_fi);
619         free_format_info(sli->global_summary_fi);
620         if (ret < 0)
621                 return ret;
622         ret = print_user_lists();
623         if (ret < 0)
624                 return ret;
625         ret = print_user_summary(sli->user_summary_fi);
626         free_format_info(sli->user_summary_fi);
627         if (ret < 0)
628                 return ret;
629         return 1;
630 }
631
632 static int read_uid_file(struct uid_range *admissible_uids)
633 {
634         size_t size;
635         uint32_t n;
636         char *filename = get_uid_list_name(), *map;
637         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
638         unsigned bits;
639
640         if (ret < 0) {
641                 INFO_LOG("failed to map %s\n", filename);
642                 free(filename);
643                 return ret;
644         }
645         num_uids = size / 4;
646         INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
647         free(filename);
648         /*
649          * Compute number of hash table bits. The hash table size must be a
650          * power of two and larger than the number of uids.
651          */
652         bits = 2;
653         while (1 << bits < num_uids)
654                 bits++;
655         create_hash_table(bits);
656         for (n = 0; n < num_uids; n++) {
657                 uint32_t uid = read_u32(map + n * sizeof(uid));
658                 ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
659                 if (ret < 0)
660                         goto out;
661         }
662 out:
663         adu_munmap(map, size);
664         return ret;
665 }
666
667 int run_select_query(struct uid_range *admissible_uids,
668                 struct select_format_info *sfi)
669 {
670         int ret;
671
672         if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) {
673                 output_file = fopen(select_conf.output_arg, "w");
674                 if (!output_file)
675                         return -ERRNO_TO_ERROR(errno);
676         } else
677                 output_file = stdout;
678
679         if (select_conf.count_unit_arg != count_unit_arg_h)
680                 count_unit_buf[1] = count_unit_abbrevs[select_conf.count_unit_arg];
681         else
682                 count_unit_buf[0] = '\0';
683         if (select_conf.size_unit_arg != size_unit_arg_h)
684                 size_unit_buf[1] = size_unit_abbrevs[select_conf.size_unit_arg];
685         else
686                 size_unit_buf[0] = '\0';
687
688         ret = open_dir_table(0);
689         if (ret < 0)
690                 goto out;
691         check_signals();
692         ret = read_uid_file(admissible_uids);
693         if (ret < 0)
694                 goto out;
695         check_signals();
696         ret = print_statistics(sfi);
697 out:
698         close_all_tables();
699         if (output_file != stdout)
700                 fclose(output_file);
701         return ret;
702 }
703
704 /* return: < 0: error, >0: OK, == 0: help given */
705 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
706                 struct uid_range **admissible_uids, struct select_format_info *sfi)
707 {
708         int ret;
709         const char **line;
710
711         if (conf.select_options_given) {
712                 int argc;
713                 char **argv;
714
715                 ret = create_argv(string, &argv);
716                 if (ret < 0)
717                         return ret;
718                 argc = ret;
719                 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
720                 free_argv(argv);
721                 if (ret)
722                         return -E_SYNTAX;
723                 if (select_conf.help_given || select_conf.detailed_help_given)
724                         goto help;
725
726         }
727         ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
728         if (ret < 0)
729                 return ret;
730         ret = parse_format_string(select_conf.user_summary_format_arg,
731                 user_summary_atoms, &sfi->user_summary_fi);
732         if (ret < 0)
733                 return ret;
734         ret = parse_format_string(select_conf.global_summary_format_arg,
735                 global_summary_atoms, &sfi->global_summary_fi);
736         if (ret < 0) {
737                 free_format_info(sfi->user_summary_fi);
738                 return ret;
739         }
740         return 1;
741 help:
742         line = select_conf.detailed_help_given?
743                 select_args_info_detailed_help : select_args_info_help;
744         if (!output_file)
745                 output_file = stdout;
746         for (; *line; line++) {
747                 ret = output("%s\n", *line);
748                 if (ret < 0)
749                         return ret;
750         }
751         return 0;
752 }
753
754 int com_select(void)
755 {
756         struct uid_range *admissible_uids = NULL;
757         struct select_format_info sfi;
758         int ret;
759         struct select_cmdline_parser_params params = {
760                 .override = 1,
761                 .initialize = 1,
762                 .check_required = 1,
763                 .check_ambiguity = 1,
764                 .print_errors = 1
765         };
766
767         select_cmdline_parser_init(&select_conf);
768         ret = parse_select_options(conf.select_options_arg, &params,
769                 &admissible_uids, &sfi);
770         if (ret <= 0) /* do not run query if help was given */
771                 return ret;
772         return run_select_query(admissible_uids, &sfi);
773 }