Optimize dmesg(1) output on Linux

This commit is contained in:
sin 2013-08-12 11:26:34 +01:00
parent 6e4a0da527
commit b8d1f83437
1 changed files with 6 additions and 2 deletions

View File

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