Subject: Re: reopening a readonly file as read-write while saving lock
To: Jeremy C. Reed <reed@reedmedia.net>
From: Giles Lean <giles@nemeton.com.au>
List: tech-misc
Date: 12/31/2001 17:47:53
"Jeremy C. Reed" <reed@reedmedia.net> wrote:

> Can fcntl(2) be used to change a readonly file as read/write?

No.

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

I recommend you open the file read/write in the first place -- that's
the simplest thing to do, and allows you to upgrade the shared (read)
lock to an exclusive (write) lock easily.

If you really want a read-only file descriptor you can open the file a
second read/write without closing the first file descriptor, but if
you use standard I/O (as your example is doing) for that then you have
two lots of buffering to deal with and it's more complicated.

> The file is locked while reading and using.
> 
>    mbox = freopen(mailbox, "r+", mbox);
> 
> But now the file is unlocked :(

That's because freopen(3) closed the file, and closing a file releases
locks held on it.

> Is this list appropriate to ask for this info?

I'd have used netbsd-help or netbsd-users, since these questions are
about programming on NetBSD rather than development of NetBSD itself.

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

I like W. Richard Stevens' "Advanced Programming in the Unix
Environment".

Regards,

Giles