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