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.8 by pippijn, Sat Dec 9 17:28:37 2006 UTC vs.
Revision 1.11 by pippijn, Sat Jan 6 14:42:29 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
3 3
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 Copyright (C) 1992 Frank Tore Johansen
6 7
7 This program is free software; you can redistribute it and/or modify 8 This program 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
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
32#ifdef WIN32 /* ---win32 exclude/include headers */
33# include "process.h"
34# define pid_t int /* we include it non global, because there is a redefinition in python.h */
35#else
36# include <ctype.h>
37# include <sys/stat.h>
38# include <sys/wait.h>
39
40# include <sys/param.h>
41# include <stdio.h>
42
43/* Need to pull in the HAVE_... values somehow */ 33/* Need to pull in the HAVE_... values somehow */
44 34
45/* win32 reminder: always put this in a ifndef win32 block */
46# include <autoconf.h> 35#include <autoconf.h>
47#endif
48 36
49 37#include <cstdio>
50#ifdef HAVE_STDLIB_H
51# include <stdlib.h> 38#include <cstdlib>
52#endif 39#include <cstdarg>
53 40
54#ifdef HAVE_UNISTD_H 41#include <cctype>
42
43#include <sys/stat.h>
44#include <sys/wait.h>
45
46#include <sys/param.h>
47
55# include <unistd.h> 48#include <unistd.h>
56#endif
57
58#include <stdarg.h>
59 49
60/* 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 */
61#include "global.h" 51#include "global.h"
62 52
63static unsigned int curtmp = 0; 53static unsigned int curtmp = 0;
64
65/*****************************************************************************
66 * File related functions
67 ****************************************************************************/
68
69/*
70 * A replacement for the tempnam() function since it's not defined
71 * at some unix variants.
72 */
73
74char *
75tempnam_local (const char *dir, const char *pfx)
76{
77 char *name;
78 pid_t pid = getpid ();
79
80/* HURD does not have a hard limit, but we do */
81#ifndef MAXPATHLEN
82# define MAXPATHLEN 4096
83#endif
84
85 if (!(name = (char *) malloc (MAXPATHLEN)))
86 return (NULL);
87
88 if (!pfx)
89 pfx = "cftmp.";
90
91 /* This is a pretty simple method - put the pid as a hex digit and
92 * just keep incrementing the last digit. Check to see if the file
93 * already exists - if so, we'll just keep looking - eventually we should
94 * find one that is free.
95 */
96 if (dir != NULL)
97 {
98 do
99 {
100#ifdef HAVE_SNPRINTF
101 (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp);
102#else
103 (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp);
104#endif
105 curtmp++;
106 }
107 while (access (name, F_OK) != -1);
108 return (name);
109 }
110 return (NULL);
111}
112
113
114 54
115/* This function removes everything in the directory. */ 55/* This function removes everything in the directory. */
116void 56void
117remove_directory (const char *path) 57remove_directory (const char *path)
118{ 58{
156 { 96 {
157 LOG (llevError, "Unable to remove directory %s\n", path); 97 LOG (llevError, "Unable to remove directory %s\n", path);
158 } 98 }
159} 99}
160 100
161#if defined(sgi)
162
163# include <stdio.h>
164# include <stdlib.h>
165# include <string.h>
166
167# define popen fixed_popen
168
169FILE *
170popen_local (const char *command, const char *type)
171{
172 int fd[2];
173 int pd;
174 FILE *ret;
175
176 if (!strcmp (type, "r"))
177 pd = STDOUT_FILENO;
178 else if (!strcmp (type, "w"))
179 pd = STDIN_FILENO;
180 else
181 return NULL;
182
183 if (pipe (fd) != -1)
184 {
185 switch (fork ())
186 {
187 case -1:
188 close (fd[0]);
189 close (fd[1]);
190 break;
191 case 0:
192 close (fd[0]);
193 if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd))
194 {
195 if (fd[1] != pd)
196 {
197 close (fd[1]);
198 }
199 execl ("/bin/sh", "sh", "-c", command, NULL);
200 close (pd);
201 }
202 _exit (1);
203 break;
204 default:
205 close (fd[1]);
206 if (ret = fdopen (fd[0], type))
207 {
208 return ret;
209 }
210 close (fd[0]);
211 break;
212 }
213 }
214 return NULL;
215}
216
217#endif /* defined(sgi) */
218
219
220/*****************************************************************************
221 * String related function
222 ****************************************************************************/
223
224/*
225 * A replacement of strdup(), since it's not defined at some
226 * unix variants.
227 */
228char *
229strdup_local (const char *str)
230{
231 char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1));
232
233 strcpy (c, str);
234 return c;
235}
236
237
238#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 101#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
239islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 102islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
240#define MBASE ('z' - 'a' + 1 + 10) 103#define MBASE ('z' - 'a' + 1 + 10)
241
242/* This seems to be lacking on some system */
243#if !defined(HAVE_STRNCASECMP)
244int
245strncasecmp (const char *s1, const char *s2, int n)
246{
247 register int c1, c2;
248
249 while (*s1 && *s2 && n)
250 {
251 c1 = tolower (*s1);
252 c2 = tolower (*s2);
253 if (c1 != c2)
254 return (c1 - c2);
255 s1++;
256 s2++;
257 n--;
258 }
259 if (!n)
260 return (0);
261 return (int) (*s1 - *s2);
262}
263#endif
264
265#if !defined(HAVE_STRCASECMP)
266int
267strcasecmp (const char *s1, const char *s2)
268{
269 register int c1, c2;
270
271 while (*s1 && *s2)
272 {
273 c1 = tolower (*s1);
274 c2 = tolower (*s2);
275 if (c1 != c2)
276 return (c1 - c2);
277 s1++;
278 s2++;
279 }
280 if (*s1 == '\0' && *s2 == '\0')
281 return 0;
282 return (int) (*s1 - *s2);
283}
284#endif
285 104
286char * 105char *
287strcasestr_local (const char *s, const char *find) 106strcasestr_local (const char *s, const char *find)
288{ 107{
289 char c, sc; 108 char c, sc;
305 while (strncasecmp (s, find, len) != 0); 124 while (strncasecmp (s, find, len) != 0);
306 s--; 125 s--;
307 } 126 }
308 return (char *) s; 127 return (char *) s;
309} 128}
310
311#if !defined(HAVE_SNPRINTF)
312
313int
314snprintf (char *dest, int max, const char *format, ...)
315{
316 va_list var;
317 int ret;
318
319 va_start (var, format);
320 ret = vsprintf (dest, format, var);
321 va_end (var);
322 if (ret > max)
323 abort ();
324
325 return ret;
326}
327#endif
328
329 129
330/* 130/*
331 * Based on (n+1)^2 = n^2 + 2n + 1 131 * Based on (n+1)^2 = n^2 + 2n + 1
332 * given that 1^2 = 1, then 132 * given that 1^2 = 1, then
333 * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 133 * 2^2 = 1 + (2 + 1) = 1 + 3 = 4
351 ++result; 151 ++result;
352 } 152 }
353 return result; 153 return result;
354} 154}
355 155
356
357/* 156/*
358 * returns a char-pointer to a static array, in which a representation 157 * returns a char-pointer to a static array, in which a representation
359 * of the decimal number given will be stored. 158 * of the decimal number given will be stored.
360 */ 159 */
361
362char * 160char *
363ltostr10 (signed long n) 161ltostr10 (signed long n)
364{ 162{
365 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 163 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
366 character for the trailing nul character */ 164 character for the trailing nul character */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines