From c73b06b03a3f43256fb1e071553af91fa6d6a825 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 17 Jul 2026 08:37:24 +0200 Subject: Fehlerbehandlung in den Beispielprogrammen verbessert --- src/simple.c | 13 +++++++++++-- src/stream.c | 20 ++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/simple.c b/src/simple.c index da79562..be92977 100644 --- a/src/simple.c +++ b/src/simple.c @@ -12,7 +12,7 @@ static void usage(void) { - (void) fprintf(stderr, "compress [-d] infile outfile\n"); + (void) fprintf(stderr, "simple [-d] infile outfile\n"); } static void @@ -142,10 +142,19 @@ main(int argc, char *argv[]) if ( argc != 2 ) { usage(); + return EXIT_FAILURE; + } + + FILE *in_file = fopen(argv[0], "rb"); + if ( in_file == NULL ) { + error("fopen() on in_file failed"); } - FILE *in_file = fopen(argv[0], "rb"); FILE *out_file = fopen(argv[1], "wb"); + if ( out_file == NULL ) { + (void) fclose(in_file); + error("fopen() on out_file failed"); + } if ( !decompress ) { do_compress(in_file, out_file, compress_level); diff --git a/src/stream.c b/src/stream.c index 286f41a..c6d2d35 100644 --- a/src/stream.c +++ b/src/stream.c @@ -15,7 +15,14 @@ enum { BUFFER_SIZE = 8192 }; static void usage(void) { - (void) fprintf(stderr, "compress [-d] infile outfile\n"); + (void) fprintf(stderr, "stream [-d] infile outfile\n"); +} + +static void +error(const char *msg) +{ + perror(msg); + exit(EXIT_FAILURE); } static void @@ -162,10 +169,19 @@ main(int argc, char *argv[]) if ( argc != 2 ) { usage(); + return EXIT_FAILURE; + } + + FILE *in_file = fopen(argv[0], "rb"); + if ( in_file == NULL ) { + error("fopen() on in_file failed"); } - FILE *in_file = fopen(argv[0], "rb"); FILE *out_file = fopen(argv[1], "wb"); + if ( out_file == NULL ) { + (void) fclose(in_file); + error("fopen() on out_file failed"); + } if ( !decompress ) { int error = do_compress(in_file, out_file, compress_level); -- cgit v1.3