]> git.tuebingen.mpg.de Git - adu.git/blob - select.c
f200953266f1bc2eeed3e224c3b4714e09d93fff
[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 "user.h"
18 #include "select.cmdline.h"
19
20 /* global list */
21 #define GLOBAL_LIST_ATOMS \
22         ATOM(size, SIZE) \
23         ATOM(files, COUNT) \
24         ATOM(dirname, STRING) \
25
26 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
27 struct atom global_list_atoms[] = {
28         GLOBAL_LIST_ATOMS
29         {.name = NULL}
30 };
31 #undef ATOM
32 #define ATOM(x, y) gla_ ## x,
33 enum global_list_atoms {GLOBAL_LIST_ATOMS};
34 #undef ATOM
35
36 /* global summary */
37 #define GLOBAL_SUMMARY_ATOMS \
38         ATOM(dirs, COUNT) \
39         ATOM(files, COUNT) \
40         ATOM(size, SIZE)
41
42 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
43 struct atom global_summary_atoms[] = {
44         GLOBAL_SUMMARY_ATOMS
45         {.name = NULL}
46 };
47 #undef ATOM
48 #define ATOM(x, y) gsa_ ## x,
49 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
50 #undef ATOM
51
52 /* user list */
53 #define USER_LIST_ATOMS \
54         ATOM(pw_name, STRING) \
55         ATOM(uid, ID) \
56         ATOM(size, SIZE) \
57         ATOM(files, COUNT) \
58         ATOM(dirname, STRING) \
59
60 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
61 struct atom user_list_atoms[] = {
62         USER_LIST_ATOMS
63         {.name = NULL}
64 };
65 #undef ATOM
66 #define ATOM(x, y) ula_ ## x,
67 enum user_list_atoms {USER_LIST_ATOMS};
68 #undef ATOM
69
70 /* user summary */
71 #define USER_SUMMARY_ATOMS \
72         ATOM(pw_name, STRING) \
73         ATOM(uid, ID) \
74         ATOM(dirs, COUNT) \
75         ATOM(files, COUNT) \
76         ATOM(size, SIZE)
77
78 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
79 struct atom user_summary_atoms[] = {
80         USER_SUMMARY_ATOMS
81         {.name = NULL}
82 };
83 #undef ATOM
84 #define ATOM(x, y) usa_ ## x,
85 enum user_summary_atoms {USER_SUMMARY_ATOMS};
86 #undef ATOM
87
88 struct global_list_info {
89         uint32_t count;
90         int ret;
91         int osl_errno;
92         struct format_info *fi;
93 };
94
95 struct global_summary_info {
96         /** Global dir count. */
97         uint64_t num_dirs;
98         /** Global files count. */
99         uint64_t num_files;
100         /** Global bytes count. */
101         uint64_t num_bytes;
102         int ret;
103         int osl_errno;
104 };
105
106 struct user_list_info {
107         uint32_t count;
108         struct user_info *ui;
109         struct format_info *fi;
110         int ret;
111         int osl_errno;
112 };
113
114 struct user_summary_info {
115         struct user_info *ui;
116         int ret;
117         int osl_errno;
118 };
119
120
121 static FILE *output_file;
122
123 __printf_1_2 static int output(const char const *fmt, ...)
124 {
125         va_list argp;
126         int ret;
127
128         va_start(argp, fmt);
129         ret = vfprintf(output_file, fmt, argp);
130         va_end(argp);
131         return ret < 0? -E_OUTPUT : 1;
132 }
133
134 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
135 {
136         char *result = NULL, *tmp;
137         struct osl_row *row;
138         uint64_t val = *dirnum;
139         struct osl_object obj;
140         int ret;
141         char *pfx;
142
143 again:
144         obj.data = &val;
145         obj.size = sizeof(val);
146         ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
147         if (ret < 0)
148                 goto out;
149         ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
150         if (ret < 0)
151                 goto out;
152         val = *(uint64_t *)obj.data;
153         ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
154         if (ret < 0)
155                 goto out;
156         pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data :  ".";
157         tmp = make_message("%s/%s", pfx, result? result : "");
158         free(result);
159         result = tmp;
160         if (val)
161                 goto again;
162 out:
163         if (ret < 0) {
164                 free(result);
165                 *name = NULL;
166         } else {
167                 assert(result);
168                 *name = result;
169         }
170         return ret;
171 }
172
173 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
174 {
175         struct osl_object obj;
176         int ret;
177
178         *name = NULL;
179         ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
180         if (ret < 0)
181                 return ret;
182         return get_dir_name_by_number((uint64_t *)obj.data, name);
183 }
184
185 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
186                 struct user_info *ui, char **dirname)
187 {
188         struct osl_object obj;
189         int ret = osl(osl_get_object(ui->table, user_table_row,
190                 UT_DIR_NUM, &obj));
191
192         if (ret < 0)
193                 return ret;
194         return get_dir_name_by_number((uint64_t *)obj.data, dirname);
195 }
196
197 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
198 {
199         struct osl_object obj;
200         int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
201         if (ret < 0)
202                 return ret;
203         *num_files = *(uint64_t *)obj.data;
204         return 1;
205 }
206
207 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
208                 uint64_t *num_files)
209 {
210         struct osl_object obj;
211         int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
212
213         if (ret < 0)
214                 return ret;
215         *num_files = *(uint64_t *)obj.data;
216         return 1;
217 }
218
219 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
220 {
221         struct osl_object obj;
222         int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
223         if (ret < 0)
224                 return ret;
225         *num_bytes = *(uint64_t *)obj.data;
226         return 1;
227 }
228
229 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
230                 uint64_t *num_bytes)
231 {
232         struct osl_object obj;
233         int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
234
235         if (ret < 0)
236                 return ret;
237         *num_bytes = *(uint64_t *)obj.data;
238         return 1;
239 }
240
241 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
242 {
243         if (ret >= 0)
244                 return ret;
245         assert(ret == -E_OSL);
246         if (osl_errno != E_OSL_LOOP)
247                 /* error not caused by loop function returning negative. */
248                 return ret;
249         assert(loop_ret < 0);
250         if (loop_ret == -E_LOOP_COMPLETE) /* no error */
251                 return 1;
252         if (loop_ret == -E_OSL) { /* osl error in loop function */
253                 assert(loop_osl_errno);
254                 osl_errno = loop_osl_errno;
255         }
256         return loop_ret;
257 }
258
259 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
260                 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
261 {
262         int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
263         return check_loop_return(ret, *loop_ret, *loop_osl_errno);
264 }
265
266 static int global_summary_loop_function(struct osl_row *row, void *data)
267 {
268         struct global_summary_info *gsi = data;
269         int ret;
270         uint64_t num;
271
272         ret = get_num_files_of_row(row, &num);
273         if (ret < 0)
274                 goto err;
275         gsi->num_files += num;
276
277         ret = get_num_bytes_of_row(row, &num);
278         if (ret < 0)
279                 goto err;
280         gsi->num_bytes += num;
281         gsi->num_dirs++;
282         return 1;
283 err:
284         gsi->ret = ret;
285         gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
286         return ret;
287 }
288
289 static int print_global_summary(struct format_info *fi)
290 {
291         int ret;
292         char *buf;
293         struct global_summary_info gsi = {.num_dirs = 0};
294
295         union atom_value values[] = {
296                 [gsa_dirs] = {.num_value = 0ULL},
297                 [gsa_files] = {.num_value =  0ULL},
298                 [gsa_size] = {.num_value =  0ULL}
299         };
300
301         ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
302                 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
303         if (ret < 0)
304                 return ret;
305         values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
306         values[gsa_files].num_value = (long long unsigned)gsi.num_files;
307         values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
308         if (!select_conf.no_headers_given) {
309                 ret = output("Global summary\n");
310                 if (ret < 0)
311                         return ret;
312         }
313         buf = format_items(fi, values);
314         ret = output("%s", buf);
315         free(buf);
316         return ret;
317 }
318
319 static int user_summary_loop_function(struct osl_row *row, void *data)
320 {
321         struct user_summary_info *usi = data;
322         uint64_t num;
323         int ret;
324
325         ret = get_num_user_files(row, usi->ui, &num);
326         if (ret < 0)
327                 goto err;
328         usi->ui->files += num;
329         ret = get_num_user_bytes(row, usi->ui, &num);
330         if (ret < 0)
331                 goto err;
332         usi->ui->bytes += num;
333         usi->ui->dirs++;
334         return 1;
335 err:
336         usi->ret = ret;
337         usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
338         return ret;
339 }
340
341 static int compute_user_summary(struct user_info *ui, __a_unused void *data)
342 {
343         struct user_summary_info usi = {.ui = ui};
344
345         return adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
346                 &usi.ret, &usi.osl_errno);
347 }
348
349 static int print_user_summary_line(struct user_info *ui, void *data)
350 {
351         struct format_info *fi = data;
352         union atom_value values[] = {
353                 [usa_pw_name] = {.string_value = ui->pw_name?
354                         ui->pw_name : "?"},
355                 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
356                 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
357                 [usa_files] = {.num_value = (long long unsigned)ui->files},
358                 [usa_size] = {.num_value =  (long long unsigned)ui->bytes}
359         };
360         char *buf;
361         int ret;
362
363         buf = format_items(fi, values);
364         ret = output("%s", buf);
365         free(buf);
366         return ret;
367 }
368
369 static int name_comp(struct user_info *a, struct user_info *b)
370 {
371         char *x = a->pw_name;
372         char *y = b->pw_name;
373
374         if (!x)
375                 return 1;
376         if (!y)
377                 return -1;
378         return strcmp(x, y);
379 }
380
381 static int uid_comp(struct user_info *a, struct user_info *b)
382 {
383         return -NUM_COMPARE(a->uid, b->uid);
384 }
385
386 static int dir_count_comp(struct user_info *a, struct user_info *b)
387 {
388         return NUM_COMPARE(a->dirs, b->dirs);
389 }
390
391 static int file_count_comp(struct user_info *a, struct user_info *b)
392 {
393         return NUM_COMPARE(a->files, b->files);
394 }
395
396 static int size_comp(struct user_info *a, struct user_info *b)
397 {
398         return NUM_COMPARE(a->bytes, b->bytes);
399 }
400
401 static int print_user_summary(struct format_info *fi)
402 {
403         int ret;
404         int (*comp)(struct user_info *a, struct user_info *b);
405
406         if (!select_conf.no_headers_given) {
407                 ret = output("User summary\n");
408                 if (ret < 0)
409                         return ret;
410         }
411         ret = for_each_admissible_user(compute_user_summary, fi);
412         if (ret < 0)
413                 return ret;
414         switch (select_conf.user_summary_sort_arg) {
415         case user_summary_sort_arg_name:
416                 comp = name_comp;
417                 break;
418         case user_summary_sort_arg_uid:
419                 comp = uid_comp;
420                 break;
421         case user_summary_sort_arg_dir_count:
422                 comp = dir_count_comp;
423                 break;
424         case user_summary_sort_arg_file_count:
425                 comp = file_count_comp;
426                 break;
427         case user_summary_sort_arg_size:
428                 comp = size_comp;
429                 break;
430         }
431         sort_hash_table(comp);
432         return for_each_admissible_user(print_user_summary_line, fi);
433 }
434
435 static int user_list_loop_function(struct osl_row *row, void *data)
436 {
437         struct user_list_info *uli = data;
438         union atom_value values[] = {
439                 [ula_pw_name] = {.string_value = uli->ui->pw_name?
440                         uli->ui->pw_name : "?"},
441                 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
442                 [ula_files] = {.num_value = 0ULL},
443                 [ula_size] = {.num_value =  0ULL},
444                 [ula_dirname] = {.string_value = NULL}
445         };
446         uint64_t num;
447         int ret;
448         char *dirname, *buf;
449
450         check_signals();
451         ret = -E_LOOP_COMPLETE;
452         if (!uli->count)
453                 goto err;
454
455         ret = get_num_user_files(row, uli->ui, &num);
456         if (ret < 0)
457                 goto err;
458         values[ula_files].num_value = num;
459
460         ret = get_num_user_bytes(row, uli->ui, &num);
461         if (ret < 0)
462                 goto err;
463         values[ula_size].num_value = num;
464
465         ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
466         if (ret < 0)
467                 goto err;
468         values[ula_dirname].string_value = dirname;
469
470         buf = format_items(uli->fi, values);
471         free(dirname);
472         ret = output("%s", buf);
473         free(buf);
474         if (ret < 0)
475                 goto err;
476         uli->count--;
477         return ret;
478 err:
479         uli->ret = ret;
480         uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
481         return ret;
482 }
483
484 static int print_user_list(struct user_info *ui, void *data)
485 {
486         struct format_info *fi = data;
487         int ret;
488         enum user_table_columns sort_column = UT_BYTES;
489         struct user_list_info uli = {
490                 .ui = ui,
491                 .fi = fi,
492                 .count = select_conf.limit_arg
493         };
494
495         if (select_conf.list_sort_arg == list_sort_arg_file_count)
496                 sort_column = UT_FILES;
497
498         if (!select_conf.no_headers_given) {
499                 ret = output("%s (uid %u)\n",
500                         ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid);
501                 if (ret < 0)
502                         return ret;
503         }
504         return adu_loop_reverse(ui->table, sort_column, &uli, user_list_loop_function,
505                 &uli.ret, &uli.osl_errno);
506 }
507
508 static int global_list_loop_function(struct osl_row *row, void *data)
509 {
510         struct global_list_info *gli = data;
511         union atom_value values[] = {
512                 [gla_size] = {.num_value = 0ULL},
513                 [gla_files] = {.num_value =  0ULL},
514                 [gla_dirname] = {.string_value = NULL}
515         };
516         uint64_t num_files, num_bytes;
517         char *dirname, *buf;
518         int ret;
519
520         check_signals();
521         ret = -E_LOOP_COMPLETE;
522         if (!gli->count)
523                 goto err;
524
525         ret = get_num_files_of_row(row, &num_files);
526         if (ret < 0)
527                 goto err;
528         values[gla_files].num_value = (long long unsigned)num_files;
529
530         ret = get_num_bytes_of_row(row, &num_bytes);
531         if (ret < 0)
532                 goto err;
533         values[gla_size].num_value = (long long unsigned)num_bytes;
534
535         ret = get_dir_name_of_row(row, &dirname);
536         if (ret < 0)
537                 goto err;
538         values[gla_dirname].string_value = dirname;
539
540         buf = format_items(gli->fi, values);
541         free(dirname);
542         ret = output("%s", buf);
543         free(buf);
544         if (ret < 0)
545                 goto err;
546         if (gli->count > 0)
547                 gli->count--;
548         return ret;
549 err:
550         gli->ret = ret;
551         gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
552         return -1;
553 }
554
555 static int print_global_list(struct format_info *fi)
556 {
557         int ret;
558         enum dir_table_columns sort_column = DT_BYTES;
559         struct global_list_info gli = {
560                 .fi = fi,
561                 .count = select_conf.limit_arg
562         };
563
564         if (!select_conf.no_headers_given) {
565                 ret = output("Global list\n");
566                 if (ret < 0)
567                         return ret;
568         }
569         if (select_conf.list_sort_arg == list_sort_arg_file_count)
570                 sort_column = DT_FILES;
571         return adu_loop_reverse(dir_table, sort_column, &gli,
572                 global_list_loop_function, &gli.ret, &gli.osl_errno);
573 }
574
575 static int print_statistics(struct format_info *fi)
576 {
577         switch (select_conf.select_mode_arg) {
578                 case select_mode_arg_global_list:
579                         return print_global_list(fi);
580                 case select_mode_arg_global_summary:
581                         return print_global_summary(fi);
582                 case select_mode_arg_user_list:
583                         return for_each_admissible_user(print_user_list, fi);
584                 case select_mode_arg_user_summary:
585                         return print_user_summary(fi);
586         };
587         ERROR_LOG("bad select mode\n");
588         return -ERRNO_TO_ERROR(EINVAL);
589 }
590
591 static int open_pipe(char *path)
592 {
593         int p[2], ret, argc;
594         char **argv;
595
596         ret = pipe(p);
597         if (ret < 0)
598                 return ERRNO_TO_ERROR(errno);
599         ret = fork();
600         if (ret < 0)
601                 return ERRNO_TO_ERROR(errno);
602         if (ret) { /* parent */
603                 DEBUG_LOG("created process %d\n", ret);
604                 close(p[0]);
605                 output_file = fdopen(p[1], "w");
606                 if (!output_file)
607                         return ERRNO_TO_ERROR(errno);
608                 return 1;
609         }
610         close(p[1]);
611         if (p[0] != STDIN_FILENO)
612                 dup2(p[0], STDIN_FILENO);
613         DEBUG_LOG("executing %s\n", path);
614         argc = split_args(path, &argv, " \t");
615         execvp(argv[0], argv);
616         ERROR_LOG("error executing %s: %s\n", path,
617                 adu_strerror(ERRNO_TO_ERROR(errno)));
618         _exit(EXIT_FAILURE);
619 }
620
621 static int open_output_stream(void)
622 {
623         char *p;
624         int ret, flags = O_WRONLY | O_CREAT;
625
626         if (!select_conf.output_given)
627                 goto use_stdout;
628         p = select_conf.output_arg;
629         switch (p[0]) {
630         case '\0': /* empty string */
631                 goto bad_output_arg;
632         case '-':
633                 if (!p[1]) /* "-" means stdout */
634                         goto use_stdout;
635                 /* string starting with a dash */
636                 flags |= O_EXCL;
637                 goto open_file;
638         case '>':
639                 if (!p[1]) /* ">" is invalid */
640                         goto bad_output_arg;
641                 if (p[1] != '>') {
642                         p++;
643                         flags |= O_TRUNC;
644                         goto open_file;
645                 }
646                 /* string starting with ">>" */
647                 if (!p[2]) /* ">>" is invalid */
648                         goto bad_output_arg;
649                 flags |= O_APPEND;
650                 p += 2;
651                 goto open_file;
652         case '|':
653                 if (!p[1]) /* "|" is invalid */
654                         goto bad_output_arg;
655                 p++;
656                 return open_pipe(p);
657         default: /* args starts with no magic character */
658                 flags |= O_EXCL;
659                 goto open_file;
660         }
661 use_stdout:
662         output_file = stdout;
663         return 1;
664 bad_output_arg:
665         output_file = NULL;
666         return -E_BAD_OUTPUT_ARG;
667 open_file:
668         /*
669          * glibc's 'x' mode to fopen is not portable, so use open() and
670          * fdopen().
671          */
672         ret = open(p, flags, 0644);
673         if (ret < 0)
674                 return -ERRNO_TO_ERROR(errno);
675         output_file = fdopen(ret, "w");
676         if (!output_file)
677                 return -ERRNO_TO_ERROR(errno);
678         return 1;
679 }
680
681 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
682 {
683         int ret = open_output_stream();
684
685         if (ret < 0)
686                 goto out;
687         ret = open_dir_table(0);
688         if (ret < 0)
689                 goto out;
690         check_signals();
691         ret = open_admissible_user_tables(admissible_uids);
692         if (ret < 0)
693                 goto out;
694         check_signals();
695         ret = print_statistics(fi);
696 out:
697         if (output_file && output_file != stdout) {
698                 fclose(output_file);
699                 output_file = NULL;
700         }
701         return ret;
702 }
703
704 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
705 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
706 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
707 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
708
709 static int setup_format_string(char *fmt, struct format_info **fi)
710 {
711         struct atom *atoms;
712
713         if (!fmt)
714                 INFO_LOG("using default format string\n");
715         switch (select_conf.select_mode_arg) {
716         case select_mode_arg_global_list:
717                 if (!fmt)
718                         fmt = GLOBAL_LIST_DFLT_FMT;
719                 atoms = global_list_atoms;
720                 break;
721         case select_mode_arg_global_summary:
722                 if (!fmt)
723                         fmt = GLOBAL_SUMMARY_DFLT_FMT;
724                 atoms = global_summary_atoms;
725                 break;
726         case select_mode_arg_user_list:
727                 if (!fmt)
728                         fmt = USER_LIST_DFLT_FMT;
729                 atoms = user_list_atoms;
730                 break;
731         case select_mode_arg_user_summary:
732                 if (!fmt)
733                         fmt = USER_SUMMARY_DFLT_FMT;
734                 atoms = user_summary_atoms;
735                 break;
736         default:
737                 ERROR_LOG("bad select mode\n");
738                 return -ERRNO_TO_ERROR(EINVAL);
739         };
740         INFO_LOG("format string: %s\n", fmt);
741         return parse_format_string(fmt, atoms, fi);
742 }
743
744 /* return: < 0: error, >0: OK, == 0: help given */
745 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
746                 struct uid_range **admissible_uids, struct format_info **fi)
747 {
748         int ret, num_uid_ranges;
749         const char **line;
750         char *fmt = NULL;
751
752         if (string) {
753                 int argc;
754                 char **argv;
755
756                 ret = create_argv(string, &argv);
757                 if (ret < 0)
758                         return ret;
759                 argc = ret;
760                 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
761                 free_argv(argv);
762                 if (ret)
763                         return -E_SYNTAX;
764                 if (select_conf.help_given || select_conf.detailed_help_given)
765                         goto help;
766                 fmt = select_conf.format_arg;
767         }
768         ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
769         if (ret < 0)
770                 return ret;
771         num_uid_ranges = ret;
772         ret = append_users(select_conf.user_arg, select_conf.user_given,
773                 admissible_uids, num_uid_ranges);
774         if (ret < 0)
775                 return ret;
776         return setup_format_string(fmt, fi);
777 help:
778         line = select_conf.detailed_help_given?
779                 select_args_info_detailed_help : select_args_info_help;
780         if (!output_file)
781                 output_file = stdout;
782         for (; *line; line++) {
783                 ret = output("%s\n", *line);
784                 if (ret < 0)
785                         return ret;
786         }
787         return 0;
788 }
789
790 int com_select(void)
791 {
792         struct uid_range *admissible_uids = NULL;
793         int ret;
794         struct format_info *fi;
795         struct select_cmdline_parser_params params = {
796                 .override = 1,
797                 .initialize = 1,
798                 .check_required = 1,
799                 .check_ambiguity = 1,
800                 .print_errors = 1
801         };
802
803         ret = parse_select_options(conf.select_options_arg, &params,
804                 &admissible_uids, &fi);
805         if (ret > 0) {
806                 ret = read_uid_file();
807                 if (ret < 0)
808                         goto out;
809                 ret = run_select_query(admissible_uids, fi);
810                 free_format_info(fi);
811         }
812 out:
813         select_cmdline_parser_free(&select_conf);
814         return ret;
815 }