140 |
|
++num_stats; |
141 |
|
} |
142 |
|
|
143 |
< |
/* Compare two stats by name, for sorting purposes. */ |
143 |
> |
/* Compare two stats by name for qsort and bsearch. */ |
144 |
|
int stats_compare(const void *a, const void *b) { |
145 |
|
return strcmp(((stat *)a)->name, ((stat *)b)->name); |
146 |
|
} |
147 |
|
|
148 |
+ |
/* Compare up to the length of the key for bsearch. */ |
149 |
+ |
int stats_compare_prefix(const void *key, const void *item) { |
150 |
+ |
const char *kn = ((stat *)key)->name; |
151 |
+ |
const char *in = ((stat *)item)->name; |
152 |
+ |
|
153 |
+ |
return strncmp(kn, in, strlen(kn)); |
154 |
+ |
} |
155 |
+ |
|
156 |
|
void populate_const() { |
157 |
|
static int zero = 0; |
158 |
|
|
476 |
|
} else { |
477 |
|
/* Print selected stats. */ |
478 |
|
for (i = optind; i < argc; i++) { |
479 |
+ |
char *name = argv[i]; |
480 |
|
stat key; |
481 |
< |
const stat *s; |
481 |
> |
const stat *s, *end; |
482 |
> |
int (*compare)(const void *, const void *); |
483 |
|
|
484 |
< |
key.name = argv[i]; |
484 |
> |
key.name = name; |
485 |
> |
if (name[strlen(name) - 1] == '.') |
486 |
> |
compare = stats_compare_prefix; |
487 |
> |
else |
488 |
> |
compare = stats_compare; |
489 |
> |
|
490 |
|
s = (const stat *)bsearch(&key, stats, num_stats, |
491 |
< |
sizeof *stats, |
492 |
< |
stats_compare); |
493 |
< |
if (s != NULL) { |
491 |
> |
sizeof *stats, compare); |
492 |
> |
if (s == NULL) { |
493 |
> |
printf("Unknown stat %s\n", name); |
494 |
> |
continue; |
495 |
> |
} |
496 |
> |
|
497 |
> |
/* Find the range of stats the user wanted. */ |
498 |
> |
for (; s >= stats; s--) { |
499 |
> |
if (compare(&key, s) != 0) |
500 |
> |
break; |
501 |
> |
} |
502 |
> |
s++; |
503 |
> |
for (end = s; end < &stats[num_stats]; end++) { |
504 |
> |
if (compare(&key, end) != 0) |
505 |
> |
break; |
506 |
> |
} |
507 |
> |
|
508 |
> |
/* And print them. */ |
509 |
> |
for (; s < end; s++) { |
510 |
|
print_stat(s); |
511 |
|
} |
512 |
|
} |
515 |
|
|
516 |
|
void usage() { |
517 |
|
printf("Usage: statgrab [OPTION]... [STAT]...\n" |
518 |
< |
"Display system statistics (all statistics by default).\n" |
518 |
> |
"Display system statistics.\n" |
519 |
> |
"\n" |
520 |
> |
"If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n" |
521 |
> |
"statistics starting with that prefix.\n" |
522 |
|
"\n"); |
523 |
|
printf(" -l Linux sysctl-style output (default)\n" |
524 |
|
" -b BSD sysctl-style output\n" |