- 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');
+ len = (flags & OPT_s) ? xatoul_range(size, 2, INT_MAX) : 16384;
+ buf = xmalloc(len);
+ len = klogctl(3 + (flags & OPT_c), buf, len);
+ if (len < 0)
+ bb_perror_msg_and_die("klogctl");
+ if (len == 0)
+ return EXIT_SUCCESS;
+
+ /* Skip <#> at the start of lines, and make sure we end with a newline. */
+
+ if (ENABLE_FEATURE_DMESG_PRETTY) {
+ int last = '\n';
+ int in = 0;
+
+ do {
+ if (last == '\n' && buf[in] == '<')
+ in += 3;
+ else {
+ last = buf[in++];
+ bb_putchar(last);
+ }
+ } while (in < len);
+ if (last != '\n')
+ bb_putchar('\n');
+ } else {
+ full_write(STDOUT_FILENO, buf, len);
+ if (buf[len-1] != '\n')
+ bb_putchar('\n');