Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.apps > #458

fopen() of a read-only file for writing?

From "Joe Shead" <Joe@SheadProgramming.com>
Newsgroups comp.os.linux.development.apps
Subject fopen() of a read-only file for writing?
Date 2012-04-16 17:03 -0600
Message-ID <s5KdnY85luiJChHSnZ2dnUVZ_uGdnZ2d@earthlink.com> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi,

I have a file that has no write permissions on it, called readonly.txt. I am logged on as root.

ls -l returns the line:

  -r-xr--r-- 1 root root   32   Apr 15 20:20 readonly.txt

I do:

<CODE>
    int errnos[4];
    FILE *f=0;

    errno=0;
    errnos[0]=errno;
    f=fopen("readonly.txt", "wb");
    errnos[1]=errno;
    if (f!=0){
        putc('a', f);
        errnos[2]=errno;
        fclose(f); f=0;
        errnos[3]=errno;}
</CODE>

I get a non-zero f from fopen(), the putc() works, and all 4 errnos are 0. After executing this code, the file is 1 byte long with just the 'a' in it.

Is this behavior correct?

I'm really not sure, but if someone could explain it to me, or direct me to the right people to talk to, that would help. It seems like fopen() ought to return 0, and set errno to EACCES.

I built my version of linux using lfs (linux from scratch). I'm using linux kernel version 2.4.21, gcc version 3.2.1, and libc version 2.3.1.

Thanks in advance,

Joe

Back to comp.os.linux.development.apps | Previous | NextNext in thread | Find similar


Thread

fopen() of a read-only file for writing? "Joe Shead" <Joe@SheadProgramming.com> - 2012-04-16 17:03 -0600
  Re: fopen() of a read-only file for writing? pacman@kosh.dhis.org (Alan Curry) - 2012-04-17 05:12 +0000
    Re: fopen() of a read-only file for writing? "Joe Shead" <Joe@SheadProgramming.com> - 2012-04-17 01:36 -0600
    Re: fopen() of a read-only file for writing? "Joe Shead" <Joe@SheadProgramming.com> - 2012-04-18 22:40 -0600
      Re: fopen() of a read-only file for writing? Richard Kettlewell <rjk@greenend.org.uk> - 2012-04-19 08:57 +0100
      Re: fopen() of a read-only file for writing? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-04-19 14:18 +0100
      Re: fopen() of a read-only file for writing? Grant Edwards <invalid@invalid.invalid> - 2012-04-19 14:16 +0000
      Re: fopen() of a read-only file for writing? "Joe Shead" <Joe@SheadProgramming.com> - 2012-04-20 19:10 -0600
        Re: fopen() of a read-only file for writing? Richard Kettlewell <rjk@greenend.org.uk> - 2012-04-21 10:20 +0100
          Re: fopen() of a read-only file for writing? "Joe Shead" <Joe@SheadProgramming.com> - 2012-04-22 00:07 -0600
          Re: fopen() of a read-only file for writing? "Joe Shead" <Joe@SheadProgramming.com> - 2012-04-23 15:24 -0600

csiph-web