Chinaunix首页 | 论坛 | 博客
  • 博客访问: 152583
  • 博文数量: 27
  • 博客积分: 710
  • 博客等级: 上士
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-03 20:08
文章分类

全部博文(27)

文章存档

2012年(1)

2011年(22)

2010年(4)

我的朋友

分类: LINUX

2011-09-27 12:59:31

  1.  err_sys以及err_quit等函数不是C语言自带函数,是作者自己编写的函数。所以,想要运行书中的源代码,就必须自建一个头文件my_err.h把作者的代码拷贝进去,然后在程序中加载。



  2. #include <errno.h> /* for definition of errno */
  3. #include <stdarg.h> /* ISO C variable aruments */

  4. static void err_doit(int, int, const char *, va_list);

  5. /*
  6.  * Nonfatal error related to a system call.
  7.  * Print a message and return.
  8.  */
  9. void
  10. err_ret(const char *fmt, ...)
  11. {
  12.     va_list ap;

  13.     va_start(ap, fmt);
  14.     err_doit(1, errno, fmt, ap);
  15.     va_end(ap);
  16. }


  17. /*
  18.  * Fatal error related to a system call.
  19.  * Print a message and terminate.
  20.  */
  21. void
  22. err_sys(const char *fmt, ...)
  23. {
  24.     va_list ap;

  25.     va_start(ap, fmt);
  26.     err_doit(1, errno, fmt, ap);
  27.     va_end(ap);
  28.     exit(1);
  29. }


  30. /*
  31.  * Fatal error unrelated to a system call.
  32.  * Error code passed as explict parameter.
  33.  * Print a message and terminate.
  34.  */
  35. void
  36. err_exit(int error, const char *fmt, ...)
  37. {
  38.     va_list ap;

  39.     va_start(ap, fmt);
  40.     err_doit(1, error, fmt, ap);
  41.     va_end(ap);
  42.     exit(1);
  43. }


  44. /*
  45.  * Fatal error related to a system call.
  46.  * Print a message, dump core, and terminate.
  47.  */
  48. void
  49. err_dump(const char *fmt, ...)
  50. {
  51.     va_list ap;

  52.     va_start(ap, fmt);
  53.     err_doit(1, errno, fmt, ap);
  54.     va_end(ap);
  55.     abort(); /* dump core and terminate */
  56.     exit(1); /* shouldn't get here */
  57. }


  58. /*
  59.  * Nonfatal error unrelated to a system call.
  60.  * Print a message and return.
  61.  */
  62. void
  63. err_msg(const char *fmt, ...)
  64. {
  65.     va_list ap;

  66.     va_start(ap, fmt);
  67.     err_doit(0, 0, fmt, ap);
  68.     va_end(ap);
  69. }


  70. /*
  71.  * Fatal error unrelated to a system call.
  72.  * Print a message and terminate.
  73.  */
  74. void
  75. err_quit(const char *fmt, ...)
  76. {
  77.     va_list ap;

  78.     va_start(ap, fmt);
  79.     err_doit(0, 0, fmt, ap);
  80.     va_end(ap);
  81.     exit(1);
  82. }


  83. /*
  84.  * Print a message and return to caller.
  85.  * Caller specifies "errnoflag".
  86.  */
  87. static void
  88. err_doit(int errnoflag, int error, const char *fmt, va_list ap)
  89. {
  90.     char buf[MAXLINE];
  91.    vsnprintf(buf, MAXLINE, fmt, ap);
  92.    if (errnoflag)
  93.        snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
  94.          strerror(error));
  95.    strcat(buf, "\n");
  96.    fflush(stdout); /* in case stdout and stderr are the same */
  97.    fputs(buf, stderr);
  98.    fflush(NULL); /* flushes all stdio output streams */
  99. }
阅读(11824) | 评论(0) | 转发(1) |
0

上一篇:moc播放器配置

下一篇:Linux wait函数

给主人留下些什么吧!~~