OSDN Git Service

* config/vax/vax.h (target_flags, MASK_UNIX_ASM, MASK_VAXC_ALIGNMENT)
[pf3gnuchains/gcc-fork.git] / gcc / ada / socket.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                               S O C K E T                                *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *          Copyright (C) 2003-2004 Free Software Foundation, Inc.          *
10  *                                                                          *
11  * GNAT is free software;  you can  redistribute it  and/or modify it under *
12  * terms of the  GNU General Public License as published  by the Free Soft- *
13  * ware  Foundation;  either version 2,  or (at your option) any later ver- *
14  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
17  * for  more details.  You should have  received  a copy of the GNU General *
18  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
19  * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
20  * MA 02111-1307, USA.                                                      *
21  *                                                                          *
22  * As a  special  exception,  if you  link  this file  with other  files to *
23  * produce an executable,  this file does not by itself cause the resulting *
24  * executable to be covered by the GNU General Public License. This except- *
25  * ion does not  however invalidate  any other reasons  why the  executable *
26  * file might be covered by the  GNU Public License.                        *
27  *                                                                          *
28  * GNAT was originally developed  by the GNAT team at  New York University. *
29  * Extensive contributions were provided by Ada Core Technologies Inc.      *
30  *                                                                          *
31  ****************************************************************************/
32
33 /*  This file provides a portable binding to the sockets API                */
34
35 #include "gsocket.h"
36 #include "raise.h"
37
38 extern void __gnat_disable_sigpipe (int fd);
39 extern void __gnat_free_socket_set (fd_set *);
40 extern void __gnat_last_socket_in_set (fd_set *, int *);
41 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
42 extern void __gnat_insert_socket_in_set (fd_set *, int);
43 extern int __gnat_is_socket_in_set (fd_set *, int);
44 extern fd_set *__gnat_new_socket_set (fd_set *);
45 extern void __gnat_remove_socket_from_set (fd_set *, int);
46 \f
47 /* Disable the sending of SIGPIPE for writes on a broken stream */
48 void
49 __gnat_disable_sigpipe (int fd)
50 {
51 #ifdef SO_NOSIGPIPE
52   int val = 1;
53   (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
54 #endif
55 }
56
57 /* Free socket set. */
58
59 void
60 __gnat_free_socket_set (fd_set *set)
61 {
62   __gnat_free (set);
63 }
64
65 /* Find the largest socket in the socket set SET. This is needed for
66    `select'.  LAST is the maximum value for the largest socket. This hint is
67    used to avoid scanning very large socket sets.  On return, LAST is the
68    actual largest socket in the socket set. */
69
70 void
71 __gnat_last_socket_in_set (fd_set *set, int *last)
72 {
73   int s;
74   int l;
75   l = -1;
76
77 #ifdef WINNT
78   /* More efficient method for NT. */
79   for (s = 0; s < set->fd_count; s++)
80     if ((int) set->fd_array[s] > l)
81       l = set->fd_array[s];
82
83 #else
84
85   for (s = *last; s != -1; s--)
86     if (FD_ISSET (s, set))
87       {
88         l = s;
89         break;
90       }
91 #endif
92
93   *last = l;
94 }
95
96 /* Get last socket and remove it from the socket set SET.  LAST is the
97    maximum value of the largest socket.  This hint is used to avoid scanning
98    very large socket sets.  On return, LAST is set to the actual largest
99    socket in the socket set. */
100
101 void
102 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
103 {
104   *socket = *last;
105   FD_CLR (*socket, set);
106   __gnat_last_socket_in_set (set, last);
107 }
108
109 /* Insert SOCKET in the socket set SET. */
110
111 void
112 __gnat_insert_socket_in_set (fd_set *set, int socket)
113 {
114   FD_SET (socket, set);
115 }
116
117 /* Check whether a given SOCKET is in the socket set SET. */
118
119 int
120 __gnat_is_socket_in_set (fd_set *set, int socket)
121 {
122   return FD_ISSET (socket, set);
123 }
124
125 /* Allocate a new socket set and set it as empty.  */
126
127 fd_set *
128 __gnat_new_socket_set (fd_set *set)
129 {
130   fd_set *new;
131
132   new = (fd_set *) __gnat_malloc (sizeof (fd_set));
133
134   if (set)
135     memcpy (new, set, sizeof (fd_set));
136   else
137     FD_ZERO (new);
138
139   return new;
140 }
141
142 /* Remove SOCKET from the socket set SET. */
143
144 void
145 __gnat_remove_socket_from_set (fd_set *set, int socket)
146 {
147   FD_CLR (socket, set);
148 }