Mercurial > hg > xemacs-beta
annotate lib-src/tcp.c @ 5602:c9e5612f5424
Support the MP library on recent FreeBSD, have it pass relevant tests.
src/ChangeLog addition:
2011-11-26 Aidan Kehoe <kehoea@parhasard.net>
* number-mp.c (bignum_to_string):
Don't overwrite the accumulator we've just set up for this
function.
* number-mp.c (BIGNUM_TO_TYPE):
mp_itom() doesn't necessarily do what this code used to think with
negative numbers, it can treat them as unsigned ints. Subtract
numbers from bignum_zero instead of multiplying them by -1 to
convert them to their negative equivalents.
* number-mp.c (bignum_to_int):
* number-mp.c (bignum_to_uint):
* number-mp.c (bignum_to_long):
* number-mp.c (bignum_to_ulong):
* number-mp.c (bignum_to_double):
Use the changed BIGNUM_TO_TYPE() in these functions.
* number-mp.c (bignum_ceil):
* number-mp.c (bignum_floor):
In these functions, be more careful about rounding to positive and
negative infinity, respectively. Don't use the sign of QUOTIENT
when working out out whether to add or subtract one, rather use
the sign QUOTIENT would have if arbitrary-precision division were
done.
* number-mp.h:
* number-mp.h (MP_GCD):
Wrap #include <mp.h> in BEGIN_C_DECLS/END_C_DECLS.
* number.c (Fbigfloat_get_precision):
* number.c (Fbigfloat_set_precision):
Don't attempt to call XBIGFLOAT_GET_PREC if this build doesn't
support big floats.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 26 Nov 2011 17:59:14 +0000 |
parents | b9167d522a9a |
children |
rev | line source |
---|---|
0 | 1 /* |
613 | 2 * TCP/IP stream emulation for XEmacs. |
0 | 3 * Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc. |
4 | |
5 * Author: Masanobu Umeda | |
6 * Maintainer: umerin@mse.kyutech.ac.jp | |
7 | |
613 | 8 This file is part of XEmacs. |
0 | 9 |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
10 XEmacs is free software: you can redistribute it and/or modify it |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
11 under the terms of the GNU General Public License as published by the |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
12 Free Software Foundation, either version 3 of the License, or (at your |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
13 option) any later version. |
0 | 14 |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
18 for more details. |
0 | 19 |
20 You should have received a copy of the GNU General Public License | |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
978
diff
changeset
|
21 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
0 | 22 |
23 * | |
24 * Yasunari, Itoh at PFU limited contributed for Fujitsu UTS and SX/A. | |
25 * | |
26 * Thu Apr 6 13:47:37 JST 1989 | |
27 * USG fixes by Sakaeda <saka@mickey.trad.pf.fujitsu.junet> | |
28 * | |
29 * For Fujitsu UTS compile with: | |
30 * cc -O -o tcp tcp.c -DFUJITSU_UTS -lu -lsocket | |
31 */ | |
32 | |
978 | 33 #ifdef HAVE_CONFIG_H |
34 #include <config.h> | |
35 #endif | |
0 | 36 #include <stdio.h> |
37 #include <fcntl.h> | |
38 #include <ctype.h> | |
39 #include <sys/types.h> | |
40 | |
41 #ifdef FUJITSU_UTS | |
42 #define USG | |
43 #include <sys/ucbtypes.h> | |
44 #include <sys/tisp/socket.h> | |
45 #include <netdb.h> | |
46 #include <sys/tisp/in.h> | |
47 #else | |
48 #include <sys/socket.h> | |
49 #include <netdb.h> | |
50 #include <netinet/in.h> | |
51 #endif | |
52 | |
53 #ifdef USG | |
54 #include <sys/stat.h> | |
978 | 55 #include "syssignal.h" |
0 | 56 #endif |
57 | |
58 #ifdef USG | |
59 int selectable = 1; | |
60 | |
61 sigout () | |
62 { | |
63 fcntl (fileno (stdin), F_SETFL, 0); | |
64 exit (-1); | |
65 } | |
66 #endif | |
67 | |
68 main (argc, argv) | |
69 int argc; | |
70 char *argv[]; | |
71 { | |
72 struct hostent *host; | |
73 struct sockaddr_in sockin, sockme; | |
74 struct servent *serv; | |
75 char *hostname = NULL; | |
76 char *service = "nntp"; | |
77 int port; | |
78 int readfds; | |
79 int writefds; | |
80 int server; /* NNTP Server */ | |
81 int emacsIn = fileno (stdin); /* Emacs intput */ | |
82 int emacsOut = fileno (stdout); /* Emacs output */ | |
83 char buffer[1024]; | |
84 int nbuffer; /* Number of bytes in buffer */ | |
85 int wret; | |
86 char *retry; /* retry bufferp */ | |
87 int false = 0; /* FALSE flag for setsockopt () */ | |
88 | |
89 if (argc < 2) | |
90 { | |
91 fprintf (stderr, "Usage: %s HOST [SERVICE]\n", argv[0]); | |
92 exit (1); | |
93 } | |
94 if (argc >= 2) | |
95 hostname = argv[1]; | |
96 if (argc >= 3) | |
97 service = argv[2]; | |
98 | |
99 if ((host = gethostbyname (hostname)) == NULL) | |
100 { | |
101 perror ("gethostbyname"); | |
102 exit (1); | |
103 } | |
104 if (isdigit (service[0])) | |
105 port = atoi (service); | |
106 else | |
107 { | |
108 serv = getservbyname (service, "tcp"); | |
109 if (serv == NULL) | |
110 { | |
111 perror ("getservbyname"); | |
112 exit (1); | |
113 } | |
114 port = serv->s_port; | |
115 } | |
116 | |
117 memset (&sockin, 0, sizeof (sockin)); | |
118 sockin.sin_family = host->h_addrtype; | |
119 memcpy (&sockin.sin_addr, host->h_addr, host->h_length); | |
120 sockin.sin_port = port; | |
121 if ((server = socket (AF_INET, SOCK_STREAM, 0)) < 0) | |
122 { | |
123 perror ("socket"); | |
124 exit (1); | |
125 } | |
126 if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR, &false, sizeof (false))) | |
127 { | |
128 perror ("setsockopt"); | |
129 exit (1); | |
130 } | |
131 memset (&sockme, 0, sizeof (sockme)); | |
132 sockme.sin_family = sockin.sin_family; | |
133 sockme.sin_addr.s_addr = INADDR_ANY; | |
134 if (bind (server, &sockme, sizeof (sockme)) < 0) | |
135 { | |
136 perror ("bind"); | |
137 exit (1); | |
138 } | |
139 if (connect (server, &sockin, sizeof (sockin)) < 0) | |
140 { | |
141 perror ("connect"); | |
142 close (server); | |
143 exit (1); | |
144 } | |
145 | |
146 #ifdef O_NDELAY | |
147 fcntl (server, F_SETFL, O_NDELAY); | |
148 | |
149 #ifdef USG | |
150 /* USG pipe cannot not select emacsIn */ | |
151 { | |
152 struct stat statbuf; | |
153 fstat (emacsIn, &statbuf); | |
154 if (statbuf.st_mode & 010000) | |
155 selectable = 0; | |
156 if (!selectable) | |
157 { | |
158 signal (SIGINT, sigout); | |
159 fcntl (emacsIn, F_SETFL, O_NDELAY); | |
160 } | |
161 } | |
162 #endif | |
163 #endif | |
164 | |
165 /* Connection established. */ | |
166 while (1) | |
167 { | |
168 readfds = (1 << server) | (1 << emacsIn); | |
169 if (select (32, &readfds, NULL, NULL, (struct timeval *)NULL) == -1) | |
170 { | |
171 perror ("select"); | |
172 exit (1); | |
173 } | |
174 if (readfds & (1 << emacsIn)) | |
175 { | |
176 /* From Emacs */ | |
177 nbuffer = read (emacsIn, buffer, sizeof buffer -1); | |
178 | |
179 #ifdef USG | |
180 if (selectable && nbuffer == 0) | |
181 { | |
182 goto finish; | |
183 } | |
184 else if (!(readfds & (1 << server)) && nbuffer == 0) | |
185 { | |
186 sleep (1); | |
187 } | |
188 else | |
189 #else | |
190 if (nbuffer == 0) | |
191 goto finish; | |
192 #endif | |
193 for (retry = buffer; nbuffer > 0; nbuffer -= wret, retry += wret) | |
194 { | |
195 writefds = 1 << server; | |
196 if (select (server+1, NULL, &writefds, NULL, (struct timeval*)NULL) == -1) | |
197 { | |
198 perror ("select"); | |
199 exit (1); | |
200 } | |
201 wret = write (server, retry, nbuffer); | |
202 if (wret < 0) goto finish; | |
203 } | |
204 } | |
205 if (readfds & (1 << server)) | |
206 { | |
207 /* From NNTP server */ | |
208 nbuffer = read (server, buffer, sizeof buffer -1); | |
209 if (nbuffer == 0) | |
210 goto finish; | |
211 for (retry = buffer; nbuffer > 0; nbuffer -= wret, retry += wret) | |
212 { | |
213 writefds = 1 << emacsOut; | |
214 #ifdef USG | |
215 if (selectable) | |
216 #endif | |
217 if (select (emacsOut+1, NULL, &writefds, NULL, (struct timeval*)NULL) == -1) | |
218 { | |
219 perror ("select"); | |
220 exit (1); | |
221 } | |
222 wret = write (emacsOut, retry, nbuffer); | |
223 if (wret < 0) goto finish; | |
224 } | |
225 } | |
226 } | |
227 | |
228 /* End of communication. */ | |
229 finish: | |
230 close (server); | |
231 #ifdef USG | |
232 if (!selectable) fcntl (emacsIn, F_SETFL, 0); | |
233 #endif | |
234 close (emacsIn); | |
235 close (emacsOut); | |
236 exit (0); | |
237 } |