How to avoid overwriting read-only files

Hi,

I use: f = fopen(“MyFile”, “wb”);
to open a binary file.

Then I use:
fwrite(…,…,…,f);
to write some data to the file;

The problem is, when MyFile exists and is read-only (chmod a-w), fopen opens
the file successfully and fwrite successfully writes data to the file.

What am I doing wrong, or what did I misunderstood?

Thank you.

Are you running your test as root?

chris

Djibril NDIAYE <djibril.ndiaye@opal-rt.com> wrote:

Hi,

I use: f = fopen(“MyFile”, “wb”);
to open a binary file.

Then I use:
fwrite(…,…,…,f);
to write some data to the file;

The problem is, when MyFile exists and is read-only (chmod a-w), fopen opens
the file successfully and fwrite successfully writes data to the file.

What am I doing wrong, or what did I misunderstood?

Thank you.
\


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Yes, I am running the test as root.

Thanks for replying.

“Chris McKillop” <cdm@qnx.com> wrote in message
news:alqv3n$85$1@nntp.qnx.com

Are you running your test as root?

chris

Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Hi,

I use: f = fopen(“MyFile”, “wb”);
to open a binary file.

Then I use:
fwrite(…,…,…,f);
to write some data to the file;

The problem is, when MyFile exists and is read-only (chmod a-w), fopen
opens
the file successfully and fwrite successfully writes data to the file.

What am I doing wrong, or what did I misunderstood?

Thank you.



\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Root is special and will always be able to write over files regardless of
the set perms. So, you can stat() the file and check for perms if you
need to run as root and not over-write files.

chris


Djibril NDIAYE <djibril.ndiaye@opal-rt.com> wrote:

Yes, I am running the test as root.

Thanks for replying.

“Chris McKillop” <> cdm@qnx.com> > wrote in message
news:alqv3n$85$> 1@nntp.qnx.com> …

Are you running your test as root?

chris

Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Hi,

I use: f = fopen(“MyFile”, “wb”);
to open a binary file.

Then I use:
fwrite(…,…,…,f);
to write some data to the file;

The problem is, when MyFile exists and is read-only (chmod a-w), fopen
opens
the file successfully and fwrite successfully writes data to the file.

What am I doing wrong, or what did I misunderstood?

Thank you.



\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

\


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

No, this is normal. For example…

qnx# cd /tmp
qnx# touch foo
qnx# chown cdm:techies foo
qnx# chmod 000 foo
qnx# ls -l foo
---------- 1 cdm techies 0 Sep 16 11:25 foo
qnx# echo Hello > foo
qnx# cat foo
Hello
qnx# ls -l foo
---------- 1 cdm techies 6 Sep 16 11:25 foo
qnx# uname
FreeBSD
qnx#

…root is god on UNIX and nothing gets in the way of god. :slight_smile:

chris


Djibril NDIAYE <djibril.ndiaye@opal-rt.com> wrote:

Ok. Thanks a lot … but one more question:
Does UNIX/LINUX systems work like this or is it a “feature” of QNX?

Djibril.


“Chris McKillop” <> cdm@qnx.com> > wrote in message
news:am4pph$3ff$> 1@nntp.qnx.com> …

Root is special and will always be able to write over files regardless of
the set perms. So, you can stat() the file and check for perms if you
need to run as root and not over-write files.

chris


Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Yes, I am running the test as root.

Thanks for replying.

“Chris McKillop” <> cdm@qnx.com> > wrote in message
news:alqv3n$85$> 1@nntp.qnx.com> …

Are you running your test as root?

chris

Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Hi,

I use: f = fopen(“MyFile”, “wb”);
to open a binary file.

Then I use:
fwrite(…,…,…,f);
to write some data to the file;

The problem is, when MyFile exists and is read-only (chmod a-w),
fopen
opens
the file successfully and fwrite successfully writes data to the
file.

What am I doing wrong, or what did I misunderstood?

Thank you.



\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I
get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/




\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

\


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Ok. Thanks a lot … but one more question:
Does UNIX/LINUX systems work like this or is it a “feature” of QNX?

Djibril.


“Chris McKillop” <cdm@qnx.com> wrote in message
news:am4pph$3ff$1@nntp.qnx.com

Root is special and will always be able to write over files regardless of
the set perms. So, you can stat() the file and check for perms if you
need to run as root and not over-write files.

chris


Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Yes, I am running the test as root.

Thanks for replying.

“Chris McKillop” <> cdm@qnx.com> > wrote in message
news:alqv3n$85$> 1@nntp.qnx.com> …

Are you running your test as root?

chris

Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Hi,

I use: f = fopen(“MyFile”, “wb”);
to open a binary file.

Then I use:
fwrite(…,…,…,f);
to write some data to the file;

The problem is, when MyFile exists and is read-only (chmod a-w),
fopen
opens
the file successfully and fwrite successfully writes data to the
file.

What am I doing wrong, or what did I misunderstood?

Thank you.



\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I
get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/




\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

God ! … thank you very much.

“Chris McKillop” <cdm@qnx.com> wrote in message
news:am4qe8$44u$1@nntp.qnx.com

No, this is normal. For example…

qnx# cd /tmp
qnx# touch foo
qnx# chown cdm:techies foo
qnx# chmod 000 foo
qnx# ls -l foo
---------- 1 cdm techies 0 Sep 16 11:25 foo
qnx# echo Hello > foo
qnx# cat foo
Hello
qnx# ls -l foo
---------- 1 cdm techies 6 Sep 16 11:25 foo
qnx# uname
FreeBSD
qnx#

…root is god on UNIX and nothing gets in the way of god. > :slight_smile:

chris


Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Ok. Thanks a lot … but one more question:
Does UNIX/LINUX systems work like this or is it a “feature” of QNX?

Djibril.


“Chris McKillop” <> cdm@qnx.com> > wrote in message
news:am4pph$3ff$> 1@nntp.qnx.com> …

Root is special and will always be able to write over files regardless
of
the set perms. So, you can stat() the file and check for perms if you
need to run as root and not over-write files.

chris


Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Yes, I am running the test as root.

Thanks for replying.

“Chris McKillop” <> cdm@qnx.com> > wrote in message
news:alqv3n$85$> 1@nntp.qnx.com> …

Are you running your test as root?

chris

Djibril NDIAYE <> djibril.ndiaye@opal-rt.com> > wrote:
Hi,

I use: f = fopen(“MyFile”, “wb”);
to open a binary file.

Then I use:
fwrite(…,…,…,f);
to write some data to the file;

The problem is, when MyFile exists and is read-only (chmod a-w),
fopen
opens
the file successfully and fwrite successfully writes data to the
file.

What am I doing wrong, or what did I misunderstood?

Thank you.



\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I
get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/




\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I
get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/




\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/