1 | /* |
|
|
2 | * static char *rcsid_porting_c = |
|
|
3 | * "$Id: porting.C,v 1.4 2006/09/07 09:28:44 root Exp $"; |
|
|
4 | */ |
|
|
5 | |
|
|
6 | /* |
1 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
8 | |
3 | |
9 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
4 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
10 | Copyright (C) 1992 Frank Tore Johansen |
5 | Copyright (C) 1992 Frank Tore Johansen |
… | |
… | |
21 | |
16 | |
22 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
23 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
24 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | |
20 | |
26 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
27 | */ |
22 | */ |
28 | |
23 | |
29 | /* This file contains various functions that are not really unique for |
24 | /* This file contains various functions that are not really unique for |
30 | * crossfire, but rather provides what should be standard functions |
25 | * crossfire, but rather provides what should be standard functions |
31 | * 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 |
32 | * nasty system dependent stuff is contained here, with the program |
27 | * nasty system dependent stuff is contained here, with the program |
33 | * calling these functions. |
28 | * calling these functions. |
34 | */ |
29 | */ |
35 | |
30 | |
36 | |
31 | |
37 | #ifdef WIN32 /* ---win32 exclude/include headers */ |
32 | #ifdef WIN32 /* ---win32 exclude/include headers */ |
38 | #include "process.h" |
33 | # include "process.h" |
39 | #define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
34 | # define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
40 | #else |
35 | #else |
41 | #include <ctype.h> |
36 | # include <ctype.h> |
42 | #include <sys/stat.h> |
37 | # include <sys/stat.h> |
43 | #include <sys/wait.h> |
38 | # include <sys/wait.h> |
44 | |
39 | |
45 | #include <sys/param.h> |
40 | # include <sys/param.h> |
46 | #include <stdio.h> |
41 | # include <stdio.h> |
47 | |
42 | |
48 | /* Need to pull in the HAVE_... values somehow */ |
43 | /* Need to pull in the HAVE_... values somehow */ |
|
|
44 | |
49 | /* win32 reminder: always put this in a ifndef win32 block */ |
45 | /* win32 reminder: always put this in a ifndef win32 block */ |
50 | #include <autoconf.h> |
46 | # include <autoconf.h> |
51 | #endif |
47 | #endif |
52 | |
48 | |
53 | |
49 | |
54 | #ifdef HAVE_STDLIB_H |
50 | #ifdef HAVE_STDLIB_H |
55 | #include <stdlib.h> |
51 | # include <stdlib.h> |
56 | #endif |
52 | #endif |
57 | |
53 | |
58 | #ifdef HAVE_UNISTD_H |
54 | #ifdef HAVE_UNISTD_H |
59 | #include <unistd.h> |
55 | # include <unistd.h> |
60 | #endif |
56 | #endif |
61 | |
57 | |
62 | #include <stdarg.h> |
58 | #include <stdarg.h> |
|
|
59 | |
63 | /* Has to be after above includes so we don't redefine some values */ |
60 | /* Has to be after above includes so we don't redefine some values */ |
64 | #include "global.h" |
61 | #include "global.h" |
65 | |
62 | |
66 | static unsigned int curtmp = 0; |
63 | static unsigned int curtmp = 0; |
67 | |
64 | |
… | |
… | |
80 | char *name; |
77 | char *name; |
81 | pid_t pid = getpid (); |
78 | pid_t pid = getpid (); |
82 | |
79 | |
83 | /* HURD does not have a hard limit, but we do */ |
80 | /* HURD does not have a hard limit, but we do */ |
84 | #ifndef MAXPATHLEN |
81 | #ifndef MAXPATHLEN |
85 | #define MAXPATHLEN 4096 |
82 | # define MAXPATHLEN 4096 |
86 | #endif |
83 | #endif |
87 | |
84 | |
88 | if (!(name = (char *) malloc (MAXPATHLEN))) |
85 | if (!(name = (char *) malloc (MAXPATHLEN))) |
89 | return (NULL); |
86 | return (NULL); |
90 | |
87 | |
… | |
… | |
97 | * find one that is free. |
94 | * find one that is free. |
98 | */ |
95 | */ |
99 | if (dir != NULL) |
96 | if (dir != NULL) |
100 | { |
97 | { |
101 | do |
98 | do |
102 | { |
99 | { |
103 | #ifdef HAVE_SNPRINTF |
100 | #ifdef HAVE_SNPRINTF |
104 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
101 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
105 | #else |
102 | #else |
106 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
103 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
107 | #endif |
104 | #endif |
108 | curtmp++; |
105 | curtmp++; |
109 | } |
106 | } |
110 | while (access (name, F_OK) != -1); |
107 | while (access (name, F_OK) != -1); |
111 | return (name); |
108 | return (name); |
112 | } |
109 | } |
113 | return (NULL); |
110 | return (NULL); |
114 | } |
111 | } |
… | |
… | |
127 | if ((dirp = opendir (path)) != NULL) |
124 | if ((dirp = opendir (path)) != NULL) |
128 | { |
125 | { |
129 | struct dirent *de; |
126 | struct dirent *de; |
130 | |
127 | |
131 | for (de = readdir (dirp); de; de = readdir (dirp)) |
128 | for (de = readdir (dirp); de; de = readdir (dirp)) |
132 | { |
129 | { |
133 | /* Don't remove '.' or '..' In theory we should do a better |
130 | /* Don't remove '.' or '..' In theory we should do a better |
134 | * check for .., but the directories we are removing are fairly |
131 | * check for .., but the directories we are removing are fairly |
135 | * limited and should not have dot files in them. |
132 | * limited and should not have dot files in them. |
136 | */ |
133 | */ |
137 | if (de->d_name[0] == '.') |
134 | if (de->d_name[0] == '.') |
138 | continue; |
135 | continue; |
139 | |
136 | |
140 | /* Linux actually has a type field in the dirent structure, |
137 | /* Linux actually has a type field in the dirent structure, |
141 | * but that is not portable - stat should be portable |
138 | * but that is not portable - stat should be portable |
142 | */ |
139 | */ |
143 | status = stat (de->d_name, &statbuf); |
140 | status = stat (de->d_name, &statbuf); |
144 | if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
141 | if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
145 | { |
142 | { |
|
|
143 | sprintf (buf, "%s/%s", path, de->d_name); |
|
|
144 | remove_directory (buf); |
|
|
145 | continue; |
|
|
146 | } |
146 | sprintf (buf, "%s/%s", path, de->d_name); |
147 | sprintf (buf, "%s/%s", path, de->d_name); |
147 | remove_directory (buf); |
|
|
148 | continue; |
|
|
149 | } |
|
|
150 | sprintf (buf, "%s/%s", path, de->d_name); |
|
|
151 | if (unlink (buf)) |
148 | if (unlink (buf)) |
152 | { |
149 | { |
153 | LOG (llevError, "Unable to remove directory %s\n", path); |
150 | LOG (llevError, "Unable to remove directory %s\n", path); |
154 | } |
151 | } |
155 | } |
152 | } |
156 | closedir (dirp); |
153 | closedir (dirp); |
157 | } |
154 | } |
158 | if (rmdir (path)) |
155 | if (rmdir (path)) |
159 | { |
156 | { |
160 | LOG (llevError, "Unable to remove directory %s\n", path); |
157 | LOG (llevError, "Unable to remove directory %s\n", path); |
161 | } |
158 | } |
162 | } |
159 | } |
163 | |
160 | |
164 | #if defined(sgi) |
161 | #if defined(sgi) |
165 | |
162 | |
166 | #include <stdio.h> |
163 | # include <stdio.h> |
167 | #include <stdlib.h> |
164 | # include <stdlib.h> |
168 | #include <string.h> |
165 | # include <string.h> |
169 | |
166 | |
170 | #define popen fixed_popen |
167 | # define popen fixed_popen |
171 | |
168 | |
172 | FILE * |
169 | FILE * |
173 | popen_local (const char *command, const char *type) |
170 | popen_local (const char *command, const char *type) |
174 | { |
171 | { |
175 | int fd[2]; |
172 | int fd[2]; |
176 | int pd; |
173 | int pd; |
177 | FILE *ret; |
174 | FILE *ret; |
|
|
175 | |
178 | if (!strcmp (type, "r")) |
176 | if (!strcmp (type, "r")) |
179 | { |
|
|
180 | pd = STDOUT_FILENO; |
177 | pd = STDOUT_FILENO; |
181 | } |
|
|
182 | else if (!strcmp (type, "w")) |
178 | else if (!strcmp (type, "w")) |
183 | { |
|
|
184 | pd = STDIN_FILENO; |
179 | pd = STDIN_FILENO; |
185 | } |
|
|
186 | else |
180 | else |
187 | { |
|
|
188 | return NULL; |
181 | return NULL; |
189 | } |
182 | |
190 | if (pipe (fd) != -1) |
183 | if (pipe (fd) != -1) |
191 | { |
184 | { |
192 | switch (fork ()) |
185 | switch (fork ()) |
193 | { |
186 | { |
194 | case -1: |
187 | case -1: |
195 | close (fd[0]); |
188 | close (fd[0]); |
196 | close (fd[1]); |
189 | close (fd[1]); |
197 | break; |
190 | break; |
198 | case 0: |
191 | case 0: |
199 | close (fd[0]); |
192 | close (fd[0]); |
200 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
193 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
201 | { |
194 | { |
202 | if (fd[1] != pd) |
195 | if (fd[1] != pd) |
203 | { |
196 | { |
204 | close (fd[1]); |
197 | close (fd[1]); |
205 | } |
198 | } |
206 | execl ("/bin/sh", "sh", "-c", command, NULL); |
199 | execl ("/bin/sh", "sh", "-c", command, NULL); |
207 | close (pd); |
200 | close (pd); |
208 | } |
201 | } |
209 | exit (1); |
202 | _exit (1); |
210 | break; |
203 | break; |
211 | default: |
204 | default: |
212 | close (fd[1]); |
205 | close (fd[1]); |
213 | if (ret = fdopen (fd[0], type)) |
206 | if (ret = fdopen (fd[0], type)) |
214 | { |
207 | { |
215 | return ret; |
208 | return ret; |
216 | } |
209 | } |
217 | close (fd[0]); |
210 | close (fd[0]); |
218 | break; |
211 | break; |
219 | } |
212 | } |
220 | } |
213 | } |
221 | return NULL; |
214 | return NULL; |
222 | } |
215 | } |
223 | |
216 | |
224 | #endif /* defined(sgi) */ |
217 | #endif /* defined(sgi) */ |
… | |
… | |
226 | |
219 | |
227 | /***************************************************************************** |
220 | /***************************************************************************** |
228 | * String related function |
221 | * String related function |
229 | ****************************************************************************/ |
222 | ****************************************************************************/ |
230 | |
223 | |
231 | |
|
|
232 | |
|
|
233 | /* |
224 | /* |
234 | * A replacement of strdup(), since it's not defined at some |
225 | * A replacement of strdup(), since it's not defined at some |
235 | * unix variants. |
226 | * unix variants. |
236 | */ |
227 | */ |
237 | char * |
228 | char * |
238 | strdup_local (const char *str) |
229 | strdup_local (const char *str) |
239 | { |
230 | { |
240 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
231 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
|
|
232 | |
241 | strcpy (c, str); |
233 | strcpy (c, str); |
242 | return c; |
234 | return c; |
243 | } |
235 | } |
244 | |
236 | |
245 | |
237 | |
… | |
… | |
257 | while (*s1 && *s2 && n) |
249 | while (*s1 && *s2 && n) |
258 | { |
250 | { |
259 | c1 = tolower (*s1); |
251 | c1 = tolower (*s1); |
260 | c2 = tolower (*s2); |
252 | c2 = tolower (*s2); |
261 | if (c1 != c2) |
253 | if (c1 != c2) |
262 | return (c1 - c2); |
254 | return (c1 - c2); |
263 | s1++; |
255 | s1++; |
264 | s2++; |
256 | s2++; |
265 | n--; |
257 | n--; |
266 | } |
258 | } |
267 | if (!n) |
259 | if (!n) |
… | |
… | |
279 | while (*s1 && *s2) |
271 | while (*s1 && *s2) |
280 | { |
272 | { |
281 | c1 = tolower (*s1); |
273 | c1 = tolower (*s1); |
282 | c2 = tolower (*s2); |
274 | c2 = tolower (*s2); |
283 | if (c1 != c2) |
275 | if (c1 != c2) |
284 | return (c1 - c2); |
276 | return (c1 - c2); |
285 | s1++; |
277 | s1++; |
286 | s2++; |
278 | s2++; |
287 | } |
279 | } |
288 | if (*s1 == '\0' && *s2 == '\0') |
280 | if (*s1 == '\0' && *s2 == '\0') |
289 | return 0; |
281 | return 0; |
… | |
… | |
300 | if ((c = *find++) != 0) |
292 | if ((c = *find++) != 0) |
301 | { |
293 | { |
302 | c = tolower (c); |
294 | c = tolower (c); |
303 | len = strlen (find); |
295 | len = strlen (find); |
304 | do |
296 | do |
305 | { |
297 | { |
306 | do |
298 | do |
307 | { |
299 | { |
308 | if ((sc = *s++) == 0) |
300 | if ((sc = *s++) == 0) |
309 | return NULL; |
301 | return NULL; |
310 | } |
302 | } |
311 | while (tolower (sc) != c); |
303 | while (tolower (sc) != c); |
312 | } |
304 | } |
313 | while (strncasecmp (s, find, len) != 0); |
305 | while (strncasecmp (s, find, len) != 0); |
314 | s--; |
306 | s--; |
315 | } |
307 | } |
316 | return (char *) s; |
308 | return (char *) s; |
317 | } |
309 | } |
… | |
… | |
347 | */ |
339 | */ |
348 | int |
340 | int |
349 | isqrt (int n) |
341 | isqrt (int n) |
350 | { |
342 | { |
351 | int result, sum, prev; |
343 | int result, sum, prev; |
|
|
344 | |
352 | result = 0; |
345 | result = 0; |
353 | prev = sum = 1; |
346 | prev = sum = 1; |
354 | while (sum <= n) |
347 | while (sum <= n) |
355 | { |
348 | { |
356 | prev += 2; |
349 | prev += 2; |
… | |
… | |
367 | */ |
360 | */ |
368 | |
361 | |
369 | char * |
362 | char * |
370 | ltostr10 (signed long n) |
363 | ltostr10 (signed long n) |
371 | { |
364 | { |
372 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
365 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
373 | character for the trailing nul character */ |
366 | character for the trailing nul character */ |
374 | snprintf (buf, sizeof (buf), "%ld", n); |
367 | snprintf (buf, sizeof (buf), "%ld", n); |
375 | return buf; |
368 | return buf; |
376 | } |
369 | } |
377 | |
370 | |
378 | char * |
371 | char * |
379 | doubletostr10 (double v) |
372 | doubletostr10 (double v) |
380 | { |
373 | { |
381 | static char tbuf[200]; |
374 | static char tbuf[200]; |
|
|
375 | |
382 | sprintf (tbuf, "%f", v); |
376 | sprintf (tbuf, "%f", v); |
383 | return tbuf; |
377 | return tbuf; |
384 | } |
378 | } |
385 | |
379 | |
386 | /** |
380 | /** |
… | |
… | |
400 | /* |
394 | /* |
401 | * See open_and_uncompress(). |
395 | * See open_and_uncompress(). |
402 | */ |
396 | */ |
403 | |
397 | |
404 | void |
398 | void |
405 | close_and_delete (FILE *fp, int compressed) |
399 | close_and_delete (FILE * fp, int compressed) |
406 | { |
400 | { |
407 | fclose (fp); |
401 | fclose (fp); |
408 | } |
402 | } |
409 | |
403 | |
410 | /* |
404 | /* |
… | |
… | |
423 | |
417 | |
424 | while ((cp = strchr (cp + 1, (int) '/'))) |
418 | while ((cp = strchr (cp + 1, (int) '/'))) |
425 | { |
419 | { |
426 | *cp = '\0'; |
420 | *cp = '\0'; |
427 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
421 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
428 | { |
422 | { |
429 | if (mkdir (buf, SAVE_DIR_MODE)) |
423 | if (mkdir (buf, SAVE_DIR_MODE)) |
430 | { |
424 | { |
431 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
425 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
432 | return; |
426 | return; |
433 | } |
427 | } |
434 | } |
428 | } |
435 | *cp = '/'; |
429 | *cp = '/'; |
436 | } |
430 | } |
437 | } |
431 | } |