10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51846Scraigm * Common Development and Distribution License (the "License").
61846Scraigm * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * This routine is a special case, in that it is aware of
340Sstevel@tonic-gate * both small and large file interfaces. It must be built
350Sstevel@tonic-gate * in the small compilation environment.
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include "lint.h"
390Sstevel@tonic-gate #include "file64.h"
400Sstevel@tonic-gate #include <mtlib.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <fcntl.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <limits.h>
460Sstevel@tonic-gate #include <thread.h>
470Sstevel@tonic-gate #include <synch.h>
480Sstevel@tonic-gate #include "stdiom.h"
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * open UNIX file name, associate with iop
530Sstevel@tonic-gate */
540Sstevel@tonic-gate
550Sstevel@tonic-gate FILE *
_endopen(const char * name,const char * type,FILE * iop,int largefile)560Sstevel@tonic-gate _endopen(const char *name, const char *type, FILE *iop, int largefile)
570Sstevel@tonic-gate {
581846Scraigm int oflag, fd, fflag;
590Sstevel@tonic-gate char plus;
600Sstevel@tonic-gate
610Sstevel@tonic-gate if (iop == NULL)
620Sstevel@tonic-gate return (NULL);
630Sstevel@tonic-gate switch (type[0]) {
640Sstevel@tonic-gate default:
650Sstevel@tonic-gate errno = EINVAL;
660Sstevel@tonic-gate return (NULL);
670Sstevel@tonic-gate case 'r':
680Sstevel@tonic-gate oflag = O_RDONLY;
691846Scraigm fflag = _IOREAD;
700Sstevel@tonic-gate break;
710Sstevel@tonic-gate case 'w':
720Sstevel@tonic-gate oflag = O_WRONLY | O_TRUNC | O_CREAT;
731846Scraigm fflag = _IOWRT;
740Sstevel@tonic-gate break;
750Sstevel@tonic-gate case 'a':
760Sstevel@tonic-gate oflag = O_WRONLY | O_APPEND | O_CREAT;
771846Scraigm fflag = _IOWRT;
780Sstevel@tonic-gate break;
790Sstevel@tonic-gate }
800Sstevel@tonic-gate /* UNIX ignores 'b' and treats text and binary the same */
810Sstevel@tonic-gate if ((plus = type[1]) == 'b')
820Sstevel@tonic-gate plus = type[2];
831846Scraigm if (plus == '+') {
840Sstevel@tonic-gate oflag = (oflag & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
851846Scraigm fflag = _IORW;
861846Scraigm }
870Sstevel@tonic-gate
880Sstevel@tonic-gate /* select small or large file open based on flag */
890Sstevel@tonic-gate if (largefile) {
900Sstevel@tonic-gate fd = open64(name, oflag, 0666);
910Sstevel@tonic-gate } else {
920Sstevel@tonic-gate fd = open(name, oflag, 0666);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate if (fd < 0)
950Sstevel@tonic-gate return (NULL);
960Sstevel@tonic-gate
971846Scraigm /* As long as we make sure _flag stays != 0, we don't need to lock */
980Sstevel@tonic-gate #ifdef _LP64
990Sstevel@tonic-gate iop->_file = fd;
1001846Scraigm iop->_flag = (iop->_flag & ~0377) | fflag;
1010Sstevel@tonic-gate #else
1021846Scraigm if (fd <= _FILE_FD_MAX) {
1031846Scraigm SET_FILE(iop, fd);
1041846Scraigm } else if (_file_set(iop, fd, type) != 0) {
1051846Scraigm /* errno set in _file_set() */
1060Sstevel@tonic-gate (void) close(fd);
1070Sstevel@tonic-gate return (NULL);
1080Sstevel@tonic-gate }
1091846Scraigm iop->_flag = fflag;
1100Sstevel@tonic-gate #endif /* _LP64 */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if (oflag == (O_WRONLY | O_APPEND | O_CREAT)) { /* type == "a" */
1130Sstevel@tonic-gate if (lseek64(fd, (off64_t)0, SEEK_END) < (off64_t)0) {
1140Sstevel@tonic-gate (void) close(fd);
1150Sstevel@tonic-gate return (NULL);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate return (iop);
1200Sstevel@tonic-gate }
121