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.9 by pippijn, Mon Dec 11 19:46:46 2006 UTC vs.
Revision 1.17 by root, Mon May 28 21:21:40 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * Crossfire TRT is free software; you can redistribute it and/or modify it
8 it under the terms of the GNU General Public License as published by 9 * under the terms of the GNU General Public License as published by the Free
9 the Free Software Foundation; either version 2 of the License, or 10 * Software Foundation; either version 2 of the License, or (at your option)
10 (at your option) any later version. 11 * any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful, but
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 GNU General Public License for more details. 16 * for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License along
18 along with this program; if not, write to the Free Software 19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <crossfire@schmorp.de>
22*/ 23 */
23 24
24/* This file contains various functions that are not really unique for 25/* This file contains various functions that are not really unique for
25 * crossfire, but rather provides what should be standard functions 26 * crossfire, but rather provides what should be standard functions
26 * for systems that do not have them. In this way, most of the 27 * for systems that do not have them. In this way, most of the
27 * nasty system dependent stuff is contained here, with the program 28 * nasty system dependent stuff is contained here, with the program
28 * calling these functions. 29 * calling these functions.
29 */ 30 */
30 31
31 32
33/* Need to pull in the HAVE_... values somehow */
34
35#include <autoconf.h>
36
37#include <cstdio>
38#include <cstdlib>
39#include <cstdarg>
40
32#include <ctype.h> 41#include <cctype>
42
33#include <sys/stat.h> 43#include <sys/stat.h>
34#include <sys/wait.h> 44#include <sys/wait.h>
35 45
36#include <sys/param.h> 46#include <sys/param.h>
37#include <stdio.h>
38 47
39/* Need to pull in the HAVE_... values somehow */
40
41#include <autoconf.h>
42
43
44#ifdef HAVE_STDLIB_H
45# include <stdlib.h>
46#endif
47
48#ifdef HAVE_UNISTD_H
49# include <unistd.h> 48#include <unistd.h>
50#endif
51
52#include <stdarg.h>
53 49
54/* Has to be after above includes so we don't redefine some values */ 50/* Has to be after above includes so we don't redefine some values */
55#include "global.h" 51#include "global.h"
56
57static unsigned int curtmp = 0;
58
59/*****************************************************************************
60 * File related functions
61 ****************************************************************************/
62
63/*
64 * A replacement for the tempnam() function since it's not defined
65 * at some unix variants.
66 */
67
68char *
69tempnam_local (const char *dir, const char *pfx)
70{
71 char *name;
72 pid_t pid = getpid ();
73
74/* HURD does not have a hard limit, but we do */
75#ifndef MAXPATHLEN
76# define MAXPATHLEN 4096
77#endif
78
79 if (!(name = (char *) malloc (MAXPATHLEN)))
80 return (NULL);
81
82 if (!pfx)
83 pfx = "cftmp.";
84
85 /* This is a pretty simple method - put the pid as a hex digit and
86 * just keep incrementing the last digit. Check to see if the file
87 * already exists - if so, we'll just keep looking - eventually we should
88 * find one that is free.
89 */
90 if (dir != NULL)
91 {
92 do
93 {
94#ifdef HAVE_SNPRINTF
95 (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp);
96#else
97 (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp);
98#endif
99 curtmp++;
100 }
101 while (access (name, F_OK) != -1);
102 return (name);
103 }
104 return (NULL);
105}
106
107
108 52
109/* This function removes everything in the directory. */ 53/* This function removes everything in the directory. */
110void 54void
111remove_directory (const char *path) 55remove_directory (const char *path)
112{ 56{
150 { 94 {
151 LOG (llevError, "Unable to remove directory %s\n", path); 95 LOG (llevError, "Unable to remove directory %s\n", path);
152 } 96 }
153} 97}
154 98
155#if defined(sgi)
156
157# include <stdio.h>
158# include <stdlib.h>
159# include <string.h>
160
161# define popen fixed_popen
162
163FILE *
164popen_local (const char *command, const char *type)
165{
166 int fd[2];
167 int pd;
168 FILE *ret;
169
170 if (!strcmp (type, "r"))
171 pd = STDOUT_FILENO;
172 else if (!strcmp (type, "w"))
173 pd = STDIN_FILENO;
174 else
175 return NULL;
176
177 if (pipe (fd) != -1)
178 {
179 switch (fork ())
180 {
181 case -1:
182 close (fd[0]);
183 close (fd[1]);
184 break;
185 case 0:
186 close (fd[0]);
187 if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd))
188 {
189 if (fd[1] != pd)
190 {
191 close (fd[1]);
192 }
193 execl ("/bin/sh", "sh", "-c", command, NULL);
194 close (pd);
195 }
196 _exit (1);
197 break;
198 default:
199 close (fd[1]);
200 if (ret = fdopen (fd[0], type))
201 {
202 return ret;
203 }
204 close (fd[0]);
205 break;
206 }
207 }
208 return NULL;
209}
210
211#endif /* defined(sgi) */
212
213
214/*****************************************************************************
215 * String related function
216 ****************************************************************************/
217
218/*
219 * A replacement of strdup(), since it's not defined at some
220 * unix variants.
221 */
222char *
223strdup_local (const char *str)
224{
225 char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1));
226
227 strcpy (c, str);
228 return c;
229}
230
231
232#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 99#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
233islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 100islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
234#define MBASE ('z' - 'a' + 1 + 10) 101#define MBASE ('z' - 'a' + 1 + 10)
235
236/* This seems to be lacking on some system */
237#if !defined(HAVE_STRNCASECMP)
238int
239strncasecmp (const char *s1, const char *s2, int n)
240{
241 register int c1, c2;
242
243 while (*s1 && *s2 && n)
244 {
245 c1 = tolower (*s1);
246 c2 = tolower (*s2);
247 if (c1 != c2)
248 return (c1 - c2);
249 s1++;
250 s2++;
251 n--;
252 }
253 if (!n)
254 return (0);
255 return (int) (*s1 - *s2);
256}
257#endif
258
259#if !defined(HAVE_STRCASECMP)
260int
261strcasecmp (const char *s1, const char *s2)
262{
263 register int c1, c2;
264
265 while (*s1 && *s2)
266 {
267 c1 = tolower (*s1);
268 c2 = tolower (*s2);
269 if (c1 != c2)
270 return (c1 - c2);
271 s1++;
272 s2++;
273 }
274 if (*s1 == '\0' && *s2 == '\0')
275 return 0;
276 return (int) (*s1 - *s2);
277}
278#endif
279 102
280char * 103char *
281strcasestr_local (const char *s, const char *find) 104strcasestr_local (const char *s, const char *find)
282{ 105{
283 char c, sc; 106 char c, sc;
300 s--; 123 s--;
301 } 124 }
302 return (char *) s; 125 return (char *) s;
303} 126}
304 127
305#if !defined(HAVE_SNPRINTF)
306
307int
308snprintf (char *dest, int max, const char *format, ...)
309{
310 va_list var;
311 int ret;
312
313 va_start (var, format);
314 ret = vsprintf (dest, format, var);
315 va_end (var);
316 if (ret > max)
317 abort ();
318
319 return ret;
320}
321#endif
322
323
324/*
325 * Based on (n+1)^2 = n^2 + 2n + 1
326 * given that 1^2 = 1, then
327 * 2^2 = 1 + (2 + 1) = 1 + 3 = 4
328 * 3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9
329 * 4^2 = 9 + (6 + 1) = 9 + 7 = 1 + 3 + 5 + 7 = 16
330 * ...
331 * In other words, a square number can be express as the sum of the
332 * series n^2 = 1 + 3 + ... + (2n-1)
333 */
334int
335isqrt (int n)
336{
337 int result, sum, prev;
338
339 result = 0;
340 prev = sum = 1;
341 while (sum <= n)
342 {
343 prev += 2;
344 sum += prev;
345 ++result;
346 }
347 return result;
348}
349
350
351/* 128/*
352 * returns a char-pointer to a static array, in which a representation 129 * returns a char-pointer to a static array, in which a representation
353 * of the decimal number given will be stored. 130 * of the decimal number given will be stored.
354 */ 131 */
355
356char * 132char *
357ltostr10 (signed long n) 133ltostr10 (signed long n)
358{ 134{
359 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 135 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
360 character for the trailing nul character */ 136 character for the trailing nul character */
402 char buf[MAX_BUF], *cp = buf; 178 char buf[MAX_BUF], *cp = buf;
403 struct stat statbuf; 179 struct stat statbuf;
404 180
405 if (!filename || !*filename) 181 if (!filename || !*filename)
406 return; 182 return;
183
407 strcpy (buf, filename); 184 assign (buf, filename);
408 185
409 while ((cp = strchr (cp + 1, (int) '/'))) 186 while ((cp = strchr (cp + 1, (int) '/')))
410 { 187 {
411 *cp = '\0'; 188 *cp = '\0';
412 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) 189 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines