1 | |
1 | /* |
2 | /* $Id: path.C,v 1.2 2006/09/10 16:00:23 root Exp $ */ |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008 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 it under |
|
|
9 | * the terms of the Affero GNU General Public License as published by the |
|
|
10 | * Free Software Foundation, either version 3 of the License, or (at your |
|
|
11 | * 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 Affero GNU General Public License |
|
|
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
|
|
21 | * |
|
|
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
|
|
23 | */ |
3 | |
24 | |
4 | #include <assert.h> |
25 | #include <assert.h> |
5 | #include <stdio.h> |
26 | #include <stdio.h> |
6 | #include <string.h> |
27 | #include <string.h> |
7 | #include <global.h> |
28 | #include <global.h> |
|
|
29 | #include <limits.h> |
8 | |
30 | |
9 | #include "define.h" |
31 | #include "define.h" |
10 | #include "path.h" |
32 | #include "path.h" |
11 | |
33 | |
|
|
34 | #ifndef PATH_MAX |
|
|
35 | # define PATH_MAX 8192 |
|
|
36 | #endif |
12 | |
37 | |
13 | #if 0 |
38 | #if 0 |
14 | |
39 | |
15 | /** |
40 | /** |
16 | * Define DEBUG_PATH to enable debug output. |
41 | * Define DEBUG_PATH to enable debug output. |
… | |
… | |
34 | |
59 | |
35 | char * |
60 | char * |
36 | path_combine (const char *src, const char *dst) |
61 | path_combine (const char *src, const char *dst) |
37 | { |
62 | { |
38 | char *p; |
63 | char *p; |
39 | static char path[HUGE_BUF]; |
64 | static char path[PATH_MAX]; |
40 | |
65 | |
41 | if (*dst == '/') |
66 | if (*dst == '/') |
42 | { |
67 | { |
43 | /* absolute destination path => ignore source path */ |
68 | /* absolute destination path => ignore source path */ |
44 | strcpy (path, dst); |
69 | strcpy (path, dst); |
… | |
… | |
46 | else |
71 | else |
47 | { |
72 | { |
48 | /* relative destination path => add after last '/' of source */ |
73 | /* relative destination path => add after last '/' of source */ |
49 | strcpy (path, src); |
74 | strcpy (path, src); |
50 | p = strrchr (path, '/'); |
75 | p = strrchr (path, '/'); |
51 | if (p != NULL) |
76 | if (p) |
52 | { |
|
|
53 | p++; |
77 | p++; |
54 | } |
|
|
55 | else |
78 | else |
56 | { |
79 | { |
57 | p = path; |
80 | p = path; |
58 | if (*src == '/') |
81 | if (*src == '/') |
59 | *p++ = '/'; |
82 | *p++ = '/'; |
60 | } |
83 | } |
|
|
84 | |
61 | strcpy (p, dst); |
85 | strcpy (p, dst); |
62 | } |
86 | } |
63 | |
87 | |
64 | #if defined(DEBUG_PATH) |
88 | #if defined(DEBUG_PATH) |
65 | LOG (llevDebug, "path_combine(%s, %s) = %s\n", src, dst, path); |
89 | LOG (llevDebug, "path_combine(%s, %s) = %s\n", src, dst, path); |
66 | #endif |
90 | #endif |
67 | return (path); |
91 | return path; |
68 | } |
92 | } |
69 | |
93 | |
70 | void |
94 | void |
71 | path_normalize (char *path) |
95 | path_normalize (char *path) |
72 | { |
96 | { |
… | |
… | |
186 | } |
210 | } |
187 | |
211 | |
188 | static void |
212 | static void |
189 | check_normalize (const char *path, const char *exp0) |
213 | check_normalize (const char *path, const char *exp0) |
190 | { |
214 | { |
191 | char tmp[HUGE_BUF]; |
215 | char tmp[PATH_MAX]; |
192 | char exp[HUGE_BUF]; |
216 | char exp[PATH_MAX]; |
193 | |
217 | |
194 | strcpy (exp, exp0 == NULL ? path : exp0); |
218 | strcpy (exp, exp0 == NULL ? path : exp0); |
195 | |
219 | |
196 | strcpy (tmp, path); |
220 | strcpy (tmp, path); |
197 | fprintf (stderr, "path_normalize(%s) = ", tmp); |
221 | fprintf (stderr, "path_normalize(%s) = ", tmp); |
198 | path_normalize (tmp); |
222 | path_normalize (tmp); |
199 | fprintf (stderr, "%s", tmp); |
223 | fprintf (stderr, "%s", tmp); |
200 | if (strcmp (tmp, exp) != 0) |
224 | if (strcmp (tmp, exp) != 0) |
201 | { |
|
|
202 | fprintf (stderr, ", should be %s\n", exp); |
225 | fprintf (stderr, ", should be %s\n", exp); |
203 | } |
|
|
204 | else |
226 | else |
205 | { |
|
|
206 | fprintf (stderr, " (OK)\n"); |
227 | fprintf (stderr, " (OK)\n"); |
207 | } |
|
|
208 | } |
228 | } |
209 | |
229 | |
210 | static void |
230 | static void |
211 | 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) |
212 | { |
232 | { |
… | |
… | |
214 | |
234 | |
215 | fprintf (stderr, "path_combine_and_normalize(%s, %s) = ", src, dst); |
235 | fprintf (stderr, "path_combine_and_normalize(%s, %s) = ", src, dst); |
216 | res = path_combine_and_normalize (src, dst); |
236 | res = path_combine_and_normalize (src, dst); |
217 | fprintf (stderr, "%s", res); |
237 | fprintf (stderr, "%s", res); |
218 | if (strcmp (res, exp) != 0) |
238 | if (strcmp (res, exp) != 0) |
219 | { |
|
|
220 | fprintf (stderr, ", should be %s\n", exp); |
239 | fprintf (stderr, ", should be %s\n", exp); |
221 | } |
|
|
222 | else |
240 | else |
223 | { |
|
|
224 | fprintf (stderr, " (OK)\n"); |
241 | fprintf (stderr, " (OK)\n"); |
225 | } |
|
|
226 | } |
242 | } |
227 | |
243 | |
228 | int |
244 | int |
229 | main () |
245 | main () |
230 | { |
246 | { |