|
|
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.6 2006/09/12 23:22:32 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 | { |
|
|
180 | pd = STDOUT_FILENO; |
183 | pd = STDOUT_FILENO; |
181 | } |
|
|
182 | else if (!strcmp (type, "w")) |
184 | else if (!strcmp (type, "w")) |
183 | { |
|
|
184 | pd = STDIN_FILENO; |
185 | pd = STDIN_FILENO; |
185 | } |
|
|
186 | else |
186 | else |
187 | { |
|
|
188 | return NULL; |
187 | return NULL; |
189 | } |
188 | |
190 | if (pipe (fd) != -1) |
189 | if (pipe (fd) != -1) |
191 | { |
190 | { |
192 | switch (fork ()) |
191 | switch (fork ()) |
193 | { |
192 | { |
194 | case -1: |
193 | case -1: |
195 | close (fd[0]); |
194 | close (fd[0]); |
196 | close (fd[1]); |
195 | close (fd[1]); |
197 | break; |
196 | break; |
198 | case 0: |
197 | case 0: |
199 | close (fd[0]); |
198 | close (fd[0]); |
200 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
199 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
201 | { |
200 | { |
202 | if (fd[1] != pd) |
201 | if (fd[1] != pd) |
203 | { |
202 | { |
204 | close (fd[1]); |
203 | close (fd[1]); |
205 | } |
204 | } |
206 | execl ("/bin/sh", "sh", "-c", command, NULL); |
205 | execl ("/bin/sh", "sh", "-c", command, NULL); |
207 | close (pd); |
206 | close (pd); |
208 | } |
207 | } |
209 | exit (1); |
208 | _exit (1); |
210 | break; |
209 | break; |
211 | default: |
210 | default: |
212 | close (fd[1]); |
211 | close (fd[1]); |
213 | if (ret = fdopen (fd[0], type)) |
212 | if (ret = fdopen (fd[0], type)) |
214 | { |
213 | { |
215 | return ret; |
214 | return ret; |
216 | } |
215 | } |
217 | close (fd[0]); |
216 | close (fd[0]); |
218 | break; |
217 | break; |
219 | } |
218 | } |
220 | } |
219 | } |
221 | return NULL; |
220 | return NULL; |
222 | } |
221 | } |
223 | |
222 | |
224 | #endif /* defined(sgi) */ |
223 | #endif /* defined(sgi) */ |
… | |
… | |
226 | |
225 | |
227 | /***************************************************************************** |
226 | /***************************************************************************** |
228 | * String related function |
227 | * String related function |
229 | ****************************************************************************/ |
228 | ****************************************************************************/ |
230 | |
229 | |
231 | |
|
|
232 | |
|
|
233 | /* |
230 | /* |
234 | * A replacement of strdup(), since it's not defined at some |
231 | * A replacement of strdup(), since it's not defined at some |
235 | * unix variants. |
232 | * unix variants. |
236 | */ |
233 | */ |
237 | char * |
234 | char * |
238 | strdup_local (const char *str) |
235 | strdup_local (const char *str) |
239 | { |
236 | { |
240 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
237 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
|
|
238 | |
241 | strcpy (c, str); |
239 | strcpy (c, str); |
242 | return c; |
240 | return c; |
243 | } |
241 | } |
244 | |
242 | |
245 | |
243 | |
… | |
… | |
257 | while (*s1 && *s2 && n) |
255 | while (*s1 && *s2 && n) |
258 | { |
256 | { |
259 | c1 = tolower (*s1); |
257 | c1 = tolower (*s1); |
260 | c2 = tolower (*s2); |
258 | c2 = tolower (*s2); |
261 | if (c1 != c2) |
259 | if (c1 != c2) |
262 | return (c1 - c2); |
260 | return (c1 - c2); |
263 | s1++; |
261 | s1++; |
264 | s2++; |
262 | s2++; |
265 | n--; |
263 | n--; |
266 | } |
264 | } |
267 | if (!n) |
265 | if (!n) |
… | |
… | |
279 | while (*s1 && *s2) |
277 | while (*s1 && *s2) |
280 | { |
278 | { |
281 | c1 = tolower (*s1); |
279 | c1 = tolower (*s1); |
282 | c2 = tolower (*s2); |
280 | c2 = tolower (*s2); |
283 | if (c1 != c2) |
281 | if (c1 != c2) |
284 | return (c1 - c2); |
282 | return (c1 - c2); |
285 | s1++; |
283 | s1++; |
286 | s2++; |
284 | s2++; |
287 | } |
285 | } |
288 | if (*s1 == '\0' && *s2 == '\0') |
286 | if (*s1 == '\0' && *s2 == '\0') |
289 | return 0; |
287 | return 0; |
… | |
… | |
300 | if ((c = *find++) != 0) |
298 | if ((c = *find++) != 0) |
301 | { |
299 | { |
302 | c = tolower (c); |
300 | c = tolower (c); |
303 | len = strlen (find); |
301 | len = strlen (find); |
304 | do |
302 | do |
305 | { |
303 | { |
306 | do |
304 | do |
307 | { |
305 | { |
308 | if ((sc = *s++) == 0) |
306 | if ((sc = *s++) == 0) |
309 | return NULL; |
307 | return NULL; |
310 | } |
308 | } |
311 | while (tolower (sc) != c); |
309 | while (tolower (sc) != c); |
312 | } |
310 | } |
313 | while (strncasecmp (s, find, len) != 0); |
311 | while (strncasecmp (s, find, len) != 0); |
314 | s--; |
312 | s--; |
315 | } |
313 | } |
316 | return (char *) s; |
314 | return (char *) s; |
317 | } |
315 | } |
… | |
… | |
347 | */ |
345 | */ |
348 | int |
346 | int |
349 | isqrt (int n) |
347 | isqrt (int n) |
350 | { |
348 | { |
351 | int result, sum, prev; |
349 | int result, sum, prev; |
|
|
350 | |
352 | result = 0; |
351 | result = 0; |
353 | prev = sum = 1; |
352 | prev = sum = 1; |
354 | while (sum <= n) |
353 | while (sum <= n) |
355 | { |
354 | { |
356 | prev += 2; |
355 | prev += 2; |
… | |
… | |
367 | */ |
366 | */ |
368 | |
367 | |
369 | char * |
368 | char * |
370 | ltostr10 (signed long n) |
369 | ltostr10 (signed long n) |
371 | { |
370 | { |
372 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
371 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
373 | character for the trailing nul character */ |
372 | character for the trailing nul character */ |
374 | snprintf (buf, sizeof (buf), "%ld", n); |
373 | snprintf (buf, sizeof (buf), "%ld", n); |
375 | return buf; |
374 | return buf; |
376 | } |
375 | } |
377 | |
376 | |
378 | char * |
377 | char * |
379 | doubletostr10 (double v) |
378 | doubletostr10 (double v) |
380 | { |
379 | { |
381 | static char tbuf[200]; |
380 | static char tbuf[200]; |
|
|
381 | |
382 | sprintf (tbuf, "%f", v); |
382 | sprintf (tbuf, "%f", v); |
383 | return tbuf; |
383 | return tbuf; |
384 | } |
384 | } |
385 | |
385 | |
386 | /** |
386 | /** |
… | |
… | |
400 | /* |
400 | /* |
401 | * See open_and_uncompress(). |
401 | * See open_and_uncompress(). |
402 | */ |
402 | */ |
403 | |
403 | |
404 | void |
404 | void |
405 | close_and_delete (FILE *fp, int compressed) |
405 | close_and_delete (FILE * fp, int compressed) |
406 | { |
406 | { |
407 | fclose (fp); |
407 | fclose (fp); |
408 | } |
408 | } |
409 | |
409 | |
410 | /* |
410 | /* |
… | |
… | |
423 | |
423 | |
424 | while ((cp = strchr (cp + 1, (int) '/'))) |
424 | while ((cp = strchr (cp + 1, (int) '/'))) |
425 | { |
425 | { |
426 | *cp = '\0'; |
426 | *cp = '\0'; |
427 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
427 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
428 | { |
428 | { |
429 | if (mkdir (buf, SAVE_DIR_MODE)) |
429 | if (mkdir (buf, SAVE_DIR_MODE)) |
430 | { |
430 | { |
431 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
431 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
432 | return; |
432 | return; |
433 | } |
433 | } |
434 | } |
434 | } |
435 | *cp = '/'; |
435 | *cp = '/'; |
436 | } |
436 | } |
437 | } |
437 | } |