diff --git a/main.c b/main.c index 3474e90..f83c7b8 100644 --- a/main.c +++ b/main.c @@ -19,6 +19,8 @@ void signal_handler_sigint(const int _signal) { } int main(void) { + int ret = 0; + // register the SIGINT handler signal(SIGINT, signal_handler_sigint); @@ -46,14 +48,16 @@ int main(void) { const ssize_t read = getline(&line_buffer, &line_len, stat_file); if (read == -1) { 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; 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) { 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; @@ -73,8 +77,9 @@ int main(void) { fflush(out_file); } +exit_label: free(line_buffer); printf("Terminating...\n"); - return 0; + return ret; } \ No newline at end of file