@matheusbsbs / matheus-bagliano / commits / b875036e5a
Fork 0

Introduce die_errno() that appends strerror(errno) to die()

There are many calls to die() that do, or should, report strerror(errno) to indicate how the syscall they guard failed. Introduce a small helper function for this case. Note: - POSIX says vsnprintf can modify errno in some unlikely cases, so we have to use errno early. - We take some care to pass the original format to die_routine(), in case someone wants to call die_errno() with custom format characters. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Jun 27, 2009 at 17:58 UTC b875036e5a2ab569a2123abe9ebfe25258227951
2 files changed +13
git-compat-util.h
+1
@@ -162,6 +162,7 @@ extern char *gitbasename(char *);
162 /* General helper functions */
163 extern void usage(const char *err) NORETURN;
164 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
165 +extern void die_errno(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
166 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
167 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
168
usage.c
+12
@@ -60,6 +60,18 @@ void die(const char *err, ...)
60 va_end(params);
61 }
62
63 +void die_errno(const char *fmt, ...)
64 +{
65 + va_list params;
66 + char fmt_with_err[1024];
67 +
68 + snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, strerror(errno));
69 +
70 + va_start(params, fmt);
71 + die_routine(fmt_with_err, params);
72 + va_end(params);
73 +}
74 +
75 int error(const char *err, ...)
76 {
77 va_list params;