2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
9 void vreportf(const char *prefix, const char *err, va_list params)
12 vsnprintf(msg, sizeof(msg), err, params);
13 fprintf(stderr, "%s%s\n", prefix, msg);
16 void vwritef(int fd, const char *prefix, const char *err, va_list params)
19 int len = vsnprintf(msg, sizeof(msg), err, params);
20 if (len > sizeof(msg))
23 write_in_full(fd, prefix, strlen(prefix));
24 write_in_full(fd, msg, len);
25 write_in_full(fd, "\n", 1);
28 static NORETURN void usage_builtin(const char *err, va_list params)
30 vreportf("usage: ", err, params);
34 static NORETURN void die_builtin(const char *err, va_list params)
36 vreportf("fatal: ", err, params);
40 static void error_builtin(const char *err, va_list params)
42 vreportf("error: ", err, params);
45 static void warn_builtin(const char *warn, va_list params)
47 vreportf("warning: ", warn, params);
50 /* If we are in a dlopen()ed .so write to a global variable would segfault
51 * (ugh), so keep things static. */
52 static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
53 static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
54 static void (*error_routine)(const char *err, va_list params) = error_builtin;
55 static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
57 void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
59 die_routine = routine;
62 void set_error_routine(void (*routine)(const char *err, va_list params))
64 error_routine = routine;
67 void NORETURN usagef(const char *err, ...)
71 va_start(params, err);
72 usage_routine(err, params);
76 void NORETURN usage(const char *err)
81 void NORETURN die(const char *err, ...)
85 va_start(params, err);
86 die_routine(err, params);
90 void NORETURN die_errno(const char *fmt, ...)
93 char fmt_with_err[1024];
94 char str_error[256], *err;
97 err = strerror(errno);
98 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
99 if ((str_error[j++] = err[i++]) != '%')
101 if (j < sizeof(str_error) - 1) {
102 str_error[j++] = '%';
104 /* No room to double the '%', so we overwrite it with
111 snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
113 va_start(params, fmt);
114 die_routine(fmt_with_err, params);
118 int error(const char *err, ...)
122 va_start(params, err);
123 error_routine(err, params);
128 void warning(const char *warn, ...)
132 va_start(params, warn);
133 warn_routine(warn, params);