1 | /* |
1 | /* |
2 | * static char *rcsid_porting_c = |
2 | * static char *rcsid_porting_c = |
3 | * "$Id: porting.C,v 1.1 2006/08/13 17:16:00 elmex Exp $"; |
3 | * "$Id: porting.C,v 1.3 2006/08/30 16:30:37 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
63 | /* Has to be after above includes so we don't redefine some values */ |
63 | /* Has to be after above includes so we don't redefine some values */ |
64 | #include "global.h" |
64 | #include "global.h" |
65 | |
65 | |
66 | static unsigned int curtmp = 0; |
66 | static unsigned int curtmp = 0; |
67 | |
67 | |
68 | /* This is a list of the suffix, uncompress and compress functions. Thus, |
|
|
69 | * if you have some other compress program you want to use, the only thing |
|
|
70 | * that needs to be done is to extended this. |
|
|
71 | * The first entry must be NULL - this is what is used for non |
|
|
72 | * compressed files. |
|
|
73 | */ |
|
|
74 | EXTERN char *uncomp[NROF_COMPRESS_METHODS][3] = { |
|
|
75 | {NULL, NULL, NULL}, |
|
|
76 | {".Z", UNCOMPRESS, COMPRESS}, |
|
|
77 | {".gz", GUNZIP, GZIP}, |
|
|
78 | {".bz2", BUNZIP, BZIP} |
|
|
79 | }; |
|
|
80 | |
|
|
81 | /***************************************************************************** |
68 | /***************************************************************************** |
82 | * File related functions |
69 | * File related functions |
83 | ****************************************************************************/ |
70 | ****************************************************************************/ |
84 | |
71 | |
85 | /* |
72 | /* |
… | |
… | |
96 | #ifndef MAXPATHLEN |
83 | #ifndef MAXPATHLEN |
97 | #define MAXPATHLEN 4096 |
84 | #define MAXPATHLEN 4096 |
98 | #endif |
85 | #endif |
99 | |
86 | |
100 | if (!(name = (char *) malloc(MAXPATHLEN))) |
87 | if (!(name = (char *) malloc(MAXPATHLEN))) |
101 | return(NULL); |
88 | return(NULL); |
102 | |
89 | |
103 | if (!pfx) |
90 | if (!pfx) |
104 | pfx = "cftmp."; |
91 | pfx = "cftmp."; |
105 | |
92 | |
106 | /* This is a pretty simple method - put the pid as a hex digit and |
93 | /* This is a pretty simple method - put the pid as a hex digit and |
107 | * just keep incrementing the last digit. Check to see if the file |
94 | * just keep incrementing the last digit. Check to see if the file |
108 | * already exists - if so, we'll just keep looking - eventually we should |
95 | * already exists - if so, we'll just keep looking - eventually we should |
109 | * find one that is free. |
96 | * find one that is free. |
110 | */ |
97 | */ |
111 | if (dir!=NULL) { |
98 | if (dir!=NULL) { |
112 | do { |
99 | do { |
113 | #ifdef HAVE_SNPRINTF |
100 | #ifdef HAVE_SNPRINTF |
114 | (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); |
115 | #else |
102 | #else |
116 | (void)sprintf(name,"%s/%s%hx%d", dir, pfx, pid, curtmp); |
103 | (void)sprintf(name,"%s/%s%hx%d", dir, pfx, pid, curtmp); |
117 | #endif |
104 | #endif |
118 | curtmp++; |
105 | curtmp++; |
119 | } while (access(name, F_OK)!=-1); |
106 | } while (access(name, F_OK)!=-1); |
120 | return(name); |
107 | return(name); |
121 | } |
108 | } |
122 | return(NULL); |
109 | return(NULL); |
123 | } |
110 | } |
124 | |
111 | |
125 | |
112 | |
… | |
… | |
131 | char buf[MAX_BUF]; |
118 | char buf[MAX_BUF]; |
132 | struct stat statbuf; |
119 | struct stat statbuf; |
133 | int status; |
120 | int status; |
134 | |
121 | |
135 | if ((dirp=opendir(path))!=NULL) { |
122 | if ((dirp=opendir(path))!=NULL) { |
136 | struct dirent *de; |
123 | struct dirent *de; |
137 | |
124 | |
138 | for (de=readdir(dirp); de; de = readdir(dirp)) { |
125 | for (de=readdir(dirp); de; de = readdir(dirp)) { |
139 | /* Don't remove '.' or '..' In theory we should do a better |
126 | /* Don't remove '.' or '..' In theory we should do a better |
140 | * check for .., but the directories we are removing are fairly |
127 | * check for .., but the directories we are removing are fairly |
141 | * limited and should not have dot files in them. |
128 | * limited and should not have dot files in them. |
142 | */ |
129 | */ |
143 | if (de->d_name[0] == '.') continue; |
130 | if (de->d_name[0] == '.') continue; |
144 | |
131 | |
145 | /* Linux actually has a type field in the dirent structure, |
132 | /* Linux actually has a type field in the dirent structure, |
146 | * but that is not portable - stat should be portable |
133 | * but that is not portable - stat should be portable |
147 | */ |
134 | */ |
148 | status=stat(de->d_name, &statbuf); |
135 | status=stat(de->d_name, &statbuf); |
149 | if ((status!=-1) && (S_ISDIR(statbuf.st_mode))) { |
136 | if ((status!=-1) && (S_ISDIR(statbuf.st_mode))) { |
150 | sprintf(buf,"%s/%s", path, de->d_name); |
137 | sprintf(buf,"%s/%s", path, de->d_name); |
151 | remove_directory(buf); |
138 | remove_directory(buf); |
152 | continue; |
139 | continue; |
153 | } |
140 | } |
154 | sprintf(buf,"%s/%s", path, de->d_name); |
141 | sprintf(buf,"%s/%s", path, de->d_name); |
155 | if (unlink(buf)) { |
142 | if (unlink(buf)) { |
156 | LOG(llevError,"Unable to remove directory %s\n", path); |
143 | LOG(llevError,"Unable to remove directory %s\n", path); |
157 | } |
144 | } |
158 | } |
145 | } |
159 | closedir(dirp); |
146 | closedir(dirp); |
160 | } |
147 | } |
161 | if (rmdir(path)) { |
148 | if (rmdir(path)) { |
162 | LOG(llevError,"Unable to remove directory %s\n", path); |
149 | LOG(llevError,"Unable to remove directory %s\n", path); |
163 | } |
150 | } |
164 | } |
151 | } |
165 | |
152 | |
166 | #if defined(sgi) |
153 | #if defined(sgi) |
167 | |
154 | |
… | |
… | |
171 | |
158 | |
172 | #define popen fixed_popen |
159 | #define popen fixed_popen |
173 | |
160 | |
174 | FILE *popen_local(const char *command, const char *type) |
161 | FILE *popen_local(const char *command, const char *type) |
175 | { |
162 | { |
176 | int fd[2]; |
163 | int fd[2]; |
177 | int pd; |
164 | int pd; |
178 | FILE *ret; |
165 | FILE *ret; |
179 | if (!strcmp(type,"r")) |
166 | if (!strcmp(type,"r")) |
180 | { |
167 | { |
181 | pd=STDOUT_FILENO; |
168 | pd=STDOUT_FILENO; |
182 | } |
169 | } |
183 | else if (!strcmp(type,"w")) |
170 | else if (!strcmp(type,"w")) |
184 | { |
171 | { |
185 | pd=STDIN_FILENO; |
172 | pd=STDIN_FILENO; |
186 | } |
173 | } |
187 | else |
174 | else |
188 | { |
175 | { |
189 | return NULL; |
176 | return NULL; |
190 | } |
177 | } |
191 | if (pipe(fd)!=-1) |
178 | if (pipe(fd)!=-1) |
192 | { |
179 | { |
193 | switch (fork()) |
180 | switch (fork()) |
194 | { |
181 | { |
195 | case -1: |
182 | case -1: |
196 | close(fd[0]); |
183 | close(fd[0]); |
197 | close(fd[1]); |
184 | close(fd[1]); |
198 | break; |
185 | break; |
199 | case 0: |
186 | case 0: |
200 | close(fd[0]); |
187 | close(fd[0]); |
201 | if ((fd[1]==pd)||(dup2(fd[1],pd)==pd)) |
188 | if ((fd[1]==pd)||(dup2(fd[1],pd)==pd)) |
202 | { |
189 | { |
203 | if (fd[1]!=pd) |
190 | if (fd[1]!=pd) |
204 | { |
191 | { |
205 | close(fd[1]); |
192 | close(fd[1]); |
206 | } |
193 | } |
207 | execl("/bin/sh","sh","-c",command,NULL); |
194 | execl("/bin/sh","sh","-c",command,NULL); |
208 | close(pd); |
195 | close(pd); |
209 | } |
196 | } |
210 | exit(1); |
197 | exit(1); |
211 | break; |
198 | break; |
212 | default: |
199 | default: |
213 | close(fd[1]); |
200 | close(fd[1]); |
214 | if (ret=fdopen(fd[0],type)) |
201 | if (ret=fdopen(fd[0],type)) |
215 | { |
202 | { |
216 | return ret; |
203 | return ret; |
217 | } |
204 | } |
218 | close(fd[0]); |
205 | close(fd[0]); |
219 | break; |
206 | break; |
220 | } |
207 | } |
221 | } |
208 | } |
222 | return NULL; |
209 | return NULL; |
223 | } |
210 | } |
224 | |
211 | |
225 | #endif /* defined(sgi) */ |
212 | #endif /* defined(sgi) */ |
226 | |
213 | |
227 | |
214 | |
… | |
… | |
279 | return (c1 - c2); |
266 | return (c1 - c2); |
280 | s1++; |
267 | s1++; |
281 | s2++; |
268 | s2++; |
282 | } |
269 | } |
283 | if (*s1=='\0' && *s2=='\0') |
270 | if (*s1=='\0' && *s2=='\0') |
284 | return 0; |
271 | return 0; |
285 | return (int) (*s1 - *s2); |
272 | return (int) (*s1 - *s2); |
286 | } |
273 | } |
287 | #endif |
274 | #endif |
288 | |
275 | |
289 | char *strcasestr_local(const char *s, const char *find) |
276 | char *strcasestr_local(const char *s, const char *find) |
290 | { |
277 | { |
291 | char c, sc; |
278 | char c, sc; |
292 | size_t len; |
279 | size_t len; |
293 | |
280 | |
294 | if ((c = *find++) != 0) { |
281 | if ((c = *find++) != 0) { |
295 | c = tolower(c); |
282 | c = tolower(c); |
296 | len = strlen(find); |
283 | len = strlen(find); |
297 | do { |
284 | do { |
298 | do { |
285 | do { |
299 | if ((sc = *s++) == 0) |
286 | if ((sc = *s++) == 0) |
300 | return NULL; |
287 | return NULL; |
… | |
… | |
320 | return ret; |
307 | return ret; |
321 | } |
308 | } |
322 | #endif |
309 | #endif |
323 | |
310 | |
324 | |
311 | |
325 | /* This takes an err number and returns a string with a description of |
|
|
326 | * the error. |
|
|
327 | */ |
|
|
328 | char *strerror_local(int errnum) |
|
|
329 | { |
|
|
330 | #if defined(HAVE_STRERROR) |
|
|
331 | return(strerror(errnum)); |
|
|
332 | #else |
|
|
333 | return("strerror_local not implemented"); |
|
|
334 | #endif |
|
|
335 | } |
|
|
336 | |
|
|
337 | /* |
312 | /* |
338 | * Based on (n+1)^2 = n^2 + 2n + 1 |
313 | * Based on (n+1)^2 = n^2 + 2n + 1 |
339 | * given that 1^2 = 1, then |
314 | * given that 1^2 = 1, then |
340 | * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 |
315 | * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 |
341 | * 3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9 |
316 | * 3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9 |
… | |
… | |
345 | * series n^2 = 1 + 3 + ... + (2n-1) |
320 | * series n^2 = 1 + 3 + ... + (2n-1) |
346 | */ |
321 | */ |
347 | int |
322 | int |
348 | isqrt(int n) |
323 | isqrt(int n) |
349 | { |
324 | { |
350 | int result, sum, prev; |
325 | int result, sum, prev; |
351 | result = 0; |
326 | result = 0; |
352 | prev = sum = 1; |
327 | prev = sum = 1; |
353 | while (sum <= n) { |
328 | while (sum <= n) { |
354 | prev += 2; |
329 | prev += 2; |
355 | sum += prev; |
330 | sum += prev; |
356 | ++result; |
331 | ++result; |
357 | } |
332 | } |
358 | return result; |
333 | return result; |
359 | } |
334 | } |
360 | |
335 | |
361 | |
336 | |
362 | /* |
337 | /* |
363 | * returns a char-pointer to a static array, in which a representation |
338 | * returns a char-pointer to a static array, in which a representation |
364 | * of the decimal number given will be stored. |
339 | * of the decimal number given will be stored. |
365 | */ |
340 | */ |
366 | |
341 | |
367 | char *ltostr10(signed long n) { |
342 | char *ltostr10(signed long n) { |
368 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
343 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
369 | character for the trailing nul character */ |
344 | character for the trailing nul character */ |
370 | snprintf(buf, sizeof(buf), "%ld", n); |
345 | snprintf(buf, sizeof(buf), "%ld", n); |
371 | return buf; |
346 | return buf; |
372 | } |
347 | } |
373 | char *doubletostr10(double v){ |
348 | char *doubletostr10(double v){ |
374 | static char tbuf[200]; |
349 | static char tbuf[200]; |
375 | sprintf(tbuf,"%f",v); |
350 | sprintf(tbuf,"%f",v); |
376 | return tbuf; |
351 | return tbuf; |
377 | } |
|
|
378 | |
|
|
379 | /* |
|
|
380 | * A fast routine which appends the name and decimal number specified |
|
|
381 | * to the given buffer. |
|
|
382 | * Could be faster, though, if the strcat()s at the end could be changed |
|
|
383 | * into alternate strcat which returned a pointer to the _end_, not the |
|
|
384 | * start! |
|
|
385 | * |
|
|
386 | * Hey good news, it IS faster now, according to changes in get_ob_diff |
|
|
387 | * Completly redone prototype and made define in loader.l. See changes there. |
|
|
388 | * Didn't touch those for speed reason (don't use them anymore) . |
|
|
389 | * Tchize |
|
|
390 | */ |
|
|
391 | |
|
|
392 | void save_long(char *buf, char *name, long n) { |
|
|
393 | char buf2[MAX_BUF]; |
|
|
394 | strcpy(buf2,name); |
|
|
395 | strcat(buf2," "); |
|
|
396 | strcat(buf2,ltostr10(n)); |
|
|
397 | strcat(buf2,"\n"); |
|
|
398 | strcat(buf,buf2); |
|
|
399 | } |
|
|
400 | |
|
|
401 | |
|
|
402 | |
|
|
403 | void save_long_long(char *buf, char *name, sint64 n) { |
|
|
404 | char buf2[MAX_BUF]; |
|
|
405 | |
|
|
406 | #ifndef WIN32 |
|
|
407 | sprintf(buf2,"%s %lld\n", name, n); |
|
|
408 | #else |
|
|
409 | sprintf(buf2,"%s %I64d\n", name, n); |
|
|
410 | #endif |
|
|
411 | strcat(buf,buf2); |
|
|
412 | } |
352 | } |
413 | |
353 | |
414 | /** |
354 | /** |
415 | * Open and possibly uncompress a file. |
355 | * Open and possibly uncompress a file. |
416 | * |
356 | * |
… | |
… | |
519 | */ |
459 | */ |
520 | FILE *open_and_uncompress(const char *name, int flag, int *compressed) { |
460 | FILE *open_and_uncompress(const char *name, int flag, int *compressed) { |
521 | size_t i; |
461 | size_t i; |
522 | FILE *fp; |
462 | FILE *fp; |
523 | |
463 | |
524 | for (i = 0; i < NROF_COMPRESS_METHODS; i++) { |
|
|
525 | *compressed = i; |
|
|
526 | fp = open_and_uncompress_file(uncomp[i][0], uncomp[i][1], name, flag, compressed); |
464 | fp = open_and_uncompress_file(0,0, name, flag, compressed); |
527 | if (fp != NULL) { |
465 | if (fp != NULL) { |
528 | return fp; |
466 | return fp; |
529 | } |
467 | } |
530 | } |
|
|
531 | |
468 | |
532 | errno = ENOENT; |
469 | errno = ENOENT; |
533 | return NULL; |
470 | return NULL; |
534 | } |
471 | } |
535 | |
472 | |
… | |
… | |
552 | { |
489 | { |
553 | char buf[MAX_BUF], *cp = buf; |
490 | char buf[MAX_BUF], *cp = buf; |
554 | struct stat statbuf; |
491 | struct stat statbuf; |
555 | |
492 | |
556 | if (!filename || !*filename) |
493 | if (!filename || !*filename) |
557 | return; |
494 | return; |
558 | strcpy (buf, filename); |
495 | strcpy (buf, filename); |
559 | |
496 | |
560 | while ((cp = strchr (cp + 1, (int) '/'))) { |
497 | while ((cp = strchr (cp + 1, (int) '/'))) { |
561 | *cp = '\0'; |
498 | *cp = '\0'; |
562 | if (stat(buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) { |
499 | if (stat(buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) { |
563 | if (mkdir (buf, SAVE_DIR_MODE)) { |
500 | if (mkdir (buf, SAVE_DIR_MODE)) { |
564 | LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror_local(errno)); |
501 | LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror(errno)); |
565 | return; |
502 | return; |
|
|
503 | } |
|
|
504 | } |
|
|
505 | *cp = '/'; |
566 | } |
506 | } |
567 | } |
|
|
568 | *cp = '/'; |
|
|
569 | } |
|
|
570 | } |
507 | } |
571 | |
508 | |