Autor Tema: [1.0RC23] fopen - bug or intended behaviour?  (Leído 1773 veces)

Sslaxx

  • Newbie
  • *
  • Mensajes: 34
  • Karma: 0
[1.0RC23] fopen - bug or intended behaviour?
« en: Junio 20, 2011, 02:43:59 pm »
So, in my code I have a line like so:

Código: [Seleccionar]
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

  • Administrator
  • *****
  • Mensajes: 4039
  • Karma: 257
    • BennuGD Mobile Worklog
Re: [1.0RC23] fopen - bug or intended behaviour?
« Respuesta #1 en: Junio 20, 2011, 03:50:53 pm »
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

  • Newbie
  • *
  • Mensajes: 34
  • Karma: 0
Re: [1.0RC23] fopen - bug or intended behaviour?
« Respuesta #2 en: Junio 20, 2011, 03:59:01 pm »
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

  • Administrator
  • *****
  • Mensajes: 4039
  • Karma: 257
    • BennuGD Mobile Worklog
Re: [1.0RC23] fopen - bug or intended behaviour?
« Respuesta #3 en: Junio 20, 2011, 04:31:08 pm »
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

  • Hero Member
  • *****
  • Mensajes: 12972
  • Karma: 379
Re: [1.0RC23] fopen - bug or intended behaviour?
« Respuesta #4 en: Junio 20, 2011, 04:47:55 pm »
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