cosmetic fixes
[k8jam.git] / pathmac.c
blobcc1fa4bc46103e4557d47fe39c390f938bfe099c
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * pathunix.c - manipulate file names on UNIX, NT, OS2
10 * External routines:
12 * path_parse() - split a file name into dir/base/suffix/member
13 * path_build() - build a filename given dir/base/suffix/member
14 * path_parent() - make a PATHNAME point to its parent dir
16 * File_parse() and path_build() just manipuate a string and a structure;
17 * they do not make system calls.
19 * 04/08/94 (seiwald) - Coherent/386 support added.
20 * 12/26/93 (seiwald) - handle dir/.suffix properly in path_build()
21 * 12/19/94 (mikem) - solaris string table insanity support
22 * 12/21/94 (wingerd) Use backslashes for pathnames - the NT way.
23 * 02/14/95 (seiwald) - parse and build /xxx properly
24 * 02/23/95 (wingerd) Compilers on NT can handle "/" in pathnames, so we
25 * should expect hdr searches to come up with strings
26 * like "thing/thing.h". So we need to test for "/" as
27 * well as "\" when parsing pathnames.
28 * 03/16/95 (seiwald) - fixed accursed typo on line 69.
29 * 05/03/96 (seiwald) - split from filent.c, fileunix.c
30 * 12/20/96 (seiwald) - when looking for the rightmost . in a file name,
31 * don't include the archive member name.
32 * 01/10/01 (seiwald) - path_parse now strips the trailing : from the
33 * directory name, unless the directory name is all
34 * :'s, so that $(d:P) works.
35 * 11/04/02 (seiwald) - const-ing for string literals
38 # include "jam.h"
39 # include "pathsys.h"
41 # ifdef OS_MAC
43 # define DELIM ':'
46 * path_parse() - split a file name into dir/base/suffix/member
48 void path_parse (const char *file, PATHNAME *f) {
49 const char *p, *q;
50 const char *end;
52 memset((char *)f, 0, sizeof(*f));
53 /* Look for <grist> */
54 if (file[0] == '<' && (p = strchr(file, '>'))) {
55 f->f_grist.ptr = file;
56 f->f_grist.len = p - file;
57 file = p+1;
59 /* Look for dir: */
60 if (p = strrchr(file, DELIM)) {
61 f->f_dir.ptr = file;
62 f->f_dir.len = p - file;
63 file = p+1;
64 /* All :'s? Include last : as part of directory name */
65 while (p > f->f_dir.ptr && *--p == DELIM) ;
66 if (p == f->f_dir.ptr) f->f_dir.len++;
68 end = file+strlen(file);
69 /* Look for (member) */
70 if ((p = strchr(file, '(')) && end[-1] == ')') {
71 f->f_member.ptr = p+1;
72 f->f_member.len = end-p-2;
73 end = p;
75 /* Look for .suffix */
76 /* This would be memrchr() */
77 p = 0;
78 q = file;
79 while (q = memchr(q, '.', end-q)) p = q++;
80 if (p) {
81 f->f_suffix.ptr = p;
82 f->f_suffix.len = end - p;
83 end = p;
85 /* Leaves base */
86 f->f_base.ptr = file;
87 f->f_base.len = end - file;
92 * path_build() - build a filename given dir/base/suffix/member
94 # define DIR_EMPTY 0 /* "" */
95 # define DIR_DOT 1 /* : */
96 # define DIR_DOTDOT 2 /* :: */
97 # define DIR_ABS 3 /* dira:dirb: */
98 # define DIR_REL 4 /* :dira:dirb: */
100 # define G_DIR 0 /* take dir */
101 # define G_ROOT 1 /* take root */
102 # define G_CAT 2 /* prepend root to dir */
103 # define G_DTDR 3 /* :: of rel dir */
104 # define G_DDDD 4 /* make it ::: (../..) */
105 # define G_MT 5 /* leave it empty */
107 char grid[5][5] = {
108 /* EMPTY DOT DOTDOT ABS REL */
109 /* EMPTY */ { G_MT, G_DIR, G_DIR, G_DIR, G_DIR },
110 /* DOT */ { G_ROOT, G_DIR, G_DIR, G_DIR, G_DIR },
111 /* DOTDOT */ { G_ROOT, G_ROOT, G_DDDD, G_DIR, G_DTDR },
112 /* ABS */ { G_ROOT, G_ROOT, G_ROOT, G_DIR, G_CAT },
113 /* REL */ { G_ROOT, G_ROOT, G_ROOT, G_DIR, G_CAT }
116 static int
117 file_flags(
118 const char *ptr,
119 int len )
121 if( !len )
122 return DIR_EMPTY;
123 if( len == 1 && ptr[0] == DELIM )
124 return DIR_DOT;
125 if( len == 2 && ptr[0] == DELIM && ptr[1] == DELIM )
126 return DIR_DOTDOT;
127 if( ptr[0] == DELIM )
128 return DIR_REL;
129 return DIR_ABS;
132 void
133 path_build(
134 PATHNAME *f,
135 char *file,
136 int binding )
138 char *ofile = file;
139 int dflag, rflag, act;
141 if( DEBUG_SEARCH )
143 printf("build file: ");
144 if( f->f_root.len )
145 printf( "root = '%.*s' ", f->f_root.len, f->f_root.ptr );
146 if( f->f_dir.len )
147 printf( "dir = '%.*s' ", f->f_dir.len, f->f_dir.ptr );
148 if( f->f_base.len )
149 printf( "base = '%.*s' ", f->f_base.len, f->f_base.ptr );
152 /* Start with the grist. If the current grist isn't */
153 /* surrounded by <>'s, add them. */
155 if( f->f_grist.len )
157 if( f->f_grist.ptr[0] != '<' ) *file++ = '<';
158 memcpy( file, f->f_grist.ptr, f->f_grist.len );
159 file += f->f_grist.len;
160 if( file[-1] != '>' ) *file++ = '>';
163 /* Combine root & directory, according to the grid. */
165 dflag = file_flags( f->f_dir.ptr, f->f_dir.len );
166 rflag = file_flags( f->f_root.ptr, f->f_root.len );
168 switch( act = grid[ rflag ][ dflag ] )
170 case G_DTDR:
171 /* :: of rel dir */
172 *file++ = DELIM;
173 /* fall through */
175 case G_DIR:
176 /* take dir */
177 memcpy( file, f->f_dir.ptr, f->f_dir.len );
178 file += f->f_dir.len;
179 break;
181 case G_ROOT:
182 /* take root */
183 memcpy( file, f->f_root.ptr, f->f_root.len );
184 file += f->f_root.len;
185 break;
187 case G_CAT:
188 /* prepend root to dir */
189 memcpy( file, f->f_root.ptr, f->f_root.len );
190 file += f->f_root.len;
191 if( file[-1] == DELIM ) --file;
192 memcpy( file, f->f_dir.ptr, f->f_dir.len );
193 file += f->f_dir.len;
194 break;
196 case G_DDDD:
197 /* make it ::: (../..) */
198 strcpy( file, ":::" );
199 file += 3;
200 break;
203 /* Put : between dir and file (if none already) */
205 if( act != G_MT &&
206 file[-1] != DELIM &&
207 ( f->f_base.len || f->f_suffix.len ) )
209 *file++ = DELIM;
212 if( f->f_base.len )
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 )
226 *file++ = '(';
227 memcpy( file, f->f_member.ptr, f->f_member.len );
228 file += f->f_member.len;
229 *file++ = ')';
231 *file = 0;
233 if( DEBUG_SEARCH )
234 printf(" -> '%s'\n", ofile);
238 * path_parent() - make a PATHNAME point to its parent dir
241 void
242 path_parent( PATHNAME *f )
244 /* just set everything else to nothing */
246 f->f_base.ptr =
247 f->f_suffix.ptr =
248 f->f_member.ptr = "";
250 f->f_base.len =
251 f->f_suffix.len =
252 f->f_member.len = 0;
255 # endif /* OS_MAC */