]> git.pond.sub.org Git - empserver/blob - src/lib/gen/io_mask.c
Import of Empire 4.2.12
[empserver] / src / lib / gen / io_mask.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  iom.c: implements the io mask routines
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include <stdlib.h> /* malloc */
35 #include <errno.h>
36 #include "misc.h"
37 #include "bit.h"
38 #include "empio.h"
39 #include "io_mask.h"
40
41 extern  int errno;
42
43 struct io_mask *
44 iom_create(int what)
45 {
46         struct  io_mask *imp;
47
48         imp = (struct io_mask *) malloc(sizeof(*imp));
49         if (what & IO_READ) {
50                 imp->readmask = bit_newfdmask();
51                 imp->user_readmask = bit_newfdmask();
52         } else {
53                 imp->readmask = 0;
54                 imp->user_readmask = 0;
55         }
56         if (what & IO_WRITE) {
57                 imp->writemask = bit_newfdmask();
58                 imp->user_writemask = bit_newfdmask();
59         } else {
60                 imp->writemask = 0;
61                 imp->user_writemask = 0;
62         }
63         imp->what = what;
64         imp->maxfd = 0;
65         return imp;
66 }
67
68 void
69 iom_getmask(struct io_mask *mask, int *nfdp, bit_fdmask *readp, bit_fdmask *writep)
70 {
71         if (mask->what & IO_READ)
72                 bit_copy(mask->readmask, mask->user_readmask);
73         if (mask->what & IO_WRITE)
74                 bit_copy(mask->writemask, mask->user_writemask);
75         *readp = mask->user_readmask;
76         *writep = mask->user_writemask;
77         *nfdp = mask->maxfd;
78 }
79
80 void
81 iom_set(struct io_mask *mask, int what, int fd)
82 {
83         if ((mask->what & what) == 0)
84                 return;
85         if (what & IO_READ)
86                 BIT_SETB(fd, mask->readmask);
87         if (what & IO_WRITE)
88                 BIT_SETB(fd, mask->writemask);
89         if (fd > mask->maxfd)
90                 mask->maxfd = fd;
91 }
92
93 void
94 iom_clear(struct io_mask *mask, int what, int fd)
95 {
96         if ((mask->what & what) == 0)
97                 return;
98         if (what & IO_READ)
99                 BIT_CLRB(fd, mask->readmask);
100         if (what & IO_WRITE)
101                 BIT_CLRB(fd, mask->writemask);
102 }
103
104 void
105 iom_zero(struct io_mask *mask, int what)
106 {
107         if ((mask->what & what) == 0)
108                 return;
109         if (what & IO_READ)
110                 bit_zero(mask->readmask);
111         if (what & IO_WRITE)
112                 bit_zero(mask->writemask);
113 }