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.5 by root, Sun Sep 10 16:00:23 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines