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