apue.h头文件(UNIX环境高级编程)


apue.h是作者自己写的一个文件,包含了常用的头文件,系统不自带。其中包含了常用的头文件,以及出错处理函数的定义。使用起来确实很方便。

apue.h内容如下: 

[cpp]
  1. #ifndef _APUE_H     
  2. #define _APUE_H     
  3.     
  4. #define _XOPEN_SOURCE   600  /* Single UNIX Specification, Version 3 */     
  5.     
  6. #include <sys/types.h>       /* some systems still require this */     
  7. #include <sys/stat.h>     
  8. #include <sys/termios.h>     /* for winsize */     
  9. #ifndef TIOCGWINSZ     
  10. #include <sys/ioctl.h>     
  11. #endif     
  12. #include <stdio.h>     /* for convenience */     
  13. #include <stdlib.h>    /* for convenience */     
  14. #include <stddef.h>    /* for offsetof */     
  15. #include <string.h>    /* for convenience */     
  16. #include <unistd.h>    /* for convenience */     
  17. #include <signal.h>    /* for SIG_ERR */     
  18.     
  19.     
  20. #define MAXLINE 4096               /* max line length */     
  21.     
  22. /*  
  23.  * Default file access permissions for new files.  
  24.  */    
  25. #define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)     
  26.     
  27. /*  
  28.  * Default permissions for new directories.  
  29.  */    
  30. #define DIR_MODE    (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)     
  31.     
  32. typedef void   Sigfunc(int);   /* for signal handlers */    
  33.     
  34. #if defined(SIG_IGN) && !defined(SIG_ERR)     
  35. #define SIG_ERR ((Sigfunc *)-1)     
  36. #endif     
  37.     
  38. #define min(a,b)     ((a) < (b) ? (a) : (b))     
  39. #define max(a,b)     ((a) > (b) ? (a) : (b))     
  40.     
  41. /*  
  42.  * Prototypes for our own functions.  
  43.  */    
  44. char    *path_alloc(int *);              /* Figure 2.15 */    
  45. long     open_max(void);                 /* Figure 2.16 */    
  46. void     clr_fl(intint);               /* Figure 3.11 */    
  47. void     set_fl(intint);               /* Figure 3.11 */    
  48. void     pr_exit(int);                   /* Figure 8.5 */    
  49. void     pr_mask(const char *);          /* Figure 10.14 */    
  50. Sigfunc *signal_intr(int, Sigfunc *);    /* Figure 10.19 */    
  51.     
  52. int      tty_cbreak(int);                /* Figure 18.20 */    
  53. int      tty_raw(int);                   /* Figure 18.20 */    
  54. int      tty_reset(int);                 /* Figure 18.20 */    
  55. void     tty_atexit(void);               /* Figure 18.20 */    
  56. #ifdef  ECHO    /* only if <termios.h>  has been included */     
  57. struct termios  *tty_termios(void);      /* Figure 18.20 */    
  58. #endif     
  59.     
  60. void     sleep_us(unsigned int);             /* Exercise 14.6 */    
  61. ssize_t  readn(intvoid *, size_t);         /* Figure 14.29 */    
  62. ssize_t  writen(intconst void *, size_t);  /* Figure 14.29 */    
  63. void     daemonize(const char *);            /* Figure 13.1 */    
  64.     
  65. int      s_pipe(int *);                 /* Figures 17.6 and 17.13 */    
  66. int      recv_fd(int, ssize_t (*func)(int,    
  67.                  const void *, size_t));/* Figures 17.21 and 17.23 */    
  68. int      send_fd(intint);             /* Figures 17.20 and 17.22 */    
  69. int      send_err(intint,    
  70.                   const char *);        /* Figure 17.19 */    
  71. int      serv_listen(const char *);     /* Figures 17.10 and 17.15 */    
  72. int      serv_accept(int, uid_t *);     /* Figures 17.11 and 17.16 */    
  73.     
  74. int      cli_conn(const char *);        /* Figures 17.12 and 17.17 */    
  75. int      buf_args(char *, int (*func)(int,    
  76.                   char **));            /* Figure 17.32 */    
  77.     
  78. int      ptym_open(char *, int);    /* Figures 19.8, 19.9, and 19.10 */    
  79. int      ptys_open(char *);         /* Figures 19.8, 19.9, and 19.10 */    
  80. #ifdef  TIOCGWINSZ     
  81. pid_t    pty_fork(int *, char *, intconst struct termios *,    
  82.                   const struct winsize *);      /* Figure 19.11 */    
  83. #endif     
  84.     
  85. int     lock_reg(intintint, off_t, int, off_t); /* Figure 14.5 */    
  86. #define read_lock(fd, offset, whence, len) \    
  87.             lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))    
  88. #define readw_lock(fd, offset, whence, len) \     
  89.             lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))    
  90. #define write_lock(fd, offset, whence, len) \     
  91.             lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))    
  92. #define writew_lock(fd, offset, whence, len) \     
  93.             lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))    
  94. #define un_lock(fd, offset, whence, len) \     
  95.             lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))    
  96.     
  97. pid_t   lock_test(intint, off_t, int, off_t);     /* Figure 14.6 */    
  98.     
  99. #define is_read_lockable(fd, offset, whence, len) \     
  100.             (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)    
  101. #define is_write_lockable(fd, offset, whence, len) \     
  102.             (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)    
  103.     
  104. void    err_dump(const char *, ...);        /* Appendix B */    
  105. void    err_msg(const char *, ...);    
  106. void    err_quit(const char *, ...);    
  107. void    err_exit(intconst char *, ...);    
  108. void    err_ret(const char *, ...);    
  109. void    err_sys(const char *, ...);    
  110.     
  111. void    log_msg(const char *, ...);         /* Appendix B */    
  112. void    log_open(const char *, intint);    
  113. void    log_quit(const char *, ...);    
  114. void    log_ret(const char *, ...);    
  115. void    log_sys(const char *, ...);    
  116.     
  117. void    TELL_WAIT(void);        /* parent/child from Section 8.9 */    
  118. void    TELL_PARENT(pid_t);    
  119. void    TELL_CHILD(pid_t);    
  120. void    WAIT_PARENT(void);    
  121. void    WAIT_CHILD(void);    
  122.     
  123. #endif  /* _APUE_H */    


  想要使用,还要进行下面操作,完成相关配置.

  在http://www.apuebook.com/下载src.tar.gz源代码的压缩包。

   1、解压至/home/user/目录下

   2、修改 Make.defines.linux中的WKDIR=/home/xxx/apue.2e,为WKDIR=/home/user/apue.2e

   3、返回至apue.2e目录下面,修改linux.mk,将里面的nawk全部改为awk,可以使用这个命令 :%s/nawk/awk/g 

   4、make

   5、将/home/user/apue.2e/include/apue.h和/home/user/apue.2e/lib/error.c复制到/usr/include目录下


    然后就可以方便的使用apue.h编译《unix高级环境编程》的的程序了

相关内容