1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
| #include "rio_csapp.h"
void unix_error(char *msg) { fprintf(stderr, "%s: %s\n", msg, strerror(errno)); exit(0); }
ssize_t rio_readn(int fd, void *usrbuf, size_t n) { size_t nleft = n; ssize_t nread; char *bufp = usrbuf;
while (nleft > 0) { if ((nread = read(fd, bufp, nleft)) < 0) { if (errno == EINTR) nread = 0; else return -1; } else if (nread == 0) break; nleft -= nread; bufp += nread; } return (n - nleft); }
ssize_t rio_writen(int fd, void *usrbuf, size_t n) { size_t nleft = n; ssize_t nwritten; char *bufp = usrbuf;
while (nleft > 0) { if ((nwritten = write(fd, bufp, nleft)) <= 0) { if (errno == EINTR) nwritten = 0; else return -1; } nleft -= nwritten; bufp += nwritten; } return n; }
static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n) { int cnt;
while (rp->rio_cnt <= 0) { rp->rio_cnt = read(rp->rio_fd, rp->rio_buf, sizeof(rp->rio_buf)); if (rp->rio_cnt < 0) { if (errno != EINTR) return -1; } else if (rp->rio_cnt == 0) return 0; else rp->rio_bufptr = rp->rio_buf; }
cnt = n; if (rp->rio_cnt < n) cnt = rp->rio_cnt; memcpy(usrbuf, rp->rio_bufptr, cnt); rp->rio_bufptr += cnt; rp->rio_cnt -= cnt; return cnt; }
void rio_readinitb(rio_t *rp, int fd) { rp->rio_fd = fd; rp->rio_cnt = 0; rp->rio_bufptr = rp->rio_buf; }
ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n) { size_t nleft = n; ssize_t nread; char *bufp = usrbuf;
while (nleft > 0) { if ((nread = rio_read(rp, bufp, nleft)) < 0) return -1; else if (nread == 0) break; nleft -= nread; bufp += nread; } return (n - nleft); }
ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen) { int n, rc; char c, *bufp = usrbuf;
for (n = 1; n < maxlen; n++) { if ((rc = rio_read(rp, &c, 1)) == 1) { *bufp++ = c; if (c == '\n') { n++; break; } } else if (rc == 0) { if (n == 1) return 0; else break; } else return -1; } *bufp = 0; return n - 1; }
ssize_t Rio_readn(int fd, void *ptr, size_t nbytes) { ssize_t n;
if ((n = rio_readn(fd, ptr, nbytes)) < 0) unix_error("Rio_readn error"); return n; }
void Rio_writen(int fd, void *usrbuf, size_t n) { if (rio_writen(fd, usrbuf, n) != n) unix_error("Rio_writen error"); }
void Rio_readinitb(rio_t *rp, int fd) { rio_readinitb(rp, fd); }
ssize_t Rio_readnb(rio_t *rp, void *usrbuf, size_t n) { ssize_t rc;
if ((rc = rio_readnb(rp, usrbuf, n)) < 0) unix_error("Rio_readnb error"); return rc; }
ssize_t Rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen) { ssize_t rc;
if ((rc = rio_readlineb(rp, usrbuf, maxlen)) < 0) unix_error("Rio_readlineb error"); return rc; }
int Open(const char *pathname, int flags, mode_t mode) { int rc;
if ((rc = open(pathname, flags, mode)) < 0) unix_error("Open error"); return rc; }
ssize_t Read(int fd, void *buf, size_t count) { ssize_t rc;
if ((rc = read(fd, buf, count)) < 0) unix_error("Read error"); return rc; }
ssize_t Write(int fd, const void *buf, size_t count) { ssize_t rc;
if ((rc = write(fd, buf, count)) < 0) unix_error("Write error"); return rc; }
off_t Lseek(int fildes, off_t offset, int whence) { off_t rc;
if ((rc = lseek(fildes, offset, whence)) < 0) unix_error("Lseek error"); return rc; }
void Close(int fd) { int rc;
if ((rc = close(fd)) < 0) unix_error("Close error"); }
int Select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { int rc;
if ((rc = select(n, readfds, writefds, exceptfds, timeout)) < 0) unix_error("Select error"); return rc; }
int Dup2(int fd1, int fd2) { int rc;
if ((rc = dup2(fd1, fd2)) < 0) unix_error("Dup2 error"); return rc; }
void Stat(const char *filename, struct stat *buf) { if (stat(filename, buf) < 0) unix_error("Stat error"); }
void Fstat(int fd, struct stat *buf) { if (fstat(fd, buf) < 0) unix_error("Fstat error"); }
DIR *Opendir(const char *name) { DIR *dirp = opendir(name);
if (!dirp) unix_error("opendir error"); return dirp; }
struct dirent *Readdir(DIR *dirp) { struct dirent *dep;
errno = 0; dep = readdir(dirp); if ((dep == NULL) && (errno != 0)) unix_error("readdir error"); return dep; }
int Closedir(DIR *dirp) { int rc;
if ((rc = closedir(dirp)) < 0) unix_error("closedir error"); return rc; }
|