1 | |
|
|
2 | /* |
1 | /* |
3 | * static char *rcsid_porting_c = |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
4 | * "$Id: porting.C,v 1.5 2006/09/10 16:00:23 root Exp $"; |
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
|
|
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
|
|
7 | * |
|
|
8 | * Deliantra is free software: you can redistribute it and/or modify |
|
|
9 | * it under the terms of the GNU General Public License as published by |
|
|
10 | * the Free Software Foundation, either version 3 of the License, or |
|
|
11 | * (at your option) any later version. |
|
|
12 | * |
|
|
13 | * This program is distributed in the hope that it will be useful, |
|
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
16 | * GNU General Public License for more details. |
|
|
17 | * |
|
|
18 | * You should have received a copy of the GNU General Public License |
|
|
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
5 | */ |
22 | */ |
6 | |
|
|
7 | /* |
|
|
8 | CrossFire, A Multiplayer game for X-windows |
|
|
9 | |
|
|
10 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
|
|
11 | Copyright (C) 1992 Frank Tore Johansen |
|
|
12 | |
|
|
13 | This program is free software; you can redistribute it and/or modify |
|
|
14 | it under the terms of the GNU General Public License as published by |
|
|
15 | the Free Software Foundation; either version 2 of the License, or |
|
|
16 | (at your option) any later version. |
|
|
17 | |
|
|
18 | This program is distributed in the hope that it will be useful, |
|
|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
21 | GNU General Public License for more details. |
|
|
22 | |
|
|
23 | You should have received a copy of the GNU General Public License |
|
|
24 | along with this program; if not, write to the Free Software |
|
|
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
26 | |
|
|
27 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
|
|
28 | */ |
|
|
29 | |
23 | |
30 | /* This file contains various functions that are not really unique for |
24 | /* This file contains various functions that are not really unique for |
31 | * crossfire, but rather provides what should be standard functions |
25 | * crossfire, but rather provides what should be standard functions |
32 | * for systems that do not have them. In this way, most of the |
26 | * for systems that do not have them. In this way, most of the |
33 | * nasty system dependent stuff is contained here, with the program |
27 | * nasty system dependent stuff is contained here, with the program |
34 | * calling these functions. |
28 | * calling these functions. |
35 | */ |
29 | */ |
36 | |
30 | |
37 | |
31 | |
38 | #ifdef WIN32 /* ---win32 exclude/include headers */ |
|
|
39 | # include "process.h" |
|
|
40 | # define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
|
|
41 | #else |
|
|
42 | # include <ctype.h> |
|
|
43 | # include <sys/stat.h> |
|
|
44 | # include <sys/wait.h> |
|
|
45 | |
|
|
46 | # include <sys/param.h> |
|
|
47 | # include <stdio.h> |
|
|
48 | |
|
|
49 | /* Need to pull in the HAVE_... values somehow */ |
32 | /* Need to pull in the HAVE_... values somehow */ |
50 | |
33 | |
51 | /* win32 reminder: always put this in a ifndef win32 block */ |
|
|
52 | # include <autoconf.h> |
34 | #include <autoconf.h> |
53 | #endif |
|
|
54 | |
35 | |
|
|
36 | #include <cstdio> |
|
|
37 | #include <cstdlib> |
|
|
38 | #include <cstdarg> |
55 | |
39 | |
56 | #ifdef HAVE_STDLIB_H |
40 | #include <cctype> |
57 | # include <stdlib.h> |
|
|
58 | #endif |
|
|
59 | |
41 | |
60 | #ifdef HAVE_UNISTD_H |
42 | #include <sys/stat.h> |
61 | # include <unistd.h> |
43 | #include <sys/wait.h> |
62 | #endif |
|
|
63 | |
44 | |
|
|
45 | #include <sys/param.h> |
|
|
46 | |
64 | #include <stdarg.h> |
47 | #include <unistd.h> |
65 | |
48 | |
66 | /* Has to be after above includes so we don't redefine some values */ |
49 | /* Has to be after above includes so we don't redefine some values */ |
67 | #include "global.h" |
50 | #include "global.h" |
68 | |
|
|
69 | static unsigned int curtmp = 0; |
|
|
70 | |
|
|
71 | /***************************************************************************** |
|
|
72 | * File related functions |
|
|
73 | ****************************************************************************/ |
|
|
74 | |
|
|
75 | /* |
|
|
76 | * A replacement for the tempnam() function since it's not defined |
|
|
77 | * at some unix variants. |
|
|
78 | */ |
|
|
79 | |
|
|
80 | char * |
|
|
81 | tempnam_local (const char *dir, const char *pfx) |
|
|
82 | { |
|
|
83 | char *name; |
|
|
84 | pid_t pid = getpid (); |
|
|
85 | |
|
|
86 | /* HURD does not have a hard limit, but we do */ |
|
|
87 | #ifndef MAXPATHLEN |
|
|
88 | # define MAXPATHLEN 4096 |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | if (!(name = (char *) malloc (MAXPATHLEN))) |
|
|
92 | return (NULL); |
|
|
93 | |
|
|
94 | if (!pfx) |
|
|
95 | pfx = "cftmp."; |
|
|
96 | |
|
|
97 | /* This is a pretty simple method - put the pid as a hex digit and |
|
|
98 | * just keep incrementing the last digit. Check to see if the file |
|
|
99 | * already exists - if so, we'll just keep looking - eventually we should |
|
|
100 | * find one that is free. |
|
|
101 | */ |
|
|
102 | if (dir != NULL) |
|
|
103 | { |
|
|
104 | do |
|
|
105 | { |
|
|
106 | #ifdef HAVE_SNPRINTF |
|
|
107 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
|
|
108 | #else |
|
|
109 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
|
|
110 | #endif |
|
|
111 | curtmp++; |
|
|
112 | } |
|
|
113 | while (access (name, F_OK) != -1); |
|
|
114 | return (name); |
|
|
115 | } |
|
|
116 | return (NULL); |
|
|
117 | } |
|
|
118 | |
|
|
119 | |
|
|
120 | |
51 | |
121 | /* This function removes everything in the directory. */ |
52 | /* This function removes everything in the directory. */ |
122 | void |
53 | void |
123 | remove_directory (const char *path) |
54 | remove_directory (const char *path) |
124 | { |
55 | { |
… | |
… | |
162 | { |
93 | { |
163 | LOG (llevError, "Unable to remove directory %s\n", path); |
94 | LOG (llevError, "Unable to remove directory %s\n", path); |
164 | } |
95 | } |
165 | } |
96 | } |
166 | |
97 | |
167 | #if defined(sgi) |
|
|
168 | |
|
|
169 | # include <stdio.h> |
|
|
170 | # include <stdlib.h> |
|
|
171 | # include <string.h> |
|
|
172 | |
|
|
173 | # define popen fixed_popen |
|
|
174 | |
|
|
175 | FILE * |
|
|
176 | popen_local (const char *command, const char *type) |
|
|
177 | { |
|
|
178 | int fd[2]; |
|
|
179 | int pd; |
|
|
180 | FILE *ret; |
|
|
181 | |
|
|
182 | if (!strcmp (type, "r")) |
|
|
183 | { |
|
|
184 | pd = STDOUT_FILENO; |
|
|
185 | } |
|
|
186 | else if (!strcmp (type, "w")) |
|
|
187 | { |
|
|
188 | pd = STDIN_FILENO; |
|
|
189 | } |
|
|
190 | else |
|
|
191 | { |
|
|
192 | return NULL; |
|
|
193 | } |
|
|
194 | if (pipe (fd) != -1) |
|
|
195 | { |
|
|
196 | switch (fork ()) |
|
|
197 | { |
|
|
198 | case -1: |
|
|
199 | close (fd[0]); |
|
|
200 | close (fd[1]); |
|
|
201 | break; |
|
|
202 | case 0: |
|
|
203 | close (fd[0]); |
|
|
204 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
|
|
205 | { |
|
|
206 | if (fd[1] != pd) |
|
|
207 | { |
|
|
208 | close (fd[1]); |
|
|
209 | } |
|
|
210 | execl ("/bin/sh", "sh", "-c", command, NULL); |
|
|
211 | close (pd); |
|
|
212 | } |
|
|
213 | exit (1); |
|
|
214 | break; |
|
|
215 | default: |
|
|
216 | close (fd[1]); |
|
|
217 | if (ret = fdopen (fd[0], type)) |
|
|
218 | { |
|
|
219 | return ret; |
|
|
220 | } |
|
|
221 | close (fd[0]); |
|
|
222 | break; |
|
|
223 | } |
|
|
224 | } |
|
|
225 | return NULL; |
|
|
226 | } |
|
|
227 | |
|
|
228 | #endif /* defined(sgi) */ |
|
|
229 | |
|
|
230 | |
|
|
231 | /***************************************************************************** |
|
|
232 | * String related function |
|
|
233 | ****************************************************************************/ |
|
|
234 | |
|
|
235 | |
|
|
236 | |
|
|
237 | /* |
|
|
238 | * A replacement of strdup(), since it's not defined at some |
|
|
239 | * unix variants. |
|
|
240 | */ |
|
|
241 | char * |
|
|
242 | strdup_local (const char *str) |
|
|
243 | { |
|
|
244 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
|
|
245 | |
|
|
246 | strcpy (c, str); |
|
|
247 | return c; |
|
|
248 | } |
|
|
249 | |
|
|
250 | |
|
|
251 | #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
98 | #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
252 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
99 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
253 | #define MBASE ('z' - 'a' + 1 + 10) |
100 | #define MBASE ('z' - 'a' + 1 + 10) |
254 | |
|
|
255 | /* This seems to be lacking on some system */ |
|
|
256 | #if !defined(HAVE_STRNCASECMP) |
|
|
257 | int |
|
|
258 | strncasecmp (const char *s1, const char *s2, int n) |
|
|
259 | { |
|
|
260 | register int c1, c2; |
|
|
261 | |
|
|
262 | while (*s1 && *s2 && n) |
|
|
263 | { |
|
|
264 | c1 = tolower (*s1); |
|
|
265 | c2 = tolower (*s2); |
|
|
266 | if (c1 != c2) |
|
|
267 | return (c1 - c2); |
|
|
268 | s1++; |
|
|
269 | s2++; |
|
|
270 | n--; |
|
|
271 | } |
|
|
272 | if (!n) |
|
|
273 | return (0); |
|
|
274 | return (int) (*s1 - *s2); |
|
|
275 | } |
|
|
276 | #endif |
|
|
277 | |
|
|
278 | #if !defined(HAVE_STRCASECMP) |
|
|
279 | int |
|
|
280 | strcasecmp (const char *s1, const char *s2) |
|
|
281 | { |
|
|
282 | register int c1, c2; |
|
|
283 | |
|
|
284 | while (*s1 && *s2) |
|
|
285 | { |
|
|
286 | c1 = tolower (*s1); |
|
|
287 | c2 = tolower (*s2); |
|
|
288 | if (c1 != c2) |
|
|
289 | return (c1 - c2); |
|
|
290 | s1++; |
|
|
291 | s2++; |
|
|
292 | } |
|
|
293 | if (*s1 == '\0' && *s2 == '\0') |
|
|
294 | return 0; |
|
|
295 | return (int) (*s1 - *s2); |
|
|
296 | } |
|
|
297 | #endif |
|
|
298 | |
101 | |
299 | char * |
102 | char * |
300 | strcasestr_local (const char *s, const char *find) |
103 | strcasestr_local (const char *s, const char *find) |
301 | { |
104 | { |
302 | char c, sc; |
105 | char c, sc; |
… | |
… | |
319 | s--; |
122 | s--; |
320 | } |
123 | } |
321 | return (char *) s; |
124 | return (char *) s; |
322 | } |
125 | } |
323 | |
126 | |
324 | #if !defined(HAVE_SNPRINTF) |
|
|
325 | |
|
|
326 | int |
|
|
327 | snprintf (char *dest, int max, const char *format, ...) |
|
|
328 | { |
|
|
329 | va_list var; |
|
|
330 | int ret; |
|
|
331 | |
|
|
332 | va_start (var, format); |
|
|
333 | ret = vsprintf (dest, format, var); |
|
|
334 | va_end (var); |
|
|
335 | if (ret > max) |
|
|
336 | abort (); |
|
|
337 | |
|
|
338 | return ret; |
|
|
339 | } |
|
|
340 | #endif |
|
|
341 | |
|
|
342 | |
|
|
343 | /* |
|
|
344 | * Based on (n+1)^2 = n^2 + 2n + 1 |
|
|
345 | * given that 1^2 = 1, then |
|
|
346 | * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 |
|
|
347 | * 3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9 |
|
|
348 | * 4^2 = 9 + (6 + 1) = 9 + 7 = 1 + 3 + 5 + 7 = 16 |
|
|
349 | * ... |
|
|
350 | * In other words, a square number can be express as the sum of the |
|
|
351 | * series n^2 = 1 + 3 + ... + (2n-1) |
|
|
352 | */ |
|
|
353 | int |
|
|
354 | isqrt (int n) |
|
|
355 | { |
|
|
356 | int result, sum, prev; |
|
|
357 | |
|
|
358 | result = 0; |
|
|
359 | prev = sum = 1; |
|
|
360 | while (sum <= n) |
|
|
361 | { |
|
|
362 | prev += 2; |
|
|
363 | sum += prev; |
|
|
364 | ++result; |
|
|
365 | } |
|
|
366 | return result; |
|
|
367 | } |
|
|
368 | |
|
|
369 | |
|
|
370 | /* |
127 | /* |
371 | * returns a char-pointer to a static array, in which a representation |
128 | * returns a char-pointer to a static array, in which a representation |
372 | * of the decimal number given will be stored. |
129 | * of the decimal number given will be stored. |
373 | */ |
130 | */ |
374 | |
|
|
375 | char * |
131 | char * |
376 | ltostr10 (signed long n) |
132 | ltostr10 (signed long n) |
377 | { |
133 | { |
378 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
134 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
379 | character for the trailing nul character */ |
135 | character for the trailing nul character */ |
… | |
… | |
395 | * then it opens it and returns the file-pointer. |
151 | * then it opens it and returns the file-pointer. |
396 | */ |
152 | */ |
397 | FILE * |
153 | FILE * |
398 | open_and_uncompress (const char *name, int flag, int *compressed) |
154 | open_and_uncompress (const char *name, int flag, int *compressed) |
399 | { |
155 | { |
400 | size_t i; |
|
|
401 | FILE *fp; |
|
|
402 | |
|
|
403 | *compressed = 0; |
156 | *compressed = 0; |
404 | return fopen (name, "r"); |
157 | return fopen (name, "r"); |
405 | } |
158 | } |
406 | |
159 | |
407 | /* |
160 | /* |
… | |
… | |
424 | char buf[MAX_BUF], *cp = buf; |
177 | char buf[MAX_BUF], *cp = buf; |
425 | struct stat statbuf; |
178 | struct stat statbuf; |
426 | |
179 | |
427 | if (!filename || !*filename) |
180 | if (!filename || !*filename) |
428 | return; |
181 | return; |
|
|
182 | |
429 | strcpy (buf, filename); |
183 | assign (buf, filename); |
430 | |
184 | |
431 | while ((cp = strchr (cp + 1, (int) '/'))) |
185 | while ((cp = strchr (cp + 1, (int) '/'))) |
432 | { |
186 | { |
433 | *cp = '\0'; |
187 | *cp = '\0'; |
434 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
188 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |