1 | /* |
---|
2 | * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc. |
---|
3 | * |
---|
4 | * This file is part of Jam - see jam.c for Copyright information. |
---|
5 | */ |
---|
6 | |
---|
7 | /* This file is ALSO: |
---|
8 | * Copyright 2001-2004 David Abrahams. |
---|
9 | * Distributed under the Boost Software License, Version 1.0. |
---|
10 | * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
---|
11 | */ |
---|
12 | |
---|
13 | # include "jam.h" |
---|
14 | # include "pathsys.h" |
---|
15 | # include "strings.h" |
---|
16 | # include "newstr.h" |
---|
17 | # include "filesys.h" |
---|
18 | |
---|
19 | # ifdef USE_PATHUNIX |
---|
20 | |
---|
21 | /* |
---|
22 | * pathunix.c - manipulate file names on UNIX, NT, OS2, AmigaOS |
---|
23 | * |
---|
24 | * External routines: |
---|
25 | * |
---|
26 | * path_parse() - split a file name into dir/base/suffix/member |
---|
27 | * path_build() - build a filename given dir/base/suffix/member |
---|
28 | * path_parent() - make a PATHNAME point to its parent dir |
---|
29 | * |
---|
30 | * File_parse() and path_build() just manipuate a string and a structure; |
---|
31 | * they do not make system calls. |
---|
32 | * |
---|
33 | * 04/08/94 (seiwald) - Coherent/386 support added. |
---|
34 | * 12/26/93 (seiwald) - handle dir/.suffix properly in path_build() |
---|
35 | * 12/19/94 (mikem) - solaris string table insanity support |
---|
36 | * 12/21/94 (wingerd) Use backslashes for pathnames - the NT way. |
---|
37 | * 02/14/95 (seiwald) - parse and build /xxx properly |
---|
38 | * 02/23/95 (wingerd) Compilers on NT can handle "/" in pathnames, so we |
---|
39 | * should expect hdr searches to come up with strings |
---|
40 | * like "thing/thing.h". So we need to test for "/" as |
---|
41 | * well as "\" when parsing pathnames. |
---|
42 | * 03/16/95 (seiwald) - fixed accursed typo on line 69. |
---|
43 | * 05/03/96 (seiwald) - split from filent.c, fileunix.c |
---|
44 | * 12/20/96 (seiwald) - when looking for the rightmost . in a file name, |
---|
45 | * don't include the archive member name. |
---|
46 | * 01/13/01 (seiwald) - turn on \ handling on UNIX, on by accident |
---|
47 | */ |
---|
48 | |
---|
49 | /* |
---|
50 | * path_parse() - split a file name into dir/base/suffix/member |
---|
51 | */ |
---|
52 | |
---|
53 | void |
---|
54 | path_parse( |
---|
55 | char *file, |
---|
56 | PATHNAME *f ) |
---|
57 | { |
---|
58 | char *p, *q; |
---|
59 | char *end; |
---|
60 | |
---|
61 | memset( (char *)f, 0, sizeof( *f ) ); |
---|
62 | |
---|
63 | /* Look for <grist> */ |
---|
64 | |
---|
65 | if( file[0] == '<' && ( p = strchr( file, '>' ) ) ) |
---|
66 | { |
---|
67 | f->f_grist.ptr = file; |
---|
68 | f->f_grist.len = p - file; |
---|
69 | file = p + 1; |
---|
70 | } |
---|
71 | |
---|
72 | /* Look for dir/ */ |
---|
73 | |
---|
74 | p = strrchr( file, '/' ); |
---|
75 | |
---|
76 | # if PATH_DELIM == '\\' |
---|
77 | /* On NT, look for dir\ as well */ |
---|
78 | { |
---|
79 | char *p1 = strrchr( file, '\\' ); |
---|
80 | p = p1 > p ? p1 : p; |
---|
81 | } |
---|
82 | # endif |
---|
83 | |
---|
84 | if( p ) |
---|
85 | { |
---|
86 | f->f_dir.ptr = file; |
---|
87 | f->f_dir.len = p - file; |
---|
88 | |
---|
89 | /* Special case for / - dirname is /, not "" */ |
---|
90 | |
---|
91 | if( !f->f_dir.len ) |
---|
92 | f->f_dir.len = 1; |
---|
93 | |
---|
94 | # if PATH_DELIM == '\\' |
---|
95 | /* Special case for D:/ - dirname is D:/, not "D:" */ |
---|
96 | |
---|
97 | if( f->f_dir.len == 2 && file[1] == ':' ) |
---|
98 | f->f_dir.len = 3; |
---|
99 | # endif |
---|
100 | |
---|
101 | file = p + 1; |
---|
102 | } |
---|
103 | |
---|
104 | end = file + strlen( file ); |
---|
105 | |
---|
106 | /* Look for (member) */ |
---|
107 | |
---|
108 | if( ( p = strchr( file, '(' ) ) && end[-1] == ')' ) |
---|
109 | { |
---|
110 | f->f_member.ptr = p + 1; |
---|
111 | f->f_member.len = end - p - 2; |
---|
112 | end = p; |
---|
113 | } |
---|
114 | |
---|
115 | /* Look for .suffix */ |
---|
116 | /* This would be memrchr() */ |
---|
117 | |
---|
118 | p = 0; |
---|
119 | q = file; |
---|
120 | |
---|
121 | while( q = (char *)memchr( q, '.', end - q ) ) |
---|
122 | p = q++; |
---|
123 | |
---|
124 | if( p ) |
---|
125 | { |
---|
126 | f->f_suffix.ptr = p; |
---|
127 | f->f_suffix.len = end - p; |
---|
128 | end = p; |
---|
129 | } |
---|
130 | |
---|
131 | /* Leaves base */ |
---|
132 | |
---|
133 | f->f_base.ptr = file; |
---|
134 | f->f_base.len = end - file; |
---|
135 | } |
---|
136 | |
---|
137 | /* |
---|
138 | * path_delims - the string of legal path delimiters |
---|
139 | */ |
---|
140 | static char path_delims[] = { |
---|
141 | PATH_DELIM, |
---|
142 | # if PATH_DELIM == '\\' |
---|
143 | '/', |
---|
144 | # endif |
---|
145 | 0 |
---|
146 | }; |
---|
147 | |
---|
148 | /* |
---|
149 | * is_path_delim() - true iff c is a path delimiter |
---|
150 | */ |
---|
151 | static int is_path_delim( char c ) |
---|
152 | { |
---|
153 | char* p = strchr( path_delims, c ); |
---|
154 | return p && *p; |
---|
155 | } |
---|
156 | |
---|
157 | /* |
---|
158 | * as_path_delim() - convert c to a path delimiter if it isn't one |
---|
159 | * already |
---|
160 | */ |
---|
161 | static char as_path_delim( char c ) |
---|
162 | { |
---|
163 | return is_path_delim( c ) ? c : PATH_DELIM; |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * path_build() - build a filename given dir/base/suffix/member |
---|
168 | * |
---|
169 | * To avoid changing slash direction on NT when reconstituting paths, |
---|
170 | * instead of unconditionally appending PATH_DELIM we check the |
---|
171 | * past-the-end character of the previous path element. If it is in |
---|
172 | * path_delims, we append that, and only append PATH_DELIM as a last |
---|
173 | * resort. This heuristic is based on the fact that PATHNAME objects |
---|
174 | * are usually the result of calling path_parse, which leaves the |
---|
175 | * original slashes in the past-the-end position. Correctness depends |
---|
176 | * on the assumption that all strings are zero terminated, so a |
---|
177 | * past-the-end character will always be available. |
---|
178 | * |
---|
179 | * As an attendant patch, we had to ensure that backslashes are used |
---|
180 | * explicitly in timestamp.c |
---|
181 | */ |
---|
182 | |
---|
183 | void |
---|
184 | path_build( |
---|
185 | PATHNAME *f, |
---|
186 | string *file, |
---|
187 | int binding ) |
---|
188 | { |
---|
189 | file_build1( f, file ); |
---|
190 | |
---|
191 | /* Don't prepend root if it's . or directory is rooted */ |
---|
192 | # if PATH_DELIM == '/' |
---|
193 | |
---|
194 | if( f->f_root.len |
---|
195 | && !( f->f_root.len == 1 && f->f_root.ptr[0] == '.' ) |
---|
196 | && !( f->f_dir.len && f->f_dir.ptr[0] == '/' ) ) |
---|
197 | |
---|
198 | # else /* unix */ |
---|
199 | |
---|
200 | if( f->f_root.len |
---|
201 | && !( f->f_root.len == 1 && f->f_root.ptr[0] == '.' ) |
---|
202 | && !( f->f_dir.len && f->f_dir.ptr[0] == '/' ) |
---|
203 | && !( f->f_dir.len && f->f_dir.ptr[0] == '\\' ) |
---|
204 | && !( f->f_dir.len && f->f_dir.ptr[1] == ':' ) ) |
---|
205 | |
---|
206 | # endif /* unix */ |
---|
207 | |
---|
208 | { |
---|
209 | string_append_range( file, f->f_root.ptr, f->f_root.ptr + f->f_root.len ); |
---|
210 | /* If 'root' already ends with path delimeter, |
---|
211 | don't add yet another one. */ |
---|
212 | if( ! is_path_delim( f->f_root.ptr[f->f_root.len-1] ) ) |
---|
213 | string_push_back( file, as_path_delim( f->f_root.ptr[f->f_root.len] ) ); |
---|
214 | } |
---|
215 | |
---|
216 | if( f->f_dir.len ) |
---|
217 | { |
---|
218 | string_append_range( file, f->f_dir.ptr, f->f_dir.ptr + f->f_dir.len ); |
---|
219 | } |
---|
220 | |
---|
221 | /* UNIX: Put / between dir and file */ |
---|
222 | /* NT: Put \ between dir and file */ |
---|
223 | |
---|
224 | if( f->f_dir.len && ( f->f_base.len || f->f_suffix.len ) ) |
---|
225 | { |
---|
226 | /* UNIX: Special case for dir \ : don't add another \ */ |
---|
227 | /* NT: Special case for dir / : don't add another / */ |
---|
228 | |
---|
229 | # if PATH_DELIM == '\\' |
---|
230 | if( !( f->f_dir.len == 3 && f->f_dir.ptr[1] == ':' ) ) |
---|
231 | # endif |
---|
232 | if( !( f->f_dir.len == 1 && is_path_delim( f->f_dir.ptr[0] ) ) ) |
---|
233 | string_push_back( file, as_path_delim( f->f_dir.ptr[f->f_dir.len] ) ); |
---|
234 | } |
---|
235 | |
---|
236 | if( f->f_base.len ) |
---|
237 | { |
---|
238 | string_append_range( file, f->f_base.ptr, f->f_base.ptr + f->f_base.len ); |
---|
239 | } |
---|
240 | |
---|
241 | if( f->f_suffix.len ) |
---|
242 | { |
---|
243 | string_append_range( file, f->f_suffix.ptr, f->f_suffix.ptr + f->f_suffix.len ); |
---|
244 | } |
---|
245 | |
---|
246 | if( f->f_member.len ) |
---|
247 | { |
---|
248 | string_push_back( file, '(' ); |
---|
249 | string_append_range( file, f->f_member.ptr, f->f_member.ptr + f->f_member.len ); |
---|
250 | string_push_back( file, ')' ); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | /* |
---|
255 | * path_parent() - make a PATHNAME point to its parent dir |
---|
256 | */ |
---|
257 | |
---|
258 | void |
---|
259 | path_parent( PATHNAME *f ) |
---|
260 | { |
---|
261 | /* just set everything else to nothing */ |
---|
262 | |
---|
263 | f->f_base.ptr = |
---|
264 | f->f_suffix.ptr = |
---|
265 | f->f_member.ptr = ""; |
---|
266 | |
---|
267 | f->f_base.len = |
---|
268 | f->f_suffix.len = |
---|
269 | f->f_member.len = 0; |
---|
270 | } |
---|
271 | |
---|
272 | #ifdef NT |
---|
273 | #include <windows.h> |
---|
274 | #include <tchar.h> |
---|
275 | |
---|
276 | /* The definition of this in winnt.h is not ANSI-C compatible. */ |
---|
277 | #undef INVALID_FILE_ATTRIBUTES |
---|
278 | #define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
---|
279 | |
---|
280 | |
---|
281 | DWORD ShortPathToLongPath(LPCTSTR lpszShortPath,LPTSTR lpszLongPath,DWORD |
---|
282 | cchBuffer) |
---|
283 | { |
---|
284 | DWORD i=0; |
---|
285 | TCHAR path[_MAX_PATH]={0}; |
---|
286 | TCHAR ret[_MAX_PATH]={0}; |
---|
287 | DWORD pos=0, prev_pos=0; |
---|
288 | DWORD len=_tcslen(lpszShortPath); |
---|
289 | |
---|
290 | /* Is the string valid? */ |
---|
291 | if (!lpszShortPath) { |
---|
292 | SetLastError(ERROR_INVALID_PARAMETER); |
---|
293 | return 0; |
---|
294 | } |
---|
295 | |
---|
296 | /* Is the path valid? */ |
---|
297 | if (GetFileAttributes(lpszShortPath)==INVALID_FILE_ATTRIBUTES) |
---|
298 | return 0; |
---|
299 | |
---|
300 | /* Convert "/" to "\" */ |
---|
301 | for (i=0;i<len;++i) { |
---|
302 | if (lpszShortPath[i]==_T('/')) |
---|
303 | path[i]=_T('\\'); |
---|
304 | else |
---|
305 | path[i]=lpszShortPath[i]; |
---|
306 | } |
---|
307 | |
---|
308 | /* UNC path? */ |
---|
309 | if (path[0]==_T('\\') && path[1]==_T('\\')) { |
---|
310 | pos=2; |
---|
311 | for (i=0;i<2;++i) { |
---|
312 | while (path[pos]!=_T('\\') && path[pos]!=_T('\0')) |
---|
313 | ++pos; |
---|
314 | ++pos; |
---|
315 | } |
---|
316 | _tcsncpy(ret,path,pos-1); |
---|
317 | } /* Drive letter? */ |
---|
318 | else if (path[1]==_T(':')) { |
---|
319 | if (path[2]==_T('\\')) |
---|
320 | pos=3; |
---|
321 | if (len==3) { |
---|
322 | if (cchBuffer>3) |
---|
323 | _tcscpy(lpszLongPath,lpszShortPath); |
---|
324 | return len; |
---|
325 | } |
---|
326 | _tcsncpy(ret,path,2); |
---|
327 | } |
---|
328 | |
---|
329 | /* Expand the path for each subpath, and strip trailing backslashes */ |
---|
330 | for (prev_pos = pos-1;pos<=len;++pos) { |
---|
331 | if (path[pos]==_T('\\') || (path[pos]==_T('\0') && |
---|
332 | path[pos-1]!=_T('\\'))) { |
---|
333 | WIN32_FIND_DATA fd; |
---|
334 | HANDLE hf=0; |
---|
335 | TCHAR c=path[pos]; |
---|
336 | char* new_element; |
---|
337 | path[pos]=_T('\0'); |
---|
338 | |
---|
339 | /* the path[prev_pos+1]... path[pos] range is the part of |
---|
340 | path we're handling right now. We need to find long |
---|
341 | name for that element and add it. */ |
---|
342 | new_element = path + prev_pos + 1; |
---|
343 | |
---|
344 | /* First add separator, but only if there's something in result already. */ |
---|
345 | if (ret[0] != _T('\0')) |
---|
346 | { |
---|
347 | _tcscat(ret,_T("\\")); |
---|
348 | } |
---|
349 | |
---|
350 | /* If it's ".." element, we need to append it, not |
---|
351 | the name in parent that FindFirstFile will return. |
---|
352 | Same goes for "." */ |
---|
353 | |
---|
354 | if (new_element[0] == _T('.') && new_element[1] == _T('\0') || |
---|
355 | new_element[0] == _T('.') && new_element[1] == _T('.') |
---|
356 | && new_element[2] == _T('\0')) |
---|
357 | { |
---|
358 | _tcscat(ret, new_element); |
---|
359 | } |
---|
360 | else |
---|
361 | { |
---|
362 | hf=FindFirstFile(path, &fd); |
---|
363 | if (hf==INVALID_HANDLE_VALUE) |
---|
364 | return 0; |
---|
365 | |
---|
366 | _tcscat(ret,fd.cFileName); |
---|
367 | FindClose(hf); |
---|
368 | } |
---|
369 | |
---|
370 | path[pos]=c; |
---|
371 | |
---|
372 | prev_pos = pos; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | len=_tcslen(ret)+1; |
---|
377 | if (cchBuffer>=len) |
---|
378 | _tcscpy(lpszLongPath,ret); |
---|
379 | |
---|
380 | return len; |
---|
381 | } |
---|
382 | |
---|
383 | char* short_path_to_long_path(char* short_path) |
---|
384 | { |
---|
385 | char buffer2[_MAX_PATH]; |
---|
386 | int ret = ShortPathToLongPath(short_path, buffer2, _MAX_PATH); |
---|
387 | |
---|
388 | if (ret) |
---|
389 | return newstr(buffer2); |
---|
390 | else |
---|
391 | return newstr(short_path); |
---|
392 | } |
---|
393 | |
---|
394 | #endif |
---|
395 | |
---|
396 | |
---|
397 | # endif /* unix, NT, OS/2, AmigaOS */ |
---|