Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1758582
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: C/C++

2011-12-25 20:33:26

APUE.H
  1. /* our own header to be included before all standard system headers */

  2. #ifndef _APUE_H
  3. #define _APUE_H

  4. #define _XOPEN_SOURCE 600 /* Single unix specification version 3 */

  5. #include <sys/types.h>
  6. #include <sys/termios.h>

  7. #ifndef TIOCGWINSZ
  8. #include <sys/ioctl.h>
  9. #endif
  10. #include <stdio.h> /* for conversions */
  11. #include <stdlib.h> /* for conversions */
  12. #include <stddef.h> /* for offsetof */
  13. #include <string.h> /*for conversions */
  14. #include <unistd.h> /* for convenience */
  15. #include <signal.h> /* for SIG_ERR */
  16. #define MAXLINE 4096 /* max line length */

  17. /*
  18.  * defualt file access permissions for new files
  19.  */
  20. #define FILE_MODE (S_IRUSR | S_IWUSR | S_RGRP | 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.  * Prototype for our owner functions
  33.  */
  34. char * path_alloc(int *);
  35. long open_max(void);
  36. void clr_fl(int,int);
  37. void set_fl(int,int);
  38. void pr_exit(int);
  39. void pr_mask(const char *);
  40. Sigfunc *signal(int,Sigfunc *);

  41. int tty_cbreak(int);
  42. int tty_raw(int);
  43. int tty_reset(int);
  44. void tty_atexit(void);
  45. #ifdef ECHO /*only if <termios.h> has been include */
  46. struct termios *tty_termios(void);;
  47. #endif
  48.  
  49. void sleep_us(unsigned int);
  50. ssize_t readn(int,void *,size_t);
  51. ssize_t writen(int,const void *,size_t);
  52. void daemonize(const char * );

  53. int s_pipe(int *);
  54. int recv_fd(int,ssize_t ( *func)(int,const void *,size_t));
  55. int send_fd(int,int);
  56. int send_err(int,int,const char *); int serv_listen(const char *);
  57. int serv_accept(int,uid_t *);
  58. int cli_conn(const char *);
  59. int buf_args(char *,int (*func)(int,char **));

  60. int ptym_open(char *,int);
  61. int ptys_open(char *);
  62. #ifdef TIOCGWINSZ
  63. pid_t pty_fork(int *,char *,int,const struct termios *,const struct winsize *);
  64. #endif

  65. int lock_reg(int,int,int,off_t,int,off_t);
  66. #define read_lock(fd,offset,whence,len) \
  67.            lock_reg((fd),F_SETLK,F_RDLCK,(offset),(whence),(len))

  68. #define readw_lock(fd,offset,whence,len) \
  69.                 lock_reg((fd),F_SETLKW,F_RDLCK,(offset),(whence),(len));
  70. #define write_lock(fd,offset,whence,len) \
  71.               lock_reg((fd),F_SETLK,F_WRLCK,(offset),(whence),(len));
  72. #define writew_lock(fd,offset,whence,len) \
  73.                  lock_reg((fd),F_SETLKW,F_WRLCK,(offset),(whence),(len));
  74. #define unlock(fd,offset,whence,len) \
  75.               lock_reg((fd),F_SETLK,F_UNLCK,(offset),(whence),(len));

  76. pid_t lock_test(int,int,off_t,int,off_t);

  77. #define is_read_lockable(fd,offsset,whence,len) \
  78.              (lock_test((fd),F_RDLCK,(offset),(whence),(len)) == 0)
  79. #define is_write_lockable(fd,offset,whence,len) \
  80.              (lock_test((fd),F_WRLCK,(offset),(whence),(len)) == 0)
  81. void err_dump(const char *,...);
  82. void err_msg(const char *,...);
  83. void err_quit(const char *,...);
  84. void err_exit(int,const char *,...);
  85. void err_ret(const char *,...);
  86. void err_sys(const char *,...);

  87. void log_dump(const,char *,...);
  88. void log_msg(const char *,...);
  89. void log_quit(const char *,...);
  90. void log_exit(int,const char *,...);
  91. void log_ret(const char *,...);
  92. void log_sys(const char *,...);

  93. void TELL_WAIT(void);
  94. void TELL_PARENT(pid_t );
  95. void TELL_CHILD(pid_t );
  96. void WAIT_CHILD(void);
  97. void WAIT_PARENT(void);
  98. #endif /* _APUE_H */
ERROR.C
  1. #include "apue.h"
  2. #include <errno.h> /* for definition of errno */
  3. #include <stdarg.h> /* iso c variable arguement */

  4. static void err_doit(int ,int,const char * ,va_list);

  5. /*
  6.  * Nonfatal error related to a system call
  7.  * Print a message and return
  8.  */
  9. void
  10. err_ret(const char * fmt,...)
  11. {
  12.     va_list ap;
  13.             
  14.     va_start(ap,fmt);
  15.     err_doit(1,errno,fmt,ap);
  16.     va_end(ap);
  17. }

  18. /*
  19.  * Fatal error message related to a system call
  20.  * Print a message and terminate
  21.  */

  22. void
  23. err_sys(const char * fmt,...)
  24. {
  25.     va_list ap;
  26.             
  27.     va_start(ap,fmt);
  28.     err_doit(1,errno,fmt,ap);
  29.     va_end(ap);
  30.     exit(1);
  31. }

  32. /*
  33.  * Fatal error unrelated to system call
  34.  * Error code passed as explicted parameter
  35.  * exit(1);
  36.  * Print a message and terminate
  37.  */

  38. void
  39. err_exit(int error,const char * fmt,...)
  40. {
  41.     va_list ap;
  42.             
  43.     va_start(ap,fmt);
  44.     err_doit(1,error,fmt,ap);
  45.     va_end(ap);
  46.     exit(1);
  47. }
  48. /* Fatal error related to system call
  49.  * Print a message ,dump core,and terminate
  50.  */

  51. void
  52. err_dump(const char * fmt,...)
  53. {
  54.     va_list ap;
  55.             
  56.     va_start(ap,fmt);
  57.     err_doit(1,errno,fmt,ap);
  58.     va_end(ap);
  59.     abort(); /* dump core and terminate */
  60.     exit(1); /* shouldn't get here */
  61. }
  62. /* Nonfatal error unrelated to system call
  63.  * Print a message and return
  64.  */
  65. void err_msg(const char * fmt,...)
  66. {
  67.     va_list ap;

  68.     va_start(ap,fmt);
  69.     err_doit(0,0,fmt,ap);
  70.     va_end(ap);
  71. }

  72. /*
  73.  *Fatal error unrelated to system call
  74.  * Print a message and terminate
  75.  */
  76. void err_quit(const char * fmt,...)
  77. {
  78.     va_list ap;

  79.     va_start(ap,fmt);
  80.     err_doit(0,0,fmt,ap);
  81.     va_end(ap);
  82.     exit(1);
  83. }
  84. /*
  85.  * Print a message and return to caller
  86.  * Caller specifies "errorflag "
  87.  */
  88. static void
  89. err_doit(int errnoflag,int error,const char * fmt,va_list ap)
  90. {
  91.     char buf[MAXLINE];

  92.     vsnprintf(buf,MAXLINE,fmt,ap);
  93.     if (errnoflag )
  94.         snprintf(buf+strlen(buf),MAXLINE-strlen(buf),";%s",strerror(error));
  95.     strcat(buf,"\n");
  96.     fflush(stdout); /* in case stdout and stderr are the saem */
  97.     fputs(buf,stderr);
  98.     fflush(NULL);     /* flush all stdio output stream */
  99. }
handler of log error :
  1. #include "apue.h"
  2. #include <errno.h> /* for definition of errno */
  3. #include <stdarg.h> /* ISO c variable arguement */
  4. #include <syslog.h>

  5. static void log_doit(int,int,const char * ,va_list );

  6. /*
  7.  * Caller must define and set this : nonzero if
  8.  * interactive ,zero if daemon
  9.  */

  10. extern int log_to_stderr;

  11. /*
  12.  * Initialize syslog(), if running as a daemon
  13.  */

  14. void log_open(const char * ident,int option,int facility)
  15. {
  16.     if ( log_to_stderr == 0 )
  17.         openlog(ident,option,facility);
  18. }

  19. /*
  20.  * Nonfatal error related to a system call
  21.  * Print a message with the system's errno value and return
  22.  */

  23. void
  24. log_ret(const char * fmt,...)
  25. {
  26.     va_list ap;
  27.     va_start(ap,fmt);

  28.     log_doit(1,LOG_ERR,fmt,ap);
  29.     va_end(ap);
  30. }

  31. /*
  32.  * Fatal error related to a system call
  33.  *Print a message and terminate
  34.  */

  35. void
  36. log_sys(const char * fmt,...)
  37. {
  38.     va_list ap;
  39.     va_start(ap,fmt);

  40.     log_doit(1,LOG_ERR,fmt,ap);
  41.     va_end(ap);
  42.     exit(2);
  43. }

  44. /*
  45.  *Nonfatal error unrelated to a system call
  46.  * Print a message and return
  47.  */

  48. void
  49. log_msg(const char * fmt,...)
  50. {
  51.     va_list ap;
  52.     va_start(ap,fmt);

  53.     log_doit(0,LOG_ERR,fmt,ap);
  54.     va_end(ap);
  55. }

  56. /*
  57.  *Fatal error unrelated to system call
  58.  * Print a message and terminate
  59.  */
  60.  void
  61. log_quit(const char * fmt,...)
  62. {
  63.     va_list ap;
  64.     va_start(ap,fmt);

  65.     log_doit(0,LOG_ERR,fmt,ap);
  66.     va_end(ap);
  67.     exit(2);
  68. }

  69. /*
  70.  *Print a message and return
  71.  * to Caller,& caller specifies errnoflag and priority
  72.  */

  73. static void
  74. log_doit(int errnoflag,int priority,const char * fmt,va_list ap)
  75. {
  76.     int errno_save = 0;
  77.     char buf[MAXLINE];

  78.     errno = errno; /* value caller might want to print */
  79.     vsnprintf(buf,MAXLINE,fmt,ap);
  80.     if ( errnoflag)
  81.         snprintf(buf+strlen(buf),MAXLINE-strlen(buf),":%s ",strerror(errno_save));
  82.     strcat(buf,"\n");
  83.     if (log_to_stderr )
  84.     {
  85.         fflush(stdout);
  86.         fputs(buf,stderr);
  87.         fflush(stderr);
  88.     } else {
  89.         syslog(priority,buf,"%s ");
  90.     }
  91. }

MY FIRST C PROGRAM OF APUE:
  1. #include "apue.h"
  2. #include <dirent.h>

  3. int main(int argc,char * argv[])
  4. {
  5.     DIR *dp;
  6.     struct dirent *dirp;
  7.     
  8.     if ( argc != 2 )
  9.         err_quit("usage : myls is a directory name ");
  10.     if ((dp =opendir(argv[1])) == NULL )
  11.         err_sys("can't open %s",argv[1]);
  12.     while(( dirp = readdir(dp)) != NULL)
  13.     printf("%s\n",dirp->d_name);
  14.     closedir(dp);
  15.     exit(0);
  16. }




阅读(718) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~