modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
[oweals/busybox.git] / dmesg.c
diff --git a/dmesg.c b/dmesg.c
index 20846f7c657098ef3bb5335d50f8b7966682fb7c..99d053a72640e5fe887eb31fe17cae699ec5af7a 100644 (file)
--- a/dmesg.c
+++ b/dmesg.c
@@ -16,7 +16,6 @@
  */
 
 #include "internal.h"
-#include <linux/unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -33,62 +32,39 @@ static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
 # include <sys/klog.h>
 #endif
 
-static const char dmesg_usage[] = "dmesg [-c] [-n LEVEL] [-s SIZE]\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       "\nPrints or controls the kernel ring buffer\n\n"
-       "Options:\n"
-       "\t-c\t\tClears the ring buffer's contents after printing\n"
-       "\t-n LEVEL\tSets console logging level\n"
-       "\t-s SIZE\t\tUse a buffer of size SIZE\n"
-#endif
-       ;
-
 int dmesg_main(int argc, char **argv)
 {
        char *buf;
+       int c;
        int bufsize = 8196;
        int i;
        int n;
        int level = 0;
        int lastc;
        int cmd = 3;
-       int stopDoingThat;
 
-       argc--;
-       argv++;
-
-       /* Parse any options */
-       while (argc && **argv == '-') {
-               stopDoingThat = FALSE;
-               while (stopDoingThat == FALSE && *++(*argv)) {
-                       switch (**argv) {
-                       case 'c':
-                               cmd = 4;
-                               break;
-                       case 'n':
-                               cmd = 8;
-                               if (--argc == 0)
-                                       goto end;
-                               level = atoi(*(++argv));
-                               if (--argc > 0)
-                                       ++argv;
-                               stopDoingThat = TRUE;
-                               break;
-                       case 's':
-                               if (--argc == 0)
-                                       goto end;
-                               bufsize = atoi(*(++argv));
-                               if (--argc > 0)
-                                       ++argv;
-                               stopDoingThat = TRUE;
-                               break;
-                       default:
-                               goto end;
-                       }
+       while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
+               switch (c) {
+               case 'c':
+                       cmd = 4;
+                       break;
+               case 'n':
+                       cmd = 8;
+                       if (optarg == NULL)
+                               usage(dmesg_usage);
+                       level = atoi(optarg);
+                       break;
+               case 's':
+                       if (optarg == NULL)
+                               usage(dmesg_usage);
+                       bufsize = atoi(optarg);
+                       break;
+               default:
+                       usage(dmesg_usage);
                }
-       }
+       }                       
 
-       if (argc > 1) {
+       if (optind < argc) {
                goto end;
        }