ded4a095ca751574bbfa9153d3ec61ad7aa32bb2
[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(const void *a, const void *b)
370 {
371         char *x = ((struct user_info *)a)->pw_name;
372         char *y = ((struct user_info *)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(const void *a, const void *b)
382 {
383         return -NUM_COMPARE(((struct user_info *)a)->uid,
384                 ((struct user_info *)b)->uid);
385 }
386
387 static int dir_count_comp(const void *a, const void *b)
388 {
389         return NUM_COMPARE(((struct user_info *)a)->dirs,
390                 ((struct user_info *)b)->dirs);
391 }
392
393 static int file_count_comp(const void *a, const void *b)
394 {
395         return NUM_COMPARE(((struct user_info *)a)->files,
396                 ((struct user_info *)b)->files);
397 }
398
399 static int size_comp(const void *a, const void *b)
400 {
401         return NUM_COMPARE(((struct user_info *)a)->bytes,
402                 ((struct user_info *)b)->bytes);
403 }
404
405 static int print_user_summary(struct format_info *fi)
406 {
407         /*
408          * The comparators for sorting the user summary.
409          *
410          * This is an array of pointers to functions taking two constant void *
411          * pointers and returning an int.
412          */
413         static int (*summary_comparators[])(const void *, const void *) = {
414                 [user_summary_sort_arg_name] = name_comp,
415                 [user_summary_sort_arg_uid] = uid_comp,
416                 [user_summary_sort_arg_dir_count] = dir_count_comp,
417                 [user_summary_sort_arg_file_count] = file_count_comp,
418                 [user_summary_sort_arg_size] = size_comp,
419         };
420
421         if (!select_conf.no_headers_given) {
422                 int ret = output("User summary\n");
423                 if (ret < 0)
424                         return ret;
425         }
426         int ret = for_each_admissible_user(compute_user_summary, fi);
427         if (ret < 0)
428                 return ret;
429         sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
430         return for_each_admissible_user(print_user_summary_line, fi);
431 }
432
433 static int user_list_loop_function(struct osl_row *row, void *data)
434 {
435         struct user_list_info *uli = data;
436         union atom_value values[] = {
437                 [ula_pw_name] = {.string_value = uli->ui->pw_name?
438                         uli->ui->pw_name : "?"},
439                 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
440                 [ula_files] = {.num_value = 0ULL},
441                 [ula_size] = {.num_value =  0ULL},
442                 [ula_dirname] = {.string_value = NULL}
443         };
444         uint64_t num;
445         int ret;
446         char *dirname, *buf;
447
448         check_signals();
449         ret = -E_LOOP_COMPLETE;
450         if (!uli->count)
451                 goto err;
452
453         ret = get_num_user_files(row, uli->ui, &num);
454         if (ret < 0)
455                 goto err;
456         values[ula_files].num_value = num;
457
458         ret = get_num_user_bytes(row, uli->ui, &num);
459         if (ret < 0)
460                 goto err;
461         values[ula_size].num_value = num;
462
463         ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
464         if (ret < 0)
465                 goto err;
466         values[ula_dirname].string_value = dirname;
467
468         buf = format_items(uli->fi, values);
469         free(dirname);
470         ret = output("%s", buf);
471         free(buf);
472         if (ret < 0)
473                 goto err;
474         uli->count--;
475         return ret;
476 err:
477         uli->ret = ret;
478         uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
479         return ret;
480 }
481
482 static int print_user_list(struct user_info *ui, void *data)
483 {
484         struct format_info *fi = data;
485         int ret;
486         enum user_table_columns sort_column = UT_BYTES;
487         struct user_list_info uli = {
488                 .ui = ui,
489                 .fi = fi,
490                 .count = select_conf.limit_arg
491         };
492
493         if (select_conf.list_sort_arg == list_sort_arg_file_count)
494                 sort_column = UT_FILES;
495
496         if (!select_conf.no_headers_given) {
497                 ret = output("%s (uid %u)\n",
498                         ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid);
499                 if (ret < 0)
500                         return ret;
501         }
502         return adu_loop_reverse(ui->table, sort_column, &uli, user_list_loop_function,
503                 &uli.ret, &uli.osl_errno);
504 }
505
506 static int global_list_loop_function(struct osl_row *row, void *data)
507 {
508         struct global_list_info *gli = data;
509         union atom_value values[] = {
510                 [gla_size] = {.num_value = 0ULL},
511                 [gla_files] = {.num_value =  0ULL},
512                 [gla_dirname] = {.string_value = NULL}
513         };
514         uint64_t num_files, num_bytes;
515         char *dirname, *buf;
516         int ret;
517
518         check_signals();
519         ret = -E_LOOP_COMPLETE;
520         if (!gli->count)
521                 goto err;
522
523         ret = get_num_files_of_row(row, &num_files);
524         if (ret < 0)
525                 goto err;
526         values[gla_files].num_value = (long long unsigned)num_files;
527
528         ret = get_num_bytes_of_row(row, &num_bytes);
529         if (ret < 0)
530                 goto err;
531         values[gla_size].num_value = (long long unsigned)num_bytes;
532
533         ret = get_dir_name_of_row(row, &dirname);
534         if (ret < 0)
535                 goto err;
536         values[gla_dirname].string_value = dirname;
537
538         buf = format_items(gli->fi, values);
539         free(dirname);
540         ret = output("%s", buf);
541         free(buf);
542         if (ret < 0)
543                 goto err;
544         if (gli->count > 0)
545                 gli->count--;
546         return ret;
547 err:
548         gli->ret = ret;
549         gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
550         return -1;
551 }
552
553 static int print_global_list(struct format_info *fi)
554 {
555         int ret;
556         enum dir_table_columns sort_column = DT_BYTES;
557         struct global_list_info gli = {
558                 .fi = fi,
559                 .count = select_conf.limit_arg
560         };
561
562         if (!select_conf.no_headers_given) {
563                 ret = output("Global list\n");
564                 if (ret < 0)
565                         return ret;
566         }
567         if (select_conf.list_sort_arg == list_sort_arg_file_count)
568                 sort_column = DT_FILES;
569         return adu_loop_reverse(dir_table, sort_column, &gli,
570                 global_list_loop_function, &gli.ret, &gli.osl_errno);
571 }
572
573 static int print_statistics(struct format_info *fi)
574 {
575         switch (select_conf.select_mode_arg) {
576                 case select_mode_arg_global_list:
577                         return print_global_list(fi);
578                 case select_mode_arg_global_summary:
579                         return print_global_summary(fi);
580                 case select_mode_arg_user_list:
581                         return for_each_admissible_user(print_user_list, fi);
582                 case select_mode_arg_user_summary:
583                         return print_user_summary(fi);
584         };
585         ERROR_LOG("bad select mode\n");
586         return -ERRNO_TO_ERROR(EINVAL);
587 }
588
589 static int open_pipe(char *path)
590 {
591         int p[2], ret, argc;
592         char **argv;
593
594         ret = pipe(p);
595         if (ret < 0)
596                 return ERRNO_TO_ERROR(errno);
597         ret = fork();
598         if (ret < 0)
599                 return ERRNO_TO_ERROR(errno);
600         if (ret) { /* parent */
601                 DEBUG_LOG("created process %d\n", ret);
602                 close(p[0]);
603                 output_file = fdopen(p[1], "w");
604                 if (!output_file)
605                         return ERRNO_TO_ERROR(errno);
606                 return 1;
607         }
608         close(p[1]);
609         if (p[0] != STDIN_FILENO)
610                 dup2(p[0], STDIN_FILENO);
611         DEBUG_LOG("executing %s\n", path);
612         argc = split_args(path, &argv, " \t");
613         execvp(argv[0], argv);
614         ERROR_LOG("error executing %s: %s\n", path,
615                 adu_strerror(ERRNO_TO_ERROR(errno)));
616         _exit(EXIT_FAILURE);
617 }
618
619 static int open_output_stream(void)
620 {
621         char *p;
622         int ret, flags = O_WRONLY | O_CREAT;
623
624         if (!select_conf.output_given)
625                 goto use_stdout;
626         p = select_conf.output_arg;
627         switch (p[0]) {
628         case '\0': /* empty string */
629                 goto bad_output_arg;
630         case '-':
631                 if (!p[1]) /* "-" means stdout */
632                         goto use_stdout;
633                 /* string starting with a dash */
634                 flags |= O_EXCL;
635                 goto open_file;
636         case '>':
637                 if (!p[1]) /* ">" is invalid */
638                         goto bad_output_arg;
639                 if (p[1] != '>') {
640                         p++;
641                         flags |= O_TRUNC;
642                         goto open_file;
643                 }
644                 /* string starting with ">>" */
645                 if (!p[2]) /* ">>" is invalid */
646                         goto bad_output_arg;
647                 flags |= O_APPEND;
648                 p += 2;
649                 goto open_file;
650         case '|':
651                 if (!p[1]) /* "|" is invalid */
652                         goto bad_output_arg;
653                 p++;
654                 return open_pipe(p);
655         default: /* args starts with no magic character */
656                 flags |= O_EXCL;
657                 goto open_file;
658         }
659 use_stdout:
660         output_file = stdout;
661         return 1;
662 bad_output_arg:
663         output_file = NULL;
664         return -E_BAD_OUTPUT_ARG;
665 open_file:
666         /*
667          * glibc's 'x' mode to fopen is not portable, so use open() and
668          * fdopen().
669          */
670         ret = open(p, flags, 0644);
671         if (ret < 0)
672                 return -ERRNO_TO_ERROR(errno);
673         output_file = fdopen(ret, "w");
674         if (!output_file)
675                 return -ERRNO_TO_ERROR(errno);
676         return 1;
677 }
678
679 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
680 {
681         int ret = open_output_stream();
682
683         if (ret < 0)
684                 goto out;
685         ret = open_dir_table(0);
686         if (ret < 0)
687                 goto out;
688         check_signals();
689         ret = read_uid_file(admissible_uids);
690         if (ret < 0)
691                 goto out;
692         check_signals();
693         ret = print_statistics(fi);
694 out:
695         close_all_tables();
696         if (output_file && output_file != stdout) {
697                 fclose(output_file);
698                 output_file = NULL;
699         }
700         return ret;
701 }
702
703 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
704 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
705 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
706 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
707
708 static int setup_format_string(char *fmt, struct format_info **fi)
709 {
710         struct atom *atoms;
711
712         if (!fmt)
713                 INFO_LOG("using default format string\n");
714         switch (select_conf.select_mode_arg) {
715         case select_mode_arg_global_list:
716                 if (!fmt)
717                         fmt = GLOBAL_LIST_DFLT_FMT;
718                 atoms = global_list_atoms;
719                 break;
720         case select_mode_arg_global_summary:
721                 if (!fmt)
722                         fmt = GLOBAL_SUMMARY_DFLT_FMT;
723                 atoms = global_summary_atoms;
724                 break;
725         case select_mode_arg_user_list:
726                 if (!fmt)
727                         fmt = USER_LIST_DFLT_FMT;
728                 atoms = user_list_atoms;
729                 break;
730         case select_mode_arg_user_summary:
731                 if (!fmt)
732                         fmt = USER_SUMMARY_DFLT_FMT;
733                 atoms = user_summary_atoms;
734                 break;
735         default:
736                 ERROR_LOG("bad select mode\n");
737                 return -ERRNO_TO_ERROR(EINVAL);
738         };
739         INFO_LOG("format string: %s\n", fmt);
740         return parse_format_string(fmt, atoms, fi);
741 }
742
743 /* return: < 0: error, >0: OK, == 0: help given */
744 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
745                 struct uid_range **admissible_uids, struct format_info **fi)
746 {
747         int ret, num_uid_ranges;
748         const char **line;
749         char *fmt = NULL;
750
751         if (string) {
752                 int argc;
753                 char **argv;
754
755                 ret = create_argv(string, &argv);
756                 if (ret < 0)
757                         return ret;
758                 argc = ret;
759                 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
760                 free_argv(argv);
761                 if (ret)
762                         return -E_SYNTAX;
763                 if (select_conf.help_given || select_conf.detailed_help_given)
764                         goto help;
765                 fmt = select_conf.format_arg;
766         }
767         ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
768         if (ret < 0)
769                 return ret;
770         num_uid_ranges = ret;
771         ret = append_users(select_conf.user_arg, select_conf.user_given,
772                 admissible_uids, num_uid_ranges);
773         if (ret < 0)
774                 return ret;
775         return setup_format_string(fmt, fi);
776 help:
777         line = select_conf.detailed_help_given?
778                 select_args_info_detailed_help : select_args_info_help;
779         if (!output_file)
780                 output_file = stdout;
781         for (; *line; line++) {
782                 ret = output("%s\n", *line);
783                 if (ret < 0)
784                         return ret;
785         }
786         return 0;
787 }
788
789 int com_select(void)
790 {
791         struct uid_range *admissible_uids = NULL;
792         int ret;
793         struct format_info *fi;
794         struct select_cmdline_parser_params params = {
795                 .override = 1,
796                 .initialize = 1,
797                 .check_required = 1,
798                 .check_ambiguity = 1,
799                 .print_errors = 1
800         };
801
802         select_cmdline_parser_init(&select_conf);
803         ret = parse_select_options(conf.select_options_arg, &params,
804                 &admissible_uids, &fi);
805         if (ret <= 0) /* do not run query if help was given */
806                 return ret;
807         ret = run_select_query(admissible_uids, fi);
808         free_format_info(fi);
809         return ret;
810 }