1 | /* OS dependent filename manipulation routines |
---|
2 | * Copyright (c) Olly Betts 1998-2001 |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #ifdef HAVE_CONFIG_H |
---|
20 | # include <config.h> |
---|
21 | #endif |
---|
22 | |
---|
23 | #include "filename.h" |
---|
24 | #include "debug.h" |
---|
25 | |
---|
26 | #include <ctype.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | /* safe_fopen should be used when writing a file |
---|
30 | * fopenWithPthAndExt should be used when reading a file |
---|
31 | */ |
---|
32 | |
---|
33 | /* Wrapper for fopen which throws a fatal error if it fails. |
---|
34 | * Some versions of fopen() are quite happy to open a directory. |
---|
35 | * We aren't, so catch this case. */ |
---|
36 | extern FILE * |
---|
37 | safe_fopen(const char *fnm, const char *mode) |
---|
38 | { |
---|
39 | FILE *f; |
---|
40 | ASSERT(mode[0] == 'w'); /* only expect to be used for writing */ |
---|
41 | if (fDirectory(fnm)) |
---|
42 | fatalerror(/*Filename `%s' refers to directory*/44, fnm); |
---|
43 | |
---|
44 | f = fopen(fnm, mode); |
---|
45 | if (!f) fatalerror(/*Failed to open output file `%s'*/47, fnm); |
---|
46 | |
---|
47 | filename_register_output(fnm); |
---|
48 | return f; |
---|
49 | } |
---|
50 | |
---|
51 | extern FILE * |
---|
52 | safe_fopen_with_ext(const char *fnm, const char *ext, const char *mode) |
---|
53 | { |
---|
54 | FILE *f; |
---|
55 | char *p; |
---|
56 | p = add_ext(fnm, ext); |
---|
57 | f = safe_fopen(p, mode); |
---|
58 | osfree(p); |
---|
59 | return f; |
---|
60 | } |
---|
61 | |
---|
62 | static FILE * |
---|
63 | fopen_not_dir(const char *fnm, const char *mode) |
---|
64 | { |
---|
65 | if (fDirectory(fnm)) return NULL; |
---|
66 | return fopen(fnm, mode); |
---|
67 | } |
---|
68 | |
---|
69 | extern char * FAR |
---|
70 | path_from_fnm(const char *fnm) |
---|
71 | { |
---|
72 | char *pth; |
---|
73 | const char *lf; |
---|
74 | int lenpth = 0; |
---|
75 | |
---|
76 | lf = strrchr(fnm, FNM_SEP_LEV); |
---|
77 | #ifdef FNM_SEP_LEV2 |
---|
78 | { |
---|
79 | const char *lf2 = strrchr(lf ? lf + 1 : fnm, FNM_SEP_LEV2); |
---|
80 | if (lf2) lf = lf2; |
---|
81 | } |
---|
82 | #endif |
---|
83 | #ifdef FNM_SEP_DRV |
---|
84 | if (!lf) lf = strrchr(fnm, FNM_SEP_DRV); |
---|
85 | #endif |
---|
86 | if (lf) lenpth = lf - fnm + 1; |
---|
87 | |
---|
88 | pth = (char*) osmalloc(lenpth + 1); |
---|
89 | memcpy(pth, fnm, lenpth); |
---|
90 | pth[lenpth] = '\0'; |
---|
91 | |
---|
92 | return pth; |
---|
93 | } |
---|
94 | |
---|
95 | extern char * |
---|
96 | base_from_fnm(const char *fnm) |
---|
97 | { |
---|
98 | char *p; |
---|
99 | |
---|
100 | p = strrchr(fnm, FNM_SEP_EXT); |
---|
101 | /* Trim off any leaf extension, but dirs can have extensions too */ |
---|
102 | if (p && !strchr(p, FNM_SEP_LEV) |
---|
103 | #ifdef FNM_SEP_LEV2 |
---|
104 | && !strchr(p, FNM_SEP_LEV2) |
---|
105 | #endif |
---|
106 | ) { |
---|
107 | size_t len = (const char *)p - fnm; |
---|
108 | |
---|
109 | p = (char*) osmalloc(len + 1); |
---|
110 | memcpy(p, fnm, len); |
---|
111 | p[len] = '\0'; |
---|
112 | return p; |
---|
113 | } |
---|
114 | |
---|
115 | return (char*) osstrdup(fnm); |
---|
116 | } |
---|
117 | |
---|
118 | extern char * |
---|
119 | baseleaf_from_fnm(const char *fnm) |
---|
120 | { |
---|
121 | const char *p; |
---|
122 | char *q; |
---|
123 | size_t len; |
---|
124 | |
---|
125 | p = fnm; |
---|
126 | q = strrchr(p, FNM_SEP_LEV); |
---|
127 | if (q) p = q + 1; |
---|
128 | #ifdef FNM_SEP_LEV2 |
---|
129 | q = strrchr(p, FNM_SEP_LEV2); |
---|
130 | if (q) p = q + 1; |
---|
131 | #endif |
---|
132 | |
---|
133 | q = strrchr(p, FNM_SEP_EXT); |
---|
134 | if (q) len = (const char *)q - p; else len = strlen(p); |
---|
135 | |
---|
136 | q = (char*) osmalloc(len + 1); |
---|
137 | memcpy(q, p, len); |
---|
138 | q[len] = '\0'; |
---|
139 | return q; |
---|
140 | } |
---|
141 | |
---|
142 | extern char * FAR |
---|
143 | leaf_from_fnm(const char *fnm) |
---|
144 | { |
---|
145 | const char *lf; |
---|
146 | lf = strrchr(fnm, FNM_SEP_LEV); |
---|
147 | if (lf) fnm = lf + 1; |
---|
148 | #ifdef FNM_SEP_LEV2 |
---|
149 | lf = strrchr(fnm, FNM_SEP_LEV2); |
---|
150 | if (lf) fnm = lf + 1; |
---|
151 | #endif |
---|
152 | #ifdef FNM_SEP_DRV |
---|
153 | lf = strrchr(fnm, FNM_SEP_DRV); |
---|
154 | if (lf) fnm = lf + 1; |
---|
155 | #endif |
---|
156 | return (char*) osstrdup(fnm); |
---|
157 | } |
---|
158 | |
---|
159 | /* Make fnm from pth and lf, inserting an FNM_SEP_LEV if appropriate */ |
---|
160 | extern char * FAR |
---|
161 | use_path(const char *pth, const char *lf) |
---|
162 | { |
---|
163 | char *fnm; |
---|
164 | int len, len_total; |
---|
165 | bool fAddSep = fFalse; |
---|
166 | |
---|
167 | len = strlen(pth); |
---|
168 | len_total = len + strlen(lf) + 1; |
---|
169 | |
---|
170 | /* if there's a path and it doesn't end in a separator, insert one */ |
---|
171 | if (len && pth[len - 1] != FNM_SEP_LEV) { |
---|
172 | #ifdef FNM_SEP_LEV2 |
---|
173 | if (pth[len - 1] != FNM_SEP_LEV2) { |
---|
174 | #endif |
---|
175 | #ifdef FNM_SEP_DRV |
---|
176 | if (pth[len - 1] != FNM_SEP_DRV) { |
---|
177 | #endif |
---|
178 | fAddSep = fTrue; |
---|
179 | len_total++; |
---|
180 | #ifdef FNM_SEP_DRV |
---|
181 | } |
---|
182 | #endif |
---|
183 | #ifdef FNM_SEP_LEV2 |
---|
184 | } |
---|
185 | #endif |
---|
186 | } |
---|
187 | |
---|
188 | fnm = (char*) osmalloc(len_total); |
---|
189 | strcpy(fnm, pth); |
---|
190 | if (fAddSep) fnm[len++] = FNM_SEP_LEV; |
---|
191 | strcpy(fnm + len, lf); |
---|
192 | return fnm; |
---|
193 | } |
---|
194 | |
---|
195 | /* Add ext to fnm, inserting an FNM_SEP_EXT if appropriate */ |
---|
196 | extern char * FAR |
---|
197 | add_ext(const char *fnm, const char *ext) |
---|
198 | { |
---|
199 | char * fnmNew; |
---|
200 | int len, len_total; |
---|
201 | #ifdef FNM_SEP_EXT |
---|
202 | bool fAddSep = fFalse; |
---|
203 | #endif |
---|
204 | |
---|
205 | len = strlen(fnm); |
---|
206 | len_total = len + strlen(ext) + 1; |
---|
207 | #ifdef FNM_SEP_EXT |
---|
208 | if (ext[0] != FNM_SEP_EXT) { |
---|
209 | fAddSep = fTrue; |
---|
210 | len_total++; |
---|
211 | } |
---|
212 | #endif |
---|
213 | |
---|
214 | fnmNew = (char*) osmalloc(len_total); |
---|
215 | strcpy(fnmNew, fnm); |
---|
216 | #ifdef FNM_SEP_EXT |
---|
217 | if (fAddSep) fnmNew[len++] = FNM_SEP_EXT; |
---|
218 | #endif |
---|
219 | strcpy(fnmNew + len, ext); |
---|
220 | return fnmNew; |
---|
221 | } |
---|
222 | |
---|
223 | /* fopen file, found using pth and fnm |
---|
224 | * fnmUsed is used to return filename used to open file (ignored if NULL) |
---|
225 | * or NULL if file didn't open |
---|
226 | */ |
---|
227 | extern FILE FAR * |
---|
228 | fopenWithPthAndExt(const char *pth, const char *fnm, const char *ext, |
---|
229 | const char *mode, char **fnmUsed) |
---|
230 | { |
---|
231 | char *fnmFull = NULL; |
---|
232 | FILE *fh = NULL; |
---|
233 | bool fAbs; |
---|
234 | |
---|
235 | /* if no pth treat fnm as absolute */ |
---|
236 | fAbs = (pth == NULL || *pth == '\0' || fAbsoluteFnm(fnm)); |
---|
237 | |
---|
238 | /* if appropriate, try it without pth */ |
---|
239 | if (fAbs) { |
---|
240 | fh = fopen_not_dir(fnm, mode); |
---|
241 | if (fh) { |
---|
242 | if (fnmUsed) fnmFull = (char*) osstrdup(fnm); |
---|
243 | } else { |
---|
244 | if (ext && *ext) { |
---|
245 | /* we've been given an extension so try using it */ |
---|
246 | fnmFull = add_ext(fnm, ext); |
---|
247 | fh = fopen_not_dir(fnmFull, mode); |
---|
248 | } |
---|
249 | } |
---|
250 | } else { |
---|
251 | /* try using path given - first of all without the extension */ |
---|
252 | fnmFull = use_path(pth, fnm); |
---|
253 | fh = fopen_not_dir(fnmFull, mode); |
---|
254 | if (!fh) { |
---|
255 | if (ext && *ext) { |
---|
256 | /* we've been given an extension so try using it */ |
---|
257 | char *fnmTmp; |
---|
258 | fnmTmp = fnmFull; |
---|
259 | fnmFull = add_ext(fnmFull, ext); |
---|
260 | osfree(fnmTmp); |
---|
261 | fh = fopen_not_dir(fnmFull, mode); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | /* either it opened or didn't. If not, fh == NULL from fopen_not_dir() */ |
---|
267 | |
---|
268 | /* free name if it didn't open or name isn't wanted */ |
---|
269 | if (fh == NULL || fnmUsed == NULL) osfree(fnmFull); |
---|
270 | if (fnmUsed) *fnmUsed = (fh ? fnmFull : NULL); |
---|
271 | return fh; |
---|
272 | } |
---|
273 | |
---|
274 | /* Like fopenWithPthAndExt except that "foreign" paths are translated to |
---|
275 | * native ones (e.g. on Unix dir\file.ext -> dir/file.ext) */ |
---|
276 | FILE * |
---|
277 | fopen_portable(const char *pth, const char *fnm, const char *ext, |
---|
278 | const char *mode, char **fnmUsed) |
---|
279 | { |
---|
280 | FILE *fh = fopenWithPthAndExt(pth, fnm, ext, mode, fnmUsed); |
---|
281 | if (fh == NULL) { |
---|
282 | #if (OS==RISCOS) || (OS==UNIX) |
---|
283 | int f_changed = 0; |
---|
284 | char *fnm_trans, *p; |
---|
285 | #if OS==RISCOS |
---|
286 | char *q; |
---|
287 | #endif |
---|
288 | fnm_trans = osstrdup(fnm); |
---|
289 | #if OS==RISCOS |
---|
290 | q = fnm_trans; |
---|
291 | #endif |
---|
292 | for (p = fnm_trans; *p; p++) { |
---|
293 | switch (*p) { |
---|
294 | #if (OS==RISCOS) |
---|
295 | /* swap either slash to a dot, and a dot to a forward slash */ |
---|
296 | /* but .. goes to ^ */ |
---|
297 | case '.': |
---|
298 | if (p[1] == '.') { |
---|
299 | *q++ = '^'; |
---|
300 | p++; /* skip second dot */ |
---|
301 | } else { |
---|
302 | *q++ = '/'; |
---|
303 | } |
---|
304 | f_changed = 1; |
---|
305 | break; |
---|
306 | case '/': case '\\': |
---|
307 | *q++ = '.'; |
---|
308 | f_changed = 1; |
---|
309 | break; |
---|
310 | default: |
---|
311 | *q++ = *p; break; |
---|
312 | #else |
---|
313 | case '\\': /* swap a backslash to a forward slash */ |
---|
314 | *p = '/'; |
---|
315 | f_changed = 1; |
---|
316 | break; |
---|
317 | #endif |
---|
318 | } |
---|
319 | } |
---|
320 | #if OS==RISCOS |
---|
321 | *q = '\0'; |
---|
322 | #endif |
---|
323 | if (f_changed) |
---|
324 | fh = fopenWithPthAndExt(pth, fnm_trans, ext, mode, fnmUsed); |
---|
325 | |
---|
326 | #if (OS==UNIX) |
---|
327 | /* as a last ditch measure, try lowercasing the filename */ |
---|
328 | if (fh == NULL) { |
---|
329 | f_changed = 0; |
---|
330 | for (p = fnm_trans; *p ; p++) |
---|
331 | if (isupper(*p)) { |
---|
332 | *p = tolower(*p); |
---|
333 | f_changed = 1; |
---|
334 | } |
---|
335 | if (f_changed) |
---|
336 | fh = fopenWithPthAndExt(pth, fnm_trans, ext, mode, fnmUsed); |
---|
337 | } |
---|
338 | #endif |
---|
339 | osfree(fnm_trans); |
---|
340 | #endif |
---|
341 | } |
---|
342 | return fh; |
---|
343 | } |
---|
344 | |
---|
345 | typedef struct filelist { |
---|
346 | char *fnm; |
---|
347 | struct filelist *next; |
---|
348 | } filelist; |
---|
349 | |
---|
350 | static filelist *flhead = NULL; |
---|
351 | |
---|
352 | void |
---|
353 | filename_register_output(const char *fnm) |
---|
354 | { |
---|
355 | filelist *p = osnew(filelist); |
---|
356 | p->fnm = (char*) osstrdup(fnm); |
---|
357 | p->next = flhead; |
---|
358 | flhead = p; |
---|
359 | } |
---|
360 | |
---|
361 | void |
---|
362 | filename_delete_output(void) |
---|
363 | { |
---|
364 | while (flhead) { |
---|
365 | filelist *p = flhead; |
---|
366 | flhead = flhead->next; |
---|
367 | (void)remove(p->fnm); |
---|
368 | osfree(p->fnm); |
---|
369 | osfree(p); |
---|
370 | } |
---|
371 | } |
---|