uopspop

Untitled

Jul 5th, 2020
93
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * csapp.h - prototypes and definitions for the CS:APP3e book
  3.  */
  4. /* $begin csapp.h */
  5. #ifndef __CSAPP_H__
  6. #define __CSAPP_H__
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <setjmp.h>
  15. #include <signal.h>
  16. #include <dirent.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <sys/mman.h>
  23. #include <errno.h>
  24. #include <math.h>
  25. #include <pthread.h>
  26. #include <semaphore.h>
  27. #include <sys/socket.h>
  28. #include <netdb.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31.  
  32. /* Default file permissions are DEF_MODE & ~DEF_UMASK */
  33. /* $begin createmasks */
  34. #define DEF_MODE   S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
  35. #define DEF_UMASK  S_IWGRP|S_IWOTH
  36. /* $end createmasks */
  37.  
  38. /* Simplifies calls to bind(), connect(), and accept() */
  39. /* $begin sockaddrdef */
  40. typedef struct sockaddr SA;
  41. /* $end sockaddrdef */
  42.  
  43. /* Persistent state for the robust I/O (Rio) package */
  44. /* $begin rio_t */
  45. #define RIO_BUFSIZE 8192
  46. typedef struct {
  47.     int rio_fd;                /* Descriptor for this internal buf */
  48.     int rio_cnt;               /* Unread bytes in internal buf */
  49.     char *rio_bufptr;          /* Next unread byte in internal buf */
  50.     char rio_buf[RIO_BUFSIZE]; /* Internal buffer */
  51. } rio_t;
  52. /* $end rio_t */
  53.  
  54. /* External variables */
  55. extern int h_errno;    /* Defined by BIND for DNS errors */
  56. extern char **environ; /* Defined by libc */
  57.  
  58. /* Misc constants */
  59. #define MAXLINE  8192  /* Max text line length */
  60. #define MAXBUF   8192  /* Max I/O buffer size */
  61. #define LISTENQ  1024  /* Second argument to listen() */
  62.  
  63. /* Our own error-handling functions */
  64. void unix_error(char *msg);
  65. void posix_error(int code, char *msg);
  66. void dns_error(char *msg);
  67. void gai_error(int code, char *msg);
  68. void app_error(char *msg);
  69.  
  70. /* Process control wrappers */
  71. pid_t Fork(void);
  72. void Execve(const char *filename, char *const argv[], char *const envp[]);
  73. pid_t Wait(int *status);
  74. pid_t Waitpid(pid_t pid, int *iptr, int options);
  75. void Kill(pid_t pid, int signum);
  76. unsigned int Sleep(unsigned int secs);
  77. void Pause(void);
  78. unsigned int Alarm(unsigned int seconds);
  79. void Setpgid(pid_t pid, pid_t pgid);
  80. pid_t Getpgrp();
  81.  
  82. /* Signal wrappers */
  83. typedef void handler_t(int);
  84. handler_t *Signal(int signum, handler_t *handler);
  85. void Sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
  86. void Sigemptyset(sigset_t *set);
  87. void Sigfillset(sigset_t *set);
  88. void Sigaddset(sigset_t *set, int signum);
  89. void Sigdelset(sigset_t *set, int signum);
  90. int Sigismember(const sigset_t *set, int signum);
  91. int Sigsuspend(const sigset_t *set);
  92.  
  93. /* Sio (Signal-safe I/O) routines */
  94. ssize_t sio_puts(char s[]);
  95. ssize_t sio_putl(long v);
  96. void sio_error(char s[]);
  97.  
  98. /* Sio wrappers */
  99. ssize_t Sio_puts(char s[]);
  100. ssize_t Sio_putl(long v);
  101. void Sio_error(char s[]);
  102.  
  103. /* Unix I/O wrappers */
  104. int Open(const char *pathname, int flags, mode_t mode);
  105. ssize_t Read(int fd, void *buf, size_t count);
  106. ssize_t Write(int fd, const void *buf, size_t count);
  107. off_t Lseek(int fildes, off_t offset, int whence);
  108. void Close(int fd);
  109. int Select(int  n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  110.        struct timeval *timeout);
  111. int Dup2(int fd1, int fd2);
  112. void Stat(const char *filename, struct stat *buf);
  113. void Fstat(int fd, struct stat *buf) ;
  114.  
  115. /* Directory wrappers */
  116. DIR *Opendir(const char *name);
  117. struct dirent *Readdir(DIR *dirp);
  118. int Closedir(DIR *dirp);
  119.  
  120. /* Memory mapping wrappers */
  121. void *Mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
  122. void Munmap(void *start, size_t length);
  123.  
  124. /* Standard I/O wrappers */
  125. void Fclose(FILE *fp);
  126. FILE *Fdopen(int fd, const char *type);
  127. char *Fgets(char *ptr, int n, FILE *stream);
  128. FILE *Fopen(const char *filename, const char *mode);
  129. void Fputs(const char *ptr, FILE *stream);
  130. size_t Fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
  131. void Fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
  132.  
  133. /* Dynamic storage allocation wrappers */
  134. void *Malloc(size_t size);
  135. void *Realloc(void *ptr, size_t size);
  136. void *Calloc(size_t nmemb, size_t size);
  137. void Free(void *ptr);
  138.  
  139. /* Sockets interface wrappers */
  140. int Socket(int domain, int type, int protocol);
  141. void Setsockopt(int s, int level, int optname, const void *optval, int optlen);
  142. void Bind(int sockfd, struct sockaddr *my_addr, int addrlen);
  143. void Listen(int s, int backlog);
  144. int Accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  145. void Connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
  146.  
  147. /* Protocol independent wrappers */
  148. void Getaddrinfo(const char *node, const char *service,
  149.                  const struct addrinfo *hints, struct addrinfo **res);
  150. void Getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host,
  151.                  size_t hostlen, char *serv, size_t servlen, int flags);
  152. void Freeaddrinfo(struct addrinfo *res);
  153. void Inet_ntop(int af, const void *src, char *dst, socklen_t size);
  154. void Inet_pton(int af, const char *src, void *dst);
  155.  
  156. /* DNS wrappers */
  157. struct hostent *Gethostbyname(const char *name);
  158. struct hostent *Gethostbyaddr(const char *addr, int len, int type);
  159.  
  160. /* Pthreads thread control wrappers */
  161. void Pthread_create(pthread_t *tidp, pthread_attr_t *attrp,
  162.             void * (*routine)(void *), void *argp);
  163. void Pthread_join(pthread_t tid, void **thread_return);
  164. void Pthread_cancel(pthread_t tid);
  165. void Pthread_detach(pthread_t tid);
  166. void Pthread_exit(void *retval);
  167. pthread_t Pthread_self(void);
  168. void Pthread_once(pthread_once_t *once_control, void (*init_function)());
  169.  
  170. /* POSIX semaphore wrappers */
  171. void Sem_init(sem_t *sem, int pshared, unsigned int value);
  172. void P(sem_t *sem);
  173. void V(sem_t *sem);
  174.  
  175. /* Rio (Robust I/O) package */
  176. ssize_t rio_readn(int fd, void *usrbuf, size_t n);
  177. ssize_t rio_writen(int fd, void *usrbuf, size_t n);
  178. void rio_readinitb(rio_t *rp, int fd);
  179. ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n);
  180. ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);
  181.  
  182. /* Wrappers for Rio package */
  183. ssize_t Rio_readn(int fd, void *usrbuf, size_t n);
  184. void Rio_writen(int fd, void *usrbuf, size_t n);
  185. void Rio_readinitb(rio_t *rp, int fd);
  186. ssize_t Rio_readnb(rio_t *rp, void *usrbuf, size_t n);
  187. ssize_t Rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);
  188.  
  189. /* Reentrant protocol-independent client/server helpers */
  190. int open_clientfd(char *hostname, char *port);
  191. int open_listenfd(char *port);
  192.  
  193. /* Wrappers for reentrant protocol-independent client/server helpers */
  194. int Open_clientfd(char *hostname, char *port);
  195. int Open_listenfd(char *port);
  196.  
  197.  
  198. #endif /* __CSAPP_H__ */
  199. /* $end csapp.h */
RAW Paste Data