The Pedigree Project 0.1
dmesg/main.c
1/* Copyright (c) 2026, Pedigree Developers. */
2#define _GNU_SOURCE
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7
8#include <sys/syscall.h>
9
10int main(int argc, char* argv[]) {
11 if (argc != 1) {
12 int help = argc == 2 && !strcmp(argv[1], "--help");
13 fprintf(help ? stdout : stderr, "Usage: dmesg\nPrint the kernel log.\n");
14 return help ? 0 : 1;
15 }
16 long capacity = syscall(SYS_syslog, 10, NULL, 0);
17 if (capacity < 0) {
18 perror("dmesg: log size");
19 return 1;
20 }
21 char* buffer = malloc(capacity ? (size_t)capacity : 1);
22 if (!buffer) {
23 perror("dmesg");
24 return 1;
25 }
26 long count = syscall(SYS_syslog, 3, buffer, capacity);
27 if (count < 0) {
28 perror("dmesg: read log");
29 free(buffer);
30 return 1;
31 }
32 int failed = fwrite(buffer, 1, (size_t)count, stdout) != (size_t)count;
33 free(buffer);
34 if (fflush(stdout) || ferror(stdout))
35 failed = 1;
36 if (failed)
37 perror("dmesg");
38 return failed;
39}