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 */ |
|
|
38 | #include "process.h" |
|
|
39 | #define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
|
|
40 | #else |
|
|
41 | #include <ctype.h> |
32 | #include <ctype.h> |
42 | #include <sys/stat.h> |
33 | #include <sys/stat.h> |
43 | #include <sys/wait.h> |
34 | #include <sys/wait.h> |
44 | |
35 | |
45 | #include <sys/param.h> |
36 | #include <sys/param.h> |
46 | #include <stdio.h> |
37 | #include <stdio.h> |
47 | |
38 | |
48 | /* Need to pull in the HAVE_... values somehow */ |
39 | /* Need to pull in the HAVE_... values somehow */ |
49 | /* win32 reminder: always put this in a ifndef win32 block */ |
40 | |
50 | #include <autoconf.h> |
41 | #include <autoconf.h> |
51 | #endif |
|
|
52 | |
42 | |
53 | |
43 | |
54 | #ifdef HAVE_STDLIB_H |
44 | #ifdef HAVE_STDLIB_H |
55 | #include <stdlib.h> |
45 | # include <stdlib.h> |
56 | #endif |
46 | #endif |
57 | |
47 | |
58 | #ifdef HAVE_UNISTD_H |
48 | #ifdef HAVE_UNISTD_H |
59 | #include <unistd.h> |
49 | # include <unistd.h> |
60 | #endif |
50 | #endif |
61 | |
51 | |
62 | #include <stdarg.h> |
52 | #include <stdarg.h> |
|
|
53 | |
63 | /* Has to be after above includes so we don't redefine some values */ |
54 | /* Has to be after above includes so we don't redefine some values */ |
64 | #include "global.h" |
55 | #include "global.h" |
65 | |
56 | |
66 | static unsigned int curtmp = 0; |
57 | static unsigned int curtmp = 0; |
67 | |
58 | |
… | |
… | |
80 | char *name; |
71 | char *name; |
81 | pid_t pid = getpid (); |
72 | pid_t pid = getpid (); |
82 | |
73 | |
83 | /* HURD does not have a hard limit, but we do */ |
74 | /* HURD does not have a hard limit, but we do */ |
84 | #ifndef MAXPATHLEN |
75 | #ifndef MAXPATHLEN |
85 | #define MAXPATHLEN 4096 |
76 | # define MAXPATHLEN 4096 |
86 | #endif |
77 | #endif |
87 | |
78 | |
88 | if (!(name = (char *) malloc (MAXPATHLEN))) |
79 | if (!(name = (char *) malloc (MAXPATHLEN))) |
89 | return (NULL); |
80 | return (NULL); |
90 | |
81 | |
… | |
… | |
97 | * find one that is free. |
88 | * find one that is free. |
98 | */ |
89 | */ |
99 | if (dir != NULL) |
90 | if (dir != NULL) |
100 | { |
91 | { |
101 | do |
92 | do |
102 | { |
93 | { |
103 | #ifdef HAVE_SNPRINTF |
94 | #ifdef HAVE_SNPRINTF |
104 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
95 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
105 | #else |
96 | #else |
106 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
97 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
107 | #endif |
98 | #endif |
108 | curtmp++; |
99 | curtmp++; |
109 | } |
100 | } |
110 | while (access (name, F_OK) != -1); |
101 | while (access (name, F_OK) != -1); |
111 | return (name); |
102 | return (name); |
112 | } |
103 | } |
113 | return (NULL); |
104 | return (NULL); |
114 | } |
105 | } |
… | |
… | |
127 | if ((dirp = opendir (path)) != NULL) |
118 | if ((dirp = opendir (path)) != NULL) |
128 | { |
119 | { |
129 | struct dirent *de; |
120 | struct dirent *de; |
130 | |
121 | |
131 | for (de = readdir (dirp); de; de = readdir (dirp)) |
122 | for (de = readdir (dirp); de; de = readdir (dirp)) |
132 | { |
123 | { |
133 | /* Don't remove '.' or '..' In theory we should do a better |
124 | /* Don't remove '.' or '..' In theory we should do a better |
134 | * check for .., but the directories we are removing are fairly |
125 | * check for .., but the directories we are removing are fairly |
135 | * limited and should not have dot files in them. |
126 | * limited and should not have dot files in them. |
136 | */ |
127 | */ |
137 | if (de->d_name[0] == '.') |
128 | if (de->d_name[0] == '.') |
138 | continue; |
129 | continue; |
139 | |
130 | |
140 | /* Linux actually has a type field in the dirent structure, |
131 | /* Linux actually has a type field in the dirent structure, |
141 | * but that is not portable - stat should be portable |
132 | * but that is not portable - stat should be portable |
142 | */ |
133 | */ |
143 | status = stat (de->d_name, &statbuf); |
134 | status = stat (de->d_name, &statbuf); |
144 | if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
135 | if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
145 | { |
136 | { |
|
|
137 | sprintf (buf, "%s/%s", path, de->d_name); |
|
|
138 | remove_directory (buf); |
|
|
139 | continue; |
|
|
140 | } |
146 | sprintf (buf, "%s/%s", path, de->d_name); |
141 | 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)) |
142 | if (unlink (buf)) |
152 | { |
143 | { |
153 | LOG (llevError, "Unable to remove directory %s\n", path); |
144 | LOG (llevError, "Unable to remove directory %s\n", path); |
154 | } |
145 | } |
155 | } |
146 | } |
156 | closedir (dirp); |
147 | closedir (dirp); |
157 | } |
148 | } |
158 | if (rmdir (path)) |
149 | if (rmdir (path)) |
159 | { |
150 | { |
160 | LOG (llevError, "Unable to remove directory %s\n", path); |
151 | LOG (llevError, "Unable to remove directory %s\n", path); |
161 | } |
152 | } |
162 | } |
153 | } |
163 | |
154 | |
164 | #if defined(sgi) |
155 | #if defined(sgi) |
165 | |
156 | |
166 | #include <stdio.h> |
157 | # include <stdio.h> |
167 | #include <stdlib.h> |
158 | # include <stdlib.h> |
168 | #include <string.h> |
159 | # include <string.h> |
169 | |
160 | |
170 | #define popen fixed_popen |
161 | # define popen fixed_popen |
171 | |
162 | |
172 | FILE * |
163 | FILE * |
173 | popen_local (const char *command, const char *type) |
164 | popen_local (const char *command, const char *type) |
174 | { |
165 | { |
175 | int fd[2]; |
166 | int fd[2]; |
176 | int pd; |
167 | int pd; |
177 | FILE *ret; |
168 | FILE *ret; |
|
|
169 | |
178 | if (!strcmp (type, "r")) |
170 | if (!strcmp (type, "r")) |
179 | { |
|
|
180 | pd = STDOUT_FILENO; |
171 | pd = STDOUT_FILENO; |
181 | } |
|
|
182 | else if (!strcmp (type, "w")) |
172 | else if (!strcmp (type, "w")) |
183 | { |
|
|
184 | pd = STDIN_FILENO; |
173 | pd = STDIN_FILENO; |
185 | } |
|
|
186 | else |
174 | else |
187 | { |
|
|
188 | return NULL; |
175 | return NULL; |
189 | } |
176 | |
190 | if (pipe (fd) != -1) |
177 | if (pipe (fd) != -1) |
191 | { |
178 | { |
192 | switch (fork ()) |
179 | switch (fork ()) |
193 | { |
180 | { |
194 | case -1: |
181 | case -1: |
195 | close (fd[0]); |
182 | close (fd[0]); |
196 | close (fd[1]); |
183 | close (fd[1]); |
197 | break; |
184 | break; |
198 | case 0: |
185 | case 0: |
199 | close (fd[0]); |
186 | close (fd[0]); |
200 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
187 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
201 | { |
188 | { |
202 | if (fd[1] != pd) |
189 | if (fd[1] != pd) |
203 | { |
190 | { |
204 | close (fd[1]); |
191 | close (fd[1]); |
205 | } |
192 | } |
206 | execl ("/bin/sh", "sh", "-c", command, NULL); |
193 | execl ("/bin/sh", "sh", "-c", command, NULL); |
207 | close (pd); |
194 | close (pd); |
208 | } |
195 | } |
209 | exit (1); |
196 | _exit (1); |
210 | break; |
197 | break; |
211 | default: |
198 | default: |
212 | close (fd[1]); |
199 | close (fd[1]); |
213 | if (ret = fdopen (fd[0], type)) |
200 | if (ret = fdopen (fd[0], type)) |
214 | { |
201 | { |
215 | return ret; |
202 | return ret; |
216 | } |
203 | } |
217 | close (fd[0]); |
204 | close (fd[0]); |
218 | break; |
205 | break; |
219 | } |
206 | } |
220 | } |
207 | } |
221 | return NULL; |
208 | return NULL; |
222 | } |
209 | } |
223 | |
210 | |
224 | #endif /* defined(sgi) */ |
211 | #endif /* defined(sgi) */ |
… | |
… | |
226 | |
213 | |
227 | /***************************************************************************** |
214 | /***************************************************************************** |
228 | * String related function |
215 | * String related function |
229 | ****************************************************************************/ |
216 | ****************************************************************************/ |
230 | |
217 | |
231 | |
|
|
232 | |
|
|
233 | /* |
218 | /* |
234 | * A replacement of strdup(), since it's not defined at some |
219 | * A replacement of strdup(), since it's not defined at some |
235 | * unix variants. |
220 | * unix variants. |
236 | */ |
221 | */ |
237 | char * |
222 | char * |
238 | strdup_local (const char *str) |
223 | strdup_local (const char *str) |
239 | { |
224 | { |
240 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
225 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
|
|
226 | |
241 | strcpy (c, str); |
227 | strcpy (c, str); |
242 | return c; |
228 | return c; |
243 | } |
229 | } |
244 | |
230 | |
245 | |
231 | |
… | |
… | |
257 | while (*s1 && *s2 && n) |
243 | while (*s1 && *s2 && n) |
258 | { |
244 | { |
259 | c1 = tolower (*s1); |
245 | c1 = tolower (*s1); |
260 | c2 = tolower (*s2); |
246 | c2 = tolower (*s2); |
261 | if (c1 != c2) |
247 | if (c1 != c2) |
262 | return (c1 - c2); |
248 | return (c1 - c2); |
263 | s1++; |
249 | s1++; |
264 | s2++; |
250 | s2++; |
265 | n--; |
251 | n--; |
266 | } |
252 | } |
267 | if (!n) |
253 | if (!n) |
… | |
… | |
279 | while (*s1 && *s2) |
265 | while (*s1 && *s2) |
280 | { |
266 | { |
281 | c1 = tolower (*s1); |
267 | c1 = tolower (*s1); |
282 | c2 = tolower (*s2); |
268 | c2 = tolower (*s2); |
283 | if (c1 != c2) |
269 | if (c1 != c2) |
284 | return (c1 - c2); |
270 | return (c1 - c2); |
285 | s1++; |
271 | s1++; |
286 | s2++; |
272 | s2++; |
287 | } |
273 | } |
288 | if (*s1 == '\0' && *s2 == '\0') |
274 | if (*s1 == '\0' && *s2 == '\0') |
289 | return 0; |
275 | return 0; |
… | |
… | |
300 | if ((c = *find++) != 0) |
286 | if ((c = *find++) != 0) |
301 | { |
287 | { |
302 | c = tolower (c); |
288 | c = tolower (c); |
303 | len = strlen (find); |
289 | len = strlen (find); |
304 | do |
290 | do |
305 | { |
291 | { |
306 | do |
292 | do |
307 | { |
293 | { |
308 | if ((sc = *s++) == 0) |
294 | if ((sc = *s++) == 0) |
309 | return NULL; |
295 | return NULL; |
310 | } |
296 | } |
311 | while (tolower (sc) != c); |
297 | while (tolower (sc) != c); |
312 | } |
298 | } |
313 | while (strncasecmp (s, find, len) != 0); |
299 | while (strncasecmp (s, find, len) != 0); |
314 | s--; |
300 | s--; |
315 | } |
301 | } |
316 | return (char *) s; |
302 | return (char *) s; |
317 | } |
303 | } |
… | |
… | |
347 | */ |
333 | */ |
348 | int |
334 | int |
349 | isqrt (int n) |
335 | isqrt (int n) |
350 | { |
336 | { |
351 | int result, sum, prev; |
337 | int result, sum, prev; |
|
|
338 | |
352 | result = 0; |
339 | result = 0; |
353 | prev = sum = 1; |
340 | prev = sum = 1; |
354 | while (sum <= n) |
341 | while (sum <= n) |
355 | { |
342 | { |
356 | prev += 2; |
343 | prev += 2; |
… | |
… | |
367 | */ |
354 | */ |
368 | |
355 | |
369 | char * |
356 | char * |
370 | ltostr10 (signed long n) |
357 | ltostr10 (signed long n) |
371 | { |
358 | { |
372 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
359 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
373 | character for the trailing nul character */ |
360 | character for the trailing nul character */ |
374 | snprintf (buf, sizeof (buf), "%ld", n); |
361 | snprintf (buf, sizeof (buf), "%ld", n); |
375 | return buf; |
362 | return buf; |
376 | } |
363 | } |
377 | |
364 | |
378 | char * |
365 | char * |
379 | doubletostr10 (double v) |
366 | doubletostr10 (double v) |
380 | { |
367 | { |
381 | static char tbuf[200]; |
368 | static char tbuf[200]; |
|
|
369 | |
382 | sprintf (tbuf, "%f", v); |
370 | sprintf (tbuf, "%f", v); |
383 | return tbuf; |
371 | return tbuf; |
384 | } |
372 | } |
385 | |
373 | |
386 | /** |
374 | /** |
… | |
… | |
388 | * then it opens it and returns the file-pointer. |
376 | * then it opens it and returns the file-pointer. |
389 | */ |
377 | */ |
390 | FILE * |
378 | FILE * |
391 | open_and_uncompress (const char *name, int flag, int *compressed) |
379 | open_and_uncompress (const char *name, int flag, int *compressed) |
392 | { |
380 | { |
393 | size_t i; |
|
|
394 | FILE *fp; |
|
|
395 | |
|
|
396 | *compressed = 0; |
381 | *compressed = 0; |
397 | return fopen (name, "r"); |
382 | return fopen (name, "r"); |
398 | } |
383 | } |
399 | |
384 | |
400 | /* |
385 | /* |
401 | * See open_and_uncompress(). |
386 | * See open_and_uncompress(). |
402 | */ |
387 | */ |
403 | |
388 | |
404 | void |
389 | void |
405 | close_and_delete (FILE *fp, int compressed) |
390 | close_and_delete (FILE * fp, int compressed) |
406 | { |
391 | { |
407 | fclose (fp); |
392 | fclose (fp); |
408 | } |
393 | } |
409 | |
394 | |
410 | /* |
395 | /* |
… | |
… | |
423 | |
408 | |
424 | while ((cp = strchr (cp + 1, (int) '/'))) |
409 | while ((cp = strchr (cp + 1, (int) '/'))) |
425 | { |
410 | { |
426 | *cp = '\0'; |
411 | *cp = '\0'; |
427 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
412 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
428 | { |
413 | { |
429 | if (mkdir (buf, SAVE_DIR_MODE)) |
414 | if (mkdir (buf, SAVE_DIR_MODE)) |
430 | { |
415 | { |
431 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
416 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
432 | return; |
417 | return; |
433 | } |
418 | } |
434 | } |
419 | } |
435 | *cp = '/'; |
420 | *cp = '/'; |
436 | } |
421 | } |
437 | } |
422 | } |