Skip to content

Commit

Permalink
improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
victorvde committed Jun 3, 2015
1 parent 2282dba commit 223a680
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
10 changes: 6 additions & 4 deletions jpeg2png.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ int main(int argc, char *argv[]) {
die("usage: jpeg2png in.jpg out.png");
}

FILE *in = fopen(argv[1], "rb");
if(!in) { die(NULL); }
FILE *out = fopen(argv[2], "wb");
if(!out) { die(NULL); }
const char *infilename = argv[1];
const char *outfilename = argv[2];
FILE *in = fopen(infilename, "rb");
if(!in) { die_perror("could not open input file `%s`", infilename); }
FILE *out = fopen(outfilename, "wb");
if(!out) { die_perror("could not open output file `%s`", outfilename); }

struct jpeg jpeg;
read_jpeg(in, &jpeg);
Expand Down
27 changes: 17 additions & 10 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
#include "utils.h"

noreturn void die(const char *msg, ...) {
if(msg) {
fprintf(stderr, "jpeg2png: ");
va_list l;
va_start(l, msg);
vfprintf(stderr, msg, l);
va_end(l);
fprintf(stderr, "\n");
} else {
perror("jpeg2png");
}
fprintf(stderr, "jpeg2png: ");
va_list l;
va_start(l, msg);
vfprintf(stderr, msg, l);
va_end(l);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}

noreturn void die_perror(const char *msg, ...) {
fprintf(stderr, "jpeg2png: ");
va_list l;
va_start(l, msg);
vfprintf(stderr, msg, l);
va_end(l);
fprintf(stderr, ": ");
perror(NULL);
exit(EXIT_FAILURE);
}

Expand Down
1 change: 1 addition & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <math.h>

noreturn void die(const char *msg, ...);
noreturn void die_perror(const char *msg, ...);
clock_t start_timer(const char *name);
void stop_timer(clock_t t, const char *n);

Expand Down

0 comments on commit 223a680

Please sign in to comment.