fcntl

How to catch file mode?

。_饼干妹妹 提交于 2021-01-27 12:24:17
问题 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

Correct way of using fdopen

大憨熊 提交于 2020-07-10 10:29:26
问题 I mean to associate a file descriptor with a file pointer and use that for writing. I put together program io.cc below: int main() { ssize_t nbytes; const int fd = 3; char c[100] = "Testing\n"; nbytes = write(fd, (void *) c, strlen(c)); // Line #1 FILE * fp = fdopen(fd, "a"); fprintf(fp, "Writing to file descriptor %d\n", fd); cout << "Testing alternate writing to stdout and to another fd" << endl; fprintf(fp, "Writing again to file descriptor %d\n", fd); close(fd); // Line #2 return 0; } I

Safety check prior to using fdopen

↘锁芯ラ 提交于 2020-06-09 07:07:04
问题 Motivated by Smart-write to arbitrary file descriptor from C/C++, I mean to associate a file descriptor with a file pointer and use that for writing. I put together program io.cc below: int main() { int nbytes; const int fd = 3; FILE * fp = fdopen(fd, "a"); fprintf(fp, "Writing to file descriptor %d\n", fd); cout << "Testing alternate writing to stdout and to another fd" << endl; fprintf(fp, "Writing again to file descriptor %d\n", fd); fclose(fp); return 0; } Should I perform some kind of

Safety check prior to using fdopen

限于喜欢 提交于 2020-06-07 07:22:26
问题 Motivated by Smart-write to arbitrary file descriptor from C/C++, I mean to associate a file descriptor with a file pointer and use that for writing. I put together program io.cc below: int main() { int nbytes; const int fd = 3; FILE * fp = fdopen(fd, "a"); fprintf(fp, "Writing to file descriptor %d\n", fd); cout << "Testing alternate writing to stdout and to another fd" << endl; fprintf(fp, "Writing again to file descriptor %d\n", fd); fclose(fp); return 0; } Should I perform some kind of

How to get the mode of a file descriptor?

送分小仙女□ 提交于 2020-06-07 06:50:47
问题 I mean to use fdopen FILE *fdopen(int fd, const char *mode); In man pages, it is stated that "The mode of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") must be compatible with the mode of the file descriptor." So I have to first know the mode of fd (which I guess is an int ) to choose an appropriate const char *mode for the stream. I understand I should use fcntl int fcntl(int fd, int cmd); to "manipulate file descriptor" (in the following, I quote from this official source).

How to make sense of O_RDONLY = 0?

你离开我真会死。 提交于 2020-05-23 16:25:26
问题 I am dealing with file status flags. Among test I performed, I found #include <stdio.h> #include "fcntl.h" int main() { const int flag = O_RDONLY; printf( "*** Flag O_RDONLY = %5d\n", flag); return 0; } produces this output *** Flag O_RDONLY = 0 which is fully consistent with #define O_RDONLY 00 from fcntl-linux.h . How can the value zero be used as a flag? I expect an "atomic" flag to be 2^n ( n>=1 ), and "composite" flags (like O_ACCMODE ) to be simply the sum of several atomic flags (which

第5课.异步通知

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-14 19:03:35
1.按键的4种控制方式对比 1.查询 :耗资源 | 2.中断 :没有超时机制 | APP---->驱动 3.poll :加入了超时机制 | 4.异步通知 :按键发生后去通知app 驱动--->APP 异步通知的注意事项 1.不是所有的设备都支持异步通知。应用程序通常假设只有套接字和终端才有异步通知能力。 2.当进程收到SIGIO信号时,它并不知道是哪个输入文件有了新的输入。如果有多于一个文件可以异步通知输入的进程,则应用程序仍然必须借助于poll或select来确定输入的来源。 2.异步通知开发流程 1.应用程序注册信号处理函数 signal(SIGIO, my_signal_fun); 2.驱动程序发信号 void kill_fasync(struct fasync_struct **fp, int sig, int band) 3.信号被发给应用程序,应用程序需要告诉驱动pid fcntl(fd, F_SETOWN, getpid()); // 告诉内核pid,让内核知道应该通知那个进程 解析:当fcntl系统调用执行F_SETOWN命令时,属主进程的进程ID号就被保存在filp->f_owner中 Oflags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, Oflags | FASYNC); // 改变fasync标记