17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51d2738a5Sraf * Common Development and Distribution License (the "License").
61d2738a5Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
211d2738a5Sraf
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24cd62a92dSRobert Mustacchi * Copyright 2020 Robert Mustacchi
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
28cd62a92dSRobert Mustacchi /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307257d1b4Sraf #include "lint.h"
317c478bd9Sstevel@tonic-gate #include "mtlib.h"
327c478bd9Sstevel@tonic-gate #include "file64.h"
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <thread.h>
377c478bd9Sstevel@tonic-gate #include <synch.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include "stdiom.h"
417c478bd9Sstevel@tonic-gate #include <wchar.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <stddef.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
45a5f69788Scraigm #include <fcntl.h>
46cd62a92dSRobert Mustacchi #include <sys/debug.h>
47cd62a92dSRobert Mustacchi #include <limits.h>
487c478bd9Sstevel@tonic-gate
497257d1b4Sraf #define _iob __iob
507257d1b4Sraf
517c478bd9Sstevel@tonic-gate #undef end
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define FILE_ARY_SZ 8 /* a nice size for FILE array & end_buffer_ptrs */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #ifdef _LP64
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate * Macros to declare and loop over a fp or fp/xfp combo to
597c478bd9Sstevel@tonic-gate * avoid some of the _LP64 ifdef hell.
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate #define FPDECL(fp) FILE *fp
637c478bd9Sstevel@tonic-gate #define FIRSTFP(lp, fp) fp = lp->iobp
647c478bd9Sstevel@tonic-gate #define NEXTFP(fp) fp++
65a5f69788Scraigm #define FPLOCK(fp) &fp->_lock
66a5f69788Scraigm #define FPSTATE(fp) &fp->_state
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #define xFILE FILE
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate #else
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate #define FPDECL(fp) FILE *fp; xFILE *x##fp
737c478bd9Sstevel@tonic-gate #define FIRSTFP(lp, fp) x##fp = lp->iobp; \
747c478bd9Sstevel@tonic-gate fp = x##fp ? &x##fp->_iob : &_iob[0]
757c478bd9Sstevel@tonic-gate #define NEXTFP(fp) (x##fp ? fp = &(++x##fp)->_iob : ++fp)
76a5f69788Scraigm #define FPLOCK(fp) x##fp ? \
77a5f69788Scraigm &x##fp->xlock : &_xftab[IOPIND(fp)]._lock
78a5f69788Scraigm #define FPSTATE(fp) x##fp ? \
79a5f69788Scraigm &x##fp->xstate : &_xftab[IOPIND(fp)]._state
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate /* The extended 32-bit file structure for use in link buffers */
827c478bd9Sstevel@tonic-gate typedef struct xFILE {
837c478bd9Sstevel@tonic-gate FILE _iob; /* must be first! */
847c478bd9Sstevel@tonic-gate struct xFILEdata _xdat;
857c478bd9Sstevel@tonic-gate } xFILE;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate #define xmagic _xdat._magic
887c478bd9Sstevel@tonic-gate #define xend _xdat._end
897c478bd9Sstevel@tonic-gate #define xlock _xdat._lock
907c478bd9Sstevel@tonic-gate #define xstate _xdat._state
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate #define FILEx(fp) ((struct xFILE *)(uintptr_t)fp)
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate * The magic number stored is actually the pointer scrambled with
967c478bd9Sstevel@tonic-gate * a magic number. Pointers to data items live everywhere in memory
977c478bd9Sstevel@tonic-gate * so we scramble the pointer in order to avoid accidental collisions.
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate #define XFILEMAGIC 0x63687367
1007c478bd9Sstevel@tonic-gate #define XMAGIC(xfp) ((uintptr_t)(xfp) ^ XFILEMAGIC)
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate #endif /* _LP64 */
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate struct _link_ /* manages a list of streams */
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate xFILE *iobp; /* the array of (x)FILE's */
1077c478bd9Sstevel@tonic-gate /* NULL for the __first_link in ILP32 */
1087c478bd9Sstevel@tonic-gate int niob; /* length of the arrays */
1097c478bd9Sstevel@tonic-gate struct _link_ *next; /* next in the list */
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate * With dynamic linking, iob may be in either the library or in the user's
1147c478bd9Sstevel@tonic-gate * a.out, so the run time linker fixes up the first entry in __first_link at
1157c478bd9Sstevel@tonic-gate * process startup time.
1167c478bd9Sstevel@tonic-gate *
1177c478bd9Sstevel@tonic-gate * In 32 bit processes, we don't have xFILE[FILE_ARY_SZ] but FILE[],
1187c478bd9Sstevel@tonic-gate * and _xftab[] instead; this is denoted by having iobp set to NULL in
1197c478bd9Sstevel@tonic-gate * 32 bit mode for the first link entry.
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate struct _link_ __first_link = /* first in linked list */
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate #if !defined(_LP64)
1247c478bd9Sstevel@tonic-gate NULL,
1257c478bd9Sstevel@tonic-gate #else
1267c478bd9Sstevel@tonic-gate &_iob[0],
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate _NFILE,
1297c478bd9Sstevel@tonic-gate NULL
1307c478bd9Sstevel@tonic-gate };
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate * Information cached to speed up searches. We remember where we
1347c478bd9Sstevel@tonic-gate * last found a free FILE* and we remember whether we saw any fcloses
1357c478bd9Sstevel@tonic-gate * in between. We also count the number of chunks we allocated, see
1367c478bd9Sstevel@tonic-gate * _findiop() for an explanation.
1377c478bd9Sstevel@tonic-gate * These variables are all protected by _first_link_lock.
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate static struct _link_ *lastlink = NULL;
1407c478bd9Sstevel@tonic-gate static int fcloses;
1417c478bd9Sstevel@tonic-gate static int nchunks;
1427c478bd9Sstevel@tonic-gate
143a5f69788Scraigm static mutex_t _first_link_lock = DEFAULTMUTEX;
1447c478bd9Sstevel@tonic-gate
145a5f69788Scraigm static int _fflush_l_iops(void);
1467c478bd9Sstevel@tonic-gate static FILE *getiop(FILE *, rmutex_t *, mbstate_t *);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * All functions that understand the linked list of iob's follow.
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate #pragma weak _cleanup = __cleanup
1527c478bd9Sstevel@tonic-gate void
__cleanup(void)1537c478bd9Sstevel@tonic-gate __cleanup(void) /* called at process end to flush ouput streams */
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate (void) fflush(NULL);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * For fork1-safety (see libc_prepare_atfork(), etc).
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate void
stdio_locks()1627c478bd9Sstevel@tonic-gate stdio_locks()
1637c478bd9Sstevel@tonic-gate {
1648cd45542Sraf (void) mutex_lock(&_first_link_lock);
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * XXX: We should acquire all of the iob locks here.
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate void
stdio_unlocks()1717c478bd9Sstevel@tonic-gate stdio_unlocks()
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate * XXX: We should release all of the iob locks here.
1757c478bd9Sstevel@tonic-gate */
1768cd45542Sraf (void) mutex_unlock(&_first_link_lock);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate void
_flushlbf(void)1807c478bd9Sstevel@tonic-gate _flushlbf(void) /* fflush() all line-buffered streams */
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate FPDECL(fp);
1837c478bd9Sstevel@tonic-gate int i;
1847c478bd9Sstevel@tonic-gate struct _link_ *lp;
185a5f69788Scraigm /* Allow compiler to optimize the loop */
186a5f69788Scraigm int threaded = __libc_threaded;
1877c478bd9Sstevel@tonic-gate
188a5f69788Scraigm if (threaded)
189a574db85Sraf cancel_safe_mutex_lock(&_first_link_lock);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate lp = &__first_link;
1927c478bd9Sstevel@tonic-gate do {
1937c478bd9Sstevel@tonic-gate FIRSTFP(lp, fp);
1947c478bd9Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
195a5f69788Scraigm /*
196a5f69788Scraigm * The additional _IONBF check guards againsts
197a5f69788Scraigm * allocated but uninitialized iops (see _findiop).
198a5f69788Scraigm * We also automatically skip non allocated iop's.
199a5f69788Scraigm * Don't block on locks.
200a5f69788Scraigm */
201a5f69788Scraigm if ((fp->_flag & (_IOLBF | _IOWRT | _IONBF)) ==
202a5f69788Scraigm (_IOLBF | _IOWRT)) {
203a5f69788Scraigm if (threaded) {
204a5f69788Scraigm rmutex_t *lk = FPLOCK(fp);
205a574db85Sraf if (cancel_safe_mutex_trylock(lk) != 0)
206a5f69788Scraigm continue;
207a5f69788Scraigm /* Recheck after locking */
208a5f69788Scraigm if ((fp->_flag & (_IOLBF | _IOWRT)) ==
209a5f69788Scraigm (_IOLBF | _IOWRT)) {
210a5f69788Scraigm (void) _fflush_u(fp);
211a5f69788Scraigm }
212a574db85Sraf cancel_safe_mutex_unlock(lk);
213a5f69788Scraigm } else {
214a5f69788Scraigm (void) _fflush_u(fp);
215a5f69788Scraigm }
216a5f69788Scraigm }
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate } while ((lp = lp->next) != NULL);
2197c478bd9Sstevel@tonic-gate
220a5f69788Scraigm if (threaded)
221a574db85Sraf cancel_safe_mutex_unlock(&_first_link_lock);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /* allocate an unused stream; NULL if cannot */
2257c478bd9Sstevel@tonic-gate FILE *
_findiop(void)2267c478bd9Sstevel@tonic-gate _findiop(void)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate struct _link_ *lp, **prev;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /* used so there only needs to be one malloc() */
2317c478bd9Sstevel@tonic-gate #ifdef _LP64
2327c478bd9Sstevel@tonic-gate typedef struct {
2337c478bd9Sstevel@tonic-gate struct _link_ hdr;
2347c478bd9Sstevel@tonic-gate FILE iob[FILE_ARY_SZ];
2357c478bd9Sstevel@tonic-gate } Pkg;
2367c478bd9Sstevel@tonic-gate #else
2377c478bd9Sstevel@tonic-gate typedef union {
2387c478bd9Sstevel@tonic-gate struct { /* Normal */
2397c478bd9Sstevel@tonic-gate struct _link_ hdr;
2407c478bd9Sstevel@tonic-gate xFILE iob[FILE_ARY_SZ];
2417c478bd9Sstevel@tonic-gate } Pkgn;
2427c478bd9Sstevel@tonic-gate struct { /* Reversed */
2437c478bd9Sstevel@tonic-gate xFILE iob[FILE_ARY_SZ];
2447c478bd9Sstevel@tonic-gate struct _link_ hdr;
2457c478bd9Sstevel@tonic-gate } Pkgr;
2467c478bd9Sstevel@tonic-gate } Pkg;
2477c478bd9Sstevel@tonic-gate uintptr_t delta;
2487c478bd9Sstevel@tonic-gate #endif
2497c478bd9Sstevel@tonic-gate Pkg *pkgp;
2507c478bd9Sstevel@tonic-gate struct _link_ *hdr;
2517c478bd9Sstevel@tonic-gate FPDECL(fp);
2527c478bd9Sstevel@tonic-gate int i;
253a5f69788Scraigm int threaded = __libc_threaded;
2547c478bd9Sstevel@tonic-gate
255a5f69788Scraigm if (threaded)
256a574db85Sraf cancel_safe_mutex_lock(&_first_link_lock);
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate if (lastlink == NULL) {
2597c478bd9Sstevel@tonic-gate rescan:
2607c478bd9Sstevel@tonic-gate fcloses = 0;
2617c478bd9Sstevel@tonic-gate lastlink = &__first_link;
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate lp = lastlink;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate * lock to make testing of fp->_flag == 0 and acquiring the fp atomic
2687c478bd9Sstevel@tonic-gate * and for allocation of new links
2697c478bd9Sstevel@tonic-gate * low contention expected on _findiop(), hence coarse locking.
2707c478bd9Sstevel@tonic-gate * for finer granularity, use fp->_lock for allocating an iop
2717c478bd9Sstevel@tonic-gate * and make the testing of lp->next and allocation of new link atomic
2727c478bd9Sstevel@tonic-gate * using lp->_lock
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate do {
2767c478bd9Sstevel@tonic-gate prev = &lp->next;
2777c478bd9Sstevel@tonic-gate FIRSTFP(lp, fp);
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
280a5f69788Scraigm FILE *ret;
281a5f69788Scraigm if (threaded) {
282a5f69788Scraigm ret = getiop(fp, FPLOCK(fp), FPSTATE(fp));
283a5f69788Scraigm if (ret != NULL) {
284a574db85Sraf cancel_safe_mutex_unlock(
285b2e86e7aScasper &_first_link_lock);
286b2e86e7aScasper return (ret);
287a5f69788Scraigm }
288a5f69788Scraigm } else {
289a5f69788Scraigm ret = getiop(fp, NULL, FPSTATE(fp));
290a5f69788Scraigm if (ret != NULL)
291a5f69788Scraigm return (ret);
292a5f69788Scraigm }
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate } while ((lastlink = lp = lp->next) != NULL);
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate * If there was a sufficient number of fcloses since we last started
2987c478bd9Sstevel@tonic-gate * at __first_link, we rescan all fp's again. We do not rescan for
2997c478bd9Sstevel@tonic-gate * all fcloses; that would simplify the algorithm but would make
3007c478bd9Sstevel@tonic-gate * search times near O(n) again.
3017c478bd9Sstevel@tonic-gate * Worst case behaviour would still be pretty bad (open a full set,
3027c478bd9Sstevel@tonic-gate * then continously opening and closing one FILE * gets you a full
3037c478bd9Sstevel@tonic-gate * scan each time). That's why we over allocate 1 FILE for each
3047c478bd9Sstevel@tonic-gate * 32 chunks. More over allocation is better; this is a nice
3057c478bd9Sstevel@tonic-gate * empirical value which doesn't cost a lot of memory, doesn't
3067c478bd9Sstevel@tonic-gate * overallocate until we reach 256 FILE *s and keeps the performance
3077c478bd9Sstevel@tonic-gate * pretty close to the optimum.
3087c478bd9Sstevel@tonic-gate */
3097c478bd9Sstevel@tonic-gate if (fcloses > nchunks/32)
3107c478bd9Sstevel@tonic-gate goto rescan;
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * Need to allocate another and put it in the linked list.
3147c478bd9Sstevel@tonic-gate */
3157c478bd9Sstevel@tonic-gate if ((pkgp = malloc(sizeof (Pkg))) == NULL) {
316a5f69788Scraigm if (threaded)
317a574db85Sraf cancel_safe_mutex_unlock(&_first_link_lock);
3187c478bd9Sstevel@tonic-gate return (NULL);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate (void) memset(pkgp, 0, sizeof (Pkg));
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate #ifdef _LP64
3247c478bd9Sstevel@tonic-gate hdr = &pkgp->hdr;
3257c478bd9Sstevel@tonic-gate hdr->iobp = &pkgp->iob[0];
3267c478bd9Sstevel@tonic-gate #else
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate * The problem with referencing a word after a FILE* is the possibility
3297c478bd9Sstevel@tonic-gate * of a SIGSEGV if a non-stdio issue FILE structure ends on a page
3307c478bd9Sstevel@tonic-gate * boundary. We run this check so we never need to run an expensive
3317c478bd9Sstevel@tonic-gate * check like mincore() in order to know whether it is
3327c478bd9Sstevel@tonic-gate * safe to dereference ((xFILE*)fp)->xmagic.
3337c478bd9Sstevel@tonic-gate * We allocate the block with two alternative layouts; if one
3347c478bd9Sstevel@tonic-gate * layout is not properly aligned for our purposes, the other layout
3357c478bd9Sstevel@tonic-gate * will be because the size of _link_ is small compared to
3367c478bd9Sstevel@tonic-gate * sizeof (xFILE).
3377c478bd9Sstevel@tonic-gate * The check performed is this:
3387c478bd9Sstevel@tonic-gate * If the distance from pkgp to the end of the page is
3397c478bd9Sstevel@tonic-gate * less than the the offset of the last xmagic field in the
3407c478bd9Sstevel@tonic-gate * xFILE structure, (the 0x1000 boundary is inside our just
3417c478bd9Sstevel@tonic-gate * allocated structure) and the distance modulo the size of xFILE
3427c478bd9Sstevel@tonic-gate * is identical to the offset of the first xmagic in the
3437c478bd9Sstevel@tonic-gate * structure (i.e., XXXXXX000 points to an xmagic field),
3447c478bd9Sstevel@tonic-gate * we need to use the reverse structure.
3457c478bd9Sstevel@tonic-gate */
3467c478bd9Sstevel@tonic-gate if ((delta = 0x1000 - ((uintptr_t)pkgp & 0xfff)) <=
347a574db85Sraf offsetof(Pkg, Pkgn.iob[FILE_ARY_SZ-1].xmagic) &&
3487c478bd9Sstevel@tonic-gate delta % sizeof (struct xFILE) ==
349a574db85Sraf offsetof(Pkg, Pkgn.iob[0].xmagic)) {
3507c478bd9Sstevel@tonic-gate /* Use reversed structure */
3517c478bd9Sstevel@tonic-gate hdr = &pkgp->Pkgr.hdr;
3527c478bd9Sstevel@tonic-gate hdr->iobp = &pkgp->Pkgr.iob[0];
3537c478bd9Sstevel@tonic-gate } else {
3547c478bd9Sstevel@tonic-gate /* Use normal structure */
3557c478bd9Sstevel@tonic-gate hdr = &pkgp->Pkgn.hdr;
3567c478bd9Sstevel@tonic-gate hdr->iobp = &pkgp->Pkgn.iob[0];
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate #endif /* _LP64 */
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate hdr->niob = FILE_ARY_SZ;
3617c478bd9Sstevel@tonic-gate nchunks++;
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate #ifdef _LP64
3647c478bd9Sstevel@tonic-gate fp = hdr->iobp;
3657c478bd9Sstevel@tonic-gate for (i = 0; i < FILE_ARY_SZ; i++)
3667257d1b4Sraf (void) mutex_init(&fp[i]._lock,
3677257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
3687c478bd9Sstevel@tonic-gate #else
3697c478bd9Sstevel@tonic-gate xfp = hdr->iobp;
3707c478bd9Sstevel@tonic-gate fp = &xfp->_iob;
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate for (i = 0; i < FILE_ARY_SZ; i++) {
3737c478bd9Sstevel@tonic-gate xfp[i].xmagic = XMAGIC(&xfp[i]);
3747257d1b4Sraf (void) mutex_init(&xfp[i].xlock,
3757257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate #endif /* _LP64 */
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate lastlink = *prev = hdr;
3807c478bd9Sstevel@tonic-gate fp->_ptr = 0;
3817c478bd9Sstevel@tonic-gate fp->_base = 0;
382cd62a92dSRobert Mustacchi /* claim the fp by setting low 8 bits */
383cd62a92dSRobert Mustacchi fp->_flag = _DEF_FLAG_MASK;
384a5f69788Scraigm if (threaded)
385a574db85Sraf cancel_safe_mutex_unlock(&_first_link_lock);
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate return (fp);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate static void
isseekable(FILE * iop)3917c478bd9Sstevel@tonic-gate isseekable(FILE *iop)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate struct stat64 fstatbuf;
394cd62a92dSRobert Mustacchi int fd, save_errno;
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate save_errno = errno;
3977c478bd9Sstevel@tonic-gate
398cd62a92dSRobert Mustacchi /*
399cd62a92dSRobert Mustacchi * non-FILE based STREAMS are required to declare their own seekability
400cd62a92dSRobert Mustacchi * and therefore we should not try and test them below.
401cd62a92dSRobert Mustacchi */
402cd62a92dSRobert Mustacchi fd = _get_fd(iop);
403cd62a92dSRobert Mustacchi if (fd == -1) {
404cd62a92dSRobert Mustacchi return;
405cd62a92dSRobert Mustacchi }
406cd62a92dSRobert Mustacchi if (fstat64(fd, &fstatbuf) != 0) {
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * when we don't know what it is we'll
4097c478bd9Sstevel@tonic-gate * do the old behaviour and flush
4107c478bd9Sstevel@tonic-gate * the stream
4117c478bd9Sstevel@tonic-gate */
4127c478bd9Sstevel@tonic-gate SET_SEEKABLE(iop);
4137c478bd9Sstevel@tonic-gate errno = save_errno;
4147c478bd9Sstevel@tonic-gate return;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate * check for what is non-SEEKABLE
4197c478bd9Sstevel@tonic-gate * otherwise assume it's SEEKABLE so we get the old
4207c478bd9Sstevel@tonic-gate * behaviour and flush the stream
4217c478bd9Sstevel@tonic-gate */
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate if (S_ISFIFO(fstatbuf.st_mode) || S_ISCHR(fstatbuf.st_mode) ||
4247c478bd9Sstevel@tonic-gate S_ISSOCK(fstatbuf.st_mode) || S_ISDOOR(fstatbuf.st_mode)) {
4257c478bd9Sstevel@tonic-gate CLEAR_SEEKABLE(iop);
4267c478bd9Sstevel@tonic-gate } else {
4277c478bd9Sstevel@tonic-gate SET_SEEKABLE(iop);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate errno = save_errno;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate #ifdef _LP64
4347c478bd9Sstevel@tonic-gate void
_setbufend(FILE * iop,Uchar * end)4357c478bd9Sstevel@tonic-gate _setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate iop->_end = end;
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate isseekable(iop);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate #undef _realbufend
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate Uchar *
_realbufend(FILE * iop)4457c478bd9Sstevel@tonic-gate _realbufend(FILE *iop) /* get the end pointer for this iop */
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate return (iop->_end);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate #else /* _LP64 */
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate * Awkward functions not needed for the sane 64 bit environment.
4547c478bd9Sstevel@tonic-gate */
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate * xmagic must not be aligned on a 4K boundary. We guarantee this in
4577c478bd9Sstevel@tonic-gate * _findiop().
4587c478bd9Sstevel@tonic-gate */
4597c478bd9Sstevel@tonic-gate #define VALIDXFILE(xfp) \
4607c478bd9Sstevel@tonic-gate (((uintptr_t)&(xfp)->xmagic & 0xfff) && \
4617c478bd9Sstevel@tonic-gate (xfp)->xmagic == XMAGIC(FILEx(xfp)))
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate static struct xFILEdata *
getxfdat(FILE * iop)4647c478bd9Sstevel@tonic-gate getxfdat(FILE *iop)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate if (STDIOP(iop))
4677c478bd9Sstevel@tonic-gate return (&_xftab[IOPIND(iop)]);
4687c478bd9Sstevel@tonic-gate else if (VALIDXFILE(FILEx(iop)))
4697c478bd9Sstevel@tonic-gate return (&FILEx(iop)->_xdat);
4707c478bd9Sstevel@tonic-gate else
4717c478bd9Sstevel@tonic-gate return (NULL);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate void
_setbufend(FILE * iop,Uchar * end)4757c478bd9Sstevel@tonic-gate _setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop);
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate if (dat != NULL)
4807c478bd9Sstevel@tonic-gate dat->_end = end;
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate isseekable(iop);
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate * For binary compatibility with user programs using the
4867c478bd9Sstevel@tonic-gate * old _bufend macro. This is *so* broken, fileno()
4877c478bd9Sstevel@tonic-gate * is not the proper index.
4887c478bd9Sstevel@tonic-gate */
489a5f69788Scraigm if (iop->_magic < _NFILE)
490a5f69788Scraigm _bufendtab[iop->_magic] = end;
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate Uchar *
_realbufend(FILE * iop)4957c478bd9Sstevel@tonic-gate _realbufend(FILE *iop) /* get the end pointer for this iop */
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop);
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate if (dat != NULL)
5007c478bd9Sstevel@tonic-gate return (dat->_end);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate return (NULL);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate * _reallock() is invoked in each stdio call through the IOB_LCK() macro,
5077c478bd9Sstevel@tonic-gate * it is therefor extremely performance sensitive. We get better performance
5087c478bd9Sstevel@tonic-gate * by inlining the STDIOP check in IOB_LCK and inlining a custom version
5097c478bd9Sstevel@tonic-gate * of getfxdat() here.
5107c478bd9Sstevel@tonic-gate */
5117c478bd9Sstevel@tonic-gate rmutex_t *
_reallock(FILE * iop)5127c478bd9Sstevel@tonic-gate _reallock(FILE *iop)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate if (VALIDXFILE(FILEx(iop)))
5157c478bd9Sstevel@tonic-gate return (&FILEx(iop)->xlock);
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate return (NULL);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate #endif /* _LP64 */
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /* make sure _cnt, _ptr are correct */
5237c478bd9Sstevel@tonic-gate void
_bufsync(FILE * iop,Uchar * bufend)5247c478bd9Sstevel@tonic-gate _bufsync(FILE *iop, Uchar *bufend)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate ssize_t spaceleft;
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate spaceleft = bufend - iop->_ptr;
5297c478bd9Sstevel@tonic-gate if (bufend < iop->_ptr) {
5307c478bd9Sstevel@tonic-gate iop->_ptr = bufend;
5317c478bd9Sstevel@tonic-gate iop->_cnt = 0;
5327c478bd9Sstevel@tonic-gate } else if (spaceleft < iop->_cnt)
5337c478bd9Sstevel@tonic-gate iop->_cnt = spaceleft;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate /* really write out current buffer contents */
5377c478bd9Sstevel@tonic-gate int
_xflsbuf(FILE * iop)5387c478bd9Sstevel@tonic-gate _xflsbuf(FILE *iop)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate ssize_t n;
5417c478bd9Sstevel@tonic-gate Uchar *base = iop->_base;
5427c478bd9Sstevel@tonic-gate Uchar *bufend;
5437c478bd9Sstevel@tonic-gate ssize_t num_wrote;
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate * Hopefully, be stable with respect to interrupts...
5477c478bd9Sstevel@tonic-gate */
5487c478bd9Sstevel@tonic-gate n = iop->_ptr - base;
5497c478bd9Sstevel@tonic-gate iop->_ptr = base;
5507c478bd9Sstevel@tonic-gate bufend = _bufend(iop);
5517c478bd9Sstevel@tonic-gate if (iop->_flag & (_IOLBF | _IONBF))
5527c478bd9Sstevel@tonic-gate iop->_cnt = 0; /* always go to a flush */
5537c478bd9Sstevel@tonic-gate else
5547c478bd9Sstevel@tonic-gate iop->_cnt = bufend - base;
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate if (_needsync(iop, bufend)) /* recover from interrupts */
5577c478bd9Sstevel@tonic-gate _bufsync(iop, bufend);
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate if (n > 0) {
560cd62a92dSRobert Mustacchi while ((num_wrote = _xwrite(iop, base, (size_t)n)) != n) {
5617c478bd9Sstevel@tonic-gate if (num_wrote <= 0) {
562a574db85Sraf if (!cancel_active())
563a574db85Sraf iop->_flag |= _IOERR;
5647c478bd9Sstevel@tonic-gate return (EOF);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate n -= num_wrote;
5677c478bd9Sstevel@tonic-gate base += num_wrote;
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate return (0);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate /* flush (write) buffer */
5747c478bd9Sstevel@tonic-gate int
fflush(FILE * iop)5757c478bd9Sstevel@tonic-gate fflush(FILE *iop)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate int res;
5787c478bd9Sstevel@tonic-gate rmutex_t *lk;
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate if (iop) {
5817c478bd9Sstevel@tonic-gate FLOCKFILE(lk, iop);
5827c478bd9Sstevel@tonic-gate res = _fflush_u(iop);
5837c478bd9Sstevel@tonic-gate FUNLOCKFILE(lk);
5847c478bd9Sstevel@tonic-gate } else {
585a5f69788Scraigm res = _fflush_l_iops(); /* flush all iops */
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate return (res);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate static int
_fflush_l_iops(void)591a5f69788Scraigm _fflush_l_iops(void) /* flush all buffers */
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate FPDECL(iop);
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate int i;
5967c478bd9Sstevel@tonic-gate struct _link_ *lp;
5977c478bd9Sstevel@tonic-gate int res = 0;
598a5f69788Scraigm rmutex_t *lk;
599a5f69788Scraigm /* Allow the compiler to optimize the load out of the loop */
600a5f69788Scraigm int threaded = __libc_threaded;
6017c478bd9Sstevel@tonic-gate
602a5f69788Scraigm if (threaded)
603a574db85Sraf cancel_safe_mutex_lock(&_first_link_lock);
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate lp = &__first_link;
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate do {
6087c478bd9Sstevel@tonic-gate /*
609a5f69788Scraigm * We need to grab the file locks or file corruption
610a5f69788Scraigm * will happen. But we first check the flags field
611a5f69788Scraigm * knowing that when it is 0, it isn't allocated and
612a5f69788Scraigm * cannot be allocated while we're holding the
613a5f69788Scraigm * _first_link_lock. And when _IONBF is set (also the
614cd62a92dSRobert Mustacchi * case when _flag is 0377 -- _DEF_FLAG_MASK, or alloc in
615cd62a92dSRobert Mustacchi * progress), we also ignore it.
616a5f69788Scraigm *
617a5f69788Scraigm * Ignore locked streams; it will appear as if
618a5f69788Scraigm * concurrent updates happened after fflush(NULL). Note
619a5f69788Scraigm * that we even attempt to lock if the locking is set to
620a5f69788Scraigm * "by caller". We don't want to penalize callers of
621a5f69788Scraigm * __fsetlocking() by not flushing their files. Note: if
622a5f69788Scraigm * __fsetlocking() callers don't employ any locking, they
623a5f69788Scraigm * may still face corruption in fflush(NULL); but that's
624a5f69788Scraigm * no change from earlier releases.
6257c478bd9Sstevel@tonic-gate */
6267c478bd9Sstevel@tonic-gate FIRSTFP(lp, iop);
6277c478bd9Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
628a5f69788Scraigm unsigned int flag = iop->_flag;
629a5f69788Scraigm
630a5f69788Scraigm /* flag 0, flag 0377, or _IONBF set */
631a5f69788Scraigm if (flag == 0 || (flag & _IONBF) != 0)
632a5f69788Scraigm continue;
633a5f69788Scraigm
634a5f69788Scraigm if (threaded) {
635a5f69788Scraigm lk = FPLOCK(iop);
636a574db85Sraf if (cancel_safe_mutex_trylock(lk) != 0)
637a5f69788Scraigm continue;
638a5f69788Scraigm }
639a5f69788Scraigm
640a5f69788Scraigm if (!(iop->_flag & _IONBF)) {
6417c478bd9Sstevel@tonic-gate /*
642a5f69788Scraigm * don't need to worry about the _IORW case
643a5f69788Scraigm * since the iop will also marked with _IOREAD
644a5f69788Scraigm * or _IOWRT whichever we are really doing
6457c478bd9Sstevel@tonic-gate */
646a5f69788Scraigm if (iop->_flag & _IOWRT) {
647a5f69788Scraigm /* Flush write buffers */
648a5f69788Scraigm res |= _fflush_u(iop);
649a5f69788Scraigm } else if (iop->_flag & _IOREAD) {
650a5f69788Scraigm /*
651a5f69788Scraigm * flush seekable read buffers
652a5f69788Scraigm * don't flush non-seekable read buffers
653a5f69788Scraigm */
654a5f69788Scraigm if (GET_SEEKABLE(iop)) {
655a5f69788Scraigm res |= _fflush_u(iop);
656a5f69788Scraigm }
657a5f69788Scraigm }
6587c478bd9Sstevel@tonic-gate }
659a5f69788Scraigm if (threaded)
660a574db85Sraf cancel_safe_mutex_unlock(lk);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate } while ((lp = lp->next) != NULL);
663a5f69788Scraigm if (threaded)
664a574db85Sraf cancel_safe_mutex_unlock(&_first_link_lock);
6657c478bd9Sstevel@tonic-gate return (res);
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate /* flush buffer */
6697c478bd9Sstevel@tonic-gate int
_fflush_u(FILE * iop)6707c478bd9Sstevel@tonic-gate _fflush_u(FILE *iop)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate int res = 0;
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate /* this portion is always assumed locked */
6757c478bd9Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) {
676cd62a92dSRobert Mustacchi (void) _xseek64(iop, -iop->_cnt, SEEK_CUR);
6777c478bd9Sstevel@tonic-gate iop->_cnt = 0;
6787c478bd9Sstevel@tonic-gate /* needed for ungetc & multibyte pushbacks */
6797c478bd9Sstevel@tonic-gate iop->_ptr = iop->_base;
6807c478bd9Sstevel@tonic-gate if (iop->_flag & _IORW) {
6817c478bd9Sstevel@tonic-gate iop->_flag &= ~_IOREAD;
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate return (0);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate if (iop->_base != NULL && iop->_ptr > iop->_base) {
6867c478bd9Sstevel@tonic-gate res = _xflsbuf(iop);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate if (iop->_flag & _IORW) {
6897c478bd9Sstevel@tonic-gate iop->_flag &= ~_IOWRT;
6907c478bd9Sstevel@tonic-gate iop->_cnt = 0;
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate return (res);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate /* flush buffer and close stream */
6967c478bd9Sstevel@tonic-gate int
fclose(FILE * iop)6977c478bd9Sstevel@tonic-gate fclose(FILE *iop)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate int res = 0;
7007c478bd9Sstevel@tonic-gate rmutex_t *lk;
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate if (iop == NULL) {
7037c478bd9Sstevel@tonic-gate return (EOF); /* avoid passing zero to FLOCKFILE */
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate FLOCKFILE(lk, iop);
7077c478bd9Sstevel@tonic-gate if (iop->_flag == 0) {
7087c478bd9Sstevel@tonic-gate FUNLOCKFILE(lk);
7097c478bd9Sstevel@tonic-gate return (EOF);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate /* Is not unbuffered and opened for read and/or write ? */
7127c478bd9Sstevel@tonic-gate if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7137c478bd9Sstevel@tonic-gate res = _fflush_u(iop);
714cd62a92dSRobert Mustacchi if (_xclose(iop) < 0)
7157c478bd9Sstevel@tonic-gate res = EOF;
7167c478bd9Sstevel@tonic-gate if (iop->_flag & _IOMYBUF) {
7177c478bd9Sstevel@tonic-gate (void) free((char *)iop->_base - PUSHBACK);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate iop->_base = NULL;
7207c478bd9Sstevel@tonic-gate iop->_ptr = NULL;
7217c478bd9Sstevel@tonic-gate iop->_cnt = 0;
7227c478bd9Sstevel@tonic-gate iop->_flag = 0; /* marks it as available */
7237c478bd9Sstevel@tonic-gate FUNLOCKFILE(lk);
7247c478bd9Sstevel@tonic-gate
7251d2738a5Sraf if (__libc_threaded)
726a574db85Sraf cancel_safe_mutex_lock(&_first_link_lock);
7277c478bd9Sstevel@tonic-gate fcloses++;
7281d2738a5Sraf if (__libc_threaded)
729a574db85Sraf cancel_safe_mutex_unlock(&_first_link_lock);
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate return (res);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate
73423a1cceaSRoger A. Faulkner /* close all open streams */
73523a1cceaSRoger A. Faulkner int
fcloseall(void)73623a1cceaSRoger A. Faulkner fcloseall(void)
73723a1cceaSRoger A. Faulkner {
73823a1cceaSRoger A. Faulkner FPDECL(iop);
73923a1cceaSRoger A. Faulkner
74023a1cceaSRoger A. Faulkner struct _link_ *lp;
74123a1cceaSRoger A. Faulkner rmutex_t *lk;
74223a1cceaSRoger A. Faulkner
74323a1cceaSRoger A. Faulkner if (__libc_threaded)
74423a1cceaSRoger A. Faulkner cancel_safe_mutex_lock(&_first_link_lock);
74523a1cceaSRoger A. Faulkner
74623a1cceaSRoger A. Faulkner lp = &__first_link;
74723a1cceaSRoger A. Faulkner
74823a1cceaSRoger A. Faulkner do {
74923a1cceaSRoger A. Faulkner int i;
75023a1cceaSRoger A. Faulkner
75123a1cceaSRoger A. Faulkner FIRSTFP(lp, iop);
75223a1cceaSRoger A. Faulkner for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
75323a1cceaSRoger A. Faulkner /* code stolen from fclose(), above */
75423a1cceaSRoger A. Faulkner
75523a1cceaSRoger A. Faulkner FLOCKFILE(lk, iop);
75623a1cceaSRoger A. Faulkner if (iop->_flag == 0) {
75723a1cceaSRoger A. Faulkner FUNLOCKFILE(lk);
75823a1cceaSRoger A. Faulkner continue;
75923a1cceaSRoger A. Faulkner }
76023a1cceaSRoger A. Faulkner
76123a1cceaSRoger A. Faulkner /* Not unbuffered and opened for read and/or write? */
76223a1cceaSRoger A. Faulkner if (!(iop->_flag & _IONBF) &&
76323a1cceaSRoger A. Faulkner (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
76423a1cceaSRoger A. Faulkner (void) _fflush_u(iop);
765cd62a92dSRobert Mustacchi (void) _xclose(iop);
76623a1cceaSRoger A. Faulkner if (iop->_flag & _IOMYBUF)
76723a1cceaSRoger A. Faulkner free((char *)iop->_base - PUSHBACK);
76823a1cceaSRoger A. Faulkner iop->_base = NULL;
76923a1cceaSRoger A. Faulkner iop->_ptr = NULL;
77023a1cceaSRoger A. Faulkner iop->_cnt = 0;
77123a1cceaSRoger A. Faulkner iop->_flag = 0; /* marks it as available */
77223a1cceaSRoger A. Faulkner FUNLOCKFILE(lk);
77323a1cceaSRoger A. Faulkner fcloses++;
77423a1cceaSRoger A. Faulkner }
77523a1cceaSRoger A. Faulkner } while ((lp = lp->next) != NULL);
77623a1cceaSRoger A. Faulkner
77723a1cceaSRoger A. Faulkner if (__libc_threaded)
77823a1cceaSRoger A. Faulkner cancel_safe_mutex_unlock(&_first_link_lock);
77923a1cceaSRoger A. Faulkner
78023a1cceaSRoger A. Faulkner return (0);
78123a1cceaSRoger A. Faulkner }
78223a1cceaSRoger A. Faulkner
7837c478bd9Sstevel@tonic-gate /* flush buffer, close fd but keep the stream used by freopen() */
7847c478bd9Sstevel@tonic-gate int
close_fd(FILE * iop)7857c478bd9Sstevel@tonic-gate close_fd(FILE *iop)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate int res = 0;
7887c478bd9Sstevel@tonic-gate mbstate_t *mb;
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate if (iop == NULL || iop->_flag == 0)
7917c478bd9Sstevel@tonic-gate return (EOF);
7927c478bd9Sstevel@tonic-gate /* Is not unbuffered and opened for read and/or write ? */
7937c478bd9Sstevel@tonic-gate if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7947c478bd9Sstevel@tonic-gate res = _fflush_u(iop);
795cd62a92dSRobert Mustacchi if (_xclose(iop) < 0)
7967c478bd9Sstevel@tonic-gate res = EOF;
7977c478bd9Sstevel@tonic-gate if (iop->_flag & _IOMYBUF) {
7987c478bd9Sstevel@tonic-gate (void) free((char *)iop->_base - PUSHBACK);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate iop->_base = NULL;
8017c478bd9Sstevel@tonic-gate iop->_ptr = NULL;
8027c478bd9Sstevel@tonic-gate mb = _getmbstate(iop);
8037c478bd9Sstevel@tonic-gate if (mb != NULL)
8047c478bd9Sstevel@tonic-gate (void) memset(mb, 0, sizeof (mbstate_t));
8057c478bd9Sstevel@tonic-gate iop->_cnt = 0;
8067c478bd9Sstevel@tonic-gate _setorientation(iop, _NO_MODE);
8077c478bd9Sstevel@tonic-gate return (res);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate
8107c478bd9Sstevel@tonic-gate static FILE *
getiop(FILE * fp,rmutex_t * lk,mbstate_t * mb)8117c478bd9Sstevel@tonic-gate getiop(FILE *fp, rmutex_t *lk, mbstate_t *mb)
8127c478bd9Sstevel@tonic-gate {
813a574db85Sraf if (lk != NULL && cancel_safe_mutex_trylock(lk) != 0)
8147c478bd9Sstevel@tonic-gate return (NULL); /* locked: fp in use */
8157c478bd9Sstevel@tonic-gate
8167c478bd9Sstevel@tonic-gate if (fp->_flag == 0) { /* unused */
8177c478bd9Sstevel@tonic-gate #ifndef _LP64
8187c478bd9Sstevel@tonic-gate fp->__orientation = 0;
8197c478bd9Sstevel@tonic-gate #endif /* _LP64 */
8207c478bd9Sstevel@tonic-gate fp->_cnt = 0;
8217c478bd9Sstevel@tonic-gate fp->_ptr = NULL;
8227c478bd9Sstevel@tonic-gate fp->_base = NULL;
823cd62a92dSRobert Mustacchi /* claim the fp by setting low 8 bits */
824cd62a92dSRobert Mustacchi fp->_flag = _DEF_FLAG_MASK;
8257c478bd9Sstevel@tonic-gate (void) memset(mb, 0, sizeof (mbstate_t));
8267c478bd9Sstevel@tonic-gate FUNLOCKFILE(lk);
8277c478bd9Sstevel@tonic-gate return (fp);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate FUNLOCKFILE(lk);
8307c478bd9Sstevel@tonic-gate return (NULL);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate #ifndef _LP64
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate * DESCRIPTION:
8367c478bd9Sstevel@tonic-gate * This function gets the pointer to the mbstate_t structure associated
8377c478bd9Sstevel@tonic-gate * with the specified iop.
8387c478bd9Sstevel@tonic-gate *
8397c478bd9Sstevel@tonic-gate * RETURNS:
8407c478bd9Sstevel@tonic-gate * If the associated mbstate_t found, the pointer to the mbstate_t is
8417c478bd9Sstevel@tonic-gate * returned. Otherwise, NULL is returned.
8427c478bd9Sstevel@tonic-gate */
8437c478bd9Sstevel@tonic-gate mbstate_t *
_getmbstate(FILE * iop)8447c478bd9Sstevel@tonic-gate _getmbstate(FILE *iop)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop);
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate if (dat != NULL)
8497c478bd9Sstevel@tonic-gate return (&dat->_state);
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate return (NULL);
8527c478bd9Sstevel@tonic-gate }
853a5f69788Scraigm
854a5f69788Scraigm /*
855a5f69788Scraigm * More 32-bit only functions.
856a5f69788Scraigm * They lookup/set large fd's for extended FILE support.
857a5f69788Scraigm */
858a5f69788Scraigm
859a5f69788Scraigm /*
860a5f69788Scraigm * The negative value indicates that Extended fd FILE's has not
861a5f69788Scraigm * been enabled by the user.
862a5f69788Scraigm */
863a5f69788Scraigm static int bad_fd = -1;
864a5f69788Scraigm
865a5f69788Scraigm int
_file_get(FILE * iop)866a5f69788Scraigm _file_get(FILE *iop)
867a5f69788Scraigm {
868a5f69788Scraigm int altfd;
869a5f69788Scraigm
870a5f69788Scraigm /*
871a5f69788Scraigm * Failure indicates a FILE * not allocated through stdio;
872a5f69788Scraigm * it means the flag values are probably bogus and that if
873a5f69788Scraigm * a file descriptor is set, it's in _magic.
874a5f69788Scraigm * Inline getxfdat() for performance reasons.
875a5f69788Scraigm */
876a5f69788Scraigm if (STDIOP(iop))
877a5f69788Scraigm altfd = _xftab[IOPIND(iop)]._altfd;
878a5f69788Scraigm else if (VALIDXFILE(FILEx(iop)))
879a5f69788Scraigm altfd = FILEx(iop)->_xdat._altfd;
880a5f69788Scraigm else
881a5f69788Scraigm return (iop->_magic);
882a5f69788Scraigm /*
883a5f69788Scraigm * if this is not an internal extended FILE then check
884a5f69788Scraigm * if _file is being changed from underneath us.
885a5f69788Scraigm * It should not be because if
886a5f69788Scraigm * it is then then we lose our ability to guard against
887a5f69788Scraigm * silent data corruption.
888a5f69788Scraigm */
889a5f69788Scraigm if (!iop->__xf_nocheck && bad_fd > -1 && iop->_magic != bad_fd) {
890e1d65dbdScraigm (void) fprintf(stderr,
891a5f69788Scraigm "Application violated extended FILE safety mechanism.\n"
892e1d65dbdScraigm "Please read the man page for extendedFILE.\nAborting\n");
893a5f69788Scraigm abort();
894a5f69788Scraigm }
895a5f69788Scraigm return (altfd);
896a5f69788Scraigm }
897a5f69788Scraigm
898a5f69788Scraigm int
_file_set(FILE * iop,int fd,const char * type)899a5f69788Scraigm _file_set(FILE *iop, int fd, const char *type)
900a5f69788Scraigm {
901a5f69788Scraigm struct xFILEdata *dat;
902a5f69788Scraigm int Fflag;
903a5f69788Scraigm
904a5f69788Scraigm /* Already known to contain at least one byte */
905a5f69788Scraigm while (*++type != '\0')
906a5f69788Scraigm ;
907a5f69788Scraigm
908a5f69788Scraigm Fflag = type[-1] == 'F';
909a5f69788Scraigm if (!Fflag && bad_fd < 0) {
910a5f69788Scraigm errno = EMFILE;
911a5f69788Scraigm return (-1);
912a5f69788Scraigm }
913a5f69788Scraigm
914a5f69788Scraigm dat = getxfdat(iop);
915a5f69788Scraigm iop->__extendedfd = 1;
916a5f69788Scraigm iop->__xf_nocheck = Fflag;
917a5f69788Scraigm dat->_altfd = fd;
918a5f69788Scraigm iop->_magic = (unsigned char)bad_fd;
919a5f69788Scraigm return (0);
920a5f69788Scraigm }
921a5f69788Scraigm
922a5f69788Scraigm /*
923a5f69788Scraigm * Activates extended fd's in FILE's
924a5f69788Scraigm */
925a5f69788Scraigm
926a5f69788Scraigm static const int tries[] = {196, 120, 60, 3};
927a5f69788Scraigm #define NTRIES (sizeof (tries)/sizeof (int))
928a5f69788Scraigm
929a5f69788Scraigm int
enable_extended_FILE_stdio(int fd,int action)930a5f69788Scraigm enable_extended_FILE_stdio(int fd, int action)
931a5f69788Scraigm {
932a5f69788Scraigm int i;
933a5f69788Scraigm
934a5f69788Scraigm if (action < 0)
935a5f69788Scraigm action = SIGABRT; /* default signal */
936a5f69788Scraigm
937a5f69788Scraigm if (fd < 0) {
938a5f69788Scraigm /*
939a5f69788Scraigm * search for an available fd and make it the badfd
940a5f69788Scraigm */
941a5f69788Scraigm for (i = 0; i < NTRIES; i++) {
942a5f69788Scraigm fd = fcntl(tries[i], F_BADFD, action);
943a5f69788Scraigm if (fd >= 0)
944a5f69788Scraigm break;
945a5f69788Scraigm }
946a5f69788Scraigm if (fd < 0) /* failed to find an available fd */
947a5f69788Scraigm return (-1);
948a5f69788Scraigm } else {
949a5f69788Scraigm /* caller requests that fd be the chosen badfd */
950a5f69788Scraigm int nfd = fcntl(fd, F_BADFD, action);
951a5f69788Scraigm if (nfd < 0 || nfd != fd)
952a5f69788Scraigm return (-1);
953a5f69788Scraigm }
954a5f69788Scraigm bad_fd = fd;
955a5f69788Scraigm return (0);
956a5f69788Scraigm }
9577c478bd9Sstevel@tonic-gate #endif
958cd62a92dSRobert Mustacchi
959cd62a92dSRobert Mustacchi /*
960cd62a92dSRobert Mustacchi * Wrappers around the various system calls that stdio needs to make on a file
961cd62a92dSRobert Mustacchi * descriptor.
962cd62a92dSRobert Mustacchi */
963cd62a92dSRobert Mustacchi static stdio_ops_t *
get_stdops(FILE * iop)964cd62a92dSRobert Mustacchi get_stdops(FILE *iop)
965cd62a92dSRobert Mustacchi {
966cd62a92dSRobert Mustacchi #ifdef _LP64
967cd62a92dSRobert Mustacchi return (iop->_ops);
968cd62a92dSRobert Mustacchi #else
969cd62a92dSRobert Mustacchi struct xFILEdata *dat = getxfdat(iop);
970cd62a92dSRobert Mustacchi return (dat->_ops);
971cd62a92dSRobert Mustacchi #endif
972cd62a92dSRobert Mustacchi }
973cd62a92dSRobert Mustacchi
974cd62a92dSRobert Mustacchi static void
set_stdops(FILE * iop,stdio_ops_t * ops)975cd62a92dSRobert Mustacchi set_stdops(FILE *iop, stdio_ops_t *ops)
976cd62a92dSRobert Mustacchi {
977cd62a92dSRobert Mustacchi #ifdef _LP64
978cd62a92dSRobert Mustacchi ASSERT3P(iop->_ops, ==, NULL);
979cd62a92dSRobert Mustacchi iop->_ops = ops;
980cd62a92dSRobert Mustacchi #else
981cd62a92dSRobert Mustacchi struct xFILEdata *dat = getxfdat(iop);
982cd62a92dSRobert Mustacchi ASSERT3P(dat->_ops, ==, NULL);
983cd62a92dSRobert Mustacchi dat->_ops = ops;
984cd62a92dSRobert Mustacchi #endif
985cd62a92dSRobert Mustacchi
986cd62a92dSRobert Mustacchi }
987cd62a92dSRobert Mustacchi
988*18c77f76SBill Sommerfeld static void
clr_stdops(FILE * iop)989*18c77f76SBill Sommerfeld clr_stdops(FILE *iop)
990*18c77f76SBill Sommerfeld {
991*18c77f76SBill Sommerfeld #ifdef _LP64
992*18c77f76SBill Sommerfeld iop->_ops = NULL;
993*18c77f76SBill Sommerfeld #else
994*18c77f76SBill Sommerfeld struct xFILEdata *dat = getxfdat(iop);
995*18c77f76SBill Sommerfeld dat->_ops = NULL;
996*18c77f76SBill Sommerfeld #endif
997*18c77f76SBill Sommerfeld
998*18c77f76SBill Sommerfeld }
999*18c77f76SBill Sommerfeld
1000cd62a92dSRobert Mustacchi ssize_t
_xread(FILE * iop,void * buf,size_t nbytes)1001cd62a92dSRobert Mustacchi _xread(FILE *iop, void *buf, size_t nbytes)
1002cd62a92dSRobert Mustacchi {
1003cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1004cd62a92dSRobert Mustacchi if (ops != NULL) {
1005cd62a92dSRobert Mustacchi return (ops->std_read(iop, buf, nbytes));
1006cd62a92dSRobert Mustacchi }
1007cd62a92dSRobert Mustacchi
1008cd62a92dSRobert Mustacchi return (read(_get_fd(iop), buf, nbytes));
1009cd62a92dSRobert Mustacchi }
1010cd62a92dSRobert Mustacchi
1011cd62a92dSRobert Mustacchi ssize_t
_xwrite(FILE * iop,const void * buf,size_t nbytes)1012cd62a92dSRobert Mustacchi _xwrite(FILE *iop, const void *buf, size_t nbytes)
1013cd62a92dSRobert Mustacchi {
1014cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1015cd62a92dSRobert Mustacchi if (ops != NULL) {
1016cd62a92dSRobert Mustacchi return (ops->std_write(iop, buf, nbytes));
1017cd62a92dSRobert Mustacchi }
1018cd62a92dSRobert Mustacchi return (write(_get_fd(iop), buf, nbytes));
1019cd62a92dSRobert Mustacchi }
1020cd62a92dSRobert Mustacchi
1021cd62a92dSRobert Mustacchi off_t
_xseek(FILE * iop,off_t off,int whence)1022cd62a92dSRobert Mustacchi _xseek(FILE *iop, off_t off, int whence)
1023cd62a92dSRobert Mustacchi {
1024cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1025cd62a92dSRobert Mustacchi if (ops != NULL) {
1026cd62a92dSRobert Mustacchi return (ops->std_seek(iop, off, whence));
1027cd62a92dSRobert Mustacchi }
1028cd62a92dSRobert Mustacchi
1029cd62a92dSRobert Mustacchi return (lseek(_get_fd(iop), off, whence));
1030cd62a92dSRobert Mustacchi }
1031cd62a92dSRobert Mustacchi
1032cd62a92dSRobert Mustacchi off64_t
_xseek64(FILE * iop,off64_t off,int whence)1033cd62a92dSRobert Mustacchi _xseek64(FILE *iop, off64_t off, int whence)
1034cd62a92dSRobert Mustacchi {
1035cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1036cd62a92dSRobert Mustacchi if (ops != NULL) {
1037cd62a92dSRobert Mustacchi /*
1038cd62a92dSRobert Mustacchi * The internal APIs only operate with an off_t. An off64_t in
1039cd62a92dSRobert Mustacchi * an ILP32 environment may represent a value larger than they
1040cd62a92dSRobert Mustacchi * can accept. As such, we try and catch such cases and error
1041cd62a92dSRobert Mustacchi * about it before we get there.
1042cd62a92dSRobert Mustacchi */
1043cd62a92dSRobert Mustacchi if (off > LONG_MAX || off < LONG_MIN) {
1044cd62a92dSRobert Mustacchi errno = EOVERFLOW;
1045cd62a92dSRobert Mustacchi return (-1);
1046cd62a92dSRobert Mustacchi }
1047cd62a92dSRobert Mustacchi return (ops->std_seek(iop, off, whence));
1048cd62a92dSRobert Mustacchi }
1049cd62a92dSRobert Mustacchi
1050cd62a92dSRobert Mustacchi return (lseek64(_get_fd(iop), off, whence));
1051cd62a92dSRobert Mustacchi }
1052cd62a92dSRobert Mustacchi
1053cd62a92dSRobert Mustacchi int
_xclose(FILE * iop)1054cd62a92dSRobert Mustacchi _xclose(FILE *iop)
1055cd62a92dSRobert Mustacchi {
1056cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1057cd62a92dSRobert Mustacchi if (ops != NULL) {
1058cd62a92dSRobert Mustacchi return (ops->std_close(iop));
1059cd62a92dSRobert Mustacchi }
1060cd62a92dSRobert Mustacchi
1061cd62a92dSRobert Mustacchi return (close(_get_fd(iop)));
1062cd62a92dSRobert Mustacchi }
1063cd62a92dSRobert Mustacchi
1064cd62a92dSRobert Mustacchi void *
_xdata(FILE * iop)1065cd62a92dSRobert Mustacchi _xdata(FILE *iop)
1066cd62a92dSRobert Mustacchi {
1067cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1068cd62a92dSRobert Mustacchi if (ops != NULL) {
1069cd62a92dSRobert Mustacchi return (ops->std_data);
1070cd62a92dSRobert Mustacchi }
1071cd62a92dSRobert Mustacchi
1072cd62a92dSRobert Mustacchi return (NULL);
1073cd62a92dSRobert Mustacchi }
1074cd62a92dSRobert Mustacchi
1075cd62a92dSRobert Mustacchi int
_xassoc(FILE * iop,fread_t readf,fwrite_t writef,fseek_t seekf,fclose_t closef,void * data)1076cd62a92dSRobert Mustacchi _xassoc(FILE *iop, fread_t readf, fwrite_t writef, fseek_t seekf,
1077cd62a92dSRobert Mustacchi fclose_t closef, void *data)
1078cd62a92dSRobert Mustacchi {
1079cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1080cd62a92dSRobert Mustacchi
1081cd62a92dSRobert Mustacchi if (ops == NULL) {
1082cd62a92dSRobert Mustacchi ops = malloc(sizeof (*ops));
1083cd62a92dSRobert Mustacchi if (ops == NULL) {
1084cd62a92dSRobert Mustacchi return (-1);
1085cd62a92dSRobert Mustacchi }
1086cd62a92dSRobert Mustacchi set_stdops(iop, ops);
1087cd62a92dSRobert Mustacchi }
1088cd62a92dSRobert Mustacchi
1089cd62a92dSRobert Mustacchi ops->std_read = readf;
1090cd62a92dSRobert Mustacchi ops->std_write = writef;
1091cd62a92dSRobert Mustacchi ops->std_seek = seekf;
1092cd62a92dSRobert Mustacchi ops->std_close = closef;
1093cd62a92dSRobert Mustacchi ops->std_data = data;
1094cd62a92dSRobert Mustacchi
1095cd62a92dSRobert Mustacchi return (0);
1096cd62a92dSRobert Mustacchi }
1097cd62a92dSRobert Mustacchi
1098cd62a92dSRobert Mustacchi void
_xunassoc(FILE * iop)1099cd62a92dSRobert Mustacchi _xunassoc(FILE *iop)
1100cd62a92dSRobert Mustacchi {
1101cd62a92dSRobert Mustacchi stdio_ops_t *ops = get_stdops(iop);
1102cd62a92dSRobert Mustacchi if (ops == NULL) {
1103cd62a92dSRobert Mustacchi return;
1104cd62a92dSRobert Mustacchi }
1105*18c77f76SBill Sommerfeld clr_stdops(iop);
1106cd62a92dSRobert Mustacchi free(ops);
1107cd62a92dSRobert Mustacchi }
1108cd62a92dSRobert Mustacchi
1109cd62a92dSRobert Mustacchi int
_get_fd(FILE * iop)1110cd62a92dSRobert Mustacchi _get_fd(FILE *iop)
1111cd62a92dSRobert Mustacchi {
1112cd62a92dSRobert Mustacchi /*
1113cd62a92dSRobert Mustacchi * Streams with an ops vector (currently the memory stream family) do
1114cd62a92dSRobert Mustacchi * not have an underlying file descriptor that we can give back to the
1115cd62a92dSRobert Mustacchi * user. In such cases, return -1 to explicitly make sure that they'll
1116cd62a92dSRobert Mustacchi * get an ebadf from things.
1117cd62a92dSRobert Mustacchi */
1118cd62a92dSRobert Mustacchi if (get_stdops(iop) != NULL) {
1119cd62a92dSRobert Mustacchi return (-1);
1120cd62a92dSRobert Mustacchi }
1121cd62a92dSRobert Mustacchi #ifdef _LP64
1122cd62a92dSRobert Mustacchi return (iop->_file);
1123cd62a92dSRobert Mustacchi #else
1124cd62a92dSRobert Mustacchi if (iop->__extendedfd) {
1125cd62a92dSRobert Mustacchi return (_file_get(iop));
1126cd62a92dSRobert Mustacchi } else {
1127cd62a92dSRobert Mustacchi return (iop->_magic);
1128cd62a92dSRobert Mustacchi }
1129cd62a92dSRobert Mustacchi #endif
1130cd62a92dSRobert Mustacchi }
1131