Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / librols / stdio / flag.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)flag.c   2.10 05/06/12 Copyright 1986-2003 J. Schilling */
14 /*
15  *      Copyright (c) 1986-2003 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #include "schilyio.h"
33 #include <stdxlib.h>
34
35 #ifdef  DO_MYFLAG
36
37 #define FL_INIT 10
38
39 EXPORT  int     _io_glflag;             /* global default flag */
40 LOCAL   int     _fl_inc = 10;           /* increment for expanding flag struct */
41 EXPORT  int     _fl_max = FL_INIT;      /* max fd currently in _io_myfl */
42 LOCAL   _io_fl  _io_smyfl[FL_INIT];     /* initial static space */
43 EXPORT  _io_fl  *_io_myfl = _io_smyfl;  /* init to static space */
44
45 LOCAL int _more_flags   __PR((FILE *));
46
47 LOCAL int
48 _more_flags(fp)
49         FILE    *fp;
50 {
51         register int    f = fileno(fp);
52         register int    n = _fl_max;
53         register _io_fl *np;
54
55         while (n <= f)
56                 n += _fl_inc;
57
58         if (_io_myfl == _io_smyfl) {
59                 np = (_io_fl *) malloc(n * sizeof (*np));
60                 fillbytes(np, n * sizeof (*np), '\0');
61                 movebytes(_io_smyfl, np, sizeof (_io_smyfl)/sizeof (*np));
62         } else {
63                 np = (_io_fl *) realloc(_io_myfl, n * sizeof (*np));
64                 if (np)
65                         fillbytes(&np[_fl_max], (n-_fl_max)*sizeof (*np), '\0');
66         }
67         if (np) {
68                 _io_myfl = np;
69                 _fl_max = n;
70                 return (_io_get_my_flag(fp));
71         } else {
72                 return (_IONORAISE);
73         }
74 }
75
76 EXPORT int
77 _io_get_my_flag(fp)
78         register FILE   *fp;
79 {
80         register int    f = fileno(fp);
81         register _io_fl *fl;
82
83         if (f >= _fl_max)
84                 return (_more_flags(fp));
85
86         fl = &_io_myfl[f];
87
88         if (fl->fl_io == 0 || fl->fl_io == fp)
89                 return (fl->fl_flags);
90
91         while (fl && fl->fl_io != fp)
92                 fl = fl->fl_next;
93
94         if (fl == 0)
95                 return (0);
96
97         return (fl->fl_flags);
98 }
99
100 EXPORT void
101 _io_set_my_flag(fp, flag)
102         FILE    *fp;
103         int     flag;
104 {
105         register int    f = fileno(fp);
106         register _io_fl *fl;
107         register _io_fl *fl2;
108
109         if (f >= _fl_max)
110                 (void) _more_flags(fp);
111
112         fl = &_io_myfl[f];
113
114         if (fl->fl_io != (FILE *)0) {
115                 fl2 = fl;
116
117                 while (fl && fl->fl_io != fp)
118                         fl = fl->fl_next;
119                 if (fl == 0) {
120                         if ((fl = (_io_fl *) malloc(sizeof (*fl))) == 0)
121                                 return;
122                         fl->fl_next = fl2->fl_next;
123                         fl2->fl_next = fl;
124                 }
125         }
126         fl->fl_io = fp;
127         fl->fl_flags = flag;
128 }
129
130 EXPORT void
131 _io_add_my_flag(fp, flag)
132         FILE    *fp;
133         int     flag;
134 {
135         int     oflag = _io_get_my_flag(fp);
136
137         oflag |= flag;
138
139         _io_set_my_flag(fp, oflag);
140 }
141
142 #endif  /* DO_MYFLAG */