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.20 by root, Tue May 6 16:55:25 2008 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra 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 * Deliantra is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version. 11 * (at your option) 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,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License 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
18 along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 *
20 21 * The authors can be reached via e-mail to <support@deliantra.net>
21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 22 */
23 23
24/* This file contains various functions that are not really unique for 24/* This file contains various functions that are not really unique for
25 * crossfire, but rather provides what should be standard functions 25 * crossfire, but rather provides what should be standard functions
26 * for systems that do not have them. In this way, most of the 26 * for systems that do not have them. In this way, most of the
27 * nasty system dependent stuff is contained here, with the program 27 * nasty system dependent stuff is contained here, with the program
28 * calling these functions. 28 * calling these functions.
29 */ 29 */
30 30
31 31
32/* Need to pull in the HAVE_... values somehow */
33
34#include <autoconf.h>
35
36#include <cstdio>
37#include <cstdlib>
38#include <cstdarg>
39
32#include <ctype.h> 40#include <cctype>
41
33#include <sys/stat.h> 42#include <sys/stat.h>
34#include <sys/wait.h> 43#include <sys/wait.h>
35 44
36#include <sys/param.h> 45#include <sys/param.h>
37#include <stdio.h>
38 46
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> 47#include <unistd.h>
50#endif
51
52#include <stdarg.h>
53 48
54/* Has to be after above includes so we don't redefine some values */ 49/* Has to be after above includes so we don't redefine some values */
55#include "global.h" 50#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 51
109/* This function removes everything in the directory. */ 52/* This function removes everything in the directory. */
110void 53void
111remove_directory (const char *path) 54remove_directory (const char *path)
112{ 55{
150 { 93 {
151 LOG (llevError, "Unable to remove directory %s\n", path); 94 LOG (llevError, "Unable to remove directory %s\n", path);
152 } 95 }
153} 96}
154 97
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' : \ 98#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
233islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 99islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
234#define MBASE ('z' - 'a' + 1 + 10) 100#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 101
280char * 102char *
281strcasestr_local (const char *s, const char *find) 103strcasestr_local (const char *s, const char *find)
282{ 104{
283 char c, sc; 105 char c, sc;
300 s--; 122 s--;
301 } 123 }
302 return (char *) s; 124 return (char *) s;
303} 125}
304 126
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/* 127/*
352 * returns a char-pointer to a static array, in which a representation 128 * returns a char-pointer to a static array, in which a representation
353 * of the decimal number given will be stored. 129 * of the decimal number given will be stored.
354 */ 130 */
355
356char * 131char *
357ltostr10 (signed long n) 132ltostr10 (signed long n)
358{ 133{
359 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 134 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
360 character for the trailing nul character */ 135 character for the trailing nul character */
402 char buf[MAX_BUF], *cp = buf; 177 char buf[MAX_BUF], *cp = buf;
403 struct stat statbuf; 178 struct stat statbuf;
404 179
405 if (!filename || !*filename) 180 if (!filename || !*filename)
406 return; 181 return;
182
407 strcpy (buf, filename); 183 assign (buf, filename);
408 184
409 while ((cp = strchr (cp + 1, (int) '/'))) 185 while ((cp = strchr (cp + 1, (int) '/')))
410 { 186 {
411 *cp = '\0'; 187 *cp = '\0';
412 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) 188 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines