I open a file in read/write mode and do a series of reads, writes and seeks (from user input).
At some point later on I want to make the file read-only to prevent any further writes to it.
Is there a Linux (or POSIX) function to do that? Perhaps some fcntl
call?
Or is my only option is to save the current position in the file, close it and reopen RD_ONLY
?
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int fd = open("/path/to/file", O_RDWR);
// mixture of:
write(fd, ...);
lseek(fd, ...);
read (fd, ...);
// etc
...
// make file read-only ???
read (fd, ...); // OK
lseek(fd, ...); // OK
write(fd, ...); // error