mmap-read.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. *
  3. * Copyright (c) 2003 The Regents of the University of California. All
  4. * rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Neither the name of the University nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * FUSD - The Framework for UserSpace Devices - Example program
  32. *
  33. * This mmap a file/device and change it a bit.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include <errno.h>
  40. #include <ctype.h>
  41. #include <unistd.h>
  42. #include <sys/mman.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. static void hexdump(void *ptr_r, int size)
  48. {
  49. int J, I;
  50. unsigned char *ptr = ptr_r;
  51. unsigned long Addr = 0;
  52. if (ptr == NULL) {
  53. puts("NULL POINTER");
  54. puts("-------------------------------------------------------------------------------------");
  55. return;
  56. }
  57. while (Addr <= size) {
  58. for (J = 0; J < 2; J++) {
  59. printf("%08p: ", Addr + ptr);
  60. for (I = 0; I < 16; I++, Addr++) { if (Addr <= size) { printf("%02lX ", (unsigned char) ptr[Addr]); } else { printf(" "); } }
  61. printf(" | "); Addr -= 16;
  62. for (I = 0; I < 16; I++, Addr++) { if (Addr <= size) { putchar(isprint(ptr[Addr]) ? ptr[Addr] : '.'); } else { putchar(' '); } }
  63. puts("");
  64. }
  65. }
  66. puts("-------------------------------------------------------------------------------------");
  67. }
  68. int main(int argc, char *argv[])
  69. {
  70. int fd, i;
  71. char *ptr;
  72. int size = 0;
  73. struct stat FileStat;
  74. srand((unsigned int) time(NULL));
  75. if (argc != 3) {
  76. printf("Usage: %s file size");
  77. }
  78. fd = open(argv[1], O_RDWR);
  79. size = atoi(argv[2]);
  80. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  81. printf("ptr = %p\n", ptr);
  82. if ((ptr != NULL) && (ptr != MAP_FAILED)) {
  83. hexdump(ptr, size);
  84. /* Let's do some changes */
  85. for (i = 0; i < 128; i++) {
  86. ptr[i] ^= rand() % 0x100;
  87. }
  88. msync(ptr, size, MS_SYNC|MS_INVALIDATE);
  89. hexdump(ptr, size);
  90. }
  91. close(fd);
  92. return 0;
  93. }