view toys/dmesg.c @ 190:e864c5ed1d25

Netcat needs this to build.
author Rob Landley <rob@landley.net>
date Mon, 03 Dec 2007 18:57:57 -0600
parents 25447caf1b4b
children 30a6db5a95c2
line wrap: on
line source

/* vi: set sw=4 ts=4: */
/*
 * dmesg.c - display/control kernel ring buffer.
 *
 * Copyright 2006 Rob Landley <rob@landley.net>
 */

#include "toys.h"
#include <sys/klog.h>

#define TT toy.dmesg

void dmesg_main(void)
{
	// For -n just tell kernel to which messages to keep.
	if (toys.optflags & 2) {
		if (klogctl(8, NULL, TT.level))
			error_exit("klogctl");
	} else {
		int size, i, last = '\n';
		char *data;

		// Figure out how much data we need, and fetch it.
		size = TT.size;
		if (size<2) size = 16384;
		data = xmalloc(size);
		size = klogctl(3 + (toys.optflags&1), data, size);
		if (size < 0) error_exit("klogctl");

		// Display data, filtering out level markers.
		for (i=0; i<size; ) {
			if (last=='\n' && data[i]=='<') i += 3;
			else xputc(last = data[i++]);
		}
		if (last!='\n') xputc('\n');
		if (CFG_TOYBOX_FREE) free(data);
	}
}