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