catpoint

simple slides viewer
git clone git://git.2f30.org/catpoint
Log | Files | Refs | README | LICENSE

commit 3d7dc566bca9e1908a459e7647d8a7761c6ae41a
parent cf2f323461e9d37b8f4e582e5179997850fa41c9
Author: sin <sin@2f30.org>
Date:   Mon, 29 Jul 2013 17:54:53 +0300

Add struct slide and fix overflow

printw(p[i]) will not work if there is no null terminator.

Diffstat:
Mcatpoint.c | 33++++++++++++++++++++++++++-------
1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/catpoint.c b/catpoint.c @@ -1,6 +1,7 @@ /* $Id: catpoint.c,v 1.2 2013/03/28 12:00:48 lostd Exp $ */ #include <sys/types.h> +#include <sys/stat.h> #include <sys/mman.h> #include <err.h> @@ -10,19 +11,35 @@ #include <stdlib.h> #include <unistd.h> +struct slide { + unsigned char *buf; + size_t siz; +}; + +int +printslide(struct slide *s) +{ + unsigned char *p = s->buf; + size_t i; + + for (i = 0; i < s->siz; i++) + printw("%c", *p++); +} + int main(int argc, char *argv[]) { int c, i, fd; - char **p; /* the slides */ + struct slide *s; + struct stat sb; if (argc == 1) errx(1, "usage: %s file ...", argv[0]); argv++; argc--; - p = malloc(argc * sizeof(*p)); - if (!p) + s = malloc(argc * sizeof(*s)); + if (!s) err(1, "malloc"); /* map files to mem */ @@ -30,9 +47,11 @@ main(int argc, char *argv[]) fd = open(argv[i], O_RDONLY); if (fd == -1) err(1, "open: %s", argv[i]); - p[i] = mmap(NULL, 0x1000, PROT_READ, MAP_PRIVATE, fd, 0); - if (p[i] == MAP_FAILED) + fstat(fd, &sb); + s[i].buf = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (s[i].buf == MAP_FAILED) err(1, "mmap"); + s[i].siz = sb.st_size; close(fd); } @@ -50,7 +69,7 @@ main(int argc, char *argv[]) show: /* display slide */ clear(); - printw(p[i]); + printslide(&s[i]); again: c = getch(); switch (c) { @@ -82,7 +101,7 @@ again: /* unmap mem */ for (i = 0; argv[i] != NULL; i++) - munmap(p[i], 0x1000); + munmap(s[i].buf, s[i].siz); endwin(); /* restore terminal */