- shrink dmesg a bit.
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Wed, 29 Mar 2006 17:26:14 +0000 (17:26 -0000)
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Wed, 29 Mar 2006 17:26:14 +0000 (17:26 -0000)
  http://busybox.net/lists/busybox/2006-March/019477.html

util-linux/dmesg.c

index 2ca882714cd4cde9ac5f3f31735f62804d7c8dac..2166edba33defa4e322cf646fb9128dbe5ea9c2f 100644 (file)
@@ -20,7 +20,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <getopt.h>
 #include <errno.h>
 #include <sys/klog.h>
 
 
 int dmesg_main(int argc, char **argv)
 {
-       char *buf
-#ifdef CONFIG_FEATURE_CLEAN_UP
-               = NULL
-#endif
-               ;
+       char *buf, *tmp;
        int bufsize = 8196;
-       int i, n;
-       int level = 0;
-       int lastc;
-       int cmd = 3;
+       int i, n = 0;
+       int c = 3;
 
-       while ((i = getopt(argc, argv, "cn:s:")) > 0) {
-               switch (i) {
-                       case 'c':
-                               cmd = 4;
-                               break;
-                       case 'n':
-                               cmd = 8;
-                               level = bb_xgetlarg(optarg, 10, 0, 10);
-                               break;
-                       case 's':
+       i = bb_getopt_ulflags(argc, argv, "cn:s:", &buf, &tmp);
+       if (i & 1)
+                               c = 4;
+       if (i & 2) {
+                               c = 8;
+                               n = bb_xgetlarg(buf, 10, 0, 10);
+       }
+       if (i & 4)
                                /* I think a 512k max kernel ring buffer is big enough for
                                 * anybody, as the default is 16k...  Could be wrong though.
                                 * If so I'm sure I'll hear about it by the enraged masses*/
-                               bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024);
-                               break;
-                       default:
-                               bb_show_usage();
-               }
-       }
+                               bufsize = bb_xgetlarg(tmp, 10, 4096, 512*1024);
 
-       if (optind < argc) {
-               bb_show_usage();
-       }
-
-       if (cmd == 8) {
-               if (klogctl(cmd, NULL, level) < 0)
+       if (c == 8) {
+               if (klogctl(c, NULL, n) < 0)
                        goto die_the_death;
                goto all_done;
        }
 
        buf = xmalloc(bufsize);
-       if ((n = klogctl(cmd, buf, bufsize)) < 0)
+       if ((n = klogctl(c, buf, bufsize)) < 0)
                goto die_the_death;
 
-       lastc = '\n';
+       c = '\n';
        for (i = 0; i < n; i++) {
-               if (lastc == '\n' && buf[i] == '<') {
+               if (c == '\n' && buf[i] == '<') {
                        i++;
                        while (buf[i] >= '0' && buf[i] <= '9')
                                i++;
                        if (buf[i] == '>')
                                i++;
                }
-               lastc = buf[i];
-               putchar(lastc);
+               c = buf[i];
+               putchar(c);
        }
-       if (lastc != '\n')
+       if (c != '\n')
                putchar('\n');
 all_done:
-#ifdef CONFIG_FEATURE_CLEAN_UP
-       if (buf) {
+       if (ENABLE_FEATURE_CLEAN_UP) {
                free(buf);
        }
-#endif
+
        return EXIT_SUCCESS;
 die_the_death:
        bb_perror_nomsg_and_die();