Skip to content

Commit 58e4e51

Browse files
pcloudsgitster
authored andcommitted
usage.c: move format processing out of die_errno()
fmt_with_err() will be shared with the coming error_errno() and warning_errno(). Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 90f7b16 commit 58e4e51

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

usage.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,11 @@ void NORETURN die(const char *err, ...)
109109
va_end(params);
110110
}
111111

112-
void NORETURN die_errno(const char *fmt, ...)
112+
static const char *fmt_with_err(char *buf, int n, const char *fmt)
113113
{
114-
va_list params;
115-
char fmt_with_err[1024];
116114
char str_error[256], *err;
117115
int i, j;
118116

119-
if (die_is_recursing()) {
120-
fputs("fatal: recursion detected in die_errno handler\n",
121-
stderr);
122-
exit(128);
123-
}
124-
125117
err = strerror(errno);
126118
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
127119
if ((str_error[j++] = err[i++]) != '%')
@@ -136,10 +128,23 @@ void NORETURN die_errno(const char *fmt, ...)
136128
}
137129
}
138130
str_error[j] = 0;
139-
snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
131+
snprintf(buf, n, "%s: %s", fmt, str_error);
132+
return buf;
133+
}
134+
135+
void NORETURN die_errno(const char *fmt, ...)
136+
{
137+
char buf[1024];
138+
va_list params;
139+
140+
if (die_is_recursing()) {
141+
fputs("fatal: recursion detected in die_errno handler\n",
142+
stderr);
143+
exit(128);
144+
}
140145

141146
va_start(params, fmt);
142-
die_routine(fmt_with_err, params);
147+
die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
143148
va_end(params);
144149
}
145150

0 commit comments

Comments
 (0)