歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> apue.h頭文件(UNIX環境高級編程)

apue.h頭文件(UNIX環境高級編程)

日期:2017/3/1 10:39:11   编辑:Linux編程
apue.h是作者自己寫的一個文件,包含了常用的頭文件,系統不自帶。其中包含了常用的頭文件,以及出錯處理函數的定義。使用起來確實很方便。

apue.h內容如下:

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