[1.0RC23] fopen - bug or intended behaviour?

Started by Sslaxx, June 20, 2011, 02:43:59 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Sslaxx

So, in my code I have a line like so:

a = fopen ("Test.ini", O_READWRITE);

If Test.ini doesn't exist fopens returns 0 and attempting file operations causes a segfault. If using O_READ or O_ZREAD that would be understandable; the file doesn't exist. O_WRITE works (for writing the file out) as it creates the file if it does not already exist. O_READWRITE, as just described, behaves in the same way as O_(Z)READ.

I'd have thought, given what it is, that O_READWRITE would have ensured the file would be created if it does not already exist. Is the fact it does not do so intended behaviour, or a bug?
Stuart "Sslaxx" Moore.

josebita

Not sure what the expected C equivalent behaviour is but as a general programming tip, it's always a good idea to ensure the file exists with file_exists() before trying to read from it.

Sslaxx

If you go by this page, then you're saying O_READWRITE is functionally the same as "r+" - "Open a file for update both reading and writing. The file must exist." Makes sense.
Stuart "Sslaxx" Moore.

josebita

I just checked the BennuGD source code for mod_file and that's right, it's doing a "r+b" for O_READWRITE. (the "b" there is just a compatibility flag for old MS OSes)

SplinterGU

no, no is a bug.

for rw operation the file must be exists, if fopen return 0, it mean that the fopen operation fail, you must check this return code before continue.
Download Lastest BennuGD Release: http://www.bennugd.org/node/2