关于err_sys未定义错误解决方法

    1. err_sys以及err_quit等函数不是C语言自带函数,是作者自己编写的函数。所以,想要运行书中的源代码,就必须自建一个头文件my_err.h把作者的代码拷贝进去,然后在程序中加载,可以和apue.h放在一个路径下,然后在代码中加上#include "error.h"。
    2. 下面是error.h的内容。



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

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

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

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


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

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


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

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


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

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


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

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


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

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


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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值