aboutsummaryrefslogtreecommitdiff
path: root/src/print_status_on_exit.c
blob: 5775afc13b50790db56edf9f4b6d9eb01eeacc6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

static void print_status(void)
{
	char buf[4096];

	FILE* status = fopen("/proc/self/status", "r");
	if (status == NULL)
	{
		perror("fopen status");
		exit(1);
	}

	FILE* output = fopen("status", "a");
	if (output == NULL)
	{
		perror("fopen output file");
		exit(1);
	}

	while (!feof(status))
	{
		fgets(&buf[0], 4096, status);
		fprintf(output, "%s", buf);
	}
	fclose(status);
}

static void __attribute__((constructor)) init()
{
	atexit(print_status);
}