ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/porting.C
(Generate patch)

Comparing deliantra/server/common/porting.C (file contents):
Revision 1.3 by root, Wed Aug 30 16:30:37 2006 UTC vs.
Revision 1.4 by root, Thu Sep 7 09:28:44 2006 UTC

1/* 1/*
2 * static char *rcsid_porting_c = 2 * static char *rcsid_porting_c =
3 * "$Id: porting.C,v 1.3 2006/08/30 16:30:37 root Exp $"; 3 * "$Id: porting.C,v 1.4 2006/09/07 09:28:44 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
32 * nasty system dependent stuff is contained here, with the program 32 * nasty system dependent stuff is contained here, with the program
33 * calling these functions. 33 * calling these functions.
34 */ 34 */
35 35
36 36
37#ifdef WIN32 /* ---win32 exclude/include headers */ 37#ifdef WIN32 /* ---win32 exclude/include headers */
38#include "process.h" 38#include "process.h"
39#define pid_t int /* we include it non global, because there is a redefinition in python.h */ 39#define pid_t int /* we include it non global, because there is a redefinition in python.h */
40#else 40#else
41#include <ctype.h> 41#include <ctype.h>
42#include <sys/stat.h> 42#include <sys/stat.h>
43#include <sys/wait.h> 43#include <sys/wait.h>
44 44
72/* 72/*
73 * A replacement for the tempnam() function since it's not defined 73 * A replacement for the tempnam() function since it's not defined
74 * at some unix variants. 74 * at some unix variants.
75 */ 75 */
76 76
77char *
77char *tempnam_local(const char *dir, const char *pfx) 78tempnam_local (const char *dir, const char *pfx)
78{ 79{
79 char *name; 80 char *name;
80 pid_t pid=getpid(); 81 pid_t pid = getpid ();
81 82
82/* HURD does not have a hard limit, but we do */ 83/* HURD does not have a hard limit, but we do */
83#ifndef MAXPATHLEN 84#ifndef MAXPATHLEN
84#define MAXPATHLEN 4096 85#define MAXPATHLEN 4096
85#endif 86#endif
86 87
87 if (!(name = (char *) malloc(MAXPATHLEN))) 88 if (!(name = (char *) malloc (MAXPATHLEN)))
88 return(NULL); 89 return (NULL);
89 90
90 if (!pfx) 91 if (!pfx)
91 pfx = "cftmp."; 92 pfx = "cftmp.";
92 93
93 /* This is a pretty simple method - put the pid as a hex digit and 94 /* This is a pretty simple method - put the pid as a hex digit and
94 * just keep incrementing the last digit. Check to see if the file 95 * just keep incrementing the last digit. Check to see if the file
95 * already exists - if so, we'll just keep looking - eventually we should 96 * already exists - if so, we'll just keep looking - eventually we should
96 * find one that is free. 97 * find one that is free.
97 */ 98 */
98 if (dir!=NULL) { 99 if (dir != NULL)
100 {
99 do { 101 do
102 {
100#ifdef HAVE_SNPRINTF 103#ifdef HAVE_SNPRINTF
101 (void)snprintf(name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); 104 (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp);
102#else 105#else
103 (void)sprintf(name,"%s/%s%hx%d", dir, pfx, pid, curtmp); 106 (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp);
104#endif 107#endif
105 curtmp++; 108 curtmp++;
109 }
106 } while (access(name, F_OK)!=-1); 110 while (access (name, F_OK) != -1);
107 return(name); 111 return (name);
108 } 112 }
109 return(NULL); 113 return (NULL);
110} 114}
111 115
112 116
113 117
114/* This function removes everything in the directory. */ 118/* This function removes everything in the directory. */
119void
115void remove_directory(const char *path) 120remove_directory (const char *path)
116{ 121{
117 DIR *dirp; 122 DIR *dirp;
118 char buf[MAX_BUF]; 123 char buf[MAX_BUF];
119 struct stat statbuf; 124 struct stat statbuf;
120 int status; 125 int status;
121 126
122 if ((dirp=opendir(path))!=NULL) { 127 if ((dirp = opendir (path)) != NULL)
128 {
123 struct dirent *de; 129 struct dirent *de;
124 130
125 for (de=readdir(dirp); de; de = readdir(dirp)) { 131 for (de = readdir (dirp); de; de = readdir (dirp))
132 {
126 /* Don't remove '.' or '..' In theory we should do a better 133 /* Don't remove '.' or '..' In theory we should do a better
127 * check for .., but the directories we are removing are fairly 134 * check for .., but the directories we are removing are fairly
128 * limited and should not have dot files in them. 135 * limited and should not have dot files in them.
129 */ 136 */
130 if (de->d_name[0] == '.') continue; 137 if (de->d_name[0] == '.')
138 continue;
131 139
132 /* Linux actually has a type field in the dirent structure, 140 /* Linux actually has a type field in the dirent structure,
133 * but that is not portable - stat should be portable 141 * but that is not portable - stat should be portable
134 */ 142 */
135 status=stat(de->d_name, &statbuf); 143 status = stat (de->d_name, &statbuf);
136 if ((status!=-1) && (S_ISDIR(statbuf.st_mode))) { 144 if ((status != -1) && (S_ISDIR (statbuf.st_mode)))
137 sprintf(buf,"%s/%s", path, de->d_name); 145 {
138 remove_directory(buf);
139 continue;
140 }
141 sprintf(buf,"%s/%s", path, de->d_name); 146 sprintf (buf, "%s/%s", path, de->d_name);
142 if (unlink(buf)) { 147 remove_directory (buf);
148 continue;
149 }
150 sprintf (buf, "%s/%s", path, de->d_name);
151 if (unlink (buf))
152 {
143 LOG(llevError,"Unable to remove directory %s\n", path); 153 LOG (llevError, "Unable to remove directory %s\n", path);
144 } 154 }
145 } 155 }
146 closedir(dirp); 156 closedir (dirp);
147 } 157 }
148 if (rmdir(path)) { 158 if (rmdir (path))
159 {
149 LOG(llevError,"Unable to remove directory %s\n", path); 160 LOG (llevError, "Unable to remove directory %s\n", path);
150 } 161 }
151} 162}
152 163
153#if defined(sgi) 164#if defined(sgi)
154 165
156#include <stdlib.h> 167#include <stdlib.h>
157#include <string.h> 168#include <string.h>
158 169
159#define popen fixed_popen 170#define popen fixed_popen
160 171
172FILE *
161FILE *popen_local(const char *command, const char *type) 173popen_local (const char *command, const char *type)
162{ 174{
163 int fd[2]; 175 int fd[2];
164 int pd; 176 int pd;
165 FILE *ret; 177 FILE *ret;
166 if (!strcmp(type,"r")) 178 if (!strcmp (type, "r"))
167 { 179 {
168 pd=STDOUT_FILENO; 180 pd = STDOUT_FILENO;
169 } 181 }
170 else if (!strcmp(type,"w")) 182 else if (!strcmp (type, "w"))
171 { 183 {
172 pd=STDIN_FILENO; 184 pd = STDIN_FILENO;
173 } 185 }
174 else 186 else
175 { 187 {
176 return NULL;
177 }
178 if (pipe(fd)!=-1)
179 {
180 switch (fork())
181 {
182 case -1:
183 close(fd[0]);
184 close(fd[1]);
185 break;
186 case 0:
187 close(fd[0]);
188 if ((fd[1]==pd)||(dup2(fd[1],pd)==pd))
189 {
190 if (fd[1]!=pd)
191 {
192 close(fd[1]);
193 }
194 execl("/bin/sh","sh","-c",command,NULL);
195 close(pd);
196 }
197 exit(1);
198 break;
199 default:
200 close(fd[1]);
201 if (ret=fdopen(fd[0],type))
202 {
203 return ret;
204 }
205 close(fd[0]);
206 break;
207 }
208 }
209 return NULL; 188 return NULL;
189 }
190 if (pipe (fd) != -1)
191 {
192 switch (fork ())
193 {
194 case -1:
195 close (fd[0]);
196 close (fd[1]);
197 break;
198 case 0:
199 close (fd[0]);
200 if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd))
201 {
202 if (fd[1] != pd)
203 {
204 close (fd[1]);
205 }
206 execl ("/bin/sh", "sh", "-c", command, NULL);
207 close (pd);
208 }
209 exit (1);
210 break;
211 default:
212 close (fd[1]);
213 if (ret = fdopen (fd[0], type))
214 {
215 return ret;
216 }
217 close (fd[0]);
218 break;
219 }
220 }
221 return NULL;
210} 222}
211 223
212#endif /* defined(sgi) */ 224#endif /* defined(sgi) */
213 225
214 226
220 232
221/* 233/*
222 * A replacement of strdup(), since it's not defined at some 234 * A replacement of strdup(), since it's not defined at some
223 * unix variants. 235 * unix variants.
224 */ 236 */
237char *
225char *strdup_local(const char *str) { 238strdup_local (const char *str)
239{
226 char *c=(char *)malloc(sizeof(char)*(strlen(str)+1)); 240 char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1));
227 strcpy(c,str); 241 strcpy (c, str);
228 return c; 242 return c;
229} 243}
230 244
231 245
232#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 246#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
233islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 247islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
234#define MBASE ('z' - 'a' + 1 + 10) 248#define MBASE ('z' - 'a' + 1 + 10)
235 249
236/* This seems to be lacking on some system */ 250/* This seems to be lacking on some system */
237#if !defined(HAVE_STRNCASECMP) 251#if !defined(HAVE_STRNCASECMP)
252int
238int strncasecmp(const char *s1, const char *s2, int n) 253strncasecmp (const char *s1, const char *s2, int n)
239{ 254{
240 register int c1, c2; 255 register int c1, c2;
241 256
242 while (*s1 && *s2 && n) { 257 while (*s1 && *s2 && n)
258 {
243 c1 = tolower(*s1); 259 c1 = tolower (*s1);
244 c2 = tolower(*s2); 260 c2 = tolower (*s2);
245 if (c1 != c2) 261 if (c1 != c2)
246 return (c1 - c2); 262 return (c1 - c2);
247 s1++; 263 s1++;
248 s2++; 264 s2++;
249 n--; 265 n--;
250 } 266 }
251 if (!n) 267 if (!n)
252 return(0); 268 return (0);
253 return (int) (*s1 - *s2); 269 return (int) (*s1 - *s2);
254} 270}
255#endif 271#endif
256 272
257#if !defined(HAVE_STRCASECMP) 273#if !defined(HAVE_STRCASECMP)
274int
258int strcasecmp(const char *s1, const char*s2) 275strcasecmp (const char *s1, const char *s2)
259{ 276{
260 register int c1, c2; 277 register int c1, c2;
261 278
262 while (*s1 && *s2) { 279 while (*s1 && *s2)
280 {
263 c1 = tolower(*s1); 281 c1 = tolower (*s1);
264 c2 = tolower(*s2); 282 c2 = tolower (*s2);
265 if (c1 != c2) 283 if (c1 != c2)
266 return (c1 - c2); 284 return (c1 - c2);
267 s1++; 285 s1++;
268 s2++; 286 s2++;
269 } 287 }
270 if (*s1=='\0' && *s2=='\0') 288 if (*s1 == '\0' && *s2 == '\0')
271 return 0; 289 return 0;
272 return (int) (*s1 - *s2); 290 return (int) (*s1 - *s2);
273} 291}
274#endif 292#endif
275 293
294char *
276char *strcasestr_local(const char *s, const char *find) 295strcasestr_local (const char *s, const char *find)
277{ 296{
278 char c, sc; 297 char c, sc;
279 size_t len; 298 size_t len;
280 299
281 if ((c = *find++) != 0) { 300 if ((c = *find++) != 0)
301 {
282 c = tolower(c); 302 c = tolower (c);
283 len = strlen(find); 303 len = strlen (find);
284 do { 304 do
285 do { 305 {
306 do
307 {
286 if ((sc = *s++) == 0) 308 if ((sc = *s++) == 0)
287 return NULL; 309 return NULL;
310 }
288 } while (tolower(sc) != c); 311 while (tolower (sc) != c);
312 }
289 } while (strncasecmp(s, find, len) != 0); 313 while (strncasecmp (s, find, len) != 0);
290 s--; 314 s--;
291 } 315 }
292 return (char *)s; 316 return (char *) s;
293} 317}
294 318
295#if !defined(HAVE_SNPRINTF) 319#if !defined(HAVE_SNPRINTF)
296 320
321int
297int snprintf(char *dest, int max, const char *format, ...) 322snprintf (char *dest, int max, const char *format, ...)
298{ 323{
299 va_list var; 324 va_list var;
300 int ret; 325 int ret;
301 326
302 va_start(var, format); 327 va_start (var, format);
303 ret = vsprintf(dest, format, var); 328 ret = vsprintf (dest, format, var);
304 va_end(var); 329 va_end (var);
305 if (ret > max) abort(); 330 if (ret > max)
331 abort ();
306 332
307 return ret; 333 return ret;
308} 334}
309#endif 335#endif
310 336
311 337
312/* 338/*
318 * ... 344 * ...
319 * In other words, a square number can be express as the sum of the 345 * In other words, a square number can be express as the sum of the
320 * series n^2 = 1 + 3 + ... + (2n-1) 346 * series n^2 = 1 + 3 + ... + (2n-1)
321 */ 347 */
322int 348int
323isqrt(int n) 349isqrt (int n)
324{ 350{
325 int result, sum, prev; 351 int result, sum, prev;
326 result = 0; 352 result = 0;
327 prev = sum = 1; 353 prev = sum = 1;
328 while (sum <= n) { 354 while (sum <= n)
355 {
329 prev += 2; 356 prev += 2;
330 sum += prev; 357 sum += prev;
331 ++result; 358 ++result;
332 } 359 }
333 return result; 360 return result;
334} 361}
335 362
336 363
337/* 364/*
338 * returns a char-pointer to a static array, in which a representation 365 * returns a char-pointer to a static array, in which a representation
339 * of the decimal number given will be stored. 366 * of the decimal number given will be stored.
340 */ 367 */
341 368
369char *
342char *ltostr10(signed long n) { 370ltostr10 (signed long n)
371{
343 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 372 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
344 character for the trailing nul character */ 373 character for the trailing nul character */
345 snprintf(buf, sizeof(buf), "%ld", n); 374 snprintf (buf, sizeof (buf), "%ld", n);
346 return buf; 375 return buf;
347} 376}
377
378char *
348char *doubletostr10(double v){ 379doubletostr10 (double v)
380{
349 static char tbuf[200]; 381 static char tbuf[200];
350 sprintf(tbuf,"%f",v); 382 sprintf (tbuf, "%f", v);
351 return tbuf; 383 return tbuf;
352}
353
354/**
355 * Open and possibly uncompress a file.
356 *
357 * @param ext the extension if the file is compressed.
358 *
359 * @param uncompressor the command to uncompress the file if the file is
360 * compressed.
361 *
362 * @param name the base file name without compression extension
363 *
364 * @param flag only used for compressed files: if set, uncompress and open the
365 * file; if unset, uncompress the file via pipe
366 *
367 * @param *compressed set to zero if the file was uncompressed
368 */
369static FILE *open_and_uncompress_file(const char *ext, const char *uncompressor, const char *name, int flag, int *compressed) {
370 struct stat st;
371 char buf[MAX_BUF];
372 char buf2[MAX_BUF];
373 int ret;
374
375 if (ext == NULL) {
376 ext = "";
377 }
378
379 if (strlen(name)+strlen(ext) >= sizeof(buf)) {
380 errno = ENAMETOOLONG; /* File name too long */
381 return NULL;
382 }
383 sprintf(buf, "%s%s", name, ext);
384
385 if (stat(buf, &st) != 0) {
386 return NULL;
387 }
388
389 if (!S_ISREG(st.st_mode)) {
390 errno = EISDIR; /* Not a regular file */
391 return NULL;
392 }
393
394 if (uncompressor == NULL) {
395 /* open without uncompression */
396
397 return fopen(buf, "r");
398 }
399
400 /* The file name buf (and its substring name) is passed as an argument to a
401 * shell command, therefore check for characters that could confuse the
402 * shell.
403 */
404 if (strpbrk(buf, "'\\\r\n") != NULL) {
405 errno = ENOENT; /* Pretend the file does not exist */
406 return NULL;
407 }
408
409 if (!flag) {
410 /* uncompress via pipe */
411
412 if (strlen(uncompressor)+4+strlen(buf)+1 >= sizeof(buf2)) {
413 errno = ENAMETOOLONG; /* File name too long */
414 return NULL;
415 }
416 sprintf(buf2, "%s < '%s'", uncompressor, buf);
417
418 return popen(buf2, "r");
419 }
420
421 /* remove compression from file, then open file */
422
423 if (stat(name, &st) == 0 && !S_ISREG(st.st_mode)) {
424 errno = EISDIR;
425 return NULL;
426 }
427
428 if (strlen(uncompressor)+4+strlen(buf)+5+strlen(name)+1 >= sizeof(buf2)) {
429 errno = ENAMETOOLONG; /* File name too long */
430 return NULL;
431 }
432 sprintf(buf2, "%s < '%s' > '%s'", uncompressor, buf, name);
433
434 ret = system(buf2);
435 if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
436 LOG(llevError, "system(%s) returned %d\n", buf2, ret);
437 errno = ENOENT;
438 return NULL;
439 }
440
441 unlink(buf); /* Delete the original */
442 *compressed = 0; /* Change to "uncompressed file" */
443 chmod(name, st.st_mode); /* Copy access mode from compressed file */
444
445 return fopen(name, "r");
446} 384}
447 385
448/** 386/**
449 * open_and_uncompress() first searches for the original filename. If it exist, 387 * open_and_uncompress() first searches for the original filename. If it exist,
450 * then it opens it and returns the file-pointer. 388 * then it opens it and returns the file-pointer.
451 * 389 */
452 * If not, it does two things depending on the flag. If the flag is set, it 390FILE *
453 * tries to create the original file by appending a compression suffix to name
454 * and uncompressing it. If the flag is not set, it creates a pipe that is used
455 * for reading the file (NOTE - you can not use fseek on pipes).
456 *
457 * The compressed pointer is set to nonzero if the file is compressed (and
458 * thus, fp is actually a pipe.) It returns 0 if it is a normal file.
459 */
460FILE *open_and_uncompress(const char *name, int flag, int *compressed) { 391open_and_uncompress (const char *name, int flag, int *compressed)
392{
461 size_t i; 393 size_t i;
462 FILE *fp; 394 FILE *fp;
463 395
464 fp = open_and_uncompress_file(0,0, name, flag, compressed); 396 *compressed = 0;
465 if (fp != NULL) { 397 return fopen (name, "r");
466 return fp;
467 }
468
469 errno = ENOENT;
470 return NULL;
471} 398}
472 399
473/* 400/*
474 * See open_and_uncompress(). 401 * See open_and_uncompress().
475 */ 402 */
476 403
404void
477void close_and_delete(FILE *fp, int compressed) { 405close_and_delete (FILE *fp, int compressed)
478 if (compressed) 406{
479 pclose(fp);
480 else
481 fclose(fp); 407 fclose (fp);
482} 408}
483 409
484/* 410/*
485 * If any directories in the given path doesn't exist, they are created. 411 * If any directories in the given path doesn't exist, they are created.
486 */ 412 */
487 413
414void
488void make_path_to_file (char *filename) 415make_path_to_file (char *filename)
489{ 416{
490 char buf[MAX_BUF], *cp = buf; 417 char buf[MAX_BUF], *cp = buf;
491 struct stat statbuf; 418 struct stat statbuf;
492 419
493 if (!filename || !*filename) 420 if (!filename || !*filename)
494 return; 421 return;
495 strcpy (buf, filename); 422 strcpy (buf, filename);
496 423
497 while ((cp = strchr (cp + 1, (int) '/'))) { 424 while ((cp = strchr (cp + 1, (int) '/')))
425 {
498 *cp = '\0'; 426 *cp = '\0';
499 if (stat(buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) { 427 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode))
428 {
500 if (mkdir (buf, SAVE_DIR_MODE)) { 429 if (mkdir (buf, SAVE_DIR_MODE))
430 {
501 LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror(errno)); 431 LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno));
502 return; 432 return;
503 } 433 }
504 } 434 }
505 *cp = '/'; 435 *cp = '/';
506 } 436 }
507} 437}
508

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines