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 = %d\n", fd);
ret = fcntl(fd, F_GETFD, 0);
printf("flag:%d\n",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