source: git/src/filename.c@ 9fcc81a

RELEASE/1.2 debug-ci debug-ci-sanitisers faster-cavernlog log-select main stereo-2025 walls-data walls-data-hanging-as-warning warn-only-for-hanging-survey
Last change on this file since 9fcc81a was dc533fe, checked in by Olly Betts <olly@…>, 12 years ago

src/filename.c: Comment tweak.

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[846746e]1/* OS dependent filename manipulation routines
[dc533fe]2 * Copyright (c) Olly Betts 1998-2003,2004,2005,2010,2011,2014
[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
[ecbc6c18]16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[846746e]17 */
[6ba8d69]18
[a420b49]19#ifdef HAVE_CONFIG_H
20# include <config.h>
21#endif
22
[9af1d7a9]23#include "filename.h"
[13a1bd16]24#include "debug.h"
[47c7a94]25
26#include <ctype.h>
[9af1d7a9]27#include <string.h>
28
[908298d]29typedef struct filelist {
30 char *fnm;
31 FILE *fh;
32 struct filelist *next;
33} filelist;
34
35static filelist *flhead = NULL;
36
37static void filename_register_output_with_fh(const char *fnm, FILE *fh);
38
[a0b076f]39/* safe_fopen should be used when writing a file
40 * fopenWithPthAndExt should be used when reading a file
41 */
[ce5b43c]42
[a420b49]43/* Wrapper for fopen which throws a fatal error if it fails.
44 * Some versions of fopen() are quite happy to open a directory.
45 * We aren't, so catch this case. */
46extern FILE *
47safe_fopen(const char *fnm, const char *mode)
48{
49 FILE *f;
[4c07c51]50 SVX_ASSERT(mode[0] == 'w'); /* only expect to be used for writing */
[a420b49]51 if (fDirectory(fnm))
[0804fbe]52 fatalerror(/*Filename “%s” refers to directory*/44, fnm);
[a420b49]53
54 f = fopen(fnm, mode);
[0804fbe]55 if (!f) fatalerror(/*Failed to open output file “%s”*/47, fnm);
[a0b076f]56
[908298d]57 filename_register_output_with_fh(fnm, f);
[a420b49]58 return f;
59}
60
[f1067a2]61/* Wrapper for fclose which throws a fatal error if there's been a write
62 * error.
63 */
64extern void
65safe_fclose(FILE *f)
66{
[4c07c51]67 SVX_ASSERT(f);
[f1067a2]68 /* NB: use of | rather than || - we always want to call fclose() */
[908298d]69 if (ferror(f) | (fclose(f) == EOF)) {
70 filelist *p;
71 for (p = flhead; p != NULL; p = p->next)
72 if (p->fh == f) break;
73
74 if (p && p->fnm) {
75 const char *fnm = p->fnm;
76 p->fnm = NULL;
77 p->fh = NULL;
78 (void)remove(fnm);
[0804fbe]79 fatalerror(/*Error writing to file “%s”*/110, fnm);
[908298d]80 }
[dc533fe]81 /* f wasn't opened with safe_fopen(), so we don't know the filename. */
[f1067a2]82 fatalerror(/*Error writing to file*/111);
[908298d]83 }
[f1067a2]84}
85
[ce5b43c]86extern FILE *
87safe_fopen_with_ext(const char *fnm, const char *ext, const char *mode)
88{
89 FILE *f;
90 char *p;
91 p = add_ext(fnm, ext);
92 f = safe_fopen(p, mode);
93 osfree(p);
94 return f;
95}
96
97static FILE *
[a420b49]98fopen_not_dir(const char *fnm, const char *mode)
99{
100 if (fDirectory(fnm)) return NULL;
101 return fopen(fnm, mode);
[9af1d7a9]102}
103
[9965b2b]104extern char *
[ce5b43c]105path_from_fnm(const char *fnm)
[a420b49]106{
[9af1d7a9]107 char *pth;
108 const char *lf;
109 int lenpth = 0;
110
[a420b49]111 lf = strrchr(fnm, FNM_SEP_LEV);
[9af1d7a9]112#ifdef FNM_SEP_LEV2
[029824d]113 {
[eee67ab]114 const char *lf2 = strrchr(lf ? lf + 1 : fnm, FNM_SEP_LEV2);
[029824d]115 if (lf2) lf = lf2;
116 }
[9af1d7a9]117#endif
118#ifdef FNM_SEP_DRV
[a420b49]119 if (!lf) lf = strrchr(fnm, FNM_SEP_DRV);
[9af1d7a9]120#endif
121 if (lf) lenpth = lf - fnm + 1;
122
[908298d]123 pth = osmalloc(lenpth + 1);
[a420b49]124 memcpy(pth, fnm, lenpth);
[9af1d7a9]125 pth[lenpth] = '\0';
126
127 return pth;
128}
129
[ce5b43c]130extern char *
131base_from_fnm(const char *fnm)
132{
133 char *p;
[cb3d1e2]134
[ce5b43c]135 p = strrchr(fnm, FNM_SEP_EXT);
136 /* Trim off any leaf extension, but dirs can have extensions too */
[029824d]137 if (p && !strchr(p, FNM_SEP_LEV)
[ce5b43c]138#ifdef FNM_SEP_LEV2
[029824d]139 && !strchr(p, FNM_SEP_LEV2)
[ce5b43c]140#endif
141 ) {
[e917a13]142 size_t len = (const char *)p - fnm;
[ce5b43c]143
[908298d]144 p = osmalloc(len + 1);
[ce5b43c]145 memcpy(p, fnm, len);
146 p[len] = '\0';
147 return p;
148 }
149
[908298d]150 return osstrdup(fnm);
[ce5b43c]151}
152
153extern char *
154baseleaf_from_fnm(const char *fnm)
155{
156 const char *p;
157 char *q;
158 size_t len;
[cb3d1e2]159
[ce5b43c]160 p = fnm;
161 q = strrchr(p, FNM_SEP_LEV);
162 if (q) p = q + 1;
163#ifdef FNM_SEP_LEV2
164 q = strrchr(p, FNM_SEP_LEV2);
165 if (q) p = q + 1;
166#endif
[cb3d1e2]167
[ce5b43c]168 q = strrchr(p, FNM_SEP_EXT);
[e917a13]169 if (q) len = (const char *)q - p; else len = strlen(p);
[ce5b43c]170
[908298d]171 q = osmalloc(len + 1);
[ce5b43c]172 memcpy(q, p, len);
173 q[len] = '\0';
174 return q;
175}
176
[9965b2b]177extern char *
[ce5b43c]178leaf_from_fnm(const char *fnm)
[a420b49]179{
[029824d]180 const char *lf;
[a420b49]181 lf = strrchr(fnm, FNM_SEP_LEV);
[029824d]182 if (lf) fnm = lf + 1;
[9af1d7a9]183#ifdef FNM_SEP_LEV2
[029824d]184 lf = strrchr(fnm, FNM_SEP_LEV2);
185 if (lf) fnm = lf + 1;
[9af1d7a9]186#endif
187#ifdef FNM_SEP_DRV
[029824d]188 lf = strrchr(fnm, FNM_SEP_DRV);
189 if (lf) fnm = lf + 1;
[9af1d7a9]190#endif
[908298d]191 return osstrdup(fnm);
[9af1d7a9]192}
193
194/* Make fnm from pth and lf, inserting an FNM_SEP_LEV if appropriate */
[9965b2b]195extern char *
[ce5b43c]196use_path(const char *pth, const char *lf)
[a420b49]197{
[9af1d7a9]198 char *fnm;
199 int len, len_total;
200 bool fAddSep = fFalse;
201
202 len = strlen(pth);
203 len_total = len + strlen(lf) + 1;
204
205 /* if there's a path and it doesn't end in a separator, insert one */
[a420b49]206 if (len && pth[len - 1] != FNM_SEP_LEV) {
[9af1d7a9]207#ifdef FNM_SEP_LEV2
[a420b49]208 if (pth[len - 1] != FNM_SEP_LEV2) {
[9af1d7a9]209#endif
210#ifdef FNM_SEP_DRV
[421b7d2]211 if (pth[len - 1] != FNM_SEP_DRV) {
[9af1d7a9]212#endif
[421b7d2]213 fAddSep = fTrue;
214 len_total++;
[9af1d7a9]215#ifdef FNM_SEP_DRV
[421b7d2]216 }
[9af1d7a9]217#endif
218#ifdef FNM_SEP_LEV2
219 }
220#endif
221 }
222
[908298d]223 fnm = osmalloc(len_total);
[a420b49]224 strcpy(fnm, pth);
[9af1d7a9]225 if (fAddSep) fnm[len++] = FNM_SEP_LEV;
[a420b49]226 strcpy(fnm + len, lf);
[9af1d7a9]227 return fnm;
228}
229
230/* Add ext to fnm, inserting an FNM_SEP_EXT if appropriate */
[9965b2b]231extern char *
[ce5b43c]232add_ext(const char *fnm, const char *ext)
[a420b49]233{
[9af1d7a9]234 char * fnmNew;
235 int len, len_total;
236#ifdef FNM_SEP_EXT
237 bool fAddSep = fFalse;
238#endif
239
240 len = strlen(fnm);
241 len_total = len + strlen(ext) + 1;
242#ifdef FNM_SEP_EXT
243 if (ext[0] != FNM_SEP_EXT) {
244 fAddSep = fTrue;
245 len_total++;
246 }
247#endif
248
[908298d]249 fnmNew = osmalloc(len_total);
[a420b49]250 strcpy(fnmNew, fnm);
[9af1d7a9]251#ifdef FNM_SEP_EXT
252 if (fAddSep) fnmNew[len++] = FNM_SEP_EXT;
253#endif
[a420b49]254 strcpy(fnmNew + len, ext);
[9af1d7a9]255 return fnmNew;
256}
257
258/* fopen file, found using pth and fnm
[25ab06b]259 * fnmUsed is used to return filename used to open file (ignored if NULL)
[9af1d7a9]260 * or NULL if file didn't open
261 */
[9965b2b]262extern FILE *
[25ab06b]263fopenWithPthAndExt(const char *pth, const char *fnm, const char *ext,
264 const char *mode, char **fnmUsed)
[a420b49]265{
266 char *fnmFull = NULL;
267 FILE *fh = NULL;
268 bool fAbs;
269
270 /* if no pth treat fnm as absolute */
271 fAbs = (pth == NULL || *pth == '\0' || fAbsoluteFnm(fnm));
272
273 /* if appropriate, try it without pth */
274 if (fAbs) {
[25ab06b]275 fh = fopen_not_dir(fnm, mode);
[a420b49]276 if (fh) {
[908298d]277 if (fnmUsed) fnmFull = osstrdup(fnm);
[a420b49]278 } else {
[25ab06b]279 if (ext && *ext) {
[a420b49]280 /* we've been given an extension so try using it */
[25ab06b]281 fnmFull = add_ext(fnm, ext);
282 fh = fopen_not_dir(fnmFull, mode);
[a420b49]283 }
[9af1d7a9]284 }
[a420b49]285 } else {
286 /* try using path given - first of all without the extension */
[ce5b43c]287 fnmFull = use_path(pth, fnm);
[25ab06b]288 fh = fopen_not_dir(fnmFull, mode);
[a420b49]289 if (!fh) {
[25ab06b]290 if (ext && *ext) {
[a420b49]291 /* we've been given an extension so try using it */
292 char *fnmTmp;
293 fnmTmp = fnmFull;
[25ab06b]294 fnmFull = add_ext(fnmFull, ext);
[a420b49]295 osfree(fnmTmp);
[25ab06b]296 fh = fopen_not_dir(fnmFull, mode);
[a420b49]297 }
[9af1d7a9]298 }
[a420b49]299 }
300
301 /* either it opened or didn't. If not, fh == NULL from fopen_not_dir() */
302
303 /* free name if it didn't open or name isn't wanted */
[25ab06b]304 if (fh == NULL || fnmUsed == NULL) osfree(fnmFull);
305 if (fnmUsed) *fnmUsed = (fh ? fnmFull : NULL);
[a420b49]306 return fh;
[9af1d7a9]307}
[47c7a94]308
309/* Like fopenWithPthAndExt except that "foreign" paths are translated to
310 * native ones (e.g. on Unix dir\file.ext -> dir/file.ext) */
311FILE *
312fopen_portable(const char *pth, const char *fnm, const char *ext,
[25ab06b]313 const char *mode, char **fnmUsed)
[47c7a94]314{
[25ab06b]315 FILE *fh = fopenWithPthAndExt(pth, fnm, ext, mode, fnmUsed);
[47c7a94]316 if (fh == NULL) {
[affaeee]317#if OS_UNIX
[47c7a94]318 int f_changed = 0;
319 char *fnm_trans, *p;
320 fnm_trans = osstrdup(fnm);
321 for (p = fnm_trans; *p; p++) {
322 switch (*p) {
[421b7d2]323 case '\\': /* swap a backslash to a forward slash */
[47c7a94]324 *p = '/';
325 f_changed = 1;
326 break;
327 }
328 }
329 if (f_changed)
[25ab06b]330 fh = fopenWithPthAndExt(pth, fnm_trans, ext, mode, fnmUsed);
[47c7a94]331
332 /* as a last ditch measure, try lowercasing the filename */
333 if (fh == NULL) {
334 f_changed = 0;
[0580c6a]335 for (p = fnm_trans; *p ; p++) {
336 unsigned char ch = *p;
337 if (isupper(ch)) {
338 *p = tolower(ch);
[47c7a94]339 f_changed = 1;
340 }
[0580c6a]341 }
[47c7a94]342 if (f_changed)
[25ab06b]343 fh = fopenWithPthAndExt(pth, fnm_trans, ext, mode, fnmUsed);
[47c7a94]344 }
345 osfree(fnm_trans);
346#endif
347 }
348 return fh;
349}
[25ab06b]350
351void
352filename_register_output(const char *fnm)
[421b7d2]353{
[908298d]354 filelist *p = osnew(filelist);
[4c07c51]355 SVX_ASSERT(fnm);
[908298d]356 p->fnm = osstrdup(fnm);
357 p->fh = NULL;
358 p->next = flhead;
359 flhead = p;
360}
361
362static void
363filename_register_output_with_fh(const char *fnm, FILE *fh)
[25ab06b]364{
365 filelist *p = osnew(filelist);
[4c07c51]366 SVX_ASSERT(fnm);
[908298d]367 p->fnm = osstrdup(fnm);
368 p->fh = fh;
[25ab06b]369 p->next = flhead;
370 flhead = p;
371}
372
373void
374filename_delete_output(void)
375{
376 while (flhead) {
377 filelist *p = flhead;
378 flhead = flhead->next;
[908298d]379 if (p->fnm) {
380 (void)remove(p->fnm);
381 osfree(p->fnm);
382 }
[25ab06b]383 osfree(p);
384 }
385}
Note: See TracBrowser for help on using the repository browser.