Skip to content

Commit a8ab233

Browse files
committed
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.
1 parent 59f49e2 commit a8ab233

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

platform/mbed_retarget.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,16 @@ static int reserve_filehandle() {
344344
}
345345

346346
int mbed::bind_to_fd(FileHandle *fh) {
347-
int fh_i = reserve_filehandle();
348-
if (fh_i < 0) {
349-
return fh_i;
347+
int fildes = reserve_filehandle();
348+
if (fildes < 0) {
349+
return fildes;
350350
}
351351

352-
filehandles[fh_i] = fh;
353-
stdio_in_prev[fh_i] = 0;
354-
stdio_out_prev[fh_i] = 0;
352+
filehandles[fildes] = fh;
353+
stdio_in_prev[fildes] = 0;
354+
stdio_out_prev[fildes] = 0;
355355

356-
return fh_i;
356+
return fildes;
357357
}
358358

359359
static int unbind_from_fd(int fd, FileHandle *fh) {
@@ -464,9 +464,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags) {
464464
}
465465

466466
extern "C" int open(const char *name, int oflag, ...) {
467-
int fh_i = reserve_filehandle();
468-
if (fh_i < 0) {
469-
return fh_i;
467+
int fildes = reserve_filehandle();
468+
if (fildes < 0) {
469+
return fildes;
470470
}
471471

472472
FileHandle *res = NULL;
@@ -476,36 +476,36 @@ extern "C" int open(const char *name, int oflag, ...) {
476476
/* The first part of the filename (between first 2 '/') is not a
477477
* registered mount point in the namespace.
478478
*/
479-
return handle_open_errors(-ENODEV, fh_i);
479+
return handle_open_errors(-ENODEV, fildes);
480480
}
481481

482482
if (path.isFile()) {
483483
res = path.file();
484484
} else {
485485
FileSystemHandle *fs = path.fileSystem();
486486
if (fs == NULL) {
487-
return handle_open_errors(-ENODEV, fh_i);
487+
return handle_open_errors(-ENODEV, fildes);
488488
}
489489
int err = fs->open(&res, path.fileName(), oflag);
490490
if (err) {
491-
return handle_open_errors(err, fh_i);
491+
return handle_open_errors(err, fildes);
492492
}
493493
}
494494

495-
filehandles[fh_i] = res;
496-
stdio_in_prev[fh_i] = 0;
497-
stdio_out_prev[fh_i] = 0;
495+
filehandles[fildes] = res;
496+
stdio_in_prev[fildes] = 0;
497+
stdio_out_prev[fildes] = 0;
498498

499-
return fh_i;
499+
return fildes;
500500
}
501501

502502
extern "C" int PREFIX(_close)(FILEHANDLE fh) {
503503
return close(fh);
504504
}
505505

506-
extern "C" int close(int fh) {
507-
FileHandle* fhc = get_fhc(fh);
508-
filehandles[fh] = NULL;
506+
extern "C" int close(int fildes) {
507+
FileHandle* fhc = get_fhc(fildes);
508+
filehandles[fildes] = NULL;
509509
if (fhc == NULL) {
510510
errno = EBADF;
511511
return -1;
@@ -610,9 +610,9 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
610610
#endif
611611
}
612612

613-
extern "C" ssize_t write(int fh, const void *buf, size_t length) {
613+
extern "C" ssize_t write(int fildes, const void *buf, size_t length) {
614614

615-
FileHandle* fhc = get_fhc(fh);
615+
FileHandle* fhc = get_fhc(fildes);
616616
if (fhc == NULL) {
617617
errno = EBADF;
618618
return -1;
@@ -700,9 +700,9 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
700700
#endif
701701
}
702702

703-
extern "C" ssize_t read(int fh, void *buf, size_t length) {
703+
extern "C" ssize_t read(int fildes, void *buf, size_t length) {
704704

705-
FileHandle* fhc = get_fhc(fh);
705+
FileHandle* fhc = get_fhc(fildes);
706706
if (fhc == NULL) {
707707
errno = EBADF;
708708
return -1;
@@ -727,8 +727,8 @@ extern "C" int _isatty(FILEHANDLE fh)
727727
return isatty(fh);
728728
}
729729

730-
extern "C" int isatty(int fh) {
731-
FileHandle* fhc = get_fhc(fh);
730+
extern "C" int isatty(int fildes) {
731+
FileHandle* fhc = get_fhc(fildes);
732732
if (fhc == NULL) {
733733
errno = EBADF;
734734
return 0;
@@ -765,8 +765,8 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
765765
return off;
766766
}
767767

768-
extern "C" off_t lseek(int fh, off_t offset, int whence) {
769-
FileHandle* fhc = get_fhc(fh);
768+
extern "C" off_t lseek(int fildes, off_t offset, int whence) {
769+
FileHandle* fhc = get_fhc(fildes);
770770
if (fhc == NULL) {
771771
errno = EBADF;
772772
return -1;
@@ -786,8 +786,8 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
786786
}
787787
#endif
788788

789-
extern "C" int fsync(int fh) {
790-
FileHandle* fhc = get_fhc(fh);
789+
extern "C" int fsync(int fildes) {
790+
FileHandle* fhc = get_fhc(fildes);
791791
if (fhc == NULL) {
792792
errno = EBADF;
793793
return -1;
@@ -850,8 +850,8 @@ extern "C" int _fstat(int fh, struct stat *st) {
850850
}
851851
#endif
852852

853-
extern "C" int fstat(int fh, struct stat *st) {
854-
FileHandle* fhc = get_fhc(fh);
853+
extern "C" int fstat(int fildes, struct stat *st) {
854+
FileHandle* fhc = get_fhc(fildes);
855855
if (fhc == NULL) {
856856
errno = EBADF;
857857
return -1;

platform/mbed_retarget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ extern "C" {
503503
off_t lseek(int fildes, off_t offset, int whence);
504504
int isatty(int fildes);
505505
int fsync(int fildes);
506-
int fstat(int fh, struct stat *st);
506+
int fstat(int fildes, struct stat *st);
507507
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
508508
int close(int fildes);
509509
int stat(const char *path, struct stat *st);

0 commit comments

Comments
 (0)