Path: blob/master/runtime/compiler/env/FilePointer.cpp
6000 views
/*******************************************************************************1* Copyright (c) 2000, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122#include <stdio.h>23#include <stdlib.h>24#include <string.h>2526#if defined(J9ZOS390)27extern "C"28{29#include "atoe.h"30}31#undef fwrite32#endif3334#include "env/FilePointer.hpp"35#include "j9.h"36#include "env/IO.hpp"3738extern char *feGetEnv(const char *);3940namespace TR41{4243FILE TR::FilePointer::_null = FilePointer(NULL);44FILE TR::FilePointer::_stdin = FilePointer(stdin);45FILE TR::FilePointer::_stdout = FilePointer(stdout);46FILE TR::FilePointer::_stderr = FilePointer(stderr);474849FilePointer::FilePointer(::FILE *stream)50{51initialize(stream);52}535455void56FilePointer::initialize(::FILE *stream)57{58_stream = stream;59_useJ9IO = false;60}616263void64FilePointer::initialize(J9PortLibrary *portLib, int32_t fileId)65{66PORT_ACCESS_FROM_PORT(portLib);67_fileId = fileId;68_useJ9IO = true;69}707172int32_t73FilePointer::write(J9PortLibrary *portLib, char *buf, int32_t length)74{75PORT_ACCESS_FROM_PORT(portLib);76if (length > 0)77{78if (_useJ9IO)79{80return j9file_write(_fileId, buf, length);81}82else83length = fwrite(buf, 1, length, _stream);84}8586return length;87}888990void91FilePointer::close(J9PortLibrary *portLib)92{93PORT_ACCESS_FROM_PORT(portLib);94if (_useJ9IO)95{96flush(portLib);97j9file_sync(_fileId);98j9file_close(_fileId);99}100else101{102fclose(_stream);103}104}105106107void108FilePointer::flush(J9PortLibrary *portLib)109{110PORT_ACCESS_FROM_PORT(portLib);111if (!_useJ9IO)112fflush(_stream);113}114115}116117118