if ((ret = fd_lock(fd, FD_RDWR)) == OK) { flags = fd_table[fd]->flags; realfd = fd_table[fd]->fd; if (fd_free(fd) == OK) { ret = fd_table[fd]->ops->close(realfd, flags); } fd_unlock(fd, FD_RDWR);
if(fd < 0 || fd >= dtablesize) { SET_ERRNO(EBADF); return -1; }  pthread_mutex_lock(&fd_table_mutex); if (fd_table[fd]) { pthread_mutex_unlock(&fd_table_mutex); mutex = &(fd_table[fd]->mutex); pthread_mutex_lock(mutex);   switch(fd_table[fd]->type) { case FD_NIU: pthread_mutex_unlock(mutex); ret = -EBADF; break; case FD_NT:  ret = machdep_sys_close(fd); fd_table[fd]->type = FD_NIU; pthread_mutex_unlock(mutex); break; case FD_TEST_FULL_DUPLEX: case FD_TEST_HALF_DUPLEX: realfd = fd_table[fd]->fd; flags = fd_table[fd]->flags; if ((entry = fd_free(fd)) == NULL) { ret = fd_table[fd]->ops->close(realfd, flags); } else {  pthread_mutex_unlock(&entry->mutex);  mutex = &(fd_table[fd]->mutex); } pthread_mutex_unlock(mutex); break; default: ret = fd_basic_lock(fd, FD_RDWR, mutex, NULL); if (ret == OK) { realfd = fd_table[fd]->fd; flags = fd_table[fd]->flags; pthread_mutex_unlock(mutex); if ((entry = fd_free(fd)) == NULL) { ret = fd_table[fd]->ops->close(realfd, flags); } else { fd_basic_basic_unlock(entry, FD_RDWR); pthread_mutex_unlock(&entry->mutex);
int close(int fd){	union fd_data realfd;	int ret, flags;	if ((ret = fd_lock(fd, FD_RDWR)) == OK) {		flags = fd_table[fd]->flags;		realfd = fd_table[fd]->fd;		if (fd_free(fd) == OK) {     		ret = fd_table[fd]->ops->close(realfd, flags);		}		fd_unlock(fd, FD_RDWR);	}	return(ret);}