1*c2c66affSColin Finck /* @(#)flag.c	2.14 10/11/06 Copyright 1986-2010 J. Schilling */
2*c2c66affSColin Finck /*
3*c2c66affSColin Finck  *	Copyright (c) 1986-2010 J. Schilling
4*c2c66affSColin Finck  */
5*c2c66affSColin Finck /*
6*c2c66affSColin Finck  * The contents of this file are subject to the terms of the
7*c2c66affSColin Finck  * Common Development and Distribution License, Version 1.0 only
8*c2c66affSColin Finck  * (the "License").  You may not use this file except in compliance
9*c2c66affSColin Finck  * with the License.
10*c2c66affSColin Finck  *
11*c2c66affSColin Finck  * See the file CDDL.Schily.txt in this distribution for details.
12*c2c66affSColin Finck  *
13*c2c66affSColin Finck  * When distributing Covered Code, include this CDDL HEADER in each
14*c2c66affSColin Finck  * file and include the License file CDDL.Schily.txt from this distribution.
15*c2c66affSColin Finck  */
16*c2c66affSColin Finck 
17*c2c66affSColin Finck #include "schilyio.h"
18*c2c66affSColin Finck #include <schily/stdlib.h>
19*c2c66affSColin Finck 
20*c2c66affSColin Finck #ifdef	DO_MYFLAG
21*c2c66affSColin Finck 
22*c2c66affSColin Finck #define	FL_INIT	10
23*c2c66affSColin Finck 
24*c2c66affSColin Finck #if	defined(IS_MACOS_X)
25*c2c66affSColin Finck /*
26*c2c66affSColin Finck  * The MAC OS X linker does not grok "common" varaibles.
27*c2c66affSColin Finck  * Make _io_glflag a "data" variable.
28*c2c66affSColin Finck  */
29*c2c66affSColin Finck EXPORT	int	_io_glflag = 0;		/* global default flag */
30*c2c66affSColin Finck #else
31*c2c66affSColin Finck EXPORT	int	_io_glflag;		/* global default flag */
32*c2c66affSColin Finck #endif
33*c2c66affSColin Finck LOCAL	int	_fl_inc = 10;		/* increment to expand flag struct */
34*c2c66affSColin Finck EXPORT	int	_fl_max = FL_INIT;	/* max fd currently in _io_myfl */
35*c2c66affSColin Finck LOCAL	_io_fl	_io_smyfl[FL_INIT];	/* initial static space */
36*c2c66affSColin Finck EXPORT	_io_fl	*_io_myfl = _io_smyfl;	/* init to static space */
37*c2c66affSColin Finck 
38*c2c66affSColin Finck LOCAL int _more_flags	__PR((FILE *));
39*c2c66affSColin Finck 
40*c2c66affSColin Finck LOCAL int
_more_flags(fp)41*c2c66affSColin Finck _more_flags(fp)
42*c2c66affSColin Finck 	FILE	*fp;
43*c2c66affSColin Finck {
44*c2c66affSColin Finck 	register int	f = fileno(fp);
45*c2c66affSColin Finck 	register int	n = _fl_max;
46*c2c66affSColin Finck 	register _io_fl	*np;
47*c2c66affSColin Finck 
48*c2c66affSColin Finck 	while (n <= f)
49*c2c66affSColin Finck 		n += _fl_inc;
50*c2c66affSColin Finck 
51*c2c66affSColin Finck 	if (_io_myfl == _io_smyfl) {
52*c2c66affSColin Finck 		np = (_io_fl *) malloc(n * sizeof (*np));
53*c2c66affSColin Finck 		fillbytes(np, n * sizeof (*np), '\0');
54*c2c66affSColin Finck 		movebytes(_io_smyfl, np, sizeof (_io_smyfl)/sizeof (*np));
55*c2c66affSColin Finck 	} else {
56*c2c66affSColin Finck 		np = (_io_fl *) realloc(_io_myfl, n * sizeof (*np));
57*c2c66affSColin Finck 		if (np)
58*c2c66affSColin Finck 			fillbytes(&np[_fl_max], (n-_fl_max)*sizeof (*np), '\0');
59*c2c66affSColin Finck 	}
60*c2c66affSColin Finck 	if (np) {
61*c2c66affSColin Finck 		_io_myfl = np;
62*c2c66affSColin Finck 		_fl_max = n;
63*c2c66affSColin Finck 		return (_io_get_my_flag(fp));
64*c2c66affSColin Finck 	} else {
65*c2c66affSColin Finck 		return (_JS_IONORAISE);
66*c2c66affSColin Finck 	}
67*c2c66affSColin Finck }
68*c2c66affSColin Finck 
69*c2c66affSColin Finck EXPORT int
_io_get_my_flag(fp)70*c2c66affSColin Finck _io_get_my_flag(fp)
71*c2c66affSColin Finck 	register FILE	*fp;
72*c2c66affSColin Finck {
73*c2c66affSColin Finck 	register int	f = fileno(fp);
74*c2c66affSColin Finck 	register _io_fl	*fl;
75*c2c66affSColin Finck 
76*c2c66affSColin Finck 	if (f >= _fl_max)
77*c2c66affSColin Finck 		return (_more_flags(fp));
78*c2c66affSColin Finck 
79*c2c66affSColin Finck 	fl = &_io_myfl[f];
80*c2c66affSColin Finck 
81*c2c66affSColin Finck 	if (fl->fl_io == 0 || fl->fl_io == fp)
82*c2c66affSColin Finck 		return (fl->fl_flags);
83*c2c66affSColin Finck 
84*c2c66affSColin Finck 	while (fl && fl->fl_io != fp)
85*c2c66affSColin Finck 		fl = fl->fl_next;
86*c2c66affSColin Finck 
87*c2c66affSColin Finck 	if (fl == 0)
88*c2c66affSColin Finck 		return (0);
89*c2c66affSColin Finck 
90*c2c66affSColin Finck 	return (fl->fl_flags);
91*c2c66affSColin Finck }
92*c2c66affSColin Finck 
93*c2c66affSColin Finck EXPORT void
_io_set_my_flag(fp,flag)94*c2c66affSColin Finck _io_set_my_flag(fp, flag)
95*c2c66affSColin Finck 	FILE	*fp;
96*c2c66affSColin Finck 	int	flag;
97*c2c66affSColin Finck {
98*c2c66affSColin Finck 	register int	f = fileno(fp);
99*c2c66affSColin Finck 	register _io_fl	*fl;
100*c2c66affSColin Finck 	register _io_fl	*fl2;
101*c2c66affSColin Finck 
102*c2c66affSColin Finck 	if (f >= _fl_max)
103*c2c66affSColin Finck 		(void) _more_flags(fp);
104*c2c66affSColin Finck 
105*c2c66affSColin Finck 	fl = &_io_myfl[f];
106*c2c66affSColin Finck 
107*c2c66affSColin Finck 	if (fl->fl_io != (FILE *)0) {
108*c2c66affSColin Finck 		fl2 = fl;
109*c2c66affSColin Finck 
110*c2c66affSColin Finck 		while (fl && fl->fl_io != fp)
111*c2c66affSColin Finck 			fl = fl->fl_next;
112*c2c66affSColin Finck 		if (fl == 0) {
113*c2c66affSColin Finck 			if ((fl = (_io_fl *) malloc(sizeof (*fl))) == 0)
114*c2c66affSColin Finck 				return;
115*c2c66affSColin Finck 			fl->fl_next = fl2->fl_next;
116*c2c66affSColin Finck 			fl2->fl_next = fl;
117*c2c66affSColin Finck 		}
118*c2c66affSColin Finck 	}
119*c2c66affSColin Finck 	fl->fl_io = fp;
120*c2c66affSColin Finck 	fl->fl_flags = flag;
121*c2c66affSColin Finck }
122*c2c66affSColin Finck 
123*c2c66affSColin Finck EXPORT void
_io_add_my_flag(fp,flag)124*c2c66affSColin Finck _io_add_my_flag(fp, flag)
125*c2c66affSColin Finck 	FILE	*fp;
126*c2c66affSColin Finck 	int	flag;
127*c2c66affSColin Finck {
128*c2c66affSColin Finck 	int	oflag = _io_get_my_flag(fp);
129*c2c66affSColin Finck 
130*c2c66affSColin Finck 	oflag |= flag;
131*c2c66affSColin Finck 
132*c2c66affSColin Finck 	_io_set_my_flag(fp, oflag);
133*c2c66affSColin Finck }
134*c2c66affSColin Finck 
135*c2c66affSColin Finck #endif	/* DO_MYFLAG */
136