~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/compilergcc/depslib/src/pathunix.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* *** This file was borrowed from jam 2.5. The copyright statement from
 
2
 * *** jam.c appears below.
 
3
 */
 
4
/*
 
5
 * /+\
 
6
 * +\   Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
 
7
 * \+/
 
8
 *
 
9
 * This file is part of jam.
 
10
 *
 
11
 * License is hereby granted to use this software and distribute it
 
12
 * freely, as long as this copyright notice is retained and modifications 
 
13
 * are clearly marked.
 
14
 *
 
15
 * ALL WARRANTIES ARE HEREBY DISCLAIMED.
 
16
 */
 
17
 
 
18
/*
 
19
 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
 
20
 *
 
21
 * This file is part of Jam - see jam.c for Copyright information.
 
22
 */
 
23
 
 
24
/*
 
25
 * pathunix.c - manipulate file names on UNIX, NT, OS2, AmigaOS
 
26
 *
 
27
 * External routines:
 
28
 *
 
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
 
32
 *
 
33
 * File_parse() and path_build() just manipuate a string and a structure;
 
34
 * they do not make system calls.
 
35
 *
 
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
 
51
 */
 
52
 
 
53
# include "jam.h"
 
54
# include "pathsys.h"
 
55
 
 
56
# ifdef USE_PATHUNIX
 
57
 
 
58
/*
 
59
 * path_parse() - split a file name into dir/base/suffix/member
 
60
 */
 
61
 
 
62
void
 
63
path_parse( 
 
64
        const char *file,
 
65
        PATHNAME *f )
 
66
{
 
67
        const char *p, *q;
 
68
        const char *end;
 
69
        
 
70
        memset( (char *)f, 0, sizeof( *f ) );
 
71
 
 
72
        /* Look for <grist> */
 
73
 
 
74
        if( file[0] == '<' && ( p = strchr( file, '>' ) ) )
 
75
        {
 
76
            f->f_grist.ptr = file;
 
77
            f->f_grist.len = p - file;
 
78
            file = p + 1;
 
79
        }
 
80
 
 
81
        /* Look for dir/ */
 
82
 
 
83
        p = strrchr( file, '/' );
 
84
 
 
85
# if PATH_DELIM == '\\'
 
86
        /* On NT, look for dir\ as well */
 
87
        {
 
88
            char *p1 = strrchr( file, '\\' );
 
89
            p = p1 > p ? p1 : p;
 
90
        }
 
91
# endif
 
92
 
 
93
        if( p )
 
94
        {
 
95
            f->f_dir.ptr = file;
 
96
            f->f_dir.len = p - file;
 
97
        
 
98
            /* Special case for / - dirname is /, not "" */
 
99
 
 
100
            if( !f->f_dir.len )
 
101
                f->f_dir.len = 1;
 
102
 
 
103
# if PATH_DELIM == '\\'
 
104
            /* Special case for D:/ - dirname is D:/, not "D:" */
 
105
 
 
106
            if( f->f_dir.len == 2 && file[1] == ':' )
 
107
                f->f_dir.len = 3;
 
108
# endif
 
109
 
 
110
            file = p + 1;
 
111
        }
 
112
 
 
113
        end = file + strlen( file );
 
114
 
 
115
        /* Look for (member) */
 
116
 
 
117
        if( ( p = strchr( file, '(' ) ) && end[-1] == ')' )
 
118
        {
 
119
            f->f_member.ptr = p + 1;
 
120
            f->f_member.len = end - p - 2;
 
121
            end = p;
 
122
        } 
 
123
 
 
124
        /* Look for .suffix */
 
125
        /* This would be memrchr() */
 
126
 
 
127
        p = 0;
 
128
        q = file;
 
129
 
 
130
        while( ( q = (char *)memchr( q, '.', end - q ) ) ) /* TNB */
 
131
            p = q++;
 
132
 
 
133
        if( p )
 
134
        {
 
135
            f->f_suffix.ptr = p;
 
136
            f->f_suffix.len = end - p;
 
137
            end = p;
 
138
        }
 
139
 
 
140
        /* Leaves base */
 
141
 
 
142
        f->f_base.ptr = file;
 
143
        f->f_base.len = end - file;
 
144
}
 
145
 
 
146
/*
 
147
 * path_build() - build a filename given dir/base/suffix/member
 
148
 */
 
149
 
 
150
void
 
151
path_build(
 
152
        PATHNAME *f,
 
153
        char    *file,
 
154
        int     binding )
 
155
{
 
156
        /* Start with the grist.  If the current grist isn't */
 
157
        /* surrounded by <>'s, add them. */
 
158
 
 
159
        if( f->f_grist.len )
 
160
        {
 
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++ = '>';
 
165
        }
 
166
 
 
167
        /* Don't prepend root if it's . or directory is rooted */
 
168
 
 
169
# if PATH_DELIM == '/'
 
170
 
 
171
        if( f->f_root.len 
 
172
            && !( f->f_root.len == 1 && f->f_root.ptr[0] == '.' )
 
173
            && !( f->f_dir.len && f->f_dir.ptr[0] == '/' ) )
 
174
 
 
175
# else /* unix */
 
176
 
 
177
        if( f->f_root.len 
 
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] == ':' ) )
 
182
 
 
183
# endif /* unix */
 
184
 
 
185
        {
 
186
            memcpy( file, f->f_root.ptr, f->f_root.len );
 
187
            file += f->f_root.len;
 
188
            *file++ = PATH_DELIM;
 
189
        }
 
190
 
 
191
        if( f->f_dir.len )
 
192
        {
 
193
            memcpy( file, f->f_dir.ptr, f->f_dir.len );
 
194
            file += f->f_dir.len;
 
195
        }
 
196
 
 
197
        /* UNIX: Put / between dir and file */
 
198
        /* NT:   Put \ between dir and file */
 
199
 
 
200
        if( f->f_dir.len && ( f->f_base.len || f->f_suffix.len ) )
 
201
        {
 
202
            /* UNIX: Special case for dir \ : don't add another \ */
 
203
            /* NT:   Special case for dir / : don't add another / */
 
204
 
 
205
# if PATH_DELIM == '\\'
 
206
            if( !( f->f_dir.len == 3 && f->f_dir.ptr[1] == ':' ) )
 
207
# endif
 
208
                if( !( f->f_dir.len == 1 && f->f_dir.ptr[0] == PATH_DELIM ) )
 
209
                    *file++ = PATH_DELIM;
 
210
        }
 
211
 
 
212
        if( f->f_base.len )
 
213
        {
 
214
            memcpy( file, f->f_base.ptr, f->f_base.len );
 
215
            file += f->f_base.len;
 
216
        }
 
217
 
 
218
        if( f->f_suffix.len )
 
219
        {
 
220
            memcpy( file, f->f_suffix.ptr, f->f_suffix.len );
 
221
            file += f->f_suffix.len;
 
222
        }
 
223
 
 
224
        if( f->f_member.len )
 
225
        {
 
226
            *file++ = '(';
 
227
            memcpy( file, f->f_member.ptr, f->f_member.len );
 
228
            file += f->f_member.len;
 
229
            *file++ = ')';
 
230
        }
 
231
        *file = 0;
 
232
}
 
233
 
 
234
/*
 
235
 *      path_parent() - make a PATHNAME point to its parent dir
 
236
 */
 
237
 
 
238
void
 
239
path_parent( PATHNAME *f )
 
240
{
 
241
        /* just set everything else to nothing */
 
242
 
 
243
        f->f_base.ptr =
 
244
        f->f_suffix.ptr =
 
245
        f->f_member.ptr = "";
 
246
 
 
247
        f->f_base.len = 
 
248
        f->f_suffix.len = 
 
249
        f->f_member.len = 0;
 
250
}
 
251
 
 
252
# endif /* unix, NT, OS/2, AmigaOS */