1 |
elmex |
1.1 |
/* |
2 |
|
|
CrossFire, A Multiplayer game for X-windows |
3 |
|
|
|
4 |
|
|
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 |
|
|
Copyright (C) 1992 Frank Tore Johansen |
6 |
|
|
|
7 |
|
|
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 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
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 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
|
|
21 |
root |
1.7 |
The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 |
elmex |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
/* This file contains various functions that are not really unique for |
25 |
|
|
* crossfire, but rather provides what should be standard functions |
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 |
28 |
|
|
* calling these functions. |
29 |
|
|
*/ |
30 |
|
|
|
31 |
|
|
|
32 |
root |
1.10 |
/* Need to pull in the HAVE_... values somehow */ |
33 |
elmex |
1.1 |
|
34 |
root |
1.10 |
#include <autoconf.h> |
35 |
elmex |
1.1 |
|
36 |
root |
1.10 |
#include <cstdio> |
37 |
|
|
#include <cstdlib> |
38 |
|
|
#include <cstdarg> |
39 |
root |
1.5 |
|
40 |
root |
1.10 |
#include <cctype> |
41 |
elmex |
1.1 |
|
42 |
root |
1.10 |
#include <sys/stat.h> |
43 |
|
|
#include <sys/wait.h> |
44 |
elmex |
1.1 |
|
45 |
root |
1.10 |
#include <sys/param.h> |
46 |
elmex |
1.1 |
|
47 |
root |
1.10 |
#include <unistd.h> |
48 |
root |
1.5 |
|
49 |
elmex |
1.1 |
/* Has to be after above includes so we don't redefine some values */ |
50 |
|
|
#include "global.h" |
51 |
|
|
|
52 |
|
|
static unsigned int curtmp = 0; |
53 |
|
|
|
54 |
|
|
/* This function removes everything in the directory. */ |
55 |
root |
1.4 |
void |
56 |
|
|
remove_directory (const char *path) |
57 |
elmex |
1.1 |
{ |
58 |
root |
1.4 |
DIR *dirp; |
59 |
|
|
char buf[MAX_BUF]; |
60 |
|
|
struct stat statbuf; |
61 |
|
|
int status; |
62 |
|
|
|
63 |
|
|
if ((dirp = opendir (path)) != NULL) |
64 |
|
|
{ |
65 |
|
|
struct dirent *de; |
66 |
|
|
|
67 |
|
|
for (de = readdir (dirp); de; de = readdir (dirp)) |
68 |
root |
1.5 |
{ |
69 |
|
|
/* Don't remove '.' or '..' In theory we should do a better |
70 |
|
|
* check for .., but the directories we are removing are fairly |
71 |
|
|
* limited and should not have dot files in them. |
72 |
|
|
*/ |
73 |
|
|
if (de->d_name[0] == '.') |
74 |
|
|
continue; |
75 |
|
|
|
76 |
|
|
/* Linux actually has a type field in the dirent structure, |
77 |
|
|
* but that is not portable - stat should be portable |
78 |
|
|
*/ |
79 |
|
|
status = stat (de->d_name, &statbuf); |
80 |
|
|
if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
81 |
|
|
{ |
82 |
|
|
sprintf (buf, "%s/%s", path, de->d_name); |
83 |
|
|
remove_directory (buf); |
84 |
|
|
continue; |
85 |
|
|
} |
86 |
|
|
sprintf (buf, "%s/%s", path, de->d_name); |
87 |
|
|
if (unlink (buf)) |
88 |
|
|
{ |
89 |
|
|
LOG (llevError, "Unable to remove directory %s\n", path); |
90 |
|
|
} |
91 |
|
|
} |
92 |
root |
1.4 |
closedir (dirp); |
93 |
|
|
} |
94 |
|
|
if (rmdir (path)) |
95 |
|
|
{ |
96 |
|
|
LOG (llevError, "Unable to remove directory %s\n", path); |
97 |
elmex |
1.1 |
} |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
101 |
|
|
islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
102 |
|
|
#define MBASE ('z' - 'a' + 1 + 10) |
103 |
|
|
|
104 |
root |
1.4 |
char * |
105 |
|
|
strcasestr_local (const char *s, const char *find) |
106 |
elmex |
1.1 |
{ |
107 |
root |
1.4 |
char c, sc; |
108 |
|
|
size_t len; |
109 |
elmex |
1.1 |
|
110 |
root |
1.4 |
if ((c = *find++) != 0) |
111 |
|
|
{ |
112 |
|
|
c = tolower (c); |
113 |
|
|
len = strlen (find); |
114 |
|
|
do |
115 |
root |
1.5 |
{ |
116 |
|
|
do |
117 |
|
|
{ |
118 |
|
|
if ((sc = *s++) == 0) |
119 |
|
|
return NULL; |
120 |
|
|
} |
121 |
|
|
while (tolower (sc) != c); |
122 |
|
|
} |
123 |
root |
1.4 |
while (strncasecmp (s, find, len) != 0); |
124 |
|
|
s--; |
125 |
|
|
} |
126 |
|
|
return (char *) s; |
127 |
elmex |
1.1 |
} |
128 |
|
|
|
129 |
|
|
/* |
130 |
|
|
* Based on (n+1)^2 = n^2 + 2n + 1 |
131 |
|
|
* given that 1^2 = 1, then |
132 |
|
|
* 2^2 = 1 + (2 + 1) = 1 + 3 = 4 |
133 |
|
|
* 3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9 |
134 |
|
|
* 4^2 = 9 + (6 + 1) = 9 + 7 = 1 + 3 + 5 + 7 = 16 |
135 |
|
|
* ... |
136 |
|
|
* In other words, a square number can be express as the sum of the |
137 |
|
|
* series n^2 = 1 + 3 + ... + (2n-1) |
138 |
|
|
*/ |
139 |
|
|
int |
140 |
root |
1.4 |
isqrt (int n) |
141 |
elmex |
1.1 |
{ |
142 |
root |
1.4 |
int result, sum, prev; |
143 |
root |
1.5 |
|
144 |
root |
1.4 |
result = 0; |
145 |
|
|
prev = sum = 1; |
146 |
|
|
while (sum <= n) |
147 |
|
|
{ |
148 |
|
|
prev += 2; |
149 |
|
|
sum += prev; |
150 |
|
|
++result; |
151 |
|
|
} |
152 |
|
|
return result; |
153 |
elmex |
1.1 |
} |
154 |
|
|
|
155 |
|
|
/* |
156 |
|
|
* returns a char-pointer to a static array, in which a representation |
157 |
|
|
* of the decimal number given will be stored. |
158 |
|
|
*/ |
159 |
root |
1.4 |
char * |
160 |
|
|
ltostr10 (signed long n) |
161 |
|
|
{ |
162 |
root |
1.5 |
static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
163 |
|
|
character for the trailing nul character */ |
164 |
root |
1.4 |
snprintf (buf, sizeof (buf), "%ld", n); |
165 |
elmex |
1.1 |
return buf; |
166 |
|
|
} |
167 |
root |
1.4 |
|
168 |
|
|
char * |
169 |
|
|
doubletostr10 (double v) |
170 |
|
|
{ |
171 |
elmex |
1.1 |
static char tbuf[200]; |
172 |
root |
1.5 |
|
173 |
root |
1.4 |
sprintf (tbuf, "%f", v); |
174 |
elmex |
1.1 |
return tbuf; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
/** |
178 |
|
|
* open_and_uncompress() first searches for the original filename. If it exist, |
179 |
|
|
* then it opens it and returns the file-pointer. |
180 |
|
|
*/ |
181 |
root |
1.4 |
FILE * |
182 |
|
|
open_and_uncompress (const char *name, int flag, int *compressed) |
183 |
|
|
{ |
184 |
|
|
*compressed = 0; |
185 |
|
|
return fopen (name, "r"); |
186 |
elmex |
1.1 |
} |
187 |
|
|
|
188 |
|
|
/* |
189 |
|
|
* See open_and_uncompress(). |
190 |
|
|
*/ |
191 |
|
|
|
192 |
root |
1.4 |
void |
193 |
root |
1.5 |
close_and_delete (FILE * fp, int compressed) |
194 |
root |
1.4 |
{ |
195 |
|
|
fclose (fp); |
196 |
elmex |
1.1 |
} |
197 |
|
|
|
198 |
|
|
/* |
199 |
|
|
* If any directories in the given path doesn't exist, they are created. |
200 |
|
|
*/ |
201 |
|
|
|
202 |
root |
1.4 |
void |
203 |
|
|
make_path_to_file (char *filename) |
204 |
elmex |
1.1 |
{ |
205 |
root |
1.4 |
char buf[MAX_BUF], *cp = buf; |
206 |
|
|
struct stat statbuf; |
207 |
elmex |
1.1 |
|
208 |
root |
1.4 |
if (!filename || !*filename) |
209 |
|
|
return; |
210 |
|
|
strcpy (buf, filename); |
211 |
|
|
|
212 |
|
|
while ((cp = strchr (cp + 1, (int) '/'))) |
213 |
|
|
{ |
214 |
|
|
*cp = '\0'; |
215 |
|
|
if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
216 |
root |
1.5 |
{ |
217 |
|
|
if (mkdir (buf, SAVE_DIR_MODE)) |
218 |
|
|
{ |
219 |
|
|
LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
220 |
|
|
return; |
221 |
|
|
} |
222 |
|
|
} |
223 |
root |
1.4 |
*cp = '/'; |
224 |
elmex |
1.1 |
} |
225 |
|
|
} |