1
/* *** This file was borrowed from jam 2.5. The copyright statement from
2
* *** jam.c appears below.
6
* +\ Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
9
* This file is part of jam.
11
* License is hereby granted to use this software and distribute it
12
* freely, as long as this copyright notice is retained and modifications
15
* ALL WARRANTIES ARE HEREBY DISCLAIMED.
19
* Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
21
* This file is part of Jam - see jam.c for Copyright information.
25
* pathunix.c - manipulate file names on UNIX, NT, OS2, AmigaOS
29
* path_parse() - split a file name into dir/base/suffix/member
30
* path_build() - build a filename given dir/base/suffix/member
31
* path_parent() - make a PATHNAME point to its parent dir
33
* File_parse() and path_build() just manipuate a string and a structure;
34
* they do not make system calls.
36
* 04/08/94 (seiwald) - Coherent/386 support added.
37
* 12/26/93 (seiwald) - handle dir/.suffix properly in path_build()
38
* 12/19/94 (mikem) - solaris string table insanity support
39
* 12/21/94 (wingerd) Use backslashes for pathnames - the NT way.
40
* 02/14/95 (seiwald) - parse and build /xxx properly
41
* 02/23/95 (wingerd) Compilers on NT can handle "/" in pathnames, so we
42
* should expect hdr searches to come up with strings
43
* like "thing/thing.h". So we need to test for "/" as
44
* well as "\" when parsing pathnames.
45
* 03/16/95 (seiwald) - fixed accursed typo on line 69.
46
* 05/03/96 (seiwald) - split from filent.c, fileunix.c
47
* 12/20/96 (seiwald) - when looking for the rightmost . in a file name,
48
* don't include the archive member name.
49
* 01/13/01 (seiwald) - turn off \ handling on UNIX, on by accident
50
* 11/04/02 (seiwald) - const-ing for string literals
59
* path_parse() - split a file name into dir/base/suffix/member
70
memset( (char *)f, 0, sizeof( *f ) );
72
/* Look for <grist> */
74
if( file[0] == '<' && ( p = strchr( file, '>' ) ) )
76
f->f_grist.ptr = file;
77
f->f_grist.len = p - file;
83
p = strrchr( file, '/' );
85
# if PATH_DELIM == '\\'
86
/* On NT, look for dir\ as well */
88
char *p1 = strrchr( file, '\\' );
96
f->f_dir.len = p - file;
98
/* Special case for / - dirname is /, not "" */
103
# if PATH_DELIM == '\\'
104
/* Special case for D:/ - dirname is D:/, not "D:" */
106
if( f->f_dir.len == 2 && file[1] == ':' )
113
end = file + strlen( file );
115
/* Look for (member) */
117
if( ( p = strchr( file, '(' ) ) && end[-1] == ')' )
119
f->f_member.ptr = p + 1;
120
f->f_member.len = end - p - 2;
124
/* Look for .suffix */
125
/* This would be memrchr() */
130
while( ( q = (char *)memchr( q, '.', end - q ) ) ) /* TNB */
136
f->f_suffix.len = end - p;
142
f->f_base.ptr = file;
143
f->f_base.len = end - file;
147
* path_build() - build a filename given dir/base/suffix/member
156
/* Start with the grist. If the current grist isn't */
157
/* surrounded by <>'s, add them. */
161
if( f->f_grist.ptr[0] != '<' ) *file++ = '<';
162
memcpy( file, f->f_grist.ptr, f->f_grist.len );
163
file += f->f_grist.len;
164
if( file[-1] != '>' ) *file++ = '>';
167
/* Don't prepend root if it's . or directory is rooted */
169
# if PATH_DELIM == '/'
172
&& !( f->f_root.len == 1 && f->f_root.ptr[0] == '.' )
173
&& !( f->f_dir.len && f->f_dir.ptr[0] == '/' ) )
178
&& !( f->f_root.len == 1 && f->f_root.ptr[0] == '.' )
179
&& !( f->f_dir.len && f->f_dir.ptr[0] == '/' )
180
&& !( f->f_dir.len && f->f_dir.ptr[0] == '\\' )
181
&& !( f->f_dir.len && f->f_dir.ptr[1] == ':' ) )
186
memcpy( file, f->f_root.ptr, f->f_root.len );
187
file += f->f_root.len;
188
*file++ = PATH_DELIM;
193
memcpy( file, f->f_dir.ptr, f->f_dir.len );
194
file += f->f_dir.len;
197
/* UNIX: Put / between dir and file */
198
/* NT: Put \ between dir and file */
200
if( f->f_dir.len && ( f->f_base.len || f->f_suffix.len ) )
202
/* UNIX: Special case for dir \ : don't add another \ */
203
/* NT: Special case for dir / : don't add another / */
205
# if PATH_DELIM == '\\'
206
if( !( f->f_dir.len == 3 && f->f_dir.ptr[1] == ':' ) )
208
if( !( f->f_dir.len == 1 && f->f_dir.ptr[0] == PATH_DELIM ) )
209
*file++ = PATH_DELIM;
214
memcpy( file, f->f_base.ptr, f->f_base.len );
215
file += f->f_base.len;
218
if( f->f_suffix.len )
220
memcpy( file, f->f_suffix.ptr, f->f_suffix.len );
221
file += f->f_suffix.len;
224
if( f->f_member.len )
227
memcpy( file, f->f_member.ptr, f->f_member.len );
228
file += f->f_member.len;
235
* path_parent() - make a PATHNAME point to its parent dir
239
path_parent( PATHNAME *f )
241
/* just set everything else to nothing */
245
f->f_member.ptr = "";
252
# endif /* unix, NT, OS/2, AmigaOS */