Skip to content

Commit b875036

Browse files
trastgitster
authored andcommitted
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>
1 parent 26c117d commit b875036

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ extern char *gitbasename(char *);
162162
/* General helper functions */
163163
extern void usage(const char *err) NORETURN;
164164
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)));
165166
extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
166167
extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
167168

usage.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ void die(const char *err, ...)
6060
va_end(params);
6161
}
6262

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+
6375
int error(const char *err, ...)
6476
{
6577
va_list params;

0 commit comments

Comments
 (0)