1 | #ifdef HAVE_CONFIG_H |
---|
2 | # include <config.h> |
---|
3 | #endif |
---|
4 | |
---|
5 | /* OS dependent filename manipulation routines */ |
---|
6 | #include "filename.h" |
---|
7 | #include <string.h> |
---|
8 | |
---|
9 | /* FIXME: finish sorting out safe_fopen vs fopenWithPthAndExt... */ |
---|
10 | |
---|
11 | /* Wrapper for fopen which throws a fatal error if it fails. |
---|
12 | * Some versions of fopen() are quite happy to open a directory. |
---|
13 | * We aren't, so catch this case. */ |
---|
14 | extern FILE * |
---|
15 | safe_fopen(const char *fnm, const char *mode) |
---|
16 | { |
---|
17 | FILE *f; |
---|
18 | if (fDirectory(fnm)) |
---|
19 | fatalerror(/*Filename '%s' refers to directory*/44, fnm); |
---|
20 | |
---|
21 | f = fopen(fnm, mode); |
---|
22 | if (!f) fatalerror(mode[0] == 'w' ? |
---|
23 | /*Failed to open output file*/47 : |
---|
24 | /*Couldn't open data file*/24, fnm); |
---|
25 | return f; |
---|
26 | } |
---|
27 | |
---|
28 | extern FILE * |
---|
29 | safe_fopen_with_ext(const char *fnm, const char *ext, const char *mode) |
---|
30 | { |
---|
31 | FILE *f; |
---|
32 | char *p; |
---|
33 | p = add_ext(fnm, ext); |
---|
34 | f = safe_fopen(p, mode); |
---|
35 | osfree(p); |
---|
36 | return f; |
---|
37 | } |
---|
38 | |
---|
39 | static FILE * |
---|
40 | fopen_not_dir(const char *fnm, const char *mode) |
---|
41 | { |
---|
42 | if (fDirectory(fnm)) return NULL; |
---|
43 | return fopen(fnm, mode); |
---|
44 | } |
---|
45 | |
---|
46 | extern char * FAR |
---|
47 | path_from_fnm(const char *fnm) |
---|
48 | { |
---|
49 | char *pth; |
---|
50 | const char *lf; |
---|
51 | int lenpth = 0; |
---|
52 | |
---|
53 | lf = strrchr(fnm, FNM_SEP_LEV); |
---|
54 | #ifdef FNM_SEP_LEV2 |
---|
55 | if (!lf) lf = strrchr(fnm, FNM_SEP_LEV2); |
---|
56 | #endif |
---|
57 | #ifdef FNM_SEP_DRV |
---|
58 | if (!lf) lf = strrchr(fnm, FNM_SEP_DRV); |
---|
59 | #endif |
---|
60 | if (lf) lenpth = lf - fnm + 1; |
---|
61 | |
---|
62 | pth = osmalloc(lenpth + 1); |
---|
63 | memcpy(pth, fnm, lenpth); |
---|
64 | pth[lenpth] = '\0'; |
---|
65 | |
---|
66 | return pth; |
---|
67 | } |
---|
68 | |
---|
69 | extern char * |
---|
70 | base_from_fnm(const char *fnm) |
---|
71 | { |
---|
72 | char *p; |
---|
73 | |
---|
74 | p = strrchr(fnm, FNM_SEP_EXT); |
---|
75 | /* Trim off any leaf extension, but dirs can have extensions too */ |
---|
76 | if (p && !strrchr(p, FNM_SEP_LEV) |
---|
77 | #ifdef FNM_SEP_LEV2 |
---|
78 | && !strrchr(p, FNM_SEP_LEV2) |
---|
79 | #endif |
---|
80 | ) { |
---|
81 | size_t len = p - fnm; |
---|
82 | |
---|
83 | p = osmalloc(len + 1); |
---|
84 | memcpy(p, fnm, len); |
---|
85 | p[len] = '\0'; |
---|
86 | return p; |
---|
87 | } |
---|
88 | |
---|
89 | return osstrdup(fnm); |
---|
90 | } |
---|
91 | |
---|
92 | extern char * |
---|
93 | baseleaf_from_fnm(const char *fnm) |
---|
94 | { |
---|
95 | const char *p; |
---|
96 | char *q; |
---|
97 | size_t len; |
---|
98 | |
---|
99 | p = fnm; |
---|
100 | q = strrchr(p, FNM_SEP_LEV); |
---|
101 | if (q) p = q + 1; |
---|
102 | #ifdef FNM_SEP_LEV2 |
---|
103 | q = strrchr(p, FNM_SEP_LEV2); |
---|
104 | if (q) p = q + 1; |
---|
105 | #endif |
---|
106 | |
---|
107 | q = strrchr(p, FNM_SEP_EXT); |
---|
108 | if (q) len = q - p; else len = strlen(p); |
---|
109 | |
---|
110 | q = osmalloc(len + 1); |
---|
111 | memcpy(q, p, len); |
---|
112 | q[len] = '\0'; |
---|
113 | return q; |
---|
114 | } |
---|
115 | |
---|
116 | extern char * FAR |
---|
117 | leaf_from_fnm(const char *fnm) |
---|
118 | { |
---|
119 | char *lf; |
---|
120 | lf = strrchr(fnm, FNM_SEP_LEV); |
---|
121 | if (lf != NULL |
---|
122 | #ifdef FNM_SEP_LEV2 |
---|
123 | || (lf = strrchr(fnm, FNM_SEP_LEV2)) != NULL |
---|
124 | #endif |
---|
125 | #ifdef FNM_SEP_DRV |
---|
126 | || (lf = strrchr(fnm, FNM_SEP_DRV)) != NULL |
---|
127 | #endif |
---|
128 | ) { |
---|
129 | return osstrdup(lf + 1); |
---|
130 | } |
---|
131 | return osstrdup(fnm); |
---|
132 | } |
---|
133 | |
---|
134 | /* Make fnm from pth and lf, inserting an FNM_SEP_LEV if appropriate */ |
---|
135 | extern char * FAR |
---|
136 | use_path(const char *pth, const char *lf) |
---|
137 | { |
---|
138 | char *fnm; |
---|
139 | int len, len_total; |
---|
140 | bool fAddSep = fFalse; |
---|
141 | |
---|
142 | len = strlen(pth); |
---|
143 | len_total = len + strlen(lf) + 1; |
---|
144 | |
---|
145 | /* if there's a path and it doesn't end in a separator, insert one */ |
---|
146 | if (len && pth[len - 1] != FNM_SEP_LEV) { |
---|
147 | #ifdef FNM_SEP_LEV2 |
---|
148 | if (pth[len - 1] != FNM_SEP_LEV2) { |
---|
149 | #endif |
---|
150 | #ifdef FNM_SEP_DRV |
---|
151 | if (pth[len - 1] != FNM_SEP_DRV) { |
---|
152 | #endif |
---|
153 | fAddSep = fTrue; |
---|
154 | len_total++; |
---|
155 | #ifdef FNM_SEP_DRV |
---|
156 | } |
---|
157 | #endif |
---|
158 | #ifdef FNM_SEP_LEV2 |
---|
159 | } |
---|
160 | #endif |
---|
161 | } |
---|
162 | |
---|
163 | fnm = osmalloc(len_total); |
---|
164 | strcpy(fnm, pth); |
---|
165 | if (fAddSep) fnm[len++] = FNM_SEP_LEV; |
---|
166 | strcpy(fnm + len, lf); |
---|
167 | return fnm; |
---|
168 | } |
---|
169 | |
---|
170 | /* Add ext to fnm, inserting an FNM_SEP_EXT if appropriate */ |
---|
171 | extern char * FAR |
---|
172 | add_ext(const char *fnm, const char *ext) |
---|
173 | { |
---|
174 | char * fnmNew; |
---|
175 | int len, len_total; |
---|
176 | #ifdef FNM_SEP_EXT |
---|
177 | bool fAddSep = fFalse; |
---|
178 | #endif |
---|
179 | |
---|
180 | len = strlen(fnm); |
---|
181 | len_total = len + strlen(ext) + 1; |
---|
182 | #ifdef FNM_SEP_EXT |
---|
183 | if (ext[0] != FNM_SEP_EXT) { |
---|
184 | fAddSep = fTrue; |
---|
185 | len_total++; |
---|
186 | } |
---|
187 | #endif |
---|
188 | |
---|
189 | fnmNew = osmalloc(len_total); |
---|
190 | strcpy(fnmNew, fnm); |
---|
191 | #ifdef FNM_SEP_EXT |
---|
192 | if (fAddSep) fnmNew[len++] = FNM_SEP_EXT; |
---|
193 | #endif |
---|
194 | strcpy(fnmNew + len, ext); |
---|
195 | return fnmNew; |
---|
196 | } |
---|
197 | |
---|
198 | /* fopen file, found using pth and fnm |
---|
199 | * pfnmUsed used to return filename used to open file (ignored if NULL) |
---|
200 | * or NULL if file didn't open |
---|
201 | */ |
---|
202 | extern FILE FAR * |
---|
203 | fopenWithPthAndExt(const char * pth, const char * fnm, const char * szExt, |
---|
204 | const char * szMode, char **pfnmUsed) |
---|
205 | { |
---|
206 | char *fnmFull = NULL; |
---|
207 | FILE *fh = NULL; |
---|
208 | bool fAbs; |
---|
209 | |
---|
210 | /* if no pth treat fnm as absolute */ |
---|
211 | fAbs = (pth == NULL || *pth == '\0' || fAbsoluteFnm(fnm)); |
---|
212 | |
---|
213 | /* if appropriate, try it without pth */ |
---|
214 | if (fAbs) { |
---|
215 | fh = fopen_not_dir(fnm, szMode); |
---|
216 | if (fh) { |
---|
217 | if (pfnmUsed) fnmFull = osstrdup(fnm); |
---|
218 | } else { |
---|
219 | if (szExt && *szExt) { |
---|
220 | /* we've been given an extension so try using it */ |
---|
221 | fnmFull = add_ext(fnm, szExt); |
---|
222 | fh = fopen_not_dir(fnmFull, szMode); |
---|
223 | } |
---|
224 | } |
---|
225 | } else { |
---|
226 | /* try using path given - first of all without the extension */ |
---|
227 | fnmFull = use_path(pth, fnm); |
---|
228 | fh = fopen_not_dir(fnmFull, szMode); |
---|
229 | if (!fh) { |
---|
230 | if (szExt && *szExt) { |
---|
231 | /* we've been given an extension so try using it */ |
---|
232 | char *fnmTmp; |
---|
233 | fnmTmp = fnmFull; |
---|
234 | fnmFull = add_ext(fnmFull, szExt); |
---|
235 | osfree(fnmTmp); |
---|
236 | fh = fopen_not_dir(fnmFull, szMode); |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | /* either it opened or didn't. If not, fh == NULL from fopen_not_dir() */ |
---|
242 | |
---|
243 | /* free name if it didn't open or name isn't wanted */ |
---|
244 | if (fh == NULL || pfnmUsed == NULL) osfree(fnmFull); |
---|
245 | if (pfnmUsed) *pfnmUsed = (fh ? fnmFull : NULL); |
---|
246 | return fh; |
---|
247 | } |
---|