3

I have to catch modes below:

"rb", "r+b" and "wb".

I tried to execute code (compiled) of this:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    FILE *file = fopen("data.bin", "r");
    if (!file){
        perror("");
        return -1;
    }

    int fd = fileno(file);
    if (fcntl(fd, F_GETFL) == O_RDONLY){
        printf("read only\n");
    }

    // printf("%d\n", O_APPEND);

    fclose(file);
    return 0;
}

But gotten nothing printed. fcntl() returns integers like 32768, but I need macros from the library like O_RDONLY.

Антон
  • 191
  • 1
  • 1
  • 9
  • 5
    You get 32768, which is `__O_LARGEFILE|O_RDONLY` – Ruslan Jan 29 '19 at 12:49
  • This sure seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What actual problem will having the raw file mode solve? And not knowing what mode a file is opened in - by your own process - has a really nasty [code smell](https://en.wikipedia.org/wiki/Code_smell). – Andrew Henle Jan 29 '19 at 13:09
  • it is not nasty code small if the intent is `assert( myfileisreadable( f ) )`. it is there not for production code, but for testing code to help detect bugs. this is the purpose of `assert`... – ivo Welch Mar 27 '20 at 19:59

1 Answers1

3

In general, you cannot catch -given a FILE* handle-, the file mode, in the fopen sense, with a portable C program.

For example, your FILE* might be obtained with popen, fmemopen, open_memstream, fdopen, etc... and could be writable only or readable only. It might not even have a valid file descriptor given by fileno.

You should adopt and define and document conventions about them.

For example, on Linux, a socket might be readable and writable at the OS level, and you could have two different FILE* handles on it (using fdopen) for the read and for the write sides.

And of course, a FILE* handle is not usable after an fclose (or a pclose)

So an hypothetical routine fprint_stuff(FILE*, struct stuff_st*) should document that the first argument is a writable file handle, and would just call fwrite and fprintf but should check that these did not fail.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547