ref: 5ebb1a29d8250027e8b7f2287f3bfa3a6f3d13ac
dir: /sys/src/ape/lib/ap/plan9/close.c/
#include "lib.h" #include <stdlib.h> #include <unistd.h> #include <errno.h> #include "sys9.h" int close(int d) { int n; Fdinfo *f; n = -1; f = &_fdinfo[d]; if(d<0 || d>=OPEN_MAX || !(f->flags&FD_ISOPEN)) errno = EBADF; else{ if(f->flags&(FD_BUFFERED|FD_BUFFEREDX)) { if(f->flags&FD_BUFFERED) _closebuf(d); f->flags &= ~FD_BUFFERED; } _fdinfo[d].flags = 0; _fdinfo[d].oflags = 0; if(_fdinfo[d].name){ free(_fdinfo[d].name); _fdinfo[d].name = 0; } n = _CLOSE(d); if(n < 0) _syserrno(); } return n; }