/**
* author:kangear@163.com
* date :2015-01-17
* func :check if the fileDescriptor is fine.
*/
#include
#include
#include
#include
#include
#include
int main() {
int fd = -1;
fd = open("/tmp/isatty.c", O_RDONLY);
// close(fd);
if(fcntl(fd, F_GETFL))
printf("%m\n");
close(fd);
}
[cpp] view plain copy
/**
* version : 1.1
* date : 2015-02-05
* func : check if the fileDescriptor is fine.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct stat _stat;
/**
* On success, zero is returned. On error, -1 is returned, and errno is set
* appropriately.
*/
int check_fd_fine(int fd) {
struct stat _stat;
int ret = -1;
if(!fcntl(fd, F_GETFL)) {
if(!fstat(fd, &_stat)) {
if(_stat.st_nlink >= 1)
ret = 0;
else
printf("File was deleted!\n");
}
}
if(errno != 0)
perror("check_fd_fine");
return ret;
}
int main() {
int fd = -1;
fd = open("/dev/ttyUSB1", O_RDONLY);
if(fd < 0) {
perror("open file fail");
return -1;
}
// close(fd);
sleep(5);
if(!check_fd_fine(fd)) {
printf("fd okay!\n");
} else {
printf("fd bad!\n");
}
close(fd);
return 0;
}