@@ -344,16 +344,16 @@ static int reserve_filehandle() {
344
344
}
345
345
346
346
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 ;
350
350
}
351
351
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 ;
355
355
356
- return fh_i ;
356
+ return fildes ;
357
357
}
358
358
359
359
static int unbind_from_fd (int fd, FileHandle *fh) {
@@ -464,9 +464,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags) {
464
464
}
465
465
466
466
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 ;
470
470
}
471
471
472
472
FileHandle *res = NULL ;
@@ -476,36 +476,36 @@ extern "C" int open(const char *name, int oflag, ...) {
476
476
/* The first part of the filename (between first 2 '/') is not a
477
477
* registered mount point in the namespace.
478
478
*/
479
- return handle_open_errors (-ENODEV, fh_i );
479
+ return handle_open_errors (-ENODEV, fildes );
480
480
}
481
481
482
482
if (path.isFile ()) {
483
483
res = path.file ();
484
484
} else {
485
485
FileSystemHandle *fs = path.fileSystem ();
486
486
if (fs == NULL ) {
487
- return handle_open_errors (-ENODEV, fh_i );
487
+ return handle_open_errors (-ENODEV, fildes );
488
488
}
489
489
int err = fs->open (&res, path.fileName (), oflag);
490
490
if (err) {
491
- return handle_open_errors (err, fh_i );
491
+ return handle_open_errors (err, fildes );
492
492
}
493
493
}
494
494
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 ;
498
498
499
- return fh_i ;
499
+ return fildes ;
500
500
}
501
501
502
502
extern " C" int PREFIX (_close)(FILEHANDLE fh) {
503
503
return close (fh);
504
504
}
505
505
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 ;
509
509
if (fhc == NULL ) {
510
510
errno = EBADF;
511
511
return -1 ;
@@ -610,9 +610,9 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
610
610
#endif
611
611
}
612
612
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) {
614
614
615
- FileHandle* fhc = get_fhc (fh );
615
+ FileHandle* fhc = get_fhc (fildes );
616
616
if (fhc == NULL ) {
617
617
errno = EBADF;
618
618
return -1 ;
@@ -700,9 +700,9 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
700
700
#endif
701
701
}
702
702
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) {
704
704
705
- FileHandle* fhc = get_fhc (fh );
705
+ FileHandle* fhc = get_fhc (fildes );
706
706
if (fhc == NULL ) {
707
707
errno = EBADF;
708
708
return -1 ;
@@ -727,8 +727,8 @@ extern "C" int _isatty(FILEHANDLE fh)
727
727
return isatty (fh);
728
728
}
729
729
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 );
732
732
if (fhc == NULL ) {
733
733
errno = EBADF;
734
734
return 0 ;
@@ -765,8 +765,8 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
765
765
return off;
766
766
}
767
767
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 );
770
770
if (fhc == NULL ) {
771
771
errno = EBADF;
772
772
return -1 ;
@@ -786,8 +786,8 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
786
786
}
787
787
#endif
788
788
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 );
791
791
if (fhc == NULL ) {
792
792
errno = EBADF;
793
793
return -1 ;
@@ -850,8 +850,8 @@ extern "C" int _fstat(int fh, struct stat *st) {
850
850
}
851
851
#endif
852
852
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 );
855
855
if (fhc == NULL ) {
856
856
errno = EBADF;
857
857
return -1 ;
@@ -862,6 +862,41 @@ extern "C" int fstat(int fh, struct stat *st) {
862
862
return 0 ;
863
863
}
864
864
865
+ extern " C" int fcntl (int fildes, int cmd, ...) {
866
+ FileHandle *fhc = get_fhc (fildes);
867
+ if (fhc == NULL ) {
868
+ errno = EBADF;
869
+ return -1 ;
870
+ }
871
+
872
+ switch (cmd) {
873
+ case F_GETFL: {
874
+ int flags = 0 ;
875
+ if (fhc->is_blocking ()) {
876
+ flags |= O_NONBLOCK;
877
+ }
878
+ return flags;
879
+ }
880
+ case F_SETFL: {
881
+ va_list ap;
882
+ va_start (ap, cmd);
883
+ int flags = va_arg (ap, int );
884
+ va_end (ap);
885
+ int ret = fhc->set_blocking (flags & O_NONBLOCK);
886
+ if (ret < 0 ) {
887
+ errno = -ret;
888
+ return -1 ;
889
+ }
890
+ return 0 ;
891
+ }
892
+
893
+ default : {
894
+ errno = EINVAL;
895
+ return -1 ;
896
+ }
897
+ }
898
+ }
899
+
865
900
extern " C" int poll (struct pollfd fds[], nfds_t nfds, int timeout)
866
901
{
867
902
if (nfds > OPEN_MAX) {
0 commit comments