| 97 |
if (fstat(fd, &sb)) |
if (fstat(fd, &sb)) |
| 98 |
err(255, "cannot stat"); |
err(255, "cannot stat"); |
| 99 |
|
|
| 100 |
buf = mmap(NULL, (len = sb.st_size), PROT_READ, MAP_FILE, fd, 0); |
/* slurp whole file into mapped memory */ |
| 101 |
if (buf == MAP_FAILED) |
if ((cp = mmap(NULL, (len = sb.st_size), PROT_READ, MAP_FILE, fd, 0)) |
| 102 |
|
== MAP_FAILED) |
| 103 |
err(255, "cannot mmap %zu bytes", len); |
err(255, "cannot mmap %zu bytes", len); |
| 104 |
|
/* make a nice NUL-terminated copy (malloc'd) */ |
| 105 |
|
buf = str_nsave(cp, len); |
| 106 |
|
if (munmap(cp, len)) |
| 107 |
|
err(255, "cannot munmap"); |
| 108 |
|
/* don't need the file any more */ |
| 109 |
|
close(fd); |
| 110 |
|
|
| 111 |
|
/* now we can operate on the NUL-terminated R/W string “buf” */ |