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.6 by root, Tue Sep 12 23:22:32 2006 UTC vs.
Revision 1.10 by root, Thu Dec 14 22:45:40 2006 UTC

1
2/*
3 * static char *rcsid_porting_c =
4 * "$Id: porting.C,v 1.6 2006/09/12 23:22:32 root Exp $";
5 */
6
7/* 1/*
8 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
9 3
10 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
11 Copyright (C) 1992 Frank Tore Johansen 5 Copyright (C) 1992 Frank Tore Johansen
22 16
23 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software 18 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 20
27 The authors can be reached via e-mail at crossfire-devel@real-time.com 21 The authors can be reached via e-mail at <crossfire@schmorp.de>
28*/ 22*/
29 23
30/* This file contains various functions that are not really unique for 24/* This file contains various functions that are not really unique for
31 * crossfire, but rather provides what should be standard functions 25 * crossfire, but rather provides what should be standard functions
32 * 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
33 * nasty system dependent stuff is contained here, with the program 27 * nasty system dependent stuff is contained here, with the program
34 * calling these functions. 28 * calling these functions.
35 */ 29 */
36 30
37 31
38#ifdef WIN32 /* ---win32 exclude/include headers */
39# include "process.h"
40# define pid_t int /* we include it non global, because there is a redefinition in python.h */
41#else
42# include <ctype.h>
43# include <sys/stat.h>
44# include <sys/wait.h>
45
46# include <sys/param.h>
47# include <stdio.h>
48
49/* Need to pull in the HAVE_... values somehow */ 32/* Need to pull in the HAVE_... values somehow */
50 33
51/* win32 reminder: always put this in a ifndef win32 block */
52# include <autoconf.h> 34#include <autoconf.h>
53#endif
54 35
55 36#include <cstdio>
56#ifdef HAVE_STDLIB_H
57# include <stdlib.h> 37#include <cstdlib>
58#endif 38#include <cstdarg>
59 39
60#ifdef HAVE_UNISTD_H 40#include <cctype>
41
42#include <sys/stat.h>
43#include <sys/wait.h>
44
45#include <sys/param.h>
46
61# include <unistd.h> 47#include <unistd.h>
62#endif
63
64#include <stdarg.h>
65 48
66/* 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 */
67#include "global.h" 50#include "global.h"
68 51
69static unsigned int curtmp = 0; 52static unsigned int curtmp = 0;
70
71/*****************************************************************************
72 * File related functions
73 ****************************************************************************/
74
75/*
76 * A replacement for the tempnam() function since it's not defined
77 * at some unix variants.
78 */
79
80char *
81tempnam_local (const char *dir, const char *pfx)
82{
83 char *name;
84 pid_t pid = getpid ();
85
86/* HURD does not have a hard limit, but we do */
87#ifndef MAXPATHLEN
88# define MAXPATHLEN 4096
89#endif
90
91 if (!(name = (char *) malloc (MAXPATHLEN)))
92 return (NULL);
93
94 if (!pfx)
95 pfx = "cftmp.";
96
97 /* This is a pretty simple method - put the pid as a hex digit and
98 * just keep incrementing the last digit. Check to see if the file
99 * already exists - if so, we'll just keep looking - eventually we should
100 * find one that is free.
101 */
102 if (dir != NULL)
103 {
104 do
105 {
106#ifdef HAVE_SNPRINTF
107 (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp);
108#else
109 (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp);
110#endif
111 curtmp++;
112 }
113 while (access (name, F_OK) != -1);
114 return (name);
115 }
116 return (NULL);
117}
118
119
120 53
121/* This function removes everything in the directory. */ 54/* This function removes everything in the directory. */
122void 55void
123remove_directory (const char *path) 56remove_directory (const char *path)
124{ 57{
162 { 95 {
163 LOG (llevError, "Unable to remove directory %s\n", path); 96 LOG (llevError, "Unable to remove directory %s\n", path);
164 } 97 }
165} 98}
166 99
167#if defined(sgi)
168
169# include <stdio.h>
170# include <stdlib.h>
171# include <string.h>
172
173# define popen fixed_popen
174
175FILE *
176popen_local (const char *command, const char *type)
177{
178 int fd[2];
179 int pd;
180 FILE *ret;
181
182 if (!strcmp (type, "r"))
183 pd = STDOUT_FILENO;
184 else if (!strcmp (type, "w"))
185 pd = STDIN_FILENO;
186 else
187 return NULL;
188
189 if (pipe (fd) != -1)
190 {
191 switch (fork ())
192 {
193 case -1:
194 close (fd[0]);
195 close (fd[1]);
196 break;
197 case 0:
198 close (fd[0]);
199 if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd))
200 {
201 if (fd[1] != pd)
202 {
203 close (fd[1]);
204 }
205 execl ("/bin/sh", "sh", "-c", command, NULL);
206 close (pd);
207 }
208 _exit (1);
209 break;
210 default:
211 close (fd[1]);
212 if (ret = fdopen (fd[0], type))
213 {
214 return ret;
215 }
216 close (fd[0]);
217 break;
218 }
219 }
220 return NULL;
221}
222
223#endif /* defined(sgi) */
224
225
226/*****************************************************************************
227 * String related function
228 ****************************************************************************/
229
230/*
231 * A replacement of strdup(), since it's not defined at some
232 * unix variants.
233 */
234char *
235strdup_local (const char *str)
236{
237 char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1));
238
239 strcpy (c, str);
240 return c;
241}
242
243
244#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 100#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
245islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 101islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
246#define MBASE ('z' - 'a' + 1 + 10) 102#define MBASE ('z' - 'a' + 1 + 10)
247
248/* This seems to be lacking on some system */
249#if !defined(HAVE_STRNCASECMP)
250int
251strncasecmp (const char *s1, const char *s2, int n)
252{
253 register int c1, c2;
254
255 while (*s1 && *s2 && n)
256 {
257 c1 = tolower (*s1);
258 c2 = tolower (*s2);
259 if (c1 != c2)
260 return (c1 - c2);
261 s1++;
262 s2++;
263 n--;
264 }
265 if (!n)
266 return (0);
267 return (int) (*s1 - *s2);
268}
269#endif
270
271#if !defined(HAVE_STRCASECMP)
272int
273strcasecmp (const char *s1, const char *s2)
274{
275 register int c1, c2;
276
277 while (*s1 && *s2)
278 {
279 c1 = tolower (*s1);
280 c2 = tolower (*s2);
281 if (c1 != c2)
282 return (c1 - c2);
283 s1++;
284 s2++;
285 }
286 if (*s1 == '\0' && *s2 == '\0')
287 return 0;
288 return (int) (*s1 - *s2);
289}
290#endif
291 103
292char * 104char *
293strcasestr_local (const char *s, const char *find) 105strcasestr_local (const char *s, const char *find)
294{ 106{
295 char c, sc; 107 char c, sc;
311 while (strncasecmp (s, find, len) != 0); 123 while (strncasecmp (s, find, len) != 0);
312 s--; 124 s--;
313 } 125 }
314 return (char *) s; 126 return (char *) s;
315} 127}
316
317#if !defined(HAVE_SNPRINTF)
318
319int
320snprintf (char *dest, int max, const char *format, ...)
321{
322 va_list var;
323 int ret;
324
325 va_start (var, format);
326 ret = vsprintf (dest, format, var);
327 va_end (var);
328 if (ret > max)
329 abort ();
330
331 return ret;
332}
333#endif
334
335 128
336/* 129/*
337 * Based on (n+1)^2 = n^2 + 2n + 1 130 * Based on (n+1)^2 = n^2 + 2n + 1
338 * given that 1^2 = 1, then 131 * given that 1^2 = 1, then
339 * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 132 * 2^2 = 1 + (2 + 1) = 1 + 3 = 4
357 ++result; 150 ++result;
358 } 151 }
359 return result; 152 return result;
360} 153}
361 154
362
363/* 155/*
364 * returns a char-pointer to a static array, in which a representation 156 * returns a char-pointer to a static array, in which a representation
365 * of the decimal number given will be stored. 157 * of the decimal number given will be stored.
366 */ 158 */
367
368char * 159char *
369ltostr10 (signed long n) 160ltostr10 (signed long n)
370{ 161{
371 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 162 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
372 character for the trailing nul character */ 163 character for the trailing nul character */
388 * then it opens it and returns the file-pointer. 179 * then it opens it and returns the file-pointer.
389 */ 180 */
390FILE * 181FILE *
391open_and_uncompress (const char *name, int flag, int *compressed) 182open_and_uncompress (const char *name, int flag, int *compressed)
392{ 183{
393 size_t i;
394 FILE *fp;
395
396 *compressed = 0; 184 *compressed = 0;
397 return fopen (name, "r"); 185 return fopen (name, "r");
398} 186}
399 187
400/* 188/*

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines