retarget: distinguish FileHandle and descriptor

Avoid using `fh` for the integer descriptor numbers, reserving
that for the `FileHandle` objects - use `fildes` or `fd`,
matching POSIX.
pull/6791/head
Kevin Bracey 2018-05-02 13:56:32 +03:00
parent 59f49e2b96
commit a8ab233a1a
2 changed files with 33 additions and 33 deletions

View File

@ -344,16 +344,16 @@ static int reserve_filehandle() {
} }
int mbed::bind_to_fd(FileHandle *fh) { int mbed::bind_to_fd(FileHandle *fh) {
int fh_i = reserve_filehandle(); int fildes = reserve_filehandle();
if (fh_i < 0) { if (fildes < 0) {
return fh_i; return fildes;
} }
filehandles[fh_i] = fh; filehandles[fildes] = fh;
stdio_in_prev[fh_i] = 0; stdio_in_prev[fildes] = 0;
stdio_out_prev[fh_i] = 0; stdio_out_prev[fildes] = 0;
return fh_i; return fildes;
} }
static int unbind_from_fd(int fd, FileHandle *fh) { static int unbind_from_fd(int fd, FileHandle *fh) {
@ -464,9 +464,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags) {
} }
extern "C" int open(const char *name, int oflag, ...) { extern "C" int open(const char *name, int oflag, ...) {
int fh_i = reserve_filehandle(); int fildes = reserve_filehandle();
if (fh_i < 0) { if (fildes < 0) {
return fh_i; return fildes;
} }
FileHandle *res = NULL; FileHandle *res = NULL;
@ -476,7 +476,7 @@ extern "C" int open(const char *name, int oflag, ...) {
/* The first part of the filename (between first 2 '/') is not a /* The first part of the filename (between first 2 '/') is not a
* registered mount point in the namespace. * registered mount point in the namespace.
*/ */
return handle_open_errors(-ENODEV, fh_i); return handle_open_errors(-ENODEV, fildes);
} }
if (path.isFile()) { if (path.isFile()) {
@ -484,28 +484,28 @@ extern "C" int open(const char *name, int oflag, ...) {
} else { } else {
FileSystemHandle *fs = path.fileSystem(); FileSystemHandle *fs = path.fileSystem();
if (fs == NULL) { if (fs == NULL) {
return handle_open_errors(-ENODEV, fh_i); return handle_open_errors(-ENODEV, fildes);
} }
int err = fs->open(&res, path.fileName(), oflag); int err = fs->open(&res, path.fileName(), oflag);
if (err) { if (err) {
return handle_open_errors(err, fh_i); return handle_open_errors(err, fildes);
} }
} }
filehandles[fh_i] = res; filehandles[fildes] = res;
stdio_in_prev[fh_i] = 0; stdio_in_prev[fildes] = 0;
stdio_out_prev[fh_i] = 0; stdio_out_prev[fildes] = 0;
return fh_i; return fildes;
} }
extern "C" int PREFIX(_close)(FILEHANDLE fh) { extern "C" int PREFIX(_close)(FILEHANDLE fh) {
return close(fh); return close(fh);
} }
extern "C" int close(int fh) { extern "C" int close(int fildes) {
FileHandle* fhc = get_fhc(fh); FileHandle* fhc = get_fhc(fildes);
filehandles[fh] = NULL; filehandles[fildes] = NULL;
if (fhc == NULL) { if (fhc == NULL) {
errno = EBADF; errno = EBADF;
return -1; return -1;
@ -610,9 +610,9 @@ finish:
#endif #endif
} }
extern "C" ssize_t write(int fh, const void *buf, size_t length) { extern "C" ssize_t write(int fildes, const void *buf, size_t length) {
FileHandle* fhc = get_fhc(fh); FileHandle* fhc = get_fhc(fildes);
if (fhc == NULL) { if (fhc == NULL) {
errno = EBADF; errno = EBADF;
return -1; return -1;
@ -700,9 +700,9 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
#endif #endif
} }
extern "C" ssize_t read(int fh, void *buf, size_t length) { extern "C" ssize_t read(int fildes, void *buf, size_t length) {
FileHandle* fhc = get_fhc(fh); FileHandle* fhc = get_fhc(fildes);
if (fhc == NULL) { if (fhc == NULL) {
errno = EBADF; errno = EBADF;
return -1; return -1;
@ -727,8 +727,8 @@ extern "C" int _isatty(FILEHANDLE fh)
return isatty(fh); return isatty(fh);
} }
extern "C" int isatty(int fh) { extern "C" int isatty(int fildes) {
FileHandle* fhc = get_fhc(fh); FileHandle* fhc = get_fhc(fildes);
if (fhc == NULL) { if (fhc == NULL) {
errno = EBADF; errno = EBADF;
return 0; return 0;
@ -765,8 +765,8 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
return off; return off;
} }
extern "C" off_t lseek(int fh, off_t offset, int whence) { extern "C" off_t lseek(int fildes, off_t offset, int whence) {
FileHandle* fhc = get_fhc(fh); FileHandle* fhc = get_fhc(fildes);
if (fhc == NULL) { if (fhc == NULL) {
errno = EBADF; errno = EBADF;
return -1; return -1;
@ -786,8 +786,8 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
} }
#endif #endif
extern "C" int fsync(int fh) { extern "C" int fsync(int fildes) {
FileHandle* fhc = get_fhc(fh); FileHandle* fhc = get_fhc(fildes);
if (fhc == NULL) { if (fhc == NULL) {
errno = EBADF; errno = EBADF;
return -1; return -1;
@ -850,8 +850,8 @@ extern "C" int _fstat(int fh, struct stat *st) {
} }
#endif #endif
extern "C" int fstat(int fh, struct stat *st) { extern "C" int fstat(int fildes, struct stat *st) {
FileHandle* fhc = get_fhc(fh); FileHandle* fhc = get_fhc(fildes);
if (fhc == NULL) { if (fhc == NULL) {
errno = EBADF; errno = EBADF;
return -1; return -1;

View File

@ -503,7 +503,7 @@ extern "C" {
off_t lseek(int fildes, off_t offset, int whence); off_t lseek(int fildes, off_t offset, int whence);
int isatty(int fildes); int isatty(int fildes);
int fsync(int fildes); int fsync(int fildes);
int fstat(int fh, struct stat *st); int fstat(int fildes, struct stat *st);
int poll(struct pollfd fds[], nfds_t nfds, int timeout); int poll(struct pollfd fds[], nfds_t nfds, int timeout);
int close(int fildes); int close(int fildes);
int stat(const char *path, struct stat *st); int stat(const char *path, struct stat *st);