file-descriptor

How to determine if a file descriptor is seekable?

跟風遠走 提交于 2019-12-05 02:22:53
Is there any portable way (on POSIX systems) to determine if a file descriptor is seekable? My thought is to use lseek(fd, 0, SEEK_CUR); and check if the return value is -1, but I'm uncertain if this could give false negatives or false positives. Using fstat and making assumptions about what types of files are seekable/nonseekable does not sound like a good idea. Any other ideas? The lseek method seems reasonable. It certainly can't cause a false negative - if it did, something is seriously wrong with the implementation. Also, according to the POSIX spec , it is supposed to fail if the

What are the possible values for file descriptors?

拥有回忆 提交于 2019-12-05 01:18:04
I am interested to know the valid values which I can expect for a file descriptor. Please let me explain a bit. I know that, for instance, when I use #include <unistd.h> on my linux system then a call to open a file for reading: int fileDescriptor; fileDescriptor = open("/some/filename",O_RDONLY); an error might occur and I receive -1 as a result. Incidently the (-1) negative one must have somewhat of a special meaning. Is it that all other values are valid file descriptors? i.e. also negative ones like -2 and -1023? Assuming that int is 4 bytes ( sizeof(int)==4 ), then would (-1) = 10000000

Is HANDLE similar to file descriptor in Linux?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:43:32
Is HANDLE similar to file descriptor in Linux? As far as I know, HANDLE is used for handling every resources on Windows, such as font, icons, files, devices..., which in essence is just a void pointer point to a memory block holding data of a specific resource Yes, Windows handles are very similar to Unix file descriptors (FDs). Note that a HANDLE is not a pointer to a block of memory. Although HANDLE is typedef 'd as void * , that's just to make it more opaque. In practice, a HANDLE is an index that is looked up in a table, just as an FD number is. This blog post explores some of the

How can I programmatically get the list of open file descriptors for a given PID on OS X?

最后都变了- 提交于 2019-12-04 23:35:41
问题 Everything I've seen says to use lsof -p , but I'm looking for something that doesn't require a fork/exec. For example on Linux one can simply walk /proc/{pid}/fd . 回答1: You can use proc_pidinfo with the PROC_PIDLISTFDS option to enumerate the files used by a given process. You can then use proc_pidfdinfo on each file in turn with the PROC_PIDFDVNODEPATHINFO option to get its path. 来源: https://stackoverflow.com/questions/15583563/how-can-i-programmatically-get-the-list-of-open-file

Creating a shell in C. How would I implement input and output redirection?

血红的双手。 提交于 2019-12-04 22:38:54
I'm creating a shell in C, and I need help implementing input and output redirection. When I try to create a file using ">" I get an error message saying the file does not exist. When I try to do something like ls > test.txt; it won't create a new file. I updated the code with the suggestions provided to me, but now I got different errors. However, a new file is still not created for the output redirection. This is my full code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #define MAX_BUF 160 #define MAX

On Windows/mingw, what is the equivalent of `fcntl(fd, F_GETFL) | O_ACCMODE`?

微笑、不失礼 提交于 2019-12-04 21:09:30
问题 I am compiling a program on Windows with Mingw. How can I get the access mode for an open file descriptor? 回答1: According to Win32.hlp, the API supplies the function BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation) in KERNEL32. LPBY_HANDLE_FILE_INFORMATION is a BY_HANDLE_FILE_INFORMATION* , where BY_HANDLE_FILE_INFORMATION is as follows: typedef struct _BY_HANDLE_FILE_INFORMATION { // bhfi DWORD dwFileAttributes; FILETIME ftCreationTime; FILETIME

bridging between two file descriptors

纵然是瞬间 提交于 2019-12-04 18:17:31
问题 I have a socket I'm doing select() on it, waiting for other process to write. Once it write, I read the data, and write it to another file descriptor. My question is, if there's a way to bridge the socket to the file descriptor, so when there's data ready, it will be automatically written to the other file descriptor ? This way, I could throw a way the buffer I'm using, and omit a thread in the system. 回答1: On linux, using splice() might be more suitable, when the direction is from socket to

Retrieve the number of opened file descriptors using the Windows API

浪尽此生 提交于 2019-12-04 16:51:13
I would like to know how many file descriptors have I opened in my C++ application. Can this be done using Windows API function? You can ask each handle in the process using GetFileType . DWORD type_char = 0, type_disk = 0, type_pipe = 0, type_remote = 0, type_unknown = 0, handles_count = 0; GetProcessHandleCount(GetCurrentProcess(), &handles_count); handles_count *= 4; for (DWORD handle = 0x4; handle < handles_count; handle += 4) { switch (GetFileType((HANDLE)handle)){ case FILE_TYPE_CHAR: type_char++; break; case FILE_TYPE_DISK: type_disk++; break; case FILE_TYPE_PIPE: type_pipe++; break;

What's a file descriptor's “exception”?

冷暖自知 提交于 2019-12-04 16:24:18
问题 When one calls select() asking which file descriptors have "exceptions" waiting, what does that mean? How does one trigger one of these "exceptions"? If anyone can point me to a nice explanation, that'd be awesome. I've been googling and can't find a thing. 回答1: Short form: exceptional situations occur when a TCP socket recieves out of band data. If you read the select manual page, you will find a reference to another supplementary manual page called select_tut with the explanation: exceptfds

Is there a need to close file descriptors before exit?

走远了吗. 提交于 2019-12-04 15:27:52
问题 Of course, the immediate answer for most situations is "yes" , and I am a firm believer that a process should correctly cleanup any resources it has allocated, but what I have in my situation is a long-running system daemon that opens a fixed number of file descriptors at the startup, and closes them all before exiting. This is an embedded platform, and I'm trying to make the code as compact as possible, while not introducing any bad style. But since file descriptors are closed before exit