ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/statgrab/statgrab.c
(Generate patch)

Comparing projects/libstatgrab/src/statgrab/statgrab.c (file contents):
Revision 1.8 by ats, Fri Aug 29 06:56:12 2003 UTC vs.
Revision 1.12 by ats, Mon Oct 20 22:18:21 2003 UTC

# Line 140 | Line 140 | void add_stat(stat_type type, void *stat, ...) {
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  
# Line 260 | Line 268 | void populate_fs() {
268          if (disk != NULL) {
269                  for (i = 0; i < n; i++) {
270                          /* FIXME it'd be nicer if libstatgrab did this */
271 <                        const char *name = disk[i].device_name,
272 <                                   *p = strrchr(name, '/');
273 <                        if (p != NULL)
274 <                                name = p + 1;
275 <                        if (*name == '\0')
276 <                                name = "root";
277 <        
271 >                        char *buf, *name, *p;
272 >                        const char *device = disk[i].device_name;
273 >
274 >                        if (strcmp(device, "/") == 0)
275 >                                device = "root";
276 >
277 >                        buf = strdup(device);
278 >                        if (buf == NULL)
279 >                                die("out of memory");
280 >
281 >                        name = buf;
282 >                        if (strncmp(name, "/dev/", 5) == 0)
283 >                                name += 5;
284 >                        while ((p = strchr(name, '/')) != NULL)
285 >                                *p = '_';
286 >
287                          add_stat(STRING, &disk[i].device_name,
288                                   "fs", name, "device_name", NULL);
289                          add_stat(STRING, &disk[i].fs_type,
# Line 285 | Line 302 | void populate_fs() {
302                                   "fs", name, "used_inodes", NULL);
303                          add_stat(LONG_LONG, &disk[i].free_inodes,
304                                   "fs", name, "free_inodes", NULL);
305 +
306 +                        free(buf);
307                  }
308          }
309   }
# Line 350 | Line 369 | void populate_page() {
369          if (page != NULL) {
370                  add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL);
371                  add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL);
372 <                add_stat(LONG_LONG, &page->systime, "page", "systime", NULL);
372 >                add_stat(TIME_T, &page->systime, "page", "systime", NULL);
373          }
374   }
375  
# Line 468 | Line 487 | void print_stats(int argc, char **argv) {
487          } else {
488                  /* Print selected stats. */
489                  for (i = optind; i < argc; i++) {
490 +                        char *name = argv[i];
491                          stat key;
492 <                        const stat *s;
492 >                        const stat *s, *end;
493 >                        int (*compare)(const void *, const void *);
494  
495 <                        key.name = argv[i];
495 >                        key.name = name;
496 >                        if (name[strlen(name) - 1] == '.')
497 >                                compare = stats_compare_prefix;
498 >                        else
499 >                                compare = stats_compare;
500 >
501                          s = (const stat *)bsearch(&key, stats, num_stats,
502 <                                                  sizeof *stats,
503 <                                                  stats_compare);
504 <                        if (s != NULL) {
502 >                                                  sizeof *stats, compare);
503 >                        if (s == NULL) {
504 >                                printf("Unknown stat %s\n", name);
505 >                                continue;
506 >                        }
507 >
508 >                        /* Find the range of stats the user wanted. */
509 >                        for (; s >= stats; s--) {
510 >                                if (compare(&key, s) != 0)
511 >                                        break;
512 >                        }
513 >                        s++;
514 >                        for (end = s; end < &stats[num_stats]; end++) {
515 >                                if (compare(&key, end) != 0)
516 >                                        break;
517 >                        }
518 >
519 >                        /* And print them. */
520 >                        for (; s < end; s++) {
521                                  print_stat(s);
522                          }
523                  }
# Line 484 | Line 526 | void print_stats(int argc, char **argv) {
526  
527   void usage() {
528          printf("Usage: statgrab [OPTION]... [STAT]...\n"
529 <               "Display system statistics (all statistics by default).\n"
529 >               "Display system statistics.\n"
530 >               "\n"
531 >               "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n"
532 >               "statistics starting with that prefix.\n"
533                 "\n");
534          printf("  -l         Linux sysctl-style output (default)\n"
535                 "  -b         BSD sysctl-style output\n"
# Line 557 | Line 602 | int main(int argc, char **argv) {
602                  use_diffs = 1;
603  
604          select_interesting(argc - optind, &argv[optind]);
605 +
606 +        /* We don't care if statgrab_init fails, because we can just display
607 +           the statistics that can be read as non-root. */
608 +        statgrab_init();
609 + #ifdef ALLBSD
610 +        if (setegid(getgid()) != 0)
611 +                die("Failed to lose effective group");
612 + #endif
613  
614          switch (repeat_mode) {
615          case REPEAT_NONE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines