An example of mmap for a read-only file

This example C program illustrates the use of mmap to map a file which has been opened read-only to memory.

/* For the size of the file. */
#include <sys/stat.h>
/* This contains the mmap calls. */
#include <sys/mman.h> 
/* These are for error printing. */
#include <errno.h>
#include <string.h>
#include <stdarg.h>
/* This is for open. */
#include <fcntl.h>
#include <stdio.h>
/* For exit. */
#include <stdlib.h>
/* For the final part of the example. */
#include <ctype.h>

/* "check" checks "test" and prints an error and exits if it is
   true. */

static void
check (int test, const char * message, ...)
{
    if (test) {
        va_list args;
        va_start (args, message);
        vfprintf (stderr, message, args);
        va_end (args);
        fprintf (stderr, "\n");
        exit (EXIT_FAILURE);
    }
}

int main ()
{
    /* The file descriptor. */
    int fd;
    /* Information about the file. */
    struct stat s;
    int status;
    size_t size;
    /* The file name to open. */
    const char * file_name = "me.c";
    /* The memory-mapped thing itself. */
    const char * mapped;
    int i;

    /* Open the file for reading. */
    fd = open ("me.c", O_RDONLY);
    check (fd < 0, "open %s failed: %s", file_name, strerror (errno));

    /* Get the size of the file. */
    status = fstat (fd, & s);
    check (status < 0, "stat %s failed: %s", file_name, strerror (errno));
    size = s.st_size;

    /* Memory-map the file. */
    mapped = mmap (0, size, PROT_READ, 0, fd, 0);
    check (mapped == MAP_FAILED, "mmap %s failed: %s",
           file_name, strerror (errno));

    /* Now do something with the information. */
    for (i = 0; i < size; i++) {
        char c;

        c = mapped[i];
        if (! isalpha (c) && ! isspace (c)) {
            putchar (c);
        }
        if (i % 80 == 79) {
            putchar ('\n');
        }
    }

    return 0;
}

(download)

The output of the example (the above C file with all the spaces and letters removed) looks like this:

/*.*/#</.>/*
.*/#</.>/*.*/#<
.>#<.>#<.>/*.*/#
<.>#<.>/*.*/#<.>/*
.*/#<.>/*""""
.*/(,
*,...){(){_;_(
,);(,,);_();
(,"\");(_);}}()
{/*.*/;/*.
*/;;_;/*
.*/*_=".";/*-
.*/*;;/*.*
/=(".",_);(<0,"%:%",
_,());/*.*/=
(,&);(<0,"%:%",_,(
));=._;/*-.*/=
(0,,_,0,,0);(==_,"%
:%",_,());/*
.*/(=0;<;++){;=
[];(!()&&!()){(
);}(%80==79){('\');}
}0;}

Copyright © Ben Bullock 2009-2017. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer