]>
diplodocus.org Git - nmh/blob - sbr/error.c
5dc84e87a438ca15b8570fa64fb6a3d4ff208605
3 * error.c -- main error handling routines
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <sys/types.h>
16 /* advise calls advertise() with no tail to print fmt, and perhaps what,
19 advise (const char *what
, const char *fmt
, ...)
24 advertise (what
, NULL
, fmt
, ap
);
29 /* adios calls advertise() with no tail to print fmt, and perhaps what,
30 * to stderr, and "ends" the program with an error exit status. The
31 * route to exit is via the done function pointer and may not be
33 * FIXME: Document if this function can ever return. If not, perhaps an
34 * abort(3) at the end of the routine would make that more clear. */
36 adios (const char *what
, const char *fmt
, ...)
41 advertise (what
, NULL
, fmt
, ap
);
47 /* admonish calls advertise() with a tail indicating the program
50 admonish (char *what
, char *fmt
, ...)
55 advertise (what
, "continuing...", fmt
, ap
);
60 /* advertise prints fmt and ap to stderr after flushing stdout.
61 * If invo_name isn't NULL or empty, it precedes the output seperated by ": ".
62 * If what isn't NULL, errno as a string is appended.
63 * A non-empty what separates fmt from errno, surrounded by " " and ": ".
64 * BUG: No space separator before errno if what is "".
65 * If tail isn't NULL or empty then ", " and tail are appended
66 * before the finishing "\n".
67 * In summary: "invo_name: fmt what: errno, tail\n". */
69 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
72 char buffer
[BUFSIZ
], errbuf
[BUFSIZ
], *err
;
73 struct iovec iob
[20], *iov
;
77 #define APPEND_IOV(p, len) \
78 iov->iov_base = (p); \
79 iov->iov_len = (len); \
82 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
83 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
85 if (invo_name
&& *invo_name
) {
90 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
95 ADD_VAR((void *)what
);
98 err
= strerror(eindex
);
100 /* this shouldn't happen, but we'll test for it just in case */
101 snprintf(errbuf
, sizeof(errbuf
), "Error %d", eindex
);
117 if (writev (fileno (stderr
), iob
, iov
- iob
) < 0) {
118 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
119 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
120 if (write(2, buffer
, strlen(buffer
)) == -1) {
121 /* Ignore. if-statement needed to shut up compiler. */