星海's Blog

老头初学编程
APUE学习笔记(一)

APUE中,error.c几个错误处理函数的不同

星海 posted @ 2011年4月01日 17:26 in APUE && UNP , 2993 阅读
#include "apue.h"
#include <errno.h>		/* for definition of errno */
#include <stdarg.h>		/* ISO C variable aruments */

static void	err_doit(int, int, const char *, va_list);
/*
   err_ret非致命错误输出
   与err_sys相比,没有exit(),并不退出函数。
   相同的是都调用stderror(errno)打印详细出错信息。
   可变参数列表中多为vsnprintf等打印参数的参数列表。
 */
void
err_ret(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	err_doit(1, errno, fmt, ap);
	va_end(ap);
}

/*
 * 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(1, errno, fmt, ap);
	va_end(ap);
        exit(1);                /* 退出 */
}

/*
 * Fatal error unrelated to a system call.
 * Error code passed as explict parameter.
 * Print a message and terminate.
 * 不调用系统打印出错信息。
 */
void
err_exit(int error, const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	err_doit(1, error, fmt, ap);
	va_end(ap);
	exit(1);
}

/*
 * 不调用系统出错信息,但退出程序
 * Fatal error related to a system call.
 * Print a message, dump core, and terminate.
 */
void
err_dump(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	err_doit(1, errno, fmt, ap);
	va_end(ap);
	abort();		/* dump core and terminate */
	exit(1);		/* shouldn't get here */
}

/*
 * Nonfatal error unrelated to a system call.
 * Print a message and return.
 */
void
err_msg(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	err_doit(0, 0, fmt, ap);
	va_end(ap);
}

/*
 * 不调用系统出错信息,但退出程序
 * Print a message and terminate.
 */
void
err_quit(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	err_doit(0, 0, fmt, ap);
	va_end(ap);
	exit(1);
}

/*
 * Print a message and return to caller.
 * Caller specifies "errnoflag".
 */
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
	char	buf[MAXLINE];

	vsnprintf(buf, MAXLINE, fmt, ap);       /* vsnprintf与snprintf区别在于vsnprintf调用可变参数列表 */
	if (errnoflag)
                                /* 在buf后追加系统出错信息 */
		snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
		  strerror(error));
	strcat(buf, "\n");
	fflush(stdout);		/* in case stdout and stderr are the same */
	fputs(buf, stderr);
	fflush(NULL);		/* flushes all stdio output streams */
}

 

Avatar_small
小白菜 说:
2012年2月21日 21:33

大侠,这个字体颜色很不习惯额,而且字体也太小了吧,眼睛都看痛了。。。

Avatar_small
星海 说:
2012年2月23日 10:26

A。-____-
我的浏览器设置的最小字号大点。。。。所以没有发觉这个问题。

ctrl+“+" 放大页面
或者设置最小字号吧。。。。

改天我再真正的改下网页最小字体 -___-。。
尽量使用非IE浏览器。。。。。。-__-

Avatar_small
星海 说:
2012年2月23日 20:58

字体都改大了,你看合适吗。。。。。

风格是VIM的DJANGO风格,我的VIM也是DJANGO风格上修改的,就不改了。。 ^___^

Avatar_small
epass status 说:
2021年11月16日 23:06

To check the status, go to the web site. When you click, the home page will appear. On the main page, you'll see an option epass status for E Pass status, which you must pick. You'll be asked for your reference number after you've clicked. Once you've completed your E pass, it will open.

Avatar_small
seo service london 说:
2024年1月14日 19:01

I came to your website for the first time I like your website very much such nice information providing much such


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter