text: #ifndef __CSAPP_H__
  #define __CSAPP_H__
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <string.h>
  #include <strings.h>
  #include <ctype.h>
  #include <setjmp.h>
  #include <signal.h>
  #include <sys/time.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <sys/mman.h>
  #include <errno.h>
  #include <math.h>
  #include <pthread.h>
  #include <semaphore.h>
  #include <sys/socket.h>
  #include <netdb.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  
  
  /* Default file permissions are DEF_MODE & ~DEF_UMASK */
  /* $begin createmasks */
  #define DEF_MODE   S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
  #define DEF_UMASK  S_IWGRP|S_IWOTH
  /* $end createmasks */
  
  /* Simplifies calls to bind(), connect(), and accept() */
  /* $begin sockaddrdef */
  typedef struct sockaddr SA;
  /* $end sockaddrdef */
  
  /* Persistent state for the robust I/O (Rio) package */
  /* $begin rio_t */
  #define RIO_BUFSIZE 8192
  typedef struct {
      int rio_fd;                /* descriptor for this internal buf */
      int rio_cnt;               /* unread bytes in internal buf */
      char *rio_bufptr;          /* next unread byte in internal buf */
      char rio_buf[RIO_BUFSIZE]; /* internal buffer */
  } rio_t;
  /* $end rio_t */
  
  /* External variables */
  //extern int h_errno;    /* defined by BIND for DNS errors */ 
  extern char **environ; /* defined by libc */
  
  /* Misc constants */
  #define MAXLINE  8192  /* max text line length */
  #define MAXBUF   8192  /* max I/O buffer size */
  #define LISTENQ  1024  /* second argument to listen() */
  
  /* Our own error-handling functions */
  void unix_error(char *msg);
  void posix_error(int code, char *msg);
  void dns_error(char *msg);
  void app_error(char *msg);
  
  
  /* Process control wrappers */
  pid_t Fork(void);
  void Execve(const char *filename, char *const argv[], char *const envp[]);
  pid_t Wait(int *status);
  
  int Gethostname(char *name, size_t len) ;
  int Setenv(const char *name, const char *value, int overwrite);
  
  /* Unix I/O wrappers */
  int Open(const char *pathname, int flags, mode_t mode);
  ssize_t Read(int fd, void *buf, size_t count);
  ssize_t Write(int fd, const void *buf, size_t count);
  off_t Lseek(int fildes, off_t offset, int whence);
  void Close(int fd);
  int Select(int  n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, 
             struct timeval *timeout);
  int Dup2(int fd1, int fd2);
  void Stat(const char *filename, struct stat *buf);
  void Fstat(int fd, struct stat *buf) ;
  
  /* Memory mapping wrappers */
  void *Mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
  void Munmap(void *start, size_t length);
  
  /* Sockets interface wrappers */
  int Socket(int domain, int type, int protocol);
  void Setsockopt(int s, int level, int optname, const void *optval, int optlen);
  void Bind(int sockfd, struct sockaddr *my_addr, int addrlen);
  void Listen(int s, int backlog);
  int Accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  void Connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
  
  /* DNS wrappers */
  struct hostent *Gethostbyname(const char *name);
  struct hostent *Gethostbyaddr(const char *addr, int len, int type);
  
  /* Rio (Robust I/O) package */
  ssize_t rio_readn(int fd, void *usrbuf, size_t n);
  ssize_t rio_writen(int fd, void *usrbuf, size_t n);
  void rio_readinitb(rio_t *rp, int fd); 
  ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n);
  ssize_t rio_readrestb(rio_t *rp, void *usrbuf);
  ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);
  
  /* Wrappers for Rio package */
  ssize_t Rio_readn(int fd, void *usrbuf, size_t n);
  void Rio_writen(int fd, void *usrbuf, size_t n);
  void Rio_readinitb(rio_t *rp, int fd); 
  ssize_t Rio_readnb(rio_t *rp, void *usrbuf, size_t n);
  ssize_t Rio_readrestb(rio_t *rp, void *usrbuf);
  ssize_t Rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);
  
  /* Client/server helper functions */
  int open_clientfd(char *hostname, int portno);
  int open_listenfd(int portno);
  
  /* Wrappers for client/server helper functions */
  int Open_clientfd(char *hostname, int port);
  int Open_listenfd(int port); 
  
  #endif /* __CSAPP_H__ */
  