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