- len = (flags & 2) ? bb_xgetlarg(size, 10, 2, INT_MAX) : 16384;
- buf = xmalloc(len);
- if (0 > (len = klogctl(3 + (flags & 1), buf, len)))
- bb_perror_msg_and_die("klogctl");
- write(1,buf,len);
- if (len && buf[len-1]!='\n') putchar('\n');
+ if (!(opts & OPT_s))
+ len = klogctl(10, NULL, 0); /* read ring buffer size */
+ if (len < 16*1024)
+ len = 16*1024;
+ if (len > 16*1024*1024)
+ len = 16*1024*1024;
+
+ buf = xmalloc(len);
+ len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
+ if (len < 0)
+ bb_perror_msg_and_die("klogctl");
+ if (len == 0)
+ return EXIT_SUCCESS;
+
+
+ if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) {
+ int last = '\n';
+ int in = 0;
+
+ /* Skip <[0-9]+> at the start of lines */
+ while (1) {
+ if (last == '\n' && buf[in] == '<') {
+ while (buf[in++] != '>' && in < len)
+ ;
+ } else {
+ last = buf[in++];
+ putchar(last);
+ }
+ if (in >= len)
+ break;
+ }
+ /* Make sure we end with a newline */
+ if (last != '\n')
+ bb_putchar('\n');
+ } else {
+ full_write(STDOUT_FILENO, buf, len);
+ if (buf[len-1] != '\n')
+ bb_putchar('\n');