unix编程书中的 ourhdr.h代码

真心不知到里面写的具体什么意思,先记下吧。

  1 /*Our own header, to be included after  all standard system headers*/
  2 #ifndef __ourhdr_h 
  3 #define __ourhdr_h
  4  
  5 #include    <errno.h>               /*for definition of errno                      */
  6 #include    <stdarg.h>              /*ANSI C header file                           */
  7 #include    <sys/types.h>           /* required for some of our prototypes         */ 
  8 #include    <stdio.h>               /* for convenience                             */ 
  9 #include    <stdlib.h>              /* for convenience                             */ 
 10 #include    <string.h>              /* for convenience                             */ 
 11 #include    <unistd.h>              /* for convenience                             */
 12   
 13 #define MAXLINE 4096                /* max line length                             */ 
 14 #define FILE_MODE       (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 
 15                                         /* default file access permissions for new files */ 
 16 #define DIR_MODE        (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH) 
 17                                         /* default permissions for new directoris */ 
 18 typedef void    Sigfunc(int);   /* for signal handlers */ 
 19                                         /* 4.3BSD Reno <signal.h> doesn't define 
 20 SIG_ERR */ 
 21 #if     defined(SIG_IGN) && !defined(SIG_ERR) 
 22 #define SIG_ERR ((Sigfunc *)-1) 
 23 #endif 
 24 #define min(a,b)        ((a) < (b) ? (a) : (b)) 
 25 #define max(a,b)        ((a) > (b) ? (a) : (b))
 26                                                 /* prototypes for our own functions */ 
 27 char    *path_alloc(int *);                     /* {Prog pathalloc} */ 
 28 int      open_max(void);                        /* {Prog openmax} */ 
 29 void     clr_fl(int, int);                      /* {Prog setfl} */ 
 30 void     set_fl(int, int);                      /* {Prog setfl} */ 
 31 void     pr_exit(int);                          /* {Prog prexit} */ 
 32 void     pr_mask(const char *);                 /* {Prog prmask} */ 
 33 Sigfunc *signal_intr(int, Sigfunc *);           /* {Prog signal_intr_function} */ 
 34 int      tty_cbreak(int);                       /* {Prog raw} */ 
 35 int      tty_raw(int);                          /* {Prog raw} */ 
 36 int      tty_reset(int);                        /* {Prog raw} */ 
 37 void     tty_atexit(void);                      /* {Prog raw} */
 38  
 39 #ifdef  ECHO                                    /* only if <termios.h> has been included */ 
 40 struct termios  *tty_termios(void);             /* {Prog raw} */ 
 41 #endif 
 42 void     sleep_us(unsigned int);                /* {Ex sleepus} */ 
 43 ssize_t  readn(int, void *, size_t);            /* {Prog readn} */ 
 44 ssize_t  writen(int, const void *, size_t);     /* {Prog writen} */ 
 45 int              daemon_init(void);                     /* {Prog daemoninit} */ 
 46 int              s_pipe(int *);                         /* {Progs svr4_spipe bsd spipe} */ 
 47 int              recv_fd(int, ssize_t (*func)(int, const void *, size_t)); 
 48                                                         /* {Prog recvfd_svr4 recvfd_43bsd} */
 49                                                           
 50 int              send_fd(int, int);                     /* {Progs sendfd_svr4 se 
 51 dfd_43bsd} */ 
 52 int              send_err(int, int, const char *);/* {Prog senderr} */ 
 53 int              serv_listen(const char *);     /* {Progs servlisten_svr4 servli 
 54 ten_4 
 55 4bsd} */ 
 56 int              serv_accept(int, uid_t *);     /* {Progs servaccept_svr4 servac 
 57 ept_4 
 58 4bsd} */ 
 59 int              cli_conn(const char *);        /* {Progs cliconn_svr4 cliconn_4 
 60 bsd} */ 
 61 int              buf_args(char *, int (*func)(int, char **)); 
 62                                                 /* {Prog bufargs} */ 
 63 int              ptym_open(char *);                     /* {Progs ptyopen_svr4 p yopen_44bsd} */ 
 64 int              ptys_open(int, char *);        /* {Progs ptyopen_svr4 ptyopen_4 bsd} */
 65   
 66 #ifdef  TIOCGWINSZ 
 67 pid_t    pty_fork(int *, char *, const struct termios *, 
 68                                   const struct winsize *);      /* {Prog ptyfork */ 
 69 #endif 
 70 int             lock_reg(int, int, int, off_t, int, off_t);     /* {Prog lockreg} */
 71  
 72 #define read_lock(fd, offset, whence, len)    lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len) 
 73 #define readw_lock(fd, offset, whence, len)   lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len) 
 74 #define write_lock(fd, offset, whence, len)   lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len) 
 75 #define writew_lock(fd, offset, whence, len)  lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len) 
 76 #define un_lock(fd, offset, whence, len)      lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len) 
 77 pid_t   lock_test(int, int, off_t, int, off_t);      /* {Prog    locktest} */ 
 78 #define is_readlock(fd, offset, whence, len)  lock_test(fd, F_RDLCK, offset, whence, len) 
 79 #define is_writelock(fd, offset, whence, len) lock_test(fd, F_WRLCK, offset, whence, len) 
 80 void    err_dump(const char *, ...);    /* {App misc_source} */ 
 81 void    err_msg(const char *, ...); 
 82 void    err_quit(const char *, ...); 
 83 void    err_ret(const char *, ...); 
 84 void    err_sys(const char *, ...); 
 85 void    log_msg(const char *, ...);             /* {App misc_source} */ 
 86 void    log_open(const char *, int, int); 
 87 void    log_quit(const char *, ...); 
 88 void    log_ret(const char *, ...); 
 89 void    log_sys(const char *, ...); 
 90 void    TELL_WAIT(void);                /* parent/child from {Sec race_condition  } */ 
 91 void    TELL_PARENT(pid_t); 
 92 void    TELL_CHILD(pid_t); 
 93 void    WAIT_PARENT(void); 
 94 void    WAIT_CHILD(void); 
 95 #endif  /* __ourhdr_h */
 96  
 97 static void err_doit(int, const char*, va_list);
 98 char *pname = NULL; /* caller can set this from argv[0]*/
 99 /* Nonfatal error realated to a system call . print a message and return . */
100 void err_ret(const char *fmt,...)
101 {
102    va_list ap;
103    va_start(ap, fmt);
104    err_doit(1, fmt, ap);
105    va_end(ap);
106     
107     return;
108 }
109  
110 /* Fatal error related to a system call . print a message and terminate. */
111 void err_sys(const char *fmt,...)
112 {
113    va_list ap;
114    va_start(ap, fmt);
115    err_doit(1, fmt, ap);
116    va_end(ap);
117    exit(1);
118 }
119 /* Fatal error related to a system call . print a message ,dump core ,and terminate. */
120 void err_dump(const char *fmt,...)
121 {
122    va_list ap;
123  
124    va_start(ap, fmt);
125    err_doit(1, fmt, ap);
126    va_end(ap);
127    abort(); /* dump core and terminate */
128    exit(1); /* shouldn't get here */
129 }
130  
131 /* Nofatal error related to a system call . print a message and return. */
132 void err_msg(const char *fmt,...)
133 {
134    va_list ap;
135   
136    va_start(ap, fmt);
137    err_doit(0, fmt, ap);
138    va_end(ap);
139     
140    return;
141 }
142 /* Fatal error related to a system call . print a message and terminate. */
143 void err_quit(const char *fmt,...)
144 {
145    va_list ap;
146    va_start(ap, fmt);
147    err_doit(0, fmt, ap);
148    va_end(ap);
149    exit(1);
150 }
151 /* print a message and return to caller. caller specifies "errnoflag". */
152 static void err_doit(int errnoflag, const char *fmt, va_list ap)
153 {
154    int errno_save;
155    char buf[MAXLINE];
156  
157    errno_save = errno;  /* value caller might want printed */
158    vsprintf(buf, fmt, ap);
159     
160    if (errnoflag)
161      sprintf(buf+strlen(buf),": %s", strerror(errno_save));
162       
163    strcat(buf, "\n");
164    fflush(stdout); /* in case stdout and stderr are the same */
165    fputs(buf, stderr);
166    fflush(NULL); /*flushes and stdio  output streams */
167     
168    return;
169 }

 

posted @ 2014-05-01 07:40  阳光下的星星cc  阅读(391)  评论(0编辑  收藏  举报