Created
January 29, 2014 09:33
-
-
Save padenot/8684546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/mman.h> | |
#include <sys/stat.h> | |
#include <errno.h> | |
int main(int argc, char *argv[]) | |
{ | |
int fd, offset; | |
char * data; | |
struct stat sbuf; | |
if (argc != 2) { | |
fprintf(stderr, "usage: %s offset\n", argv[0]); | |
exit(1); | |
} | |
if ((fd = open("mmap.c", O_RDWR)) == -1) { | |
perror("open"); | |
exit(1); | |
} | |
if (stat("mmap.c", &sbuf) == -1) { | |
perror("stat"); | |
exit(1); | |
} | |
offset = atoi(argv[1]); | |
if (offset < 0 || offset > sbuf.st_size - 1) { | |
fprintf(stderr, "offset must be in the range 0-%lu\n", sbuf.st_size - 1); | |
exit(1); | |
} | |
if ((data = mmap((caddr_t)0, | |
sbuf.st_size, | |
PROT_READ|PROT_WRITE, | |
MAP_SHARED, fd, 0)) == (caddr_t)(-1)) { | |
perror("mmap"); | |
exit(1); | |
} | |
printf("byte at offset %d is '%c'\n", offset, data[offset]); | |
printf("change it, and press enter\n"); | |
getchar(); | |
printf("byte at offset %d is now '%c'\n", offset, data[offset]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment