From 2de381f919829aec1e35d6c7cc33519295dcd053 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 13 Apr 2005 02:28:48 -0700 Subject: [PATCH] Consolidate the error handling Now there is error() for "library" errors and die() for fatal "application" errors. usage() is now used strictly only for usage errors. Signed-off-by: Petr Baudis --- read-cache.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'read-cache.c') diff --git a/read-cache.c b/read-cache.c index f1abae1442..d8b383fe6b 100644 --- a/read-cache.c +++ b/read-cache.c @@ -3,24 +3,47 @@ * * Copyright (C) Linus Torvalds, 2005 */ +#include #include "cache.h" const char *sha1_file_directory = NULL; struct cache_entry **active_cache = NULL; unsigned int active_nr = 0, active_alloc = 0; -void usage(const char *err, ...) +void usage(const char *err) { - va_list args; - char string[200]; + fprintf(stderr, "usage: %s\n", err); + exit(1); +} + +static void report(const char *prefix, const char *err, va_list params) +{ + fputs(prefix, stderr); + vfprintf(stderr, err, params); + fputs("\n", stderr); +} - va_start(args, err); - vsnprintf(string, sizeof(string), err, args); - va_end(args); - fprintf(stderr, "%s\n", string); +void die(const char *err, ...) +{ + va_list params; + + va_start(params, err); + report("fatal: ", err, params); + va_end(params); exit(1); } +int error(const char *err, ...) +{ + va_list params; + + va_start(params, err); + report("error: ", err, params); + va_end(params); + return -1; +} + + static unsigned hexval(char c) { if (c >= '0' && c <= '9') @@ -218,7 +241,6 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size) fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666); if (fd < 0) { void *map; - static int error(const char * string); if (errno != EEXIST) return -1; @@ -240,12 +262,6 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size) return 0; } -static int error(const char * string) -{ - fprintf(stderr, "error: %s\n", string); - return -1; -} - int cache_match_stat(struct cache_entry *ce, struct stat *st) { unsigned int changed = 0; -- cgit v1.2.3