view toys/dmesg.c @ 233:d4176f3f3835

Zap toys/ and instead create generated/ from contents of toys/*.c. Move relevant info into comment at the top of each toys/*.c. Also convert more of Makefile into a thin wrapper around shell scripts that actually do the work. (Makefile is only still there for the user interface.)
author Rob Landley <>
date Sat, 19 Jan 2008 17:08:39 -0600
parents 30a6db5a95c2
children 163498bf547b
line wrap: on
line source

/* vi: set sw=4 ts=4:
 * dmesg.c - display/control kernel ring buffer.
 * Copyright 2006, 2007 Rob Landley <>
 * Not in SUSv3.

config DMESG
	bool "dmesg"
	default y
	  usage: dmesg [-n level] [-s bufsize] | -c

	  Print or control the kernel ring buffer.

	  -n	Set kernel logging level (1-9).
	  -s	Size of buffer to read (in bytes), default 16384.
	  -c	Clear the ring buffer after printing.

#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))
	} 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);