TransWikia.com

How to get flags of opened fd in C?

Stack Overflow Asked by code_worker on December 9, 2021

I want to get flags of fd was opened before in C.

But I use fcntl(fd,F_GETFD,0) reference by fcntl man page, it always return 1 to me.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define XSZ(x) (int)(sizeof(x)*2)

int main()
{
    int ret;
    static const char str [] = "hello c program!n";
    int fd = open("test.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("fd = %dn", fd);

    ret = fcntl(fd, F_GETFD, 0);
    printf("flag:%dn",ret);
    write(fd, str, strlen(str)-1);

    return 0;
}

It always print:

fd = 3
flag:1

What I thought the ret is the sum of O_RDWR | O_APPEND | O_CREAT

2 Answers

You should use F_GETFL instead of F_GETFD. Also remember to print in octal to compare.

One important thing is not all flags are returned by fcntl. Only the access mode flags are remembered, like O_RDWR. However O_CREATE/O_TRUNC etc. are operating mode or open-time flags which are not remembered by the system and hence not returned.

Here is your modified code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define XSZ(x) (int)(sizeof(x)*2)

int main()
{
    int ret;
    int fd = open("test.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    printf("fd = %dn", fd);
    ret = fcntl(fd, F_GETFL);
    perror("fcntl");
    printf("flag:%on",ret);
    printf("flag:%on",O_RDWR|O_APPEND);
    write(fd, "hello c programn", strlen("hello c program!n"));

    return 0;
}

here is the output of the above code

fd = 3
fcntl: Success
flag:102002
flag:2002

Answered by Bhaskar Tallamraju on December 9, 2021

F_GETFD does not query the open flags, but just FD_CLOEXEC (see here).

The line

write(fd, "hello c programn", strlen("hello c program!n"));

is wrong, as you query the length of a longer string than what you write, possibly causing a buffer overflow. A safer and more efficient way to do this would be:

static const char str [] = "hello c program!n";
write(fd, str, sizeof(str)-1);

The -1 is needed to avoid writing the terminating 0 byte.

I don't know the purpose of

#define XSZ(x) (int)(sizeof(x)*2)

but casting size_t (the result type of sizeof()) to int is probably not a good idea.

Answered by Erlkoenig on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP