|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_porting_c = |
3 | * static char *rcsid_porting_c = |
3 | * "$Id: porting.C,v 1.4 2006/09/07 09:28:44 root Exp $"; |
4 | * "$Id: porting.C,v 1.5 2006/09/10 16:00:23 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
32 | * nasty system dependent stuff is contained here, with the program |
33 | * nasty system dependent stuff is contained here, with the program |
33 | * calling these functions. |
34 | * calling these functions. |
34 | */ |
35 | */ |
35 | |
36 | |
36 | |
37 | |
37 | #ifdef WIN32 /* ---win32 exclude/include headers */ |
38 | #ifdef WIN32 /* ---win32 exclude/include headers */ |
38 | #include "process.h" |
39 | # include "process.h" |
39 | #define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
40 | # define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
40 | #else |
41 | #else |
41 | #include <ctype.h> |
42 | # include <ctype.h> |
42 | #include <sys/stat.h> |
43 | # include <sys/stat.h> |
43 | #include <sys/wait.h> |
44 | # include <sys/wait.h> |
44 | |
45 | |
45 | #include <sys/param.h> |
46 | # include <sys/param.h> |
46 | #include <stdio.h> |
47 | # include <stdio.h> |
47 | |
48 | |
48 | /* Need to pull in the HAVE_... values somehow */ |
49 | /* Need to pull in the HAVE_... values somehow */ |
|
|
50 | |
49 | /* win32 reminder: always put this in a ifndef win32 block */ |
51 | /* win32 reminder: always put this in a ifndef win32 block */ |
50 | #include <autoconf.h> |
52 | # include <autoconf.h> |
51 | #endif |
53 | #endif |
52 | |
54 | |
53 | |
55 | |
54 | #ifdef HAVE_STDLIB_H |
56 | #ifdef HAVE_STDLIB_H |
55 | #include <stdlib.h> |
57 | # include <stdlib.h> |
56 | #endif |
58 | #endif |
57 | |
59 | |
58 | #ifdef HAVE_UNISTD_H |
60 | #ifdef HAVE_UNISTD_H |
59 | #include <unistd.h> |
61 | # include <unistd.h> |
60 | #endif |
62 | #endif |
61 | |
63 | |
62 | #include <stdarg.h> |
64 | #include <stdarg.h> |
|
|
65 | |
63 | /* Has to be after above includes so we don't redefine some values */ |
66 | /* Has to be after above includes so we don't redefine some values */ |
64 | #include "global.h" |
67 | #include "global.h" |
65 | |
68 | |
66 | static unsigned int curtmp = 0; |
69 | static unsigned int curtmp = 0; |
67 | |
70 | |
… | |
… | |
80 | char *name; |
83 | char *name; |
81 | pid_t pid = getpid (); |
84 | pid_t pid = getpid (); |
82 | |
85 | |
83 | /* HURD does not have a hard limit, but we do */ |
86 | /* HURD does not have a hard limit, but we do */ |
84 | #ifndef MAXPATHLEN |
87 | #ifndef MAXPATHLEN |
85 | #define MAXPATHLEN 4096 |
88 | # define MAXPATHLEN 4096 |
86 | #endif |
89 | #endif |
87 | |
90 | |
88 | if (!(name = (char *) malloc (MAXPATHLEN))) |
91 | if (!(name = (char *) malloc (MAXPATHLEN))) |
89 | return (NULL); |
92 | return (NULL); |
90 | |
93 | |
… | |
… | |
97 | * find one that is free. |
100 | * find one that is free. |
98 | */ |
101 | */ |
99 | if (dir != NULL) |
102 | if (dir != NULL) |
100 | { |
103 | { |
101 | do |
104 | do |
102 | { |
105 | { |
103 | #ifdef HAVE_SNPRINTF |
106 | #ifdef HAVE_SNPRINTF |
104 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
107 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
105 | #else |
108 | #else |
106 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
109 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
107 | #endif |
110 | #endif |
108 | curtmp++; |
111 | curtmp++; |
109 | } |
112 | } |
110 | while (access (name, F_OK) != -1); |
113 | while (access (name, F_OK) != -1); |
111 | return (name); |
114 | return (name); |
112 | } |
115 | } |
113 | return (NULL); |
116 | return (NULL); |
114 | } |
117 | } |
… | |
… | |
127 | if ((dirp = opendir (path)) != NULL) |
130 | if ((dirp = opendir (path)) != NULL) |
128 | { |
131 | { |
129 | struct dirent *de; |
132 | struct dirent *de; |
130 | |
133 | |
131 | for (de = readdir (dirp); de; de = readdir (dirp)) |
134 | for (de = readdir (dirp); de; de = readdir (dirp)) |
132 | { |
135 | { |
133 | /* Don't remove '.' or '..' In theory we should do a better |
136 | /* Don't remove '.' or '..' In theory we should do a better |
134 | * check for .., but the directories we are removing are fairly |
137 | * check for .., but the directories we are removing are fairly |
135 | * limited and should not have dot files in them. |
138 | * limited and should not have dot files in them. |
136 | */ |
139 | */ |
137 | if (de->d_name[0] == '.') |
140 | if (de->d_name[0] == '.') |
138 | continue; |
141 | continue; |
139 | |
142 | |
140 | /* Linux actually has a type field in the dirent structure, |
143 | /* Linux actually has a type field in the dirent structure, |
141 | * but that is not portable - stat should be portable |
144 | * but that is not portable - stat should be portable |
142 | */ |
145 | */ |
143 | status = stat (de->d_name, &statbuf); |
146 | status = stat (de->d_name, &statbuf); |
144 | if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
147 | if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
145 | { |
148 | { |
|
|
149 | sprintf (buf, "%s/%s", path, de->d_name); |
|
|
150 | remove_directory (buf); |
|
|
151 | continue; |
|
|
152 | } |
146 | sprintf (buf, "%s/%s", path, de->d_name); |
153 | 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)) |
154 | if (unlink (buf)) |
152 | { |
155 | { |
153 | LOG (llevError, "Unable to remove directory %s\n", path); |
156 | LOG (llevError, "Unable to remove directory %s\n", path); |
154 | } |
157 | } |
155 | } |
158 | } |
156 | closedir (dirp); |
159 | closedir (dirp); |
157 | } |
160 | } |
158 | if (rmdir (path)) |
161 | if (rmdir (path)) |
159 | { |
162 | { |
160 | LOG (llevError, "Unable to remove directory %s\n", path); |
163 | LOG (llevError, "Unable to remove directory %s\n", path); |
161 | } |
164 | } |
162 | } |
165 | } |
163 | |
166 | |
164 | #if defined(sgi) |
167 | #if defined(sgi) |
165 | |
168 | |
166 | #include <stdio.h> |
169 | # include <stdio.h> |
167 | #include <stdlib.h> |
170 | # include <stdlib.h> |
168 | #include <string.h> |
171 | # include <string.h> |
169 | |
172 | |
170 | #define popen fixed_popen |
173 | # define popen fixed_popen |
171 | |
174 | |
172 | FILE * |
175 | FILE * |
173 | popen_local (const char *command, const char *type) |
176 | popen_local (const char *command, const char *type) |
174 | { |
177 | { |
175 | int fd[2]; |
178 | int fd[2]; |
176 | int pd; |
179 | int pd; |
177 | FILE *ret; |
180 | FILE *ret; |
|
|
181 | |
178 | if (!strcmp (type, "r")) |
182 | if (!strcmp (type, "r")) |
179 | { |
183 | { |
180 | pd = STDOUT_FILENO; |
184 | pd = STDOUT_FILENO; |
181 | } |
185 | } |
182 | else if (!strcmp (type, "w")) |
186 | else if (!strcmp (type, "w")) |
… | |
… | |
188 | return NULL; |
192 | return NULL; |
189 | } |
193 | } |
190 | if (pipe (fd) != -1) |
194 | if (pipe (fd) != -1) |
191 | { |
195 | { |
192 | switch (fork ()) |
196 | switch (fork ()) |
193 | { |
197 | { |
194 | case -1: |
198 | case -1: |
195 | close (fd[0]); |
199 | close (fd[0]); |
196 | close (fd[1]); |
200 | close (fd[1]); |
197 | break; |
201 | break; |
198 | case 0: |
202 | case 0: |
199 | close (fd[0]); |
203 | close (fd[0]); |
200 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
204 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
201 | { |
205 | { |
202 | if (fd[1] != pd) |
206 | if (fd[1] != pd) |
203 | { |
207 | { |
204 | close (fd[1]); |
208 | close (fd[1]); |
205 | } |
209 | } |
206 | execl ("/bin/sh", "sh", "-c", command, NULL); |
210 | execl ("/bin/sh", "sh", "-c", command, NULL); |
207 | close (pd); |
211 | close (pd); |
208 | } |
212 | } |
209 | exit (1); |
213 | exit (1); |
210 | break; |
214 | break; |
211 | default: |
215 | default: |
212 | close (fd[1]); |
216 | close (fd[1]); |
213 | if (ret = fdopen (fd[0], type)) |
217 | if (ret = fdopen (fd[0], type)) |
214 | { |
218 | { |
215 | return ret; |
219 | return ret; |
216 | } |
220 | } |
217 | close (fd[0]); |
221 | close (fd[0]); |
218 | break; |
222 | break; |
219 | } |
223 | } |
220 | } |
224 | } |
221 | return NULL; |
225 | return NULL; |
222 | } |
226 | } |
223 | |
227 | |
224 | #endif /* defined(sgi) */ |
228 | #endif /* defined(sgi) */ |
… | |
… | |
236 | */ |
240 | */ |
237 | char * |
241 | char * |
238 | strdup_local (const char *str) |
242 | strdup_local (const char *str) |
239 | { |
243 | { |
240 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
244 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
|
|
245 | |
241 | strcpy (c, str); |
246 | strcpy (c, str); |
242 | return c; |
247 | return c; |
243 | } |
248 | } |
244 | |
249 | |
245 | |
250 | |
… | |
… | |
257 | while (*s1 && *s2 && n) |
262 | while (*s1 && *s2 && n) |
258 | { |
263 | { |
259 | c1 = tolower (*s1); |
264 | c1 = tolower (*s1); |
260 | c2 = tolower (*s2); |
265 | c2 = tolower (*s2); |
261 | if (c1 != c2) |
266 | if (c1 != c2) |
262 | return (c1 - c2); |
267 | return (c1 - c2); |
263 | s1++; |
268 | s1++; |
264 | s2++; |
269 | s2++; |
265 | n--; |
270 | n--; |
266 | } |
271 | } |
267 | if (!n) |
272 | if (!n) |
… | |
… | |
279 | while (*s1 && *s2) |
284 | while (*s1 && *s2) |
280 | { |
285 | { |
281 | c1 = tolower (*s1); |
286 | c1 = tolower (*s1); |
282 | c2 = tolower (*s2); |
287 | c2 = tolower (*s2); |
283 | if (c1 != c2) |
288 | if (c1 != c2) |
284 | return (c1 - c2); |
289 | return (c1 - c2); |
285 | s1++; |
290 | s1++; |
286 | s2++; |
291 | s2++; |
287 | } |
292 | } |
288 | if (*s1 == '\0' && *s2 == '\0') |
293 | if (*s1 == '\0' && *s2 == '\0') |
289 | return 0; |
294 | return 0; |
… | |
… | |
300 | if ((c = *find++) != 0) |
305 | if ((c = *find++) != 0) |
301 | { |
306 | { |
302 | c = tolower (c); |
307 | c = tolower (c); |
303 | len = strlen (find); |
308 | len = strlen (find); |
304 | do |
309 | do |
305 | { |
310 | { |
306 | do |
311 | do |
307 | { |
312 | { |
308 | if ((sc = *s++) == 0) |
313 | if ((sc = *s++) == 0) |
309 | return NULL; |
314 | return NULL; |
310 | } |
315 | } |
311 | while (tolower (sc) != c); |
316 | while (tolower (sc) != c); |
312 | } |
317 | } |
313 | while (strncasecmp (s, find, len) != 0); |
318 | while (strncasecmp (s, find, len) != 0); |
314 | s--; |
319 | s--; |
315 | } |
320 | } |
316 | return (char *) s; |
321 | return (char *) s; |
317 | } |
322 | } |
… | |
… | |
347 | */ |
352 | */ |
348 | int |
353 | int |
349 | isqrt (int n) |
354 | isqrt (int n) |
350 | { |
355 | { |
351 | int result, sum, prev; |
356 | int result, sum, prev; |
|
|
357 | |
352 | result = 0; |
358 | result = 0; |
353 | prev = sum = 1; |
359 | prev = sum = 1; |
354 | while (sum <= n) |
360 | while (sum <= n) |
355 | { |
361 | { |
356 | prev += 2; |
362 | prev += 2; |
… | |
… | |
367 | */ |
373 | */ |
368 | |
374 | |
369 | char * |
375 | char * |
370 | ltostr10 (signed long n) |
376 | ltostr10 (signed long n) |
371 | { |
377 | { |
372 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
378 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
373 | character for the trailing nul character */ |
379 | character for the trailing nul character */ |
374 | snprintf (buf, sizeof (buf), "%ld", n); |
380 | snprintf (buf, sizeof (buf), "%ld", n); |
375 | return buf; |
381 | return buf; |
376 | } |
382 | } |
377 | |
383 | |
378 | char * |
384 | char * |
379 | doubletostr10 (double v) |
385 | doubletostr10 (double v) |
380 | { |
386 | { |
381 | static char tbuf[200]; |
387 | static char tbuf[200]; |
|
|
388 | |
382 | sprintf (tbuf, "%f", v); |
389 | sprintf (tbuf, "%f", v); |
383 | return tbuf; |
390 | return tbuf; |
384 | } |
391 | } |
385 | |
392 | |
386 | /** |
393 | /** |
… | |
… | |
400 | /* |
407 | /* |
401 | * See open_and_uncompress(). |
408 | * See open_and_uncompress(). |
402 | */ |
409 | */ |
403 | |
410 | |
404 | void |
411 | void |
405 | close_and_delete (FILE *fp, int compressed) |
412 | close_and_delete (FILE * fp, int compressed) |
406 | { |
413 | { |
407 | fclose (fp); |
414 | fclose (fp); |
408 | } |
415 | } |
409 | |
416 | |
410 | /* |
417 | /* |
… | |
… | |
423 | |
430 | |
424 | while ((cp = strchr (cp + 1, (int) '/'))) |
431 | while ((cp = strchr (cp + 1, (int) '/'))) |
425 | { |
432 | { |
426 | *cp = '\0'; |
433 | *cp = '\0'; |
427 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
434 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
428 | { |
435 | { |
429 | if (mkdir (buf, SAVE_DIR_MODE)) |
436 | if (mkdir (buf, SAVE_DIR_MODE)) |
430 | { |
437 | { |
431 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
438 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
432 | return; |
439 | return; |
433 | } |
440 | } |
434 | } |
441 | } |
435 | *cp = '/'; |
442 | *cp = '/'; |
436 | } |
443 | } |
437 | } |
444 | } |