- int fd = open(argv[1], O_RDONLY);
- if (fd == -1) {
- err(EX_NOINPUT, "Could not open specified file");
- }
-
- struct stat stats;
- if (fstat(fd, &stats) == -1) {
- err(EX_NOINPUT, "Could not stat input");
- }
-
- long page_size = sysconf(_SC_PAGE_SIZE);
- off_t pages = (stats.st_size - 1) / page_size + 1;
- long map_size = pages * page_size;
- char *m = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, 0);
- if (m == MAP_FAILED) {
- err(EX_NOINPUT, "Could not mmap input");
- }
-
- char buf[BUFFER_SIZE];
- off_t buf_offset = 0;
- for (off_t p = stats.st_size - 1; p >= 0; p--) {
- buf[buf_offset++] = m[p];
- if (buf_offset >= BUFFER_SIZE) {
- write_all(1, buf, buf_offset);
- buf_offset = 0;
- }
- }
- if (buf_offset) {
- write_all(1, buf, buf_offset);
- }
-
- if (munmap(m, map_size) == -1) {
- err(EX_IOERR, "Could not unmap input");
- }
- if (close(fd) == -1) {
- err(EX_IOERR, "Could not close input");
- }