changed error handling

This commit is contained in:
Linus Vogel 2026-03-15 21:34:04 +01:00
parent 1ec753a440
commit 79ec1dbdc4

11
main.c
View File

@ -19,6 +19,8 @@ void signal_handler_sigint(const int _signal) {
} }
int main(void) { int main(void) {
int ret = 0;
// register the SIGINT handler // register the SIGINT handler
signal(SIGINT, signal_handler_sigint); signal(SIGINT, signal_handler_sigint);
@ -46,14 +48,16 @@ int main(void) {
const ssize_t read = getline(&line_buffer, &line_len, stat_file); const ssize_t read = getline(&line_buffer, &line_len, stat_file);
if (read == -1) { if (read == -1) {
printf("Failed to read the stat file!\n"); printf("Failed to read the stat file!\n");
exit(1); ret = 1;
goto exit_label;
} }
uint64_t user, user_nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice; uint64_t user, user_nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
const int n_read = sscanf(line_buffer, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", &user, &user_nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice); // NOLINT(*-err34-c) const int n_read = sscanf(line_buffer, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", &user, &user_nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice); // NOLINT(*-err34-c)
if (n_read != 10) { if (n_read != 10) {
printf("Failed to read the stat file: Invalid format?\n"); printf("Failed to read the stat file: Invalid format?\n");
exit(1); ret = 1;
goto exit_label;
} }
const uint64_t current_total_jiffies_active = user + user_nice + system + irq + softirq + guest + guest_nice; const uint64_t current_total_jiffies_active = user + user_nice + system + irq + softirq + guest + guest_nice;
@ -73,8 +77,9 @@ int main(void) {
fflush(out_file); fflush(out_file);
} }
exit_label:
free(line_buffer); free(line_buffer);
printf("Terminating...\n"); printf("Terminating...\n");
return 0; return ret;
} }