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