/src/e2fsprogs/lib/et/com_err.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1987, 1988 by MIT Student Information Processing Board. |
3 | | * |
4 | | * Permission to use, copy, modify, and distribute this software and |
5 | | * its documentation for any purpose is hereby granted, provided that |
6 | | * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in |
7 | | * advertising or publicity pertaining to distribution of the software |
8 | | * without specific, written prior permission. M.I.T. and the |
9 | | * M.I.T. S.I.P.B. make no representations about the suitability of |
10 | | * this software for any purpose. It is provided "as is" without |
11 | | * express or implied warranty. |
12 | | */ |
13 | | |
14 | | #include "config.h" |
15 | | #include <stdio.h> |
16 | | #ifdef HAVE_TERMIOS_H |
17 | | #include <termios.h> |
18 | | #endif |
19 | | #ifdef HAVE_UNISTD_H |
20 | | #include <unistd.h> |
21 | | #endif |
22 | | #include "com_err.h" |
23 | | #include "error_table.h" |
24 | | #include "internal.h" |
25 | | |
26 | | static void |
27 | | default_com_err_proc (const char *whoami, errcode_t code, const |
28 | | char *fmt, va_list args) |
29 | | COM_ERR_ATTR((format(printf, 3, 0))); |
30 | | |
31 | | static void |
32 | | default_com_err_proc (const char *whoami, errcode_t code, const |
33 | | char *fmt, va_list args) |
34 | 65.3k | { |
35 | 65.3k | int do_cr = 1, fd = fileno(stderr); |
36 | | |
37 | 65.3k | if (whoami) { |
38 | 0 | fputs(whoami, stderr); |
39 | 0 | fputs(": ", stderr); |
40 | 0 | } |
41 | 65.3k | if (code) { |
42 | 65.3k | fputs(error_message(code), stderr); |
43 | 65.3k | fputs(" ", stderr); |
44 | 65.3k | } |
45 | 65.3k | if (fmt) { |
46 | 65.3k | vfprintf (stderr, fmt, args); |
47 | 65.3k | } |
48 | 65.3k | if (!isatty(fd)) |
49 | 65.3k | do_cr = 0; |
50 | 0 | #ifdef HAVE_TERMIOS_H |
51 | 0 | else { |
52 | 0 | struct termios t; |
53 | |
|
54 | 0 | if ((tcgetattr(fd, &t)) == 0 && |
55 | 0 | (t.c_oflag & OPOST) && (t.c_oflag & ONLCR)) |
56 | 0 | do_cr = 0; |
57 | 0 | } |
58 | 65.3k | #endif |
59 | 65.3k | if (do_cr) |
60 | 0 | fputc('\r', stderr); |
61 | 65.3k | fputc('\n', stderr); |
62 | 65.3k | fflush(stderr); |
63 | 65.3k | } |
64 | | |
65 | | typedef void (*errf) (const char *, errcode_t, const char *, va_list) |
66 | | COM_ERR_ATTR((format(printf, 3, 0))); |
67 | | |
68 | | errf com_err_hook = default_com_err_proc; |
69 | | |
70 | | void com_err_va (const char *whoami, errcode_t code, const char *fmt, |
71 | | va_list args) |
72 | 65.3k | { |
73 | 65.3k | (*com_err_hook) (whoami, code, fmt, args); |
74 | 65.3k | } |
75 | | |
76 | | void com_err (const char *whoami, |
77 | | errcode_t code, |
78 | | const char *fmt, ...) |
79 | 65.3k | { |
80 | 65.3k | va_list pvar; |
81 | | |
82 | 65.3k | if (!com_err_hook) |
83 | 0 | com_err_hook = default_com_err_proc; |
84 | 65.3k | va_start(pvar, fmt); |
85 | 65.3k | com_err_va (whoami, code, fmt, pvar); |
86 | 65.3k | va_end(pvar); |
87 | 65.3k | } |
88 | | |
89 | | errf set_com_err_hook(errf new_proc) |
90 | 0 | { |
91 | 0 | errf x = com_err_hook; |
92 | |
|
93 | 0 | if (new_proc) |
94 | 0 | com_err_hook = new_proc; |
95 | 0 | else |
96 | 0 | com_err_hook = default_com_err_proc; |
97 | |
|
98 | 0 | return x; |
99 | 0 | } |
100 | | |
101 | 0 | errf reset_com_err_hook(void) { |
102 | 0 | errf x = com_err_hook; |
103 | 0 | com_err_hook = default_com_err_proc; |
104 | 0 | return x; |
105 | 0 | } |