std::string read_string(std::istream * in) { if (in->eof() or in->fail() or in->bad()) { throw InvalidStream(); } std::string str; std::getline(*in, str, '\0'); return str; }
int fputc(int ch, FILE *file) { int count; ui8 c = (ui8)ch; #if OS_PARM_CHECK /*-------------------------------------------------------------------*/ /* Check for valid stream. */ /*-------------------------------------------------------------------*/ if (InvalidStream(file)) { set_errno(EBADF); return EOF; } #endif /*-------------------------------------------------------------------*/ /* Acquire exclusive access to stream. */ /*-------------------------------------------------------------------*/ file->acquire(file, F_WRITE); /*-------------------------------------------------------------------*/ /* Return error if file is closed. */ /*-------------------------------------------------------------------*/ if (file->ioctl == NULL) { set_errno(EBADF); file->release(file, F_WRITE); return EOF; } /*-------------------------------------------------------------------*/ /* Write character to stream. */ /*-------------------------------------------------------------------*/ count = file->write(file, &c, 1); /*-------------------------------------------------------------------*/ /* Release exclusive access to stream and return result. */ /*-------------------------------------------------------------------*/ file->release(file, F_WRITE); return (count == 1) ? ch : EOF; }
std::string read_string(/*_IO_*/FILE * file) { if (feof(file) or ferror(file)) { throw InvalidStream(); } char target[MAX_STRING_BUFFER + 1]; for (size_t i = 0; i < MAX_STRING_BUFFER; ++i) { target[i] = static_cast<char>(getc(file)); if (target[i] == '\0') { break; } } target[MAX_STRING_BUFFER] = '\0'; std::string str(target); return str; }
FILE *freopen(const char *filename, const char *mode, FILE *file) { void *dir; FILE *rv = NULL; #if !_PATH_NO_TRUNC char trunc_filename[PATH_MAX + 1]; #endif #if OS_PARM_CHECK /*-------------------------------------------------------------------*/ /* If file handle is invalid, return NULL. */ /*-------------------------------------------------------------------*/ if (InvalidStream(file)) { set_errno(EBADF); return NULL; } /*-------------------------------------------------------------------*/ /* If file name is NULL, return NULL. */ /*-------------------------------------------------------------------*/ if (filename == NULL) { file->errcode = EINVAL; set_errno(EINVAL); return NULL; } #endif /*-------------------------------------------------------------------*/ /* Get exclusive access to upper file system. */ /*-------------------------------------------------------------------*/ semPend(FileSysSem, WAIT_FOREVER); /*-------------------------------------------------------------------*/ /* Flush and close the file, acquiring and releasing volume access. */ /*-------------------------------------------------------------------*/ file->acquire(file, F_READ | F_WRITE); file->ioctl(file, FFLUSH); file->ioctl(file, FCLOSE); file->release(file, F_READ | F_WRITE); /*-------------------------------------------------------------------*/ /* Verify path of new file. Error exit if path is invalid. */ /*-------------------------------------------------------------------*/ dir = FSearch(file, &filename, PARENT_DIR); if (dir == NULL) goto end; /*-------------------------------------------------------------------*/ /* If path too long, return error if no truncation, else truncate. */ /*-------------------------------------------------------------------*/ if (strlen(filename) > PATH_MAX) { #if _PATH_NO_TRUNC set_errno(ENAMETOOLONG); goto end; #else strncpy(trunc_filename, filename, PATH_MAX); trunc_filename[PATH_MAX] = '\0'; filename = trunc_filename; #endif } /*-------------------------------------------------------------------*/ /* Open the file, acquiring and releasing volume access. */ /*-------------------------------------------------------------------*/ file->acquire(file, F_READ | F_WRITE); rv = file->ioctl(file, FOPEN, filename, mode, dir); file->release(file, F_READ | F_WRITE); end: /*-------------------------------------------------------------------*/ /* Free control block if error. Release access to upper file system. */ /*-------------------------------------------------------------------*/ if (rv == NULL) file->ioctl = NULL; semPost(FileSysSem); return rv; }
int fflush(FILE *file) { int rv = 0; /*-------------------------------------------------------------------*/ /* Acquire exclusive access to upper file system. */ /*-------------------------------------------------------------------*/ semPend(FileSysSem, WAIT_FOREVER); /*-------------------------------------------------------------------*/ /* If file is not NULL, flush just the specified file. */ /*-------------------------------------------------------------------*/ if (file) { #if OS_PARM_CHECK /*-----------------------------------------------------------------*/ /* Return error if file handle is invalid. */ /*-----------------------------------------------------------------*/ if (InvalidStream(file)) { set_errno(EBADF); semPost(FileSysSem); return EOF; } #endif /*-----------------------------------------------------------------*/ /* Return error if file is closed. */ /*-----------------------------------------------------------------*/ if (file->ioctl == NULL) { set_errno(EBADF); semPost(FileSysSem); return EOF; } /*-----------------------------------------------------------------*/ /* Acquire exclusive access to stream. */ /*-----------------------------------------------------------------*/ file->acquire(file, F_WRITE); /*-----------------------------------------------------------------*/ /* Call file system specific FFLUSH routine. */ /*-----------------------------------------------------------------*/ rv = (int)file->ioctl(file, FFLUSH); /*-----------------------------------------------------------------*/ /* Release exclusive access to stream. */ /*-----------------------------------------------------------------*/ file->release(file, F_WRITE); } /*-------------------------------------------------------------------*/ /* Else flush all open files. */ /*-------------------------------------------------------------------*/ else { /*-----------------------------------------------------------------*/ /* Loop over every file. */ /*-----------------------------------------------------------------*/ for (file = &Files[0]; file < &Files[FOPEN_MAX]; ++file) { /*---------------------------------------------------------------*/ /* Check if file is open. */ /*---------------------------------------------------------------*/ if (file->ioctl) { /*-------------------------------------------------------------*/ /* Acquire exclusive write access to lower file system. */ /*-------------------------------------------------------------*/ file->acquire(file, F_WRITE); /*-------------------------------------------------------------*/ /* If any flush fails, set return value to -1. */ /*-------------------------------------------------------------*/ if (file->ioctl(file, FFLUSH)) rv = EOF; /*-------------------------------------------------------------*/ /* Release exclusive write access to lower file system. */ /*-------------------------------------------------------------*/ file->release(file, F_WRITE); } } } /*-------------------------------------------------------------------*/ /* Release exclusive access to upper file system and return result. */ /*-------------------------------------------------------------------*/ semPost(FileSysSem); return rv; }