分享

apue.h包头找不到的问题

 书剑阁2013 2013-11-15

        在运行《UNIX环境高级编程》中的程序时会遇到apue.h包头找不到的情况,这是作者为了方便程序书写封闭了一些功能函数和错误处理等。在http://www./中可以下载到书的所有的实例代码,解压后为apue.2e。

        我们把apue.2e中的两个文件拷贝到/usr/include中就行了:

(1) apue.h:apue.2e/include/apue.h,我们需要在#endif前面加上#include "error.c"

  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. #include "error.c"   
  129.   
  130. #endif  /* _APUE_H */  

(2) error.c:apue.2e/lib/error.c

  1. #include "apue.h"   
  2. #include <errno.h>        /* for definition of errno */   
  3. #include <stdarg.h>       /* ISO C variable aruments */   
  4.   
  5. static void err_doit(intintconst char *, va_list);  
  6.   
  7. /* 
  8.  * Nonfatal error related to a system call. 
  9.  * Print a message and return. 
  10.  */  
  11. void  
  12. err_ret(const char *fmt, ...)  
  13. {  
  14.     va_list     ap;  
  15.   
  16.     va_start(ap, fmt);  
  17.     err_doit(1, errno, fmt, ap);  
  18.     va_end(ap);  
  19. }  
  20.   
  21. /* 
  22.  * Fatal error related to a system call. 
  23.  * Print a message and terminate. 
  24.  */  
  25. void  
  26. err_sys(const char *fmt, ...)  
  27. {  
  28.     va_list     ap;  
  29.   
  30.     va_start(ap, fmt);  
  31.     err_doit(1, errno, fmt, ap);  
  32.     va_end(ap);  
  33.     exit(1);  
  34. }  
  35.   
  36. /* 
  37.  * Fatal error unrelated to a system call. 
  38.  * Error code passed as explict parameter. 
  39.  * Print a message and terminate. 
  40.  */  
  41. void  
  42. err_exit(int error, const char *fmt, ...)  
  43. {  
  44.     va_list     ap;  
  45.   
  46.     va_start(ap, fmt);  
  47.     err_doit(1, error, fmt, ap);  
  48.     va_end(ap);  
  49.     exit(1);  
  50. }  
  51.   
  52. /* 
  53.  * Fatal error related to a system call. 
  54.  * Print a message, dump core, and terminate. 
  55.  */  
  56. void  
  57. err_dump(const char *fmt, ...)  
  58. {  
  59.     va_list     ap;  
  60.   
  61.     va_start(ap, fmt);  
  62.     err_doit(1, errno, fmt, ap);  
  63.     va_end(ap);  
  64.     abort();        /* dump core and terminate */  
  65.     exit(1);        /* shouldn't get here */  
  66. }  
  67.   
  68. /* 
  69.  * Nonfatal error unrelated to a system call. 
  70.  * Print a message and return. 
  71.  */  
  72. void  
  73. err_msg(const char *fmt, ...)  
  74. {  
  75.     va_list     ap;  
  76.   
  77.     va_start(ap, fmt);  
  78.     err_doit(0, 0, fmt, ap);  
  79.     va_end(ap);  
  80. }  
  81.   
  82. /* 
  83.  * Fatal error unrelated to a system call. 
  84.  * Print a message and terminate. 
  85.  */  
  86. void  
  87. err_quit(const char *fmt, ...)  
  88. {  
  89.     va_list     ap;  
  90.   
  91.     va_start(ap, fmt);  
  92.     err_doit(0, 0, fmt, ap);  
  93.     va_end(ap);  
  94.     exit(1);  
  95. }  
  96.   
  97. /* 
  98.  * Print a message and return to caller. 
  99.  * Caller specifies "errnoflag". 
  100.  */  
  101. static void  
  102. err_doit(int errnoflag, int error, const char *fmt, va_list ap)  
  103. {  
  104.     char    buf[MAXLINE];  
  105.   
  106.     vsnprintf(buf, MAXLINE, fmt, ap);  
  107.     if (errnoflag)  
  108.         snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",  
  109.           strerror(error));  
  110.     strcat(buf, "\n");  
  111.     fflush(stdout);     /* in case stdout and stderr are the same */  
  112.     fputs(buf, stderr);  
  113.     fflush(NULL);       /* flushes all stdio output streams */  
  114. }  

至此,环境搭配完成,我们就能放心地运行apue中的实例啦!


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多