LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 5737|回复: 19

ourhdr.h

[复制链接]
发表于 2004-10-9 16:17:42 | 显示全部楼层 |阅读模式
  1. #include [color=crimson]"ourhdr.h"[/color]
  2. int
  3. main(void)
  4. {
  5.   int c;
  6.   while ((c=getc(stdin)) != EOF)
  7.     if (putc(c,stdout) == EOF)
  8.       err_sys("output error");
  9.     if (ferror(stdin))
  10.       err_sys("input error");
  11.     exit(0);
  12. }
复制代码


1:红色部分头文件我在网上找到了,但一看,有很多个,不知道放到哪里去才好 ?

/tmp/apue/advio/ourhdr.h
/tmp/apue/call/ourhdr.h
/tmp/apue/calld/ourhdr.h
/tmp/apue/datafiles/ourhdr.h
/tmp/apue/db.lock.fine/ourhdr.h
/tmp/apue/environ/ourhdr.h
/tmp/apue/file/ourhdr.h
/tmp/apue/ipc/ourhdr.h
/tmp/apue/lib.sun/ourhdr.h
/tmp/apue/lib.44/ourhdr.h
/tmp/apue/lib.svr4/ourhdr.h
/tmp/apue/lock/ourhdr.h
/tmp/apue/mycat/ourhdr.h
/tmp/apue/open.fe/ourhdr.h
/tmp/apue/open/ourhdr.h
/tmp/apue/opend.fe/ourhdr.h
/tmp/apue/opend/ourhdr.h
/tmp/apue/printer/ourhdr.h
/tmp/apue/proc/ourhdr.h
/tmp/apue/pty/ourhdr.h
/tmp/apue/sess/ourhdr.h
/tmp/apue/signals/ourhdr.h
/tmp/apue/stdio/ourhdr.h
/tmp/apue/streams/ourhdr.h
/tmp/apue/termios/ourhdr.h
/tmp/apue/test/ourhdr.h

头文件下载地址:http://www.kohala.com/start/apue.tar.Z

2:而且红色部分""与<>有什么区别,如果头文件放在与我的c在同一目录下,头文件路径怎么办 ?

请兄弟们帮帮我
 楼主| 发表于 2004-10-9 16:33:25 | 显示全部楼层
<<unix环境高级编>>程整本书的例子都有ourhdr.h头文件啊,你们帮帮我,要不我学不下去啊 ,,,怎么办 ?
发表于 2004-10-9 17:41:52 | 显示全部楼层
哥哥 看看附录~~
那时作者自己写的
发表于 2004-10-9 17:42:50 | 显示全部楼层
你不用他的头文件也可以的
需要什么就包括什么
发表于 2004-10-9 17:48:04 | 显示全部楼层
先要把lib.xxx下的东西编译出来,源码包里面有编译说明的
发表于 2004-10-9 18:30:06 | 显示全部楼层
最初由 shell 发表
<<unix环境高级编>>程整本书的例子都有ourhdr.h头文件啊,你们帮帮我,要不我学不下去啊 ,,,怎么办 ?


后面的附录有ourhdr.h,兄弟不想敲字我这有:p ,因为我敲过了
  1. /* Our own header, to be included *after* all standard system headers */

  2. #ifndef __ourhdr_h
  3. #define __ourhdr_h

  4. #include <sys/types.h> /* required for some of our prototypes */
  5. #include <stdio.h>     /* for convenience */
  6. #include <stdlib.h>    /* for convenience */
  7. #include <string.h>    /* for convenience */
  8. #include <unistd.h>    /* for convenience */

  9. #define MAXLINE 4096  /* max line length */

  10. #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
  11. /* default file access permissions for new files */

  12. #define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
  13. /* default permissions for new directories */

  14. typedef void Sigfunc(int);    /* for signal handlers */
  15. /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */

  16. #if defined(SIG_IGN) && !defined(SIG_ERR)
  17. #define SIG_ERR ((Sigfunc *) -1)
  18. #endif

  19. #define min(a,b) ((a) < (b) ? (a) : (b))
  20. #define max(a,b) ((a) > (b) ? (a) : (b))

  21. /* prototypes for our own functions */
  22. char *path_alloc(int *);    /* Program 2.2 */
  23. int open_max(void);         /* Program 2.3 */
  24. void clr_fl(int, int);      /* Program 3.5 */
  25. void set_fl(int, int);      /* Program 3.5 */
  26. void pr_exit(int);          /* Program 8.3 */
  27. void pr_mask(const char *); /* Program 10.10 */
  28. Sigfunc *signal_intr(int, Sigfunc *); /* Program 10.13 */

  29. int tty_cbreak(int);        /* Program 11.10 */
  30. int tty_raw(int);           /* Program 11.10 */
  31. int tty_reset(int);         /* Program 11.10 */
  32. int tty_atexit(int);        /* program 11.10 */
  33. #ifdef ECHO                     /* only if <termios.h> has been included */
  34. struct termios *tty_termios(void); /* Program 11.10 */
  35. #endif

  36. void sleep_us(unsigned int);    /* Exercise 12.6 */
  37. ssize_t readn(int, void *, size_t); /* Program 12.13 */
  38. ssize_t writen(int, const void *, size_t); /* Program 12.12 */
  39. int daemon_init(void);          /* Programs 13.1 */
  40. int s_pipe(int *);              /* Programs 15.2  and 15.3 */
  41. int recv_fd(int, ssize_t (*func) (int, const void *, size_t));
  42. /* Program 15.6 and 15.8 */
  43. int send_fd(int, int);          /* Programs 15.5 and 15.7 */
  44. int send_err(int, int, const char *); /* Programs 15.20 */
  45. int serv_listen(const char *); /* Programs 15.19 and 15.22 */
  46. int serv_accept(int, uid_t *);  /* Programs 15.20 and 15.24 */
  47. int cli_conn(const char *);     /* Programs 15.21 and 15.23 */
  48. int buf_args(char *, int (*func)(int,char **)); /* Program 15.17 */

  49. int ptym_open(char *);          /* Programs 19.1 and 19.2 */
  50. int ptys_open(int, char *);     /* Programs 19.1 ans 19.2 */
  51. #ifdef TIOCGWINSZ
  52. pid_z pty_fork(int *, char *, const struct termios *, const struct winsize *);
  53. /* Program 19.3 */
  54. #endif

  55. int lock_fork(int, int, int, off_t, int, off_t); /* Programs 12.2 */
  56. #define read_lock(fd, offset, whence, len) \
  57. lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len)
  58. #define readw_lock(fd, offset, whence, len) \
  59. lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len)
  60. #define write_lock(fd, offset, whence, len) \
  61. lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)
  62. #define writew_lock(fd, offset, whence, len) \
  63. lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len)
  64. #define un_lock(fd, offset, whence, len) \
  65. lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len)

  66. pid_t lock_test(int, int, off_t, int, off_t); /* Program 12.3 */

  67. #define is_readlock(fd, offset, whence, len) \
  68. lock_test(fd, F_RDLCK, offset, whence, len)
  69. #define is_writelock(fd, offset, whence, len) \
  70. lock_test(fd, F_WRLCK, offset, whence, len)

  71. void err_dump(const char *, ...); /* Appendix B */
  72. void err_msg(const char *, ...);
  73. void err_quit(const char *, ...);
  74. void err_ret(const char *, ...);
  75. void err_sys(const char *, ...);

  76. void log_msg(const char *, ...); /* Appendix B */
  77. void log_open(const char *, int, int);
  78. void log_quit(const char *, ...);
  79. void log_ret(const char *, ...);
  80. void log_sys(const char *, ...);

  81. void TELL_WAIT(void);           /* parent/child form Section 8.8 */
  82. void TELL_PARENT(pid_t);
  83. void TELL_CHILD(pid_t);
  84. void WAIT_PARENT(void);
  85. void WAIT_CHILD(void);

  86. #endif /* __ourhdr_h */
复制代码



另外还有一个错误处理文件,你可以把它先编译出来,比如我把它命名为myerror.c:
  1. #include <errno.h>              /* for definition of errno */
  2. #include <stdarg.h>             /* ANSI C header file */
  3. #include "ourhdr.h"

  4. static void err_doit(int, const char *, va_list);
  5. char *pname = NULL;             /* caller can set this from argv[0] */

  6. /* Nonfatal error related to a system call.
  7.    Print a message and return */

  8. void
  9. err_ret(const char *fmt, ...)
  10. {
  11.   va_list ap;
  12.   va_start(ap, fmt);
  13.   err_doit(1, fmt, ap);
  14.   va_end(ap);
  15.   return;
  16. }

  17. /* Fatal error related to a system call.
  18. Print a message and terminate. */

  19. void
  20. err_sys(const char *fmt, ...)
  21. {
  22.   va_list ap;
  23.   va_start(ap, fmt);
  24.   err_doit(1, fmt, ap);
  25.   va_end(ap);
  26.   exit(1);
  27. }

  28. /* Fatal error related to a system call.
  29.    Print a message and terminate. */

  30. void
  31. err_dump(const char *fmt, ...)
  32. {
  33.   va_list ap;

  34.   va_start(ap, fmt);
  35.   err_doit(1, fmt, ap);
  36.   va_end(ap);
  37.   abort();                      /* dump core and terminate */
  38.   exit(1);                      /* shouldn't get here */
  39. }

  40. /* Nonfatal error unrelated to a system call.
  41.    Print a message and return. */

  42. void
  43. err_msg(const char *fmt, ...)
  44. {
  45.   va_list ap;

  46.   va_stat(ap, fmt);
  47.   err_doit(0, fmt, ap);
  48.   va_end(ap);
  49.   return;
  50. }

  51. /* Fatal error unrelated to a system call.
  52.    Print a message and terminate. */

  53. void
  54. err_quit(const char *fmt, ...)
  55. {
  56.   va_list ap;
  57.   va_start(ap, fmt);
  58.   err_doit(0, fmt, ap);
  59.   va_end(ap);
  60.   exit(1);
  61. }

  62. /* Print a message and return to caller.
  63.    Caller specifies "errnoflag". */

  64. static void
  65. err_doit(int errnoflag, const char *fmt, va_list ap)
  66. {
  67.   int errno_save;
  68.   char buf[MAXLINE];
  69.    
  70.   errno_save = errno;           /* value caller might want printed */
  71.   vsprintf(buf, fmt, ap);
  72.   if (errnoflag)
  73.     sprintf (buf+strlen(buf), ": %s", strerror (errno_save));
  74.   strcat (buf, "\n");
  75.   fflush(stdout);               /* in case stdout and stderr are the same */
  76.   fputs(buf, stderr);
  77.   fflush(NULL);                 /* flushes all stdio output streams */
  78.   return;
  79. }
复制代码


用gcc -c myerror.c编译得到myerror.o
发表于 2004-10-9 22:02:47 | 显示全部楼层
放在一个特定目录,设为$OH,
每次如果引用了ourhdr.h,请在gcc编译的时候加入参数: -I$OH
“-”后边是大写i。
 楼主| 发表于 2004-10-10 11:48:35 | 显示全部楼层
最初由 kiron 发表
后面的附录有ourhdr.h,兄弟不想敲字我这有:p ,因为我敲过了

用gcc -c myerror.c编译得到myerror.o


不懂你的意思,我想问下将头文件放哪个目录,我用as3,还是要编译后才能使用头文件 ?
发表于 2004-10-10 11:59:06 | 显示全部楼层
源码包包里有编译的说明
发表于 2004-10-10 13:47:58 | 显示全部楼层
最初由 shell 发表
不懂你的意思,我想问下将头文件放哪个目录,我用as3,还是要编译后才能使用头文件 ?


头文件哪里放都可以,但是你执行gcc来编译的时候要指定参数-I后跟头文件的路径,具体看gcc的文档

对于错误处理函数,是要先连接做成.o比如我命名为myerror.o文件

若我有一个主文件名为 first.c,它引用了ourhdr.h头文件和myerror.o文件,我可以这样编译:
gcc -o a.out first.c myerror.o -I/ourhdr.h文件上一级目录的路径/


兄弟若还不能明白,应该先看看gcc的用法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表