1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Crossfire TRT 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 3 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, see <http://www.gnu.org/licenses/>. |
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
20 | * |
21 | * |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | #include <assert.h> |
25 | #include <assert.h> |
25 | #include <stdio.h> |
26 | #include <stdio.h> |
26 | #include <string.h> |
27 | #include <string.h> |
27 | #include <global.h> |
28 | #include <global.h> |
|
|
29 | #include <limits.h> |
28 | |
30 | |
29 | #include "define.h" |
31 | #include "define.h" |
30 | #include "path.h" |
32 | #include "path.h" |
31 | |
33 | |
|
|
34 | #ifndef PATH_MAX |
|
|
35 | # define PATH_MAX 8192 |
|
|
36 | #endif |
32 | |
37 | |
33 | #if 0 |
38 | #if 0 |
34 | |
39 | |
35 | /** |
40 | /** |
36 | * Define DEBUG_PATH to enable debug output. |
41 | * Define DEBUG_PATH to enable debug output. |
… | |
… | |
54 | |
59 | |
55 | char * |
60 | char * |
56 | path_combine (const char *src, const char *dst) |
61 | path_combine (const char *src, const char *dst) |
57 | { |
62 | { |
58 | char *p; |
63 | char *p; |
59 | static char path[HUGE_BUF]; |
64 | static char path[PATH_MAX]; |
60 | |
65 | |
61 | if (*dst == '/') |
66 | if (*dst == '/') |
62 | { |
67 | { |
63 | /* absolute destination path => ignore source path */ |
68 | /* absolute destination path => ignore source path */ |
64 | strcpy (path, dst); |
69 | strcpy (path, dst); |
… | |
… | |
66 | else |
71 | else |
67 | { |
72 | { |
68 | /* relative destination path => add after last '/' of source */ |
73 | /* relative destination path => add after last '/' of source */ |
69 | strcpy (path, src); |
74 | strcpy (path, src); |
70 | p = strrchr (path, '/'); |
75 | p = strrchr (path, '/'); |
71 | if (p != NULL) |
76 | if (p) |
72 | { |
|
|
73 | p++; |
77 | p++; |
74 | } |
|
|
75 | else |
78 | else |
76 | { |
79 | { |
77 | p = path; |
80 | p = path; |
78 | if (*src == '/') |
81 | if (*src == '/') |
79 | *p++ = '/'; |
82 | *p++ = '/'; |
80 | } |
83 | } |
|
|
84 | |
81 | strcpy (p, dst); |
85 | strcpy (p, dst); |
82 | } |
86 | } |
83 | |
87 | |
84 | #if defined(DEBUG_PATH) |
88 | #if defined(DEBUG_PATH) |
85 | LOG (llevDebug, "path_combine(%s, %s) = %s\n", src, dst, path); |
89 | LOG (llevDebug, "path_combine(%s, %s) = %s\n", src, dst, path); |
86 | #endif |
90 | #endif |
87 | return (path); |
91 | return path; |
88 | } |
92 | } |
89 | |
93 | |
90 | void |
94 | void |
91 | path_normalize (char *path) |
95 | path_normalize (char *path) |
92 | { |
96 | { |
… | |
… | |
206 | } |
210 | } |
207 | |
211 | |
208 | static void |
212 | static void |
209 | check_normalize (const char *path, const char *exp0) |
213 | check_normalize (const char *path, const char *exp0) |
210 | { |
214 | { |
211 | char tmp[HUGE_BUF]; |
215 | char tmp[PATH_MAX]; |
212 | char exp[HUGE_BUF]; |
216 | char exp[PATH_MAX]; |
213 | |
217 | |
214 | strcpy (exp, exp0 == NULL ? path : exp0); |
218 | strcpy (exp, exp0 == NULL ? path : exp0); |
215 | |
219 | |
216 | strcpy (tmp, path); |
220 | strcpy (tmp, path); |
217 | fprintf (stderr, "path_normalize(%s) = ", tmp); |
221 | fprintf (stderr, "path_normalize(%s) = ", tmp); |
218 | path_normalize (tmp); |
222 | path_normalize (tmp); |
219 | fprintf (stderr, "%s", tmp); |
223 | fprintf (stderr, "%s", tmp); |
220 | if (strcmp (tmp, exp) != 0) |
224 | if (strcmp (tmp, exp) != 0) |
221 | { |
|
|
222 | fprintf (stderr, ", should be %s\n", exp); |
225 | fprintf (stderr, ", should be %s\n", exp); |
223 | } |
|
|
224 | else |
226 | else |
225 | { |
|
|
226 | fprintf (stderr, " (OK)\n"); |
227 | fprintf (stderr, " (OK)\n"); |
227 | } |
|
|
228 | } |
228 | } |
229 | |
229 | |
230 | static void |
230 | static void |
231 | check_combine_and_normalize (const char *src, const char *dst, const char *exp) |
231 | check_combine_and_normalize (const char *src, const char *dst, const char *exp) |
232 | { |
232 | { |
… | |
… | |
234 | |
234 | |
235 | fprintf (stderr, "path_combine_and_normalize(%s, %s) = ", src, dst); |
235 | fprintf (stderr, "path_combine_and_normalize(%s, %s) = ", src, dst); |
236 | res = path_combine_and_normalize (src, dst); |
236 | res = path_combine_and_normalize (src, dst); |
237 | fprintf (stderr, "%s", res); |
237 | fprintf (stderr, "%s", res); |
238 | if (strcmp (res, exp) != 0) |
238 | if (strcmp (res, exp) != 0) |
239 | { |
|
|
240 | fprintf (stderr, ", should be %s\n", exp); |
239 | fprintf (stderr, ", should be %s\n", exp); |
241 | } |
|
|
242 | else |
240 | else |
243 | { |
|
|
244 | fprintf (stderr, " (OK)\n"); |
241 | fprintf (stderr, " (OK)\n"); |
245 | } |
|
|
246 | } |
242 | } |
247 | |
243 | |
248 | int |
244 | int |
249 | main () |
245 | main () |
250 | { |
246 | { |