1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (©) 1992,2007 Frank Tore Johansen |
7 |
* |
8 |
* Deliantra 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 3 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, see <http://www.gnu.org/licenses/>. |
20 |
* |
21 |
* The authors can be reached via e-mail to <support@deliantra.net> |
22 |
*/ |
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 |
/* Need to pull in the HAVE_... values somehow */ |
33 |
|
34 |
#include <autoconf.h> |
35 |
|
36 |
#include <cstdio> |
37 |
#include <cstdlib> |
38 |
#include <cstdarg> |
39 |
|
40 |
#include <cctype> |
41 |
|
42 |
#include <sys/stat.h> |
43 |
#include <sys/wait.h> |
44 |
|
45 |
#include <sys/param.h> |
46 |
|
47 |
#include <unistd.h> |
48 |
|
49 |
/* Has to be after above includes so we don't redefine some values */ |
50 |
#include "global.h" |
51 |
|
52 |
/* This function removes everything in the directory. */ |
53 |
void |
54 |
remove_directory (const char *path) |
55 |
{ |
56 |
DIR *dirp; |
57 |
char buf[MAX_BUF]; |
58 |
struct stat statbuf; |
59 |
int status; |
60 |
|
61 |
if ((dirp = opendir (path)) != NULL) |
62 |
{ |
63 |
struct dirent *de; |
64 |
|
65 |
for (de = readdir (dirp); de; de = readdir (dirp)) |
66 |
{ |
67 |
/* Don't remove '.' or '..' In theory we should do a better |
68 |
* check for .., but the directories we are removing are fairly |
69 |
* limited and should not have dot files in them. |
70 |
*/ |
71 |
if (de->d_name[0] == '.') |
72 |
continue; |
73 |
|
74 |
/* Linux actually has a type field in the dirent structure, |
75 |
* but that is not portable - stat should be portable |
76 |
*/ |
77 |
status = stat (de->d_name, &statbuf); |
78 |
if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
79 |
{ |
80 |
sprintf (buf, "%s/%s", path, de->d_name); |
81 |
remove_directory (buf); |
82 |
continue; |
83 |
} |
84 |
sprintf (buf, "%s/%s", path, de->d_name); |
85 |
if (unlink (buf)) |
86 |
{ |
87 |
LOG (llevError, "Unable to remove directory %s\n", path); |
88 |
} |
89 |
} |
90 |
closedir (dirp); |
91 |
} |
92 |
if (rmdir (path)) |
93 |
{ |
94 |
LOG (llevError, "Unable to remove directory %s\n", path); |
95 |
} |
96 |
} |
97 |
|
98 |
#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
99 |
islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
100 |
#define MBASE ('z' - 'a' + 1 + 10) |
101 |
|
102 |
char * |
103 |
strcasestr_local (const char *s, const char *find) |
104 |
{ |
105 |
char c, sc; |
106 |
size_t len; |
107 |
|
108 |
if ((c = *find++) != 0) |
109 |
{ |
110 |
c = tolower (c); |
111 |
len = strlen (find); |
112 |
do |
113 |
{ |
114 |
do |
115 |
{ |
116 |
if ((sc = *s++) == 0) |
117 |
return NULL; |
118 |
} |
119 |
while (tolower (sc) != c); |
120 |
} |
121 |
while (strncasecmp (s, find, len) != 0); |
122 |
s--; |
123 |
} |
124 |
return (char *) s; |
125 |
} |
126 |
|
127 |
/* |
128 |
* returns a char-pointer to a static array, in which a representation |
129 |
* of the decimal number given will be stored. |
130 |
*/ |
131 |
char * |
132 |
ltostr10 (signed long n) |
133 |
{ |
134 |
static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
135 |
character for the trailing nul character */ |
136 |
snprintf (buf, sizeof (buf), "%ld", n); |
137 |
return buf; |
138 |
} |
139 |
|
140 |
char * |
141 |
doubletostr10 (double v) |
142 |
{ |
143 |
static char tbuf[200]; |
144 |
|
145 |
sprintf (tbuf, "%f", v); |
146 |
return tbuf; |
147 |
} |
148 |
|
149 |
/** |
150 |
* open_and_uncompress() first searches for the original filename. If it exist, |
151 |
* then it opens it and returns the file-pointer. |
152 |
*/ |
153 |
FILE * |
154 |
open_and_uncompress (const char *name, int flag, int *compressed) |
155 |
{ |
156 |
*compressed = 0; |
157 |
return fopen (name, "r"); |
158 |
} |
159 |
|
160 |
/* |
161 |
* See open_and_uncompress(). |
162 |
*/ |
163 |
|
164 |
void |
165 |
close_and_delete (FILE * fp, int compressed) |
166 |
{ |
167 |
fclose (fp); |
168 |
} |
169 |
|
170 |
/* |
171 |
* If any directories in the given path doesn't exist, they are created. |
172 |
*/ |
173 |
|
174 |
void |
175 |
make_path_to_file (char *filename) |
176 |
{ |
177 |
char buf[MAX_BUF], *cp = buf; |
178 |
struct stat statbuf; |
179 |
|
180 |
if (!filename || !*filename) |
181 |
return; |
182 |
|
183 |
assign (buf, filename); |
184 |
|
185 |
while ((cp = strchr (cp + 1, (int) '/'))) |
186 |
{ |
187 |
*cp = '\0'; |
188 |
if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
189 |
{ |
190 |
if (mkdir (buf, SAVE_DIR_MODE)) |
191 |
{ |
192 |
LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
193 |
return; |
194 |
} |
195 |
} |
196 |
*cp = '/'; |
197 |
} |
198 |
} |