Hey, does anybody know how to check whether the file descriptor is valid?
Hey, does anybody know how to check whether the file descriptor is valid?
You might need to clarify that question a bit...
Functions like open() will return a certain value (-1) for failure. Otherwise, the easiest way is that it's up to you to keep track of file descriptors.
Alternatively, if you're in Linux, you could read your process's dir in /proc, but again, this all depends - what are you wanting to do?
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
there are two ways to achieve this:
1. check for the return value when returned from open() system call.
Code:int fdesc; if(fdesc = open(//arguments) == -1) { perror("open"); }
2. the other way is ioctl() system calls and check the inode file structure.
Hope i answered your query...
To just see if a file descriptor is valid you could use this:
fcntl(GETFL) is probably the cheapest and least likely to fail operation you can perform on a file descriptor, so I've chosen it. In particular, the specification suggests that it cannot be interrupted by signals, nor is it affected by any sort of lock held anywhere.Code:int is_valid_fd(int fd) { return fcntl(fd, F_GETFL) != -1 || errno != EBADF; }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law