2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * University Copyright- Copyright (c) 1982, 1986, 1988
2N/A * The Regents of the University of California
2N/A * All Rights Reserved
2N/A *
2N/A * University Acknowledgment- Portions of this document are derived from
2N/A * software developed by the University of California, Berkeley, and its
2N/A * contributors.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "lint.h"
2N/A#include "file64.h"
2N/A#include <sys/types.h>
2N/A#include <stdio.h>
2N/A#include <mtlib.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <limits.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <stdlib.h>
2N/A#include <errno.h>
2N/A#include "stdiom.h"
2N/A#include "xpg6.h"
2N/A
2N/A/* Final argument to _endopen depends on build environment */
2N/A#define LARGE_OPEN (_FILE_OFFSET_BITS == 64)
2N/A
2N/AFILE *
2N/Afopen(const char *name, const char *type) /* open name, return new stream */
2N/A{
2N/A FILE *iop;
2N/A FILE *rc;
2N/A
2N/A iop = _findiop();
2N/A /*
2N/A * Note that iop is not locked here, since no other thread could
2N/A * possibly call _endopen with the same iop at this point.
2N/A */
2N/A rc = _endopen(name, type, iop, LARGE_OPEN);
2N/A
2N/A if (rc == NULL && iop != NULL)
2N/A iop->_flag = 0; /* release iop */
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/Astatic FILE *
2N/A_freopen_null(const char *type, FILE *iop)
2N/A{
2N/A char plus, mode;
2N/A int oflag, nflag, fd, accmode;
2N/A mbstate_t *mb;
2N/A
2N/A if (iop == NULL || iop->_flag == 0) {
2N/A errno = EBADF;
2N/A return (NULL);
2N/A }
2N/A
2N/A if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
2N/A (void) _fflush_u(iop);
2N/A
2N/A if (iop->_flag & _IOMYBUF) {
2N/A free((char *)iop->_base - PUSHBACK);
2N/A }
2N/A iop->_base = NULL;
2N/A iop->_ptr = NULL;
2N/A /*
2N/A * Clear stream orientation, clear stream encoding rule, and set
2N/A * stream's mbstate_t object to describe an initial conversion state.
2N/A */
2N/A mb = _getmbstate(iop);
2N/A if (mb != NULL)
2N/A (void) memset(mb, 0, sizeof (mbstate_t));
2N/A iop->_cnt = 0;
2N/A _setorientation(iop, _NO_MODE);
2N/A
2N/A fd = FILENO(iop);
2N/A mode = type[0];
2N/A if (mode != 'r' && mode != 'w' && mode != 'a') {
2N/A errno = EINVAL;
2N/A goto errret;
2N/A }
2N/A
2N/A if ((oflag = fcntl(fd, F_GETFL)) == -1)
2N/A goto errret;
2N/A
2N/A if ((plus = type[1]) == 'b')
2N/A plus = type[2];
2N/A
2N/A /*
2N/A * Because the filename has not been specified, the underlying file
2N/A * will not be closed and reopened. The access modes of an open
2N/A * file descriptor can't be changed via fcntl(). When '+' is
2N/A * specified, the old access mode needs to be O_RDWR. When 'r' is
2N/A * specified, the old access mode needs to be O_RDONLY or O_RDWR.
2N/A * When 'a' or 'w' is specified, the old access mode needs to be
2N/A * O_WRONLY or O_RDWR. Otherwise, fail with EBADF, indicating that
2N/A * the underlying file descriptor was not opened with a mode that
2N/A * would allow the stream to do successful I/O with the requested mode.
2N/A */
2N/A
2N/A accmode = oflag & O_ACCMODE;
2N/A if ((accmode == O_RDONLY && (mode != 'r' || plus == '+')) ||
2N/A (accmode == O_WRONLY && (mode == 'r' || plus == '+'))) {
2N/A (void) close(fd);
2N/A errno = EBADF;
2N/A goto errret_noclose;
2N/A }
2N/A
2N/A#ifdef _LP64
2N/A iop->_flag &= ~0377; /* clear lower 8-bits */
2N/A if (mode == 'r') {
2N/A iop->_flag |= _IOREAD;
2N/A nflag = oflag & ~O_APPEND;
2N/A } else if (mode == 'w') {
2N/A iop->_flag |= _IOWRT;
2N/A nflag = oflag & ~O_APPEND;
2N/A } else {
2N/A iop->_flag |= _IOWRT;
2N/A nflag = oflag | O_APPEND;
2N/A }
2N/A if (plus == '+') {
2N/A iop->_flag = (iop->_flag & ~(_IOREAD | _IOWRT)) | _IORW;
2N/A }
2N/A#else
2N/A if (mode == 'r') {
2N/A iop->_flag = _IOREAD;
2N/A nflag = oflag & ~O_APPEND;
2N/A } else if (mode == 'w') {
2N/A iop->_flag = _IOWRT;
2N/A nflag = oflag & ~O_APPEND;
2N/A } else {
2N/A iop->_flag = _IOWRT;
2N/A nflag = oflag | O_APPEND;
2N/A }
2N/A if (plus == '+') {
2N/A iop->_flag = _IORW;
2N/A }
2N/A#endif
2N/A /*
2N/A * Change mode of underlying fd as much as possible without closing
2N/A * and reopening it. Ignore truncate failures, eg. with stdout.
2N/A */
2N/A if (mode == 'w')
2N/A (void) ftruncate64(fd, (off64_t)0);
2N/A
2N/A if (fcntl(fd, F_SETFL, nflag) == -1)
2N/A goto errret;
2N/A
2N/A /* ignore seek failures, eg. with pipes */
2N/A (void) lseek64(fd, (off64_t)0, SEEK_SET);
2N/A
2N/A return (iop);
2N/A
2N/Aerrret:
2N/A if (errno != EBADF)
2N/A (void) close(fd);
2N/A
2N/Aerrret_noclose:
2N/A iop->_flag = 0; /* release iop */
2N/A return (NULL);
2N/A}
2N/A
2N/AFILE *
2N/Afreopen(const char *name, const char *type, FILE *iop)
2N/A{
2N/A FILE *rc;
2N/A rmutex_t *lk;
2N/A
2N/A if (name == NULL && __xpg6 & _C99SUSv3_freopen_NULL_filename) {
2N/A /*
2N/A * XPG6: If name is a null pointer, freopen will attempt to
2N/A * change the mode of the stream to that specified by type.
2N/A */
2N/A FLOCKFILE(lk, iop);
2N/A rc = _freopen_null(type, iop);
2N/A FUNLOCKFILE(lk);
2N/A return (rc);
2N/A }
2N/A /*
2N/A * there may be concurrent calls to reopen the same stream - need
2N/A * to make freopen() atomic
2N/A */
2N/A FLOCKFILE(lk, iop);
2N/A /*
2N/A * new function to do everything that fclose() does, except
2N/A * to release the iop - this cannot yet be released since
2N/A * _endopen() is yet to be called on this iop
2N/A */
2N/A
2N/A (void) close_fd(iop);
2N/A
2N/A rc = _endopen(name, type, iop, LARGE_OPEN);
2N/A
2N/A if (rc == NULL)
2N/A iop->_flag = 0; /* release iop */
2N/A
2N/A FUNLOCKFILE(lk);
2N/A return (rc);
2N/A}