Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * Simple program to compare two mmap'd areas.
  3  *
  4  * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
  5  * Copyright (C) 2001 O'Reilly & Associates
  6  *
  7  * The source code in this file can be freely used, adapted,
  8  * and redistributed in source or binary form, so long as an
  9  * acknowledgment appears in derived source files.  The citation
 10  * should list that the code comes from the book "Linux Device
 11  * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 12  * by O'Reilly & Associates.   No warranty is attached;
 13  * we cannot take responsibility for errors or fitness for use.
 14  *
 15  * $Id: mapcmp.c,v 1.2 2004/03/05 17:35:41 corbet Exp $
 16  */
 17 
 18 #include <stdio.h>
 19 #include <stdlib.h>
 20 #include <unistd.h>
 21 #include <sys/mman.h>
 22 #include <sys/errno.h>
 23 #include <fcntl.h>
 24 
 25 static char *mapdev (const char *, unsigned long, unsigned long);
 26 #define PAGE_SIZE 4096
 27 
 28 /*
 29  * memcmp dev1 dev2 offset pages
 30  */
 31 int main (int argc, char **argv)
 32 {
 33         unsigned long offset, size, i;
 34         char *addr1, *addr2;
 35 /*
 36  * Sanity check.
 37  */
 38         if (argc != 5)
 39         {
 40                 fprintf (stderr, "Usage: mapcmp dev1 dev2 offset pages\n");
 41                 exit (1);
 42         }
 43 /*
 44  * Map the two devices.
 45  */
 46         offset = strtoul (argv[3], NULL, 16);
 47         size = atoi (argv[4])*PAGE_SIZE;
 48         printf ("Offset is 0x%lx\n", offset);
 49         addr1 = mapdev (argv[1], offset, size);
 50         addr2 = mapdev (argv[2], offset, size);
 51 /*
 52  * Do the comparison.
 53  */
 54         printf ("Comparing...");
 55         fflush (stdout);
 56         for (i = 0; i < size; i++)
 57                 if (*addr1++ != *addr2++)
 58                 {
 59                         printf ("areas differ at byte %ld\n", i);
 60                         exit (0);
 61                 }
 62         printf ("areas are identical.\n");
 63         exit (0);
 64 }
 65 
 66 
 67 
 68 static char *mapdev (const char *dev, unsigned long offset,
 69                 unsigned long size)
 70 {
 71         char *addr;
 72         int fd = open (dev, O_RDONLY);
 73 
 74         if (fd < 0)
 75         {
 76                 perror (dev);
 77                 exit (1);
 78         }
 79         addr = mmap (0, size, PROT_READ, MAP_PRIVATE, fd, offset);
 80         if (addr == MAP_FAILED)
 81         {
 82                 perror (dev);
 83                 exit (1);
 84         }
 85         printf ("Mapped %s (%lu @ %lx) at %p\n", dev, size, offset, addr);
 86         return (addr);
 87 }
 88 
  This page was automatically generated by the LXR engine.