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