1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * it under the terms of the GNU General Public License as published by |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
11 | * option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * along with this program; if not, write to the Free Software |
19 | * and the GNU General Public License along with this program. If not, see |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * <http://www.gnu.org/licenses/>. |
21 | * |
21 | * |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | /* This file contains various functions that are not really unique for |
25 | /* This file contains various functions that are not really unique for |
26 | * crossfire, but rather provides what should be standard functions |
26 | * crossfire, but rather provides what should be standard functions |
27 | * for systems that do not have them. In this way, most of the |
27 | * for systems that do not have them. In this way, most of the |
… | |
… | |
48 | #include <unistd.h> |
48 | #include <unistd.h> |
49 | |
49 | |
50 | /* Has to be after above includes so we don't redefine some values */ |
50 | /* Has to be after above includes so we don't redefine some values */ |
51 | #include "global.h" |
51 | #include "global.h" |
52 | |
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' : \ |
53 | #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
100 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
54 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
101 | #define MBASE ('z' - 'a' + 1 + 10) |
55 | #define MBASE ('z' - 'a' + 1 + 10) |
102 | |
56 | |
103 | char * |
57 | char * |
… | |
… | |
123 | s--; |
77 | s--; |
124 | } |
78 | } |
125 | return (char *) s; |
79 | return (char *) s; |
126 | } |
80 | } |
127 | |
81 | |
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 | /** |
82 | /** |
151 | * open_and_uncompress() first searches for the original filename. If it exist, |
83 | * open_and_uncompress() first searches for the original filename. If it exist, |
152 | * then it opens it and returns the file-pointer. |
84 | * then it opens it and returns the file-pointer. |
153 | */ |
85 | */ |
154 | FILE * |
86 | FILE * |
… | |
… | |
166 | close_and_delete (FILE * fp, int compressed) |
98 | close_and_delete (FILE * fp, int compressed) |
167 | { |
99 | { |
168 | fclose (fp); |
100 | fclose (fp); |
169 | } |
101 | } |
170 | |
102 | |
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 | } |
|
|