#include #include #include #include #include #include #include #include int main (int argc, char **argv) { int c; int fd; void *addr; int read_write = 0, write_only = 0, private = 0; int prot, flags; const struct option options[] = { { "read-write", no_argument, &read_write, 1 }, { "write-only", no_argument, &write_only, 1 }, { "private", no_argument, &private, 1 }, { NULL, 0, NULL, 0 } }; while (1) { c = getopt_long (argc, argv, "", options, NULL); if (c == '?') return EXIT_FAILURE; if (c == -1) break; } if (optind != argc - 1) error (EXIT_FAILURE, 0, "please specify one file name"); if (read_write) flags = O_RDWR; else if (write_only) flags = O_WRONLY; else flags = O_RDONLY; fd = open (argv[optind], flags | O_NOCTTY); if (fd == -1) error (EXIT_FAILURE, errno, "failed to open"); if (read_write) prot = PROT_READ|PROT_WRITE; else if (write_only) prot = PROT_WRITE; else prot = PROT_READ; flags = MAP_FILE; if (private) flags |= MAP_PRIVATE; else flags |= MAP_SHARED; addr = mmap (NULL, 4096, prot, flags, fd, 0); if (addr == MAP_FAILED) error (EXIT_FAILURE, errno, "failed to mmap"); fprintf (stderr, "mmaped at %p\n", addr); fflush (stderr); if (!write_only) printf ("%s\n", addr); if (read_write || write_only) strcpy(addr, "well hello friends! :^)\n"); return 0; }