update mapper1.c mapper2.c
authorErez Zadok <ezk@cs.sunysb.edu>
Thu, 12 May 2022 22:09:44 +0000 (18:09 -0400)
committerErez Zadok <ezk@cs.sunysb.edu>
Thu, 12 May 2022 22:09:44 +0000 (18:09 -0400)
tests/mapper1.c [new file with mode: 0644]
tests/mapper2.c [new file with mode: 0644]

diff --git a/tests/mapper1.c b/tests/mapper1.c
new file mode 100644 (file)
index 0000000..f0af4e9
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 1997-2007 Erez Zadok <ezk@cs.stonybrook.edu>
+ * Copyright (c) 2001-2007 Stony Brook University
+ *
+ * For specific licensing information, see the COPYING file distributed with
+ * this package, or get one from
+ * ftp://ftp.filesystems.org/pub/fistgen/COPYING.
+ *
+ * This Copyright notice must be kept intact and distributed with all
+ * fistgen sources INCLUDING sources generated by fistgen.
+ *
+ * increment the first byte of a file by 1
+ * usage: mapper file [flag]
+ */
+
+#include <stdio.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#define BASE 0
+#define SIZE 10
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
+int
+main(int argc, char *argv[])
+{
+  int fd, i;
+  caddr_t pa;
+
+  fd = open(argv[1], O_RDWR);
+  if (fd < 0) {
+    perror(argv[1]);
+    exit(1);
+  }
+  printf("opened file %s\n", argv[1]);
+
+  pa = mmap((caddr_t) 0,
+           SIZE,
+           (PROT_READ | PROT_WRITE),
+           MAP_SHARED,
+           fd,
+           BASE);
+  if (pa == MAP_FAILED) {
+    perror("mmap");
+    exit(1);
+  }
+  printf("file is mapped with address 0x%x\n", (u_int) pa);
+  close(fd);
+  printf("closed file %s\n", argv[1]);
+
+  fd = open(argv[1], O_RDWR);
+  if (fd < 0) {
+    perror(argv[1]);
+    exit(1);
+  }
+  printf("opened file %s\n", argv[1]);
+  close(fd);
+  printf("closed file %s\n", argv[1]);
+
+#if 1
+  for (i = 0; i < SIZE; ++i) {
+    printf("\tbyte %2d is %3d (%c)\n", i, pa[i],
+          isprint(pa[i]) ? pa[i] : '.');
+  }
+
+  /* hack to modify one byte in the mapped page */
+  if (argc > 2) {
+    printf("hit ENTER to try and modify the page...\n");
+    getchar();
+    pa[0] += 1;
+    printf("waiting...");
+    getchar();
+  }
+#endif
+  if (munmap(pa, SIZE) < 0) {
+    perror("munmap");
+    exit(1);
+  }
+  printf("file is unmapped\n");
+
+  exit(0);
+}
diff --git a/tests/mapper2.c b/tests/mapper2.c
new file mode 100644 (file)
index 0000000..e175d50
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 1997-2007 Erez Zadok <ezk@cs.stonybrook.edu>
+ * Copyright (c) 2001-2007 Stony Brook University
+ *
+ * For specific licensing information, see the COPYING file distributed with
+ * this package, or get one from
+ * ftp://ftp.filesystems.org/pub/fistgen/COPYING.
+ *
+ * This Copyright notice must be kept intact and distributed with all
+ * fistgen sources INCLUDING sources generated by fistgen.
+ *
+ * increment the first byte of a file by 1
+ * usage: mapper2 [flag]
+ */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <stdio.h>
+
+#define BASE 0
+#define SIZE 10
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
+int
+main(int argc, char *argv[])
+{
+  int fd, i, counter = 10;
+  caddr_t pa;
+  char buf[SIZE];
+
+  fd = open(argv[1], O_RDONLY);
+  if (fd < 0) {
+    perror(argv[1]);
+    exit(1);
+  }
+  printf("opened file %s\n", argv[1]);
+
+  pa = mmap((caddr_t) 0,
+           SIZE,
+           (PROT_READ),
+           MAP_SHARED,
+           fd,
+           BASE);
+  if (pa == MAP_FAILED) {
+    perror("mmap");
+    exit(1);
+  }
+  printf("file is mapped with address 0x%x\n", (u_int) pa);
+
+  while (counter--) {
+    memset(buf, 0, SIZE);
+    memcpy(buf, pa, SIZE-1);
+    printf("%d: contents of file: %s\n", counter, buf);
+    sleep(1);
+  }
+
+  if (munmap(pa, SIZE) < 0) {
+    perror("munmap");
+    exit(1);
+  }
+  printf("file is unmapped\n");
+  close(fd);
+  printf("closed file %s\n", argv[1]);
+
+  exit(0);
+}