}
if (bestfd) { /* found a usable slot */
tfd = bestfd;
- if (tfd->fd >= 0)
+ if (tfd->fd >= 0)
close(tfd->fd);
tfd->fd = fd;
tfd->refCount = 1; /* us */
return EBADF;
tfd = fdcache;
for (i = 0; i < MAXFDCACHE; i++, tfd++) {
- if (tfd->fd == afd && tfd->fileID != -10000) {
- tfd->refCount--;
- return 0;
- }
+ if (tfd->fd == afd)
+ if (tfd->fileID != -10000) {
+ tfd->refCount--;
+ return 0;
+ } else {
+ if (tfd->refCount > 0) {
+ tfd->refCount--;
+ if (tfd->refCount == 0) {
+ close(tfd->fd);
+ tfd->fd = -1;
+ }
+ return 0;
+ }
+ tfd->fd = -1;
+ break;
+ }
}
return close(afd);
}
for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
if (afid == tfd->fileID) {
tfd->fileID = -10000;
- if (tfd->fd >= 0 && tfd->refCount == 0)
+ if (tfd->fd >= 0 && tfd->refCount == 0) {
close(tfd->fd);
+ tfd->fd = -1;
+ }
return;
}
}