- if ( ( rc->status = _PDCLIB_filemode( mode ) ) == 0 ) goto fail; /* invalid mode */
- rc->handle = _PDCLIB_open( filename, rc->status );
- if ( rc->handle == _PDCLIB_NOHANDLE ) goto fail; /* OS open() failed */
- /* Adding to list of open files */
- rc->next = _PDCLIB_filelist;
- _PDCLIB_filelist = rc;
- /* Setting buffer, and mark as internal. TODO: Check for unbuffered */
- if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL ) goto fail;
- rc->bufsize = BUFSIZ;
- rc->bufidx = 0;
- rc->status |= ( _PDCLIB_LIBBUFFER | _PDCLIB_VIRGINSTR );
- /* TODO: Setting mbstate */
- return rc;
-fail:
- free( rc );
- return NULL;
+
+ FILE * f = _PDCLIB_fvopen( fd, ops, imode, filename );
+ if(!f) {
+ int saveErrno = errno;
+ ops->close(fd);
+ errno = saveErrno;
+ }
+ return f;