tStat each file and mmap(3) only as much as is really needed - catpoint - Catpoint simple presenting software.
 (HTM) git clone git://r-36.net/catpoint
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 7f4f52730310c65d30761a75ded86e2d82863298
 (DIR) parent 78504360bf7518b5f7caadcf34ecb22187bdeeb8
 (HTM) Author: Ekkie <ekkie@envs.net>
       Date:   Mon,  4 May 2020 12:51:49 +0200
       
       Stat each file and mmap(3) only as much as is really needed
       
       Signed-off-by: Christoph Lohmann <20h@r-36.net>
       
       Diffstat:
         catpoint.c                          |       6 +++++-
       
       1 file changed, 5 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/catpoint.c b/catpoint.c
       t@@ -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>
       t@@ -42,6 +43,7 @@ int
        main(int argc, char *argv[])
        {
                int c, i, fd;
       +        struct stat statbuf;
        
                if (argc == 1)
                        errx(1, "usage: %s file ...", argv[0]);
       t@@ -59,7 +61,9 @@ main(int argc, char *argv[])
                        fd = open(argv[i], O_RDONLY, 0);
                        if (fd == -1)
                                err(1, "open: %s", argv[i]);
       -                p[i] = mmap(NULL, 0x1000, PROT_READ, MAP_PRIVATE, fd, 0);
       +                if (fstat(fd, &statbuf) < 0)
       +                        err(1, "fstat: %s", argv[i]);
       +                p[i] = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
                        if (p[i] == MAP_FAILED)
                                err(1, "mmap");
                        close(fd);