feat: handle Ctrl+C gracefully, cleaning up partial output

This commit is contained in:
2026-09-15 14:06:23 +02:00
parent e72df334ef
commit cea82dfe50
2 changed files with 42 additions and 1 deletions
+23 -1
View File
@@ -5,6 +5,7 @@
#include "fpk.h"
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -102,6 +103,13 @@ static void format_duration(double secs, char *buf, size_t bufsize)
#define FUNPACK_VERBOSE (1u << 3)
static unsigned char flags;
static volatile sig_atomic_t interrupted;
static void handle_sigint(int sig)
{
(void)sig;
interrupted = 1;
}
static struct option long_opts[] = {
{"dir", required_argument, NULL, 'd'},
@@ -340,10 +348,17 @@ int main(int argc, char **argv)
return 1;
}
signal(SIGINT, handle_sigint);
for (i = 0; i < count; i++) {
const fpk_entry_t *entry = fpk_entry_at(pak, i);
size_t size;
void *data = fpk_read(pak, entry, &size);
void *data;
if (interrupted)
break;
data = fpk_read(pak, entry, &size);
if (!data) {
fprintf(stderr, "funpack: cannot read: %s\n", entry->path);
@@ -377,6 +392,13 @@ int main(int argc, char **argv)
if ((flags & FUNPACK_PROGRESS) && count > 0)
printf("\n");
if (interrupted) {
fprintf(stderr, "funpack: interrupted, %lu of %lu entries extracted\n",
(unsigned long)i, (unsigned long)count);
free(out_dir);
return 130;
}
if (!(flags & FUNPACK_QUIET)) {
char dur[64];