Optimize dmesg(1) output on Linux

This commit is contained in:
sin 2013-08-12 11:26:34 +01:00
parent 6e4a0da527
commit b8d1f83437

View File

@ -2,6 +2,7 @@
#include <sys/klog.h> #include <sys/klog.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
enum { enum {
SYSLOG_ACTION_READ_ALL = 3, SYSLOG_ACTION_READ_ALL = 3,
@ -24,20 +25,23 @@ int
dmesg_show(int fd, const void *buf, size_t n) dmesg_show(int fd, const void *buf, size_t n)
{ {
int last = '\n'; int last = '\n';
char newbuf[n], *q = newbuf;
const char *p = buf; const char *p = buf;
size_t i; size_t i;
memset(newbuf, 0, n);
for (i = 0; i < n; ) { for (i = 0; i < n; ) {
if (last == '\n' && p[i] == '<') { if (last == '\n' && p[i] == '<') {
i += 2; i += 2;
if (i + 1 < n && p[i + 1] == '>') if (i + 1 < n && p[i + 1] == '>')
i++; i++;
} else { } else {
if (write(fd, &p[i], 1) != 1) *q++ = p[i];
return -1;
} }
last = p[i++]; last = p[i++];
} }
if (write(fd, newbuf, n) != n)
return -1;
if (last != '\n') if (last != '\n')
if (write(fd, "\n", 1) != 1) if (write(fd, "\n", 1) != 1)
return -1; return -1;