Simple question about mmap() system call




Hello!

I'm writing an utility that should examine some bytes of a large file
and modify them - that't all. I've decided to mmap() the file:

void *diskp;

if ((fd=open(argv[1], O_RDWR)) == -1)
err(EX_NOINPUT, "Can't open %s for readind and writing", argv[1]);

if ((diskp=mmap(NULL, 512,
PROT_READ | PROT_WRITE, 0, fd, 0)) == MAP_FAILED)
err(EX_IOERR, "Can't mmap() file");

printf("<%c>\n",* (char *)diskp);
* (char *)diskp = '!';

if (msync(diskp, 0, MS_SYNC) || close(fd))
err(EX_IOERR, "Error closing file");

All proceeds w/o errors with the sample (2 bytes long) file, printf()
shows actual first byte of my file. But modification doesn't get written back to the disk, file contents are unchanged after execution of my code. I'm sure I'm overlooking something very basic and stupid, but can't find what exactly.

Sincerely, Dmitry
--
Atlantis ISP, System Administrator
e-mail: dmitry@xxxxxxxxxxxxxx
nic-hdl: LYNX-RIPE
_______________________________________________
freebsd-hackers@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Simple question about mmap() system call
    ... I'm writing an utility that should examine some bytes of a large file ... and modify them - that't all. ... I've decided to mmap() the file: ... If the facts don't fit the theory, ...
    (freebsd-hackers)
  • Re: Simple question about mmap() system call
    ... file and modify them - that't all. ... I've decided to mmap() the file: ... void *diskp; ...
    (freebsd-hackers)
  • Re: What the?
    ... void f1 ... So if you're writing i++ as a statement rather than as part of a larger ... Old line C programmers are used to writing ... there's no rational reason to cling to that preference in C++. ...
    (microsoft.public.vc.mfc)
  • Re: memfrob and strfry ? OT
    ... It's a very very poor encryption routine (it merely xors each byte ... attempts to modify the first 64 bytes of the string literal "file". ... but a function that returned a void* and took a void*s as a first parameter. ... My advice: Forget about memfrob(). ...
    (comp.lang.c)
  • const and call by value parameters
    ... Now function gdoesn't modify it's local copy of d in any way, ... void myobject::g ... lot of function parameters using the const modifier. ... Mark ...
    (microsoft.public.vc.language)