#include <stdio.h> /* printf */
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
#include <string.h> /* strdup - duplicate a string */
#include <stdlib.h> /* exit(0) */
#include <errno.h>
#include <unistd.h> /* STDOUT_FILENO */
#define MAXLINE 4096
/* Print a message and return to caller.
* Caller specifies "errnoflag". */
static void err_doit(int errnoflag, const char *fmt, va_list ap) {
int errno_save;
char buf[MAXLINE];
errno_save = errno; /* value caller might want printed */
vsprintf(buf, fmt, ap);
if (errnoflag)
sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
return;
}
/* Fatal error related to a system call.
* Print a message and terminate. */
void err_sys(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
err_doit(0, fmt, ap);
va_end(ap);
exit(1);
}
int main ()
{
err_sys("===============================================================================\n");
return 0;
}
Result
err_doit(1, fmt, ap);
===============================================================================
: Success
err_doit(0, fmt, ap);
===============================================================================
Reference:
1. [OpenSource] ipband-0.8.1
0 意見:
張貼留言