Subject: reopening a readonly file as read-write while saving lock
To: None <>
From: Jeremy C. Reed <>
List: tech-misc
Date: 12/31/2001 17:04:12
Can fcntl(2) be used to change a readonly file as read/write?

Or how can I reopen a readonly and locked file so I can write to it?

Currently, I am doing:

   mbox = fopen(mailbox, "r");

   struct flock fl;
   fl.l_type = F_RDLCK;
   fl.l_whence = SEEK_CUR;
   fl.l_start = 0;
   fl.l_len = 0;
   fcntl(fileno(mbox), F_SETLK, &fl)

The file is locked while reading and using.

   mbox = freopen(mailbox, "r+", mbox);

But now the file is unlocked :(

It is unclear with the fcntl(2) manual page on how I can used fcntl to
change it to read/write.

The fcntl manual page doesn't even mention O_RDWR.

Can anyone explain this or share an example or point me to the correct

I guess I should just try it ...

fcntl(fileno(mbox), F_SETFL, O_RDRW);

... It doesn't seem to work (the lock is saved but it is not writable).

Is this list appropriate to ask for this info?

What other mailing lists would be good?

What are some good books on Unix programming? (I have Curry's Unix Systems
Programming for SVR4.)


   Jeremy C. Reed