UNIX环境高级编程中的apue.h和apueerror.h

本文详细介绍了在阅读《Unix环境高级编程》时,若遇到编译时找不到err_sys等错误提示的解决方案。通过引入apue.h和apueerror.h两个头文件,并在代码中正确包含它们,可以解决相关问题。文中还提供了关键函数定义和使用说明,帮助开发者顺利进行Unix编程。
相信很多一开始看《unix环境高级编程》的新手们按照书上的代码的话,会遇到编译时找不到err_sys等这类问题(因为只包含了apue.h)解决办法如下:
  1. /************** 
  2.  * 
  3.  *apueerror.h 
  4.  * 
  5.  *************/  
  6. #include <apue.h>  
  7. #include <stdio.h>  
  8. #include <errno.h> /* for definition of errno */  
  9.   
  10. #include <stdarg.h> /* ISO C variable aruments */  
  11.   
  12. static void err_doit(intintconst char *, va_list);  
  13. /* 
  14.  * Nonfatal error related to a system call. 
  15.  * Print a message and return. 
  16.  */  
  17. void err_ret(const char *fmt, ...)  
  18. {  
  19.     va_list ap;  
  20.   
  21.     va_start(ap, fmt);  
  22.     err_doit(1, errno, fmt, ap);  
  23.     va_end(ap);  
  24. }  
  25.   
  26.   
  27. /* 
  28.  * Fatal error related to a system call. 
  29.  * Print a message and terminate. 
  30.  */  
  31. void err_sys(const char *fmt, ...)  
  32. {  
  33.     va_list ap;  
  34.   
  35.     va_start(ap, fmt);  
  36.     err_doit(1, errno, fmt, ap);  
  37.     va_end(ap);  
  38.     exit(1);  
  39. }  
  40.   
  41.   
  42. /* 
  43.  * Fatal error unrelated to a system call. 
  44.  * Error code passed as explict parameter. 
  45.  * Print a message and terminate. 
  46.  */  
  47. void err_exit(int error, const char *fmt, ...)  
  48. {  
  49.     va_list ap;  
  50.   
  51.     va_start(ap, fmt);  
  52.     err_doit(1, error, fmt, ap);  
  53.     va_end(ap);  
  54.     exit(1);  
  55. }  
  56.   
  57.   
  58. /* 
  59.  * Fatal error related to a system call. 
  60.  * Print a message, dump core, and terminate. 
  61.  */  
  62. void err_dump(const char *fmt, ...)  
  63. {  
  64.     va_list ap;  
  65.   
  66.     va_start(ap, fmt);  
  67.     err_doit(1, errno, fmt, ap);  
  68.     va_end(ap);  
  69.     abort(); /* dump core and terminate */  
  70.     exit(1); /* shouldn't get here */  
  71. }  
  72.   
  73.   
  74. /* 
  75.  * Nonfatal error unrelated to a system call. 
  76.  * Print a message and return. 
  77.  */  
  78. void err_msg(const char *fmt, ...)  
  79. {  
  80.     va_list ap;  
  81.   
  82.     va_start(ap, fmt);  
  83.     err_doit(0, 0, fmt, ap);  
  84.     va_end(ap);  
  85. }  
  86.   
  87.   
  88. /* 
  89.  * Fatal error unrelated to a system call. 
  90.  * Print a message and terminate. 
  91.  */  
  92. void err_quit(const char *fmt, ...)  
  93. {  
  94.     va_list ap;  
  95.   
  96.     va_start(ap, fmt);  
  97.     err_doit(0, 0, fmt, ap);  
  98.     va_end(ap);  
  99.     exit(1);  
  100. }  
  101.   
  102.   
  103. /* 
  104.  * Print a message and return to caller. 
  105.  * Caller specifies "errnoflag". 
  106.  */  
  107. static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)  
  108. {  
  109.     char buf[MAXLINE];  
  110.    vsnprintf(buf, MAXLINE, fmt, ap);  
  111.    if (errnoflag)  
  112.        snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",  
  113.          strerror(error));  
  114.    strcat(buf, "\n");  
  115.    fflush(stdout); /* in case stdout and stderr are the same */  
  116.    fputs(buf, stderr);  
  117.    fflush(NULL); /* flushes all stdio output streams */  
  118. }  

apue.h代码如下:

  1. /* Our own header, to be included before all standard system headers */  
  2.   
  3. #ifndef _APUE_H  
  4. #define _APUE_H  
  5.   
  6. #if defined(SOLARIS)  
  7. #define _XOPEN_SOURCE   500 /* Single UNIX Specification, Version 2  for Solaris 9 */  
  8. #define CMSG_LEN(x) _CMSG_DATA_ALIGN(sizeof(struct cmsghdr)+(x))  
  9. #elif !defined(BSD)  
  10. #define _XOPEN_SOURCE   600 /* Single UNIX Specification, Version 3 */  
  11. #endif  
  12.   
  13. #include <sys/types.h>        /* some systems still require this */  
  14. #include <sys/stat.h>  
  15. #include <sys/termios.h>  /* for winsize */  
  16. #ifndef TIOCGWINSZ  
  17. #include <sys/ioctl.h>  
  18. #endif  
  19. #include <stdio.h>        /* for convenience */  
  20. #include <stdlib.h>       /* for convenience */  
  21. #include <stddef.h>       /* for offsetof */  
  22. #include <string.h>       /* for convenience */  
  23. #include <unistd.h>       /* for convenience */  
  24. #include <signal.h>       /* for SIG_ERR */  
  25.   
  26. #define MAXLINE 4096            /* max line length */  
  27.   
  28. /* 
  29.  * Default file access permissions for new files. 
  30.  */  
  31. #define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)  
  32.   
  33. /* 
  34.  * Default permissions for new directories. 
  35.  */  
  36. #define DIR_MODE    (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)  
  37.   
  38. typedef void    Sigfunc(int);   /* for signal handlers */  
  39.   
  40. #if defined(SIG_IGN) && !defined(SIG_ERR)  
  41. #define SIG_ERR ((Sigfunc *)-1)  
  42. #endif  
  43.   
  44. #define min(a,b)    ((a) < (b) ? (a) : (b))  
  45. #define max(a,b)    ((a) > (b) ? (a) : (b))  
  46.   
  47. /* 
  48.  * Prototypes for our own functions. 
  49.  */  
  50. char    *path_alloc(int *);             /* {Prog pathalloc} */  
  51. long     open_max(void);                /* {Prog openmax} */  
  52. void     clr_fl(intint);              /* {Prog setfl} */  
  53. void     set_fl(intint);              /* {Prog setfl} */  
  54. void     pr_exit(int);                  /* {Prog prexit} */  
  55. void     pr_mask(const char *);         /* {Prog prmask} */  
  56. Sigfunc *signal_intr(int, Sigfunc *);   /* {Prog signal_intr_function} */  
  57.   
  58. int      tty_cbreak(int);               /* {Prog raw} */  
  59. int      tty_raw(int);                  /* {Prog raw} */  
  60. int      tty_reset(int);                /* {Prog raw} */  
  61. void     tty_atexit(void);              /* {Prog raw} */  
  62. #ifdef  ECHO    /* only if <termios.h> has been included */  
  63. struct termios  *tty_termios(void);     /* {Prog raw} */  
  64. #endif  
  65.   
  66. void     sleep_us(unsigned int);            /* {Ex sleepus} */  
  67. ssize_t  readn(intvoid *, size_t);        /* {Prog readn_writen} */  
  68. ssize_t  writen(intconst void *, size_t); /* {Prog readn_writen} */  
  69. void     daemonize(const char *);           /* {Prog daemoninit} */  
  70.   
  71. int      s_pipe(int *);                 /* {Progs streams_spipe sock_spipe} */  
  72. int      recv_fd(int, ssize_t (*func)(int,  
  73.                  const void *, size_t));/* {Progs recvfd_streams recvfd_sockets} */  
  74. int      send_fd(intint);             /* {Progs sendfd_streams sendfd_sockets} */  
  75. int      send_err(intint,  
  76.                   const char *);        /* {Prog senderr} */  
  77. int      serv_listen(const char *);     /* {Progs servlisten_streams servlisten_sockets} */  
  78. int      serv_accept(int, uid_t *);     /* {Progs servaccept_streams servaccept_sockets} */  
  79. int      cli_conn(const char *);        /* {Progs cliconn_streams cliconn_sockets} */  
  80. int      buf_args(char *, int (*func)(int,  
  81.                   char **));            /* {Prog bufargs} */  
  82.   
  83. int      ptym_open(char *, int);    /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */  
  84. int      ptys_open(char *);         /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */  
  85. #ifdef  TIOCGWINSZ  
  86. pid_t    pty_fork(int *, char *, intconst struct termios *,  
  87.                   const struct winsize *);      /* {Prog ptyfork} */  
  88. #endif  
  89.   
  90. int     lock_reg(intintint, off_t, int, off_t); /* {Prog lockreg} */  
  91. #define read_lock(fd, offset, whence, len) \  
  92.             lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))  
  93. #define readw_lock(fd, offset, whence, len) \  
  94.             lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))  
  95. #define write_lock(fd, offset, whence, len) \  
  96.             lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))  
  97. #define writew_lock(fd, offset, whence, len) \  
  98.             lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))  
  99. #define un_lock(fd, offset, whence, len) \  
  100.             lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))  
  101.   
  102. pid_t   lock_test(intint, off_t, int, off_t);     /* {Prog locktest} */  
  103.   
  104. #define is_read_lockable(fd, offset, whence, len) \  
  105.             (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)  
  106. #define is_write_lockable(fd, offset, whence, len) \  
  107.             (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)  
  108.   
  109. void    err_dump(const char *, ...);        /* {App misc_source} */  
  110. void    err_msg(const char *, ...);  
  111. void    err_quit(const char *, ...);  
  112. void    err_exit(intconst char *, ...);  
  113. void    err_ret(const char *, ...);  
  114. void    err_sys(const char *, ...);  
  115.   
  116. void    log_msg(const char *, ...);         /* {App misc_source} */  
  117. void    log_open(const char *, intint);  
  118. void    log_quit(const char *, ...);  
  119. void    log_ret(const char *, ...);  
  120. void    log_sys(const char *, ...);  
  121.   
  122. void    TELL_WAIT(void);        /* parent/child from {Sec race_conditions} */  
  123. void    TELL_PARENT(pid_t);  
  124. void    TELL_CHILD(pid_t);  
  125. void    WAIT_PARENT(void);  
  126. void    WAIT_CHILD(void);  
  127.   
  128. #endif  /* _APUE_H */  

如上给出了apue.h和apueerror.h的代码,只需要在apueerror.h中包含apue.h,然后在自己写的源码中包含apueerror.h这个头文件即可.

版权声明:本文为博主原创文章,未经博主允许不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值