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.10 by root, Thu Dec 14 22:45:40 2006 UTC

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 51
57static unsigned int curtmp = 0; 52static 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 53
109/* This function removes everything in the directory. */ 54/* This function removes everything in the directory. */
110void 55void
111remove_directory (const char *path) 56remove_directory (const char *path)
112{ 57{
150 { 95 {
151 LOG (llevError, "Unable to remove directory %s\n", path); 96 LOG (llevError, "Unable to remove directory %s\n", path);
152 } 97 }
153} 98}
154 99
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' : \ 100#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
233islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 101islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
234#define MBASE ('z' - 'a' + 1 + 10) 102#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 103
280char * 104char *
281strcasestr_local (const char *s, const char *find) 105strcasestr_local (const char *s, const char *find)
282{ 106{
283 char c, sc; 107 char c, sc;
299 while (strncasecmp (s, find, len) != 0); 123 while (strncasecmp (s, find, len) != 0);
300 s--; 124 s--;
301 } 125 }
302 return (char *) s; 126 return (char *) s;
303} 127}
304
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 128
324/* 129/*
325 * Based on (n+1)^2 = n^2 + 2n + 1 130 * Based on (n+1)^2 = n^2 + 2n + 1
326 * given that 1^2 = 1, then 131 * given that 1^2 = 1, then
327 * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 132 * 2^2 = 1 + (2 + 1) = 1 + 3 = 4
345 ++result; 150 ++result;
346 } 151 }
347 return result; 152 return result;
348} 153}
349 154
350
351/* 155/*
352 * 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
353 * of the decimal number given will be stored. 157 * of the decimal number given will be stored.
354 */ 158 */
355
356char * 159char *
357ltostr10 (signed long n) 160ltostr10 (signed long n)
358{ 161{
359 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
360 character for the trailing nul character */ 163 character for the trailing nul character */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines