]>
git.scottworley.com Git - overonion/blob - reverse_lib.c
f217b1cf2a46bf4c9c5eea4f53b68b16bd0adceb
1 #define _FILE_OFFSET_BITS 64
11 #include <sys/types.h>
15 void reverse_file(const char* input_filename
, FILE* output_stream
) {
16 int fd
= open(input_filename
, O_RDONLY
);
17 if (fd
== -1) err(EX_NOINPUT
, "Could not open specified file");
20 if (fstat(fd
, &stats
) == -1) err(EX_NOINPUT
, "Could not stat input");
22 long page_size
= sysconf(_SC_PAGE_SIZE
);
23 off_t pages
= (stats
.st_size
- 1) / page_size
+ 1;
24 long map_size
= pages
* page_size
;
25 char *m
= mmap(NULL
, map_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
26 if (m
== MAP_FAILED
) err(EX_NOINPUT
, "Could not mmap input");
28 for (off_t p
= stats
.st_size
- 1; p
>= 0; p
--) {
29 if (fputc(m
[p
], output_stream
) == EOF
) errx(EX_IOERR
, "Could not write");
32 if (munmap(m
, map_size
) == -1) err(EX_IOERR
, "Could not unmap input");
33 if (close(fd
) == -1) err(EX_IOERR
, "Could not close input");
36 /* Copy data from input to output until EOF is reached. */
37 static void copy(FILE* input
, FILE* output
) {
41 if (ferror(input
)) errx(EX_IOERR
, "Could not read");
42 if (!feof(input
)) errx(EX_IOERR
, "Unexpected end of file");
45 if (fputc(c
, output
) == EOF
) errx(EX_IOERR
, "Could not write");
49 void reverse_stream(FILE* input_stream
, FILE* output_stream
) {
52 make_temporary_file(&temp_filename
, &temp_file
);
54 copy(input_stream
, temp_file
);
55 if (fclose(temp_file
) != 0) err(EX_IOERR
, "Could not close temporary file");
57 reverse_file(temp_filename
, output_stream
);
59 if (unlink(temp_filename
) == -1) err(EX_IOERR
, "Could not remove temporary file");