]>
Commit | Line | Data |
---|---|---|
add7091b | 1 | /* Part of CPP library. (include file handling) |
5e7b4e25 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, |
d6d52dd6 | 3 | 1999, 2000, 2001 Free Software Foundation, Inc. |
add7091b ZW |
4 | Written by Per Bothner, 1994. |
5 | Based on CCCP program by Paul Rubin, June 1986 | |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
7 | Split out of cpplib.c, Zack Weinberg, Oct 1998 | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
11 | Free Software Foundation; either version 2, or (at your option) any | |
12 | later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
e38992e8 | 21 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
add7091b ZW |
22 | |
23 | #include "config.h" | |
24 | #include "system.h" | |
add7091b | 25 | #include "cpplib.h" |
88ae23e7 | 26 | #include "cpphash.h" |
c1212d2f | 27 | #include "intl.h" |
168d3732 | 28 | #include "mkdeps.h" |
c31a6508 | 29 | #include "splay-tree.h" |
add7091b | 30 | |
f8f769ea ZW |
31 | #ifdef HAVE_MMAP_FILE |
32 | # include <sys/mman.h> | |
33 | # ifndef MMAP_THRESHOLD | |
34 | # define MMAP_THRESHOLD 3 /* Minimum page count to mmap the file. */ | |
35 | # endif | |
36 | ||
37 | #else /* No MMAP_FILE */ | |
38 | # undef MMAP_THRESHOLD | |
39 | # define MMAP_THRESHOLD 0 | |
40 | #endif | |
41 | ||
d7a2e0f7 ZW |
42 | #ifndef O_BINARY |
43 | # define O_BINARY 0 | |
44 | #endif | |
45 | ||
a58d32c2 ZW |
46 | /* If errno is inspected immediately after a system call fails, it will be |
47 | nonzero, and no error number will ever be zero. */ | |
48 | #ifndef ENOENT | |
49 | # define ENOENT 0 | |
50 | #endif | |
51 | #ifndef ENOTDIR | |
52 | # define ENOTDIR 0 | |
53 | #endif | |
a58d32c2 | 54 | |
f9a0e96c ZW |
55 | /* Suppress warning about function macros used w/o arguments in traditional |
56 | C. It is unlikely that glibc's strcmp macro helps this file at all. */ | |
57 | #undef strcmp | |
58 | ||
642ce434 NB |
59 | /* This structure is used for the table of all includes. */ |
60 | struct include_file | |
61 | { | |
62 | const char *name; /* actual path name of file */ | |
63 | const cpp_hashnode *cmacro; /* macro, if any, preventing reinclusion. */ | |
591e15a1 | 64 | const struct search_path *foundhere; |
642ce434 NB |
65 | /* location in search path where file was |
66 | found, for #include_next and sysp. */ | |
67 | const unsigned char *buffer; /* pointer to cached file contents */ | |
68 | struct stat st; /* copy of stat(2) data for file */ | |
69 | int fd; /* fd open on file (short term storage only) */ | |
70 | unsigned short include_count; /* number of times file has been read */ | |
71 | unsigned short refcnt; /* number of stacked buffers using this file */ | |
72 | unsigned char mapped; /* file buffer is mmapped */ | |
73 | unsigned char defined; /* cmacro prevents inclusion in this state */ | |
74 | }; | |
75 | ||
28e0f040 NB |
76 | /* The cmacro works like this: If it's NULL, the file is to be |
77 | included again. If it's NEVER_REREAD, the file is never to be | |
78 | included again. Otherwise it is a macro hashnode, and the file is | |
79 | to be included again if the macro is defined or not as specified by | |
80 | DEFINED. */ | |
81 | #define NEVER_REREAD ((const cpp_hashnode *)-1) | |
82 | #define DO_NOT_REREAD(inc) \ | |
83 | ((inc)->cmacro && ((inc)->cmacro == NEVER_REREAD \ | |
84 | || ((inc)->cmacro->type == NT_MACRO) == (inc)->defined)) | |
85 | ||
a73ac7a5 | 86 | static struct file_name_map *read_name_map |
38b24ee2 ZW |
87 | PARAMS ((cpp_reader *, const char *)); |
88 | static char *read_filename_string PARAMS ((int, FILE *)); | |
89 | static char *remap_filename PARAMS ((cpp_reader *, char *, | |
591e15a1 NB |
90 | struct search_path *)); |
91 | static struct search_path *search_from PARAMS ((cpp_reader *, | |
92 | struct include_file *)); | |
c31a6508 ZW |
93 | static struct include_file *find_include_file |
94 | PARAMS ((cpp_reader *, const char *, | |
591e15a1 | 95 | struct search_path *)); |
2047e26f | 96 | static struct include_file *open_file PARAMS ((cpp_reader *, const char *)); |
3cf3593f NB |
97 | static void read_include_file PARAMS ((cpp_reader *, struct include_file *)); |
98 | static void stack_include_file PARAMS ((cpp_reader *, struct include_file *)); | |
a58d32c2 | 99 | static void purge_cache PARAMS ((struct include_file *)); |
a36c54fa | 100 | static void destroy_node PARAMS ((splay_tree_value)); |
c71f835b | 101 | static int report_missing_guard PARAMS ((splay_tree_node, void *)); |
a36c54fa NB |
102 | static splay_tree_node find_or_create_entry PARAMS ((cpp_reader *, |
103 | const char *)); | |
104 | static void handle_missing_header PARAMS ((cpp_reader *, const char *, int)); | |
105 | ||
106 | /* Set up the splay tree we use to store information about all the | |
107 | file names seen in this compilation. We also have entries for each | |
108 | file we tried to open but failed; this saves system calls since we | |
109 | don't try to open it again in future. | |
110 | ||
111 | The key of each node is the file name, after processing by | |
112 | _cpp_simplify_pathname. The path name may or may not be absolute. | |
113 | The path string has been malloced, as is automatically freed by | |
114 | registering free () as the splay tree key deletion function. | |
115 | ||
116 | A node's value is a pointer to a struct include_file, and is never | |
117 | NULL. */ | |
d35364d1 | 118 | void |
c71f835b | 119 | _cpp_init_includes (pfile) |
d35364d1 ZW |
120 | cpp_reader *pfile; |
121 | { | |
122 | pfile->all_include_files | |
c31a6508 ZW |
123 | = splay_tree_new ((splay_tree_compare_fn) strcmp, |
124 | (splay_tree_delete_key_fn) free, | |
a36c54fa | 125 | destroy_node); |
0b3d776a | 126 | } |
add7091b | 127 | |
a36c54fa | 128 | /* Tear down the splay tree. */ |
c71f835b ZW |
129 | void |
130 | _cpp_cleanup_includes (pfile) | |
131 | cpp_reader *pfile; | |
132 | { | |
133 | splay_tree_delete (pfile->all_include_files); | |
134 | } | |
135 | ||
a36c54fa NB |
136 | /* Free a node. The path string is automatically freed. */ |
137 | static void | |
138 | destroy_node (v) | |
139 | splay_tree_value v; | |
140 | { | |
141 | struct include_file *f = (struct include_file *)v; | |
142 | ||
143 | if (f) | |
144 | { | |
145 | purge_cache (f); | |
146 | free (f); | |
147 | } | |
148 | } | |
149 | ||
642ce434 NB |
150 | /* Mark a file to not be reread (e.g. #import, read failure). */ |
151 | void | |
152 | _cpp_never_reread (file) | |
153 | struct include_file *file; | |
154 | { | |
155 | file->cmacro = NEVER_REREAD; | |
156 | } | |
157 | ||
a36c54fa NB |
158 | /* Lookup a simplified filename, and create an entry if none exists. */ |
159 | static splay_tree_node | |
160 | find_or_create_entry (pfile, fname) | |
d6d52dd6 NB |
161 | cpp_reader *pfile; |
162 | const char *fname; | |
163 | { | |
a36c54fa NB |
164 | splay_tree_node node; |
165 | struct include_file *file; | |
d6d52dd6 | 166 | |
a36c54fa NB |
167 | node = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname); |
168 | if (! node) | |
d6d52dd6 | 169 | { |
a36c54fa | 170 | file = xcnew (struct include_file); |
d6d52dd6 | 171 | file->name = xstrdup (fname); |
a36c54fa NB |
172 | node = splay_tree_insert (pfile->all_include_files, |
173 | (splay_tree_key) file->name, | |
174 | (splay_tree_value) file); | |
d6d52dd6 | 175 | } |
a36c54fa NB |
176 | |
177 | return node; | |
178 | } | |
179 | ||
180 | /* Enter a simplified file name in the splay tree, for the sake of | |
181 | cpp_included (). */ | |
182 | void | |
183 | _cpp_fake_include (pfile, fname) | |
184 | cpp_reader *pfile; | |
185 | const char *fname; | |
186 | { | |
187 | find_or_create_entry (pfile, fname); | |
d6d52dd6 NB |
188 | } |
189 | ||
a58d32c2 | 190 | /* Given a file name, look it up in the cache; if there is no entry, |
2047e26f NB |
191 | create one with a non-NULL value (regardless of success in opening |
192 | the file). If the file doesn't exist or is inaccessible, this | |
193 | entry is flagged so we don't attempt to open it again in the | |
373e2177 NB |
194 | future. If the file isn't open, open it. The empty string is |
195 | interpreted as stdin. | |
2047e26f NB |
196 | |
197 | Returns an include_file structure with an open file descriptor on | |
198 | success, or NULL on failure. */ | |
add7091b | 199 | |
c31a6508 | 200 | static struct include_file * |
2047e26f | 201 | open_file (pfile, filename) |
c31a6508 ZW |
202 | cpp_reader *pfile; |
203 | const char *filename; | |
2047e26f | 204 | { |
a36c54fa NB |
205 | splay_tree_node nd = find_or_create_entry (pfile, filename); |
206 | struct include_file *file = (struct include_file *) nd->value; | |
add7091b | 207 | |
a36c54fa NB |
208 | /* Don't retry opening if we failed previously. */ |
209 | if (file->fd == -2) | |
210 | return 0; | |
2047e26f | 211 | |
a36c54fa NB |
212 | /* Don't reopen an idempotent file. */ |
213 | if (DO_NOT_REREAD (file)) | |
214 | return file; | |
def3263a | 215 | |
a36c54fa NB |
216 | /* Don't reopen one which is already loaded. */ |
217 | if (file->buffer != NULL) | |
218 | return file; | |
add7091b | 219 | |
c31a6508 ZW |
220 | /* We used to open files in nonblocking mode, but that caused more |
221 | problems than it solved. Do take care not to acquire a | |
222 | controlling terminal by mistake (this can't happen on sane | |
223 | systems, but paranoia is a virtue). | |
add7091b | 224 | |
c31a6508 ZW |
225 | Use the three-argument form of open even though we aren't |
226 | specifying O_CREAT, to defend against broken system headers. | |
add7091b | 227 | |
c31a6508 ZW |
228 | O_BINARY tells some runtime libraries (notably DJGPP) not to do |
229 | newline translation; we can handle DOS line breaks just fine | |
230 | ourselves. | |
b0699dad | 231 | |
c31a6508 | 232 | Special case: the empty string is translated to stdin. */ |
d4506961 | 233 | |
c31a6508 | 234 | if (filename[0] == '\0') |
2047e26f | 235 | file->fd = 0; |
f2d5f0cc | 236 | else |
2047e26f | 237 | file->fd = open (filename, O_RDONLY | O_NOCTTY | O_BINARY, 0666); |
a58d32c2 | 238 | |
2047e26f NB |
239 | if (file->fd != -1 && fstat (file->fd, &file->st) == 0) |
240 | { | |
241 | /* Mark a regular, zero-length file never-reread now. */ | |
242 | if (S_ISREG (file->st.st_mode) && file->st.st_size == 0) | |
def3263a | 243 | { |
642ce434 | 244 | _cpp_never_reread (file); |
def3263a NS |
245 | close (file->fd); |
246 | file->fd = -1; | |
247 | } | |
a58d32c2 | 248 | |
2047e26f NB |
249 | return file; |
250 | } | |
a58d32c2 ZW |
251 | |
252 | /* Don't issue an error message if the file doesn't exist. */ | |
253 | if (errno != ENOENT && errno != ENOTDIR) | |
254 | cpp_error_from_errno (pfile, filename); | |
255 | ||
2047e26f NB |
256 | /* Create a negative node for this path, and return null. */ |
257 | file->fd = -2; | |
258 | ||
a58d32c2 ZW |
259 | return 0; |
260 | } | |
261 | ||
3cf3593f NB |
262 | /* Place the file referenced by INC into a new buffer on PFILE's |
263 | stack. If there are errors, or the file should not be re-included, | |
264 | a null buffer is pushed. */ | |
a58d32c2 | 265 | |
3cf3593f | 266 | static void |
a58d32c2 ZW |
267 | stack_include_file (pfile, inc) |
268 | cpp_reader *pfile; | |
269 | struct include_file *inc; | |
270 | { | |
eb1f4d9d | 271 | size_t len = 0; |
a58d32c2 | 272 | cpp_buffer *fp; |
51d0f328 NB |
273 | int sysp, deps_sysp; |
274 | ||
275 | /* We'll try removing deps_sysp after the release of 3.0. */ | |
276 | deps_sysp = pfile->system_include_depth != 0; | |
277 | sysp = ((pfile->buffer && pfile->buffer->sysp) | |
278 | || (inc->foundhere && inc->foundhere->sysp)); | |
279 | ||
280 | /* For -M, add the file to the dependencies on its first inclusion. */ | |
281 | if (CPP_OPTION (pfile, print_deps) > deps_sysp && !inc->include_count) | |
282 | deps_add_dep (pfile->deps, inc->name); | |
283 | ||
284 | /* We don't want multiple include guard advice for the main file. */ | |
285 | if (pfile->buffer) | |
286 | inc->include_count++; | |
a58d32c2 | 287 | |
3cf3593f NB |
288 | /* Not in cache? */ |
289 | if (! inc->buffer) | |
290 | read_include_file (pfile, inc); | |
a58d32c2 | 291 | |
3cf3593f | 292 | if (! DO_NOT_REREAD (inc)) |
eb1f4d9d NB |
293 | len = inc->st.st_size; |
294 | ||
295 | /* Push a buffer. */ | |
296 | fp = cpp_push_buffer (pfile, inc->buffer, len, BUF_FILE, inc->name); | |
297 | fp->inc = inc; | |
3cf3593f | 298 | fp->inc->refcnt++; |
51d0f328 | 299 | fp->sysp = sysp; |
591e15a1 | 300 | fp->search_from = search_from (pfile, inc); |
a58d32c2 | 301 | |
3cf3593f NB |
302 | /* Initialise controlling macro state. */ |
303 | pfile->mi_state = MI_OUTSIDE; | |
304 | pfile->mi_cmacro = 0; | |
a58d32c2 | 305 | pfile->include_depth++; |
eb1f4d9d NB |
306 | |
307 | /* Generate the call back. */ | |
308 | fp->lineno = 0; | |
309 | _cpp_do_file_change (pfile, FC_ENTER, 0, 0); | |
310 | fp->lineno = 1; | |
a58d32c2 ZW |
311 | } |
312 | ||
313 | /* Read the file referenced by INC into the file cache. | |
314 | ||
315 | If fd points to a plain file, we might be able to mmap it; we can | |
316 | definitely allocate the buffer all at once. If fd is a pipe or | |
317 | terminal, we can't do either. If fd is something weird, like a | |
318 | block device or a directory, we don't want to read it at all. | |
319 | ||
320 | Unfortunately, different systems use different st.st_mode values | |
321 | for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and | |
322 | zero the entire struct stat except a couple fields. Hence we don't | |
323 | even try to figure out what something is, except for plain files, | |
324 | directories, and block devices. | |
325 | ||
326 | FIXME: Flush file cache and try again if we run out of memory. */ | |
327 | ||
3cf3593f | 328 | static void |
a58d32c2 ZW |
329 | read_include_file (pfile, inc) |
330 | cpp_reader *pfile; | |
331 | struct include_file *inc; | |
332 | { | |
333 | ssize_t size, offset, count; | |
334 | U_CHAR *buf; | |
335 | #if MMAP_THRESHOLD | |
336 | static int pagesize = -1; | |
337 | #endif | |
338 | ||
3cf3593f NB |
339 | if (DO_NOT_REREAD (inc)) |
340 | return; | |
341 | ||
a58d32c2 | 342 | if (S_ISREG (inc->st.st_mode)) |
f2d5f0cc | 343 | { |
a58d32c2 ZW |
344 | /* off_t might have a wider range than ssize_t - in other words, |
345 | the max size of a file might be bigger than the address | |
346 | space. We can't handle a file that large. (Anyone with | |
347 | a single source file bigger than 2GB needs to rethink | |
348 | their coding style.) Some systems (e.g. AIX 4.1) define | |
349 | SSIZE_MAX to be much smaller than the actual range of the | |
350 | type. Use INTTYPE_MAXIMUM unconditionally to ensure this | |
351 | does not bite us. */ | |
352 | if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t)) | |
f2d5f0cc | 353 | { |
a58d32c2 ZW |
354 | cpp_error (pfile, "%s is too large", inc->name); |
355 | goto fail; | |
f2d5f0cc | 356 | } |
a58d32c2 ZW |
357 | size = inc->st.st_size; |
358 | ||
ae0f4dee | 359 | inc->mapped = 0; |
a58d32c2 ZW |
360 | #if MMAP_THRESHOLD |
361 | if (pagesize == -1) | |
362 | pagesize = getpagesize (); | |
363 | ||
364 | if (size / pagesize >= MMAP_THRESHOLD) | |
365 | { | |
366 | buf = (U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0); | |
367 | if (buf == (U_CHAR *)-1) | |
368 | goto perror_fail; | |
369 | inc->mapped = 1; | |
370 | } | |
371 | else | |
c31a6508 | 372 | #endif |
d4506961 | 373 | { |
a58d32c2 ZW |
374 | buf = (U_CHAR *) xmalloc (size); |
375 | offset = 0; | |
376 | while (offset < size) | |
377 | { | |
378 | count = read (inc->fd, buf + offset, size - offset); | |
379 | if (count < 0) | |
380 | goto perror_fail; | |
381 | if (count == 0) | |
382 | { | |
383 | cpp_warning (pfile, "%s is shorter than expected", inc->name); | |
384 | break; | |
385 | } | |
386 | offset += count; | |
387 | } | |
d4506961 | 388 | } |
a58d32c2 ZW |
389 | } |
390 | else if (S_ISBLK (inc->st.st_mode)) | |
391 | { | |
392 | cpp_error (pfile, "%s is a block device", inc->name); | |
393 | goto fail; | |
394 | } | |
395 | else if (S_ISDIR (inc->st.st_mode)) | |
396 | { | |
397 | cpp_error (pfile, "%s is a directory", inc->name); | |
398 | goto fail; | |
399 | } | |
400 | else | |
401 | { | |
402 | /* 8 kilobytes is a sensible starting size. It ought to be | |
403 | bigger than the kernel pipe buffer, and it's definitely | |
404 | bigger than the majority of C source files. */ | |
405 | size = 8 * 1024; | |
d4506961 | 406 | |
a58d32c2 ZW |
407 | buf = (U_CHAR *) xmalloc (size); |
408 | offset = 0; | |
409 | while ((count = read (inc->fd, buf + offset, size - offset)) > 0) | |
f2d5f0cc | 410 | { |
a58d32c2 ZW |
411 | offset += count; |
412 | if (offset == size) | |
413 | buf = xrealloc (buf, (size *= 2)); | |
f2d5f0cc | 414 | } |
a58d32c2 ZW |
415 | if (count < 0) |
416 | goto perror_fail; | |
417 | ||
a58d32c2 ZW |
418 | if (offset < size) |
419 | buf = xrealloc (buf, offset); | |
420 | inc->st.st_size = offset; | |
f2d5f0cc | 421 | } |
d7a2e0f7 | 422 | |
a58d32c2 ZW |
423 | close (inc->fd); |
424 | inc->buffer = buf; | |
425 | inc->fd = -1; | |
3cf3593f | 426 | return; |
a58d32c2 ZW |
427 | |
428 | perror_fail: | |
429 | cpp_error_from_errno (pfile, inc->name); | |
430 | fail: | |
431 | /* Do not try to read this file again. */ | |
432 | close (inc->fd); | |
433 | inc->fd = -1; | |
642ce434 | 434 | _cpp_never_reread (inc); |
3cf3593f | 435 | return; |
a58d32c2 ZW |
436 | } |
437 | ||
438 | static void | |
439 | purge_cache (inc) | |
440 | struct include_file *inc; | |
441 | { | |
442 | if (inc->buffer) | |
c31a6508 | 443 | { |
ae0f4dee | 444 | #if MMAP_THRESHOLD |
a58d32c2 | 445 | if (inc->mapped) |
6f84c9bd | 446 | munmap ((PTR) inc->buffer, inc->st.st_size); |
a58d32c2 | 447 | else |
ae0f4dee | 448 | #endif |
a58d32c2 ZW |
449 | free ((PTR) inc->buffer); |
450 | inc->buffer = NULL; | |
c31a6508 | 451 | } |
e576beb0 ZW |
452 | } |
453 | ||
c31a6508 ZW |
454 | /* Return 1 if the file named by FNAME has been included before in |
455 | any context, 0 otherwise. */ | |
456 | int | |
457 | cpp_included (pfile, fname) | |
add7091b | 458 | cpp_reader *pfile; |
bcc5cac9 | 459 | const char *fname; |
add7091b | 460 | { |
591e15a1 | 461 | struct search_path *path; |
0b3d776a | 462 | char *name; |
c31a6508 | 463 | splay_tree_node nd; |
0b3d776a | 464 | |
05e81724 | 465 | if (IS_ABSOLUTE_PATHNAME (fname)) |
0b3d776a | 466 | { |
c31a6508 ZW |
467 | /* Just look it up. */ |
468 | nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname); | |
469 | return (nd && nd->value); | |
0b3d776a | 470 | } |
c31a6508 ZW |
471 | |
472 | /* Search directory path for the file. */ | |
b6464a73 | 473 | name = (char *) alloca (strlen (fname) + pfile->max_include_len + 2); |
c31a6508 | 474 | for (path = CPP_OPTION (pfile, quote_include); path; path = path->next) |
0b3d776a | 475 | { |
591e15a1 NB |
476 | memcpy (name, path->name, path->len); |
477 | name[path->len] = '/'; | |
478 | strcpy (&name[path->len + 1], fname); | |
c31a6508 ZW |
479 | _cpp_simplify_pathname (name); |
480 | if (CPP_OPTION (pfile, remap)) | |
481 | name = remap_filename (pfile, name, path); | |
482 | ||
483 | nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name); | |
484 | if (nd && nd->value) | |
485 | return 1; | |
0b3d776a | 486 | } |
c31a6508 | 487 | return 0; |
add7091b ZW |
488 | } |
489 | ||
c31a6508 ZW |
490 | /* Search for include file FNAME in the include chain starting at |
491 | SEARCH_START. Return 0 if there is no such file (or it's un-openable), | |
a58d32c2 | 492 | otherwise an include_file structure. */ |
c31a6508 ZW |
493 | |
494 | static struct include_file * | |
495 | find_include_file (pfile, fname, search_start) | |
f2d5f0cc ZW |
496 | cpp_reader *pfile; |
497 | const char *fname; | |
591e15a1 | 498 | struct search_path *search_start; |
f2d5f0cc | 499 | { |
591e15a1 | 500 | struct search_path *path; |
c31a6508 ZW |
501 | char *name; |
502 | struct include_file *file; | |
add7091b | 503 | |
05e81724 | 504 | if (IS_ABSOLUTE_PATHNAME (fname)) |
2047e26f | 505 | return open_file (pfile, fname); |
c31a6508 ZW |
506 | |
507 | /* Search directory path for the file. */ | |
b6464a73 | 508 | name = (char *) alloca (strlen (fname) + pfile->max_include_len + 2); |
c31a6508 | 509 | for (path = search_start; path; path = path->next) |
add7091b | 510 | { |
591e15a1 NB |
511 | memcpy (name, path->name, path->len); |
512 | name[path->len] = '/'; | |
513 | strcpy (&name[path->len + 1], fname); | |
c31a6508 ZW |
514 | _cpp_simplify_pathname (name); |
515 | if (CPP_OPTION (pfile, remap)) | |
516 | name = remap_filename (pfile, name, path); | |
517 | ||
2047e26f | 518 | file = open_file (pfile, name); |
c31a6508 | 519 | if (file) |
add7091b | 520 | { |
c31a6508 ZW |
521 | file->foundhere = path; |
522 | return file; | |
add7091b ZW |
523 | } |
524 | } | |
591e15a1 | 525 | |
c31a6508 | 526 | return 0; |
add7091b ZW |
527 | } |
528 | ||
e605b040 | 529 | /* Not everyone who wants to set system-header-ness on a buffer can |
642ce434 NB |
530 | see the details of a buffer. This is an exported interface because |
531 | fix-header needs it. */ | |
e605b040 | 532 | void |
614c7d37 | 533 | cpp_make_system_header (pfile, syshdr, externc) |
e605b040 | 534 | cpp_reader *pfile; |
614c7d37 | 535 | int syshdr, externc; |
e605b040 | 536 | { |
614c7d37 NB |
537 | int flags = 0; |
538 | ||
539 | /* 1 = system header, 2 = system header to be treated as C. */ | |
540 | if (syshdr) | |
541 | flags = 1 + (externc != 0); | |
642ce434 | 542 | pfile->buffer->sysp = flags; |
e4a345f8 NB |
543 | _cpp_do_file_change (pfile, FC_RENAME, pfile->buffer->nominal_fname, |
544 | pfile->buffer->lineno); | |
e605b040 ZW |
545 | } |
546 | ||
c71f835b ZW |
547 | /* Report on all files that might benefit from a multiple include guard. |
548 | Triggered by -H. */ | |
549 | void | |
550 | _cpp_report_missing_guards (pfile) | |
551 | cpp_reader *pfile; | |
552 | { | |
553 | int banner = 0; | |
554 | splay_tree_foreach (pfile->all_include_files, report_missing_guard, | |
555 | (PTR) &banner); | |
556 | } | |
557 | ||
558 | static int | |
559 | report_missing_guard (n, b) | |
560 | splay_tree_node n; | |
561 | void *b; | |
562 | { | |
563 | struct include_file *f = (struct include_file *) n->value; | |
564 | int *bannerp = (int *)b; | |
565 | ||
566 | if (f && f->cmacro == 0 && f->include_count == 1) | |
567 | { | |
568 | if (*bannerp == 0) | |
569 | { | |
570 | fputs (_("Multiple include guards may be useful for:\n"), stderr); | |
571 | *bannerp = 1; | |
572 | } | |
573 | fputs (f->name, stderr); | |
574 | putc ('\n', stderr); | |
575 | } | |
576 | return 0; | |
577 | } | |
578 | ||
a36c54fa NB |
579 | /* Create a dependency, or issue an error message as appropriate. */ |
580 | static void | |
581 | handle_missing_header (pfile, fname, angle_brackets) | |
582 | cpp_reader *pfile; | |
583 | const char *fname; | |
584 | int angle_brackets; | |
585 | { | |
586 | /* We will try making the RHS pfile->buffer->sysp after 3.0. */ | |
587 | int print_dep = CPP_PRINT_DEPS(pfile) > (angle_brackets | |
588 | || pfile->system_include_depth); | |
589 | if (CPP_OPTION (pfile, print_deps_missing_files) && print_dep) | |
590 | { | |
591 | if (!angle_brackets || IS_ABSOLUTE_PATHNAME (fname)) | |
592 | deps_add_dep (pfile->deps, fname); | |
593 | else | |
594 | { | |
595 | /* If requested as a system header, assume it belongs in | |
596 | the first system header directory. */ | |
597 | struct search_path *ptr = CPP_OPTION (pfile, bracket_include); | |
598 | char *p; | |
599 | int len = 0, fname_len = strlen (fname); | |
600 | ||
601 | if (ptr) | |
602 | len = ptr->len; | |
603 | ||
604 | p = (char *) alloca (len + fname_len + 2); | |
605 | if (len) | |
606 | { | |
607 | memcpy (p, ptr->name, len); | |
608 | p[len++] = '/'; | |
609 | } | |
610 | memcpy (p + len, fname, fname_len + 1); | |
611 | _cpp_simplify_pathname (p); | |
612 | deps_add_dep (pfile->deps, p); | |
613 | } | |
614 | } | |
615 | /* If -M was specified, and this header file won't be added to | |
616 | the dependency list, then don't count this as an error, | |
617 | because we can still produce correct output. Otherwise, we | |
618 | can't produce correct output, because there may be | |
619 | dependencies we need inside the missing file, and we don't | |
620 | know what directory this missing file exists in. */ | |
621 | else if (CPP_PRINT_DEPS (pfile) && ! print_dep) | |
622 | cpp_warning (pfile, "No include path in which to find %s", fname); | |
623 | else | |
624 | cpp_error_from_errno (pfile, fname); | |
625 | } | |
626 | ||
168d3732 | 627 | void |
642ce434 | 628 | _cpp_execute_include (pfile, header, no_reinclude, include_next) |
168d3732 | 629 | cpp_reader *pfile; |
93c80368 | 630 | const cpp_token *header; |
168d3732 | 631 | int no_reinclude; |
642ce434 | 632 | int include_next; |
168d3732 | 633 | { |
591e15a1 | 634 | struct search_path *search_start = 0; |
93c80368 | 635 | unsigned int angle_brackets = header->type == CPP_HEADER_NAME; |
7868b4a2 | 636 | const char *fname = (const char *) header->val.str.text; |
c31a6508 | 637 | struct include_file *inc; |
168d3732 | 638 | |
3cf3593f NB |
639 | /* Help protect #include or similar from recursion. */ |
640 | if (pfile->buffer_stack_depth >= CPP_STACK_MAX) | |
641 | { | |
642 | cpp_fatal (pfile, "#include nested too deeply"); | |
643 | return; | |
644 | } | |
645 | ||
646 | /* Check we've tidied up #include before entering the buffer. */ | |
647 | if (pfile->context->prev) | |
648 | { | |
649 | cpp_ice (pfile, "attempt to push file buffer with contexts stacked"); | |
650 | return; | |
651 | } | |
652 | ||
642ce434 NB |
653 | /* For #include_next, skip in the search path past the dir in which |
654 | the current file was found. If this is the last directory in the | |
655 | search path, don't include anything. If the current file was | |
656 | specified with an absolute path, use the normal search logic. If | |
657 | this is the primary source file, use the normal search logic and | |
658 | generate a warning. */ | |
659 | if (include_next) | |
660 | { | |
661 | if (! pfile->buffer->prev) | |
662 | cpp_warning (pfile, "#include_next in primary source file"); | |
663 | else | |
664 | { | |
665 | if (pfile->buffer->inc->foundhere) | |
666 | { | |
667 | search_start = pfile->buffer->inc->foundhere->next; | |
668 | if (! search_start) | |
669 | return; | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
168d3732 ZW |
674 | if (!search_start) |
675 | { | |
676 | if (angle_brackets) | |
ae79697b | 677 | search_start = CPP_OPTION (pfile, bracket_include); |
168d3732 | 678 | else |
591e15a1 | 679 | search_start = pfile->buffer->search_from; |
168d3732 | 680 | |
93c80368 NB |
681 | if (!search_start) |
682 | { | |
683 | cpp_error (pfile, "No include path in which to find %s", fname); | |
684 | return; | |
685 | } | |
168d3732 ZW |
686 | } |
687 | ||
c31a6508 | 688 | inc = find_include_file (pfile, fname, search_start); |
c31a6508 | 689 | if (inc) |
168d3732 | 690 | { |
3cf3593f NB |
691 | if (angle_brackets) |
692 | pfile->system_include_depth++; | |
a58d32c2 | 693 | |
51d0f328 NB |
694 | stack_include_file (pfile, inc); |
695 | ||
3cf3593f NB |
696 | if (! DO_NOT_REREAD (inc)) |
697 | { | |
a58d32c2 | 698 | if (no_reinclude) |
642ce434 | 699 | _cpp_never_reread (inc); |
a58d32c2 ZW |
700 | |
701 | /* Handle -H option. */ | |
702 | if (CPP_OPTION (pfile, print_include_names)) | |
703 | { | |
7868b4a2 NB |
704 | cpp_buffer *fp = pfile->buffer; |
705 | while ((fp = fp->prev) != NULL) | |
a58d32c2 ZW |
706 | putc ('.', stderr); |
707 | fprintf (stderr, " %s\n", inc->name); | |
708 | } | |
c31a6508 | 709 | } |
168d3732 | 710 | } |
c31a6508 | 711 | else |
a36c54fa | 712 | handle_missing_header (pfile, fname, angle_brackets); |
168d3732 ZW |
713 | } |
714 | ||
f3f751ad NS |
715 | /* Locate file F, and determine whether it is newer than PFILE. Return -1, |
716 | if F cannot be located or dated, 1, if it is newer and 0 if older. */ | |
f3f751ad | 717 | int |
93c80368 | 718 | _cpp_compare_file_date (pfile, f) |
f3f751ad | 719 | cpp_reader *pfile; |
93c80368 | 720 | const cpp_token *f; |
f3f751ad | 721 | { |
7868b4a2 | 722 | const char *fname = (const char *) f->val.str.text; |
591e15a1 | 723 | struct search_path *search_start; |
f3f751ad | 724 | struct include_file *inc; |
f3f751ad | 725 | |
93c80368 | 726 | if (f->type == CPP_HEADER_NAME) |
041c3194 | 727 | search_start = CPP_OPTION (pfile, bracket_include); |
b6464a73 | 728 | else |
591e15a1 | 729 | search_start = pfile->buffer->search_from; |
f3f751ad | 730 | |
f3f751ad NS |
731 | inc = find_include_file (pfile, fname, search_start); |
732 | ||
733 | if (!inc) | |
734 | return -1; | |
a58d32c2 | 735 | if (inc->fd > 0) |
f3f751ad | 736 | { |
f3f751ad NS |
737 | close (inc->fd); |
738 | inc->fd = -1; | |
739 | } | |
a58d32c2 | 740 | |
93c80368 | 741 | return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime; |
f3f751ad NS |
742 | } |
743 | ||
744 | ||
c45da1ca | 745 | /* Push an input buffer and load it up with the contents of FNAME. |
373e2177 | 746 | If FNAME is "", read standard input. */ |
c45da1ca | 747 | int |
614c7d37 | 748 | _cpp_read_file (pfile, fname) |
c45da1ca ZW |
749 | cpp_reader *pfile; |
750 | const char *fname; | |
751 | { | |
373e2177 | 752 | struct include_file *f = open_file (pfile, fname); |
c45da1ca | 753 | |
041c3194 ZW |
754 | if (f == NULL) |
755 | { | |
756 | cpp_error_from_errno (pfile, fname); | |
757 | return 0; | |
758 | } | |
759 | ||
3cf3593f NB |
760 | stack_include_file (pfile, f); |
761 | return 1; | |
f8f769ea ZW |
762 | } |
763 | ||
f9a0e96c ZW |
764 | /* Do appropriate cleanup when a file buffer is popped off the input |
765 | stack. */ | |
766 | void | |
767 | _cpp_pop_file_buffer (pfile, buf) | |
768 | cpp_reader *pfile; | |
769 | cpp_buffer *buf; | |
770 | { | |
771 | struct include_file *inc = buf->inc; | |
772 | ||
773 | if (pfile->system_include_depth) | |
774 | pfile->system_include_depth--; | |
775 | if (pfile->include_depth) | |
776 | pfile->include_depth--; | |
93c80368 NB |
777 | |
778 | /* Record the inclusion-preventing macro and its definedness. */ | |
779 | if (pfile->mi_state == MI_OUTSIDE && inc->cmacro != NEVER_REREAD) | |
f9a0e96c | 780 | { |
93c80368 NB |
781 | /* This could be NULL meaning no controlling macro. */ |
782 | inc->cmacro = pfile->mi_cmacro; | |
783 | inc->defined = 1; | |
f9a0e96c | 784 | } |
93c80368 NB |
785 | |
786 | /* Invalidate control macros in the #including file. */ | |
787 | pfile->mi_state = MI_FAILED; | |
f9a0e96c | 788 | |
a58d32c2 ZW |
789 | inc->refcnt--; |
790 | if (inc->refcnt == 0 && DO_NOT_REREAD (inc)) | |
791 | purge_cache (inc); | |
f9a0e96c ZW |
792 | } |
793 | ||
591e15a1 NB |
794 | /* Returns the first place in the include chain to start searching for |
795 | "" includes. This involves stripping away the basename of the | |
796 | current file, unless -I- was specified. */ | |
797 | static struct search_path * | |
798 | search_from (pfile, inc) | |
799 | cpp_reader *pfile; | |
800 | struct include_file *inc; | |
801 | { | |
802 | cpp_buffer *buffer = pfile->buffer; | |
803 | unsigned int dlen; | |
804 | ||
805 | /* Ignore the current file's directory if -I- was given. */ | |
806 | if (CPP_OPTION (pfile, ignore_srcdir)) | |
807 | return CPP_OPTION (pfile, quote_include); | |
808 | ||
2251fd78 | 809 | dlen = lbasename (inc->name) - inc->name; |
591e15a1 NB |
810 | if (dlen) |
811 | { | |
812 | /* We don't guarantee NAME is null-terminated. This saves | |
813 | allocating and freeing memory, and duplicating it when faking | |
814 | buffers in cpp_push_buffer. Drop a trailing '/'. */ | |
815 | buffer->dir.name = inc->name; | |
816 | if (dlen > 1) | |
817 | dlen--; | |
818 | } | |
819 | else | |
820 | { | |
821 | buffer->dir.name = "."; | |
822 | dlen = 1; | |
823 | } | |
824 | ||
825 | if (dlen > pfile->max_include_len) | |
826 | pfile->max_include_len = dlen; | |
827 | ||
828 | buffer->dir.len = dlen; | |
829 | buffer->dir.next = CPP_OPTION (pfile, quote_include); | |
830 | buffer->dir.sysp = buffer->sysp; | |
831 | ||
832 | return &buffer->dir; | |
833 | } | |
834 | ||
c31a6508 ZW |
835 | /* The file_name_map structure holds a mapping of file names for a |
836 | particular directory. This mapping is read from the file named | |
837 | FILE_NAME_MAP_FILE in that directory. Such a file can be used to | |
838 | map filenames on a file system with severe filename restrictions, | |
839 | such as DOS. The format of the file name map file is just a series | |
840 | of lines with two tokens on each line. The first token is the name | |
841 | to map, and the second token is the actual name to use. */ | |
842 | ||
843 | struct file_name_map | |
844 | { | |
845 | struct file_name_map *map_next; | |
846 | char *map_from; | |
847 | char *map_to; | |
848 | }; | |
849 | ||
850 | #define FILE_NAME_MAP_FILE "header.gcc" | |
851 | ||
852 | /* Read a space delimited string of unlimited length from a stdio | |
853 | file. */ | |
854 | ||
855 | static char * | |
856 | read_filename_string (ch, f) | |
857 | int ch; | |
858 | FILE *f; | |
859 | { | |
860 | char *alloc, *set; | |
861 | int len; | |
862 | ||
863 | len = 20; | |
864 | set = alloc = xmalloc (len + 1); | |
865 | if (! is_space(ch)) | |
866 | { | |
867 | *set++ = ch; | |
868 | while ((ch = getc (f)) != EOF && ! is_space(ch)) | |
869 | { | |
870 | if (set - alloc == len) | |
871 | { | |
872 | len *= 2; | |
873 | alloc = xrealloc (alloc, len + 1); | |
874 | set = alloc + len / 2; | |
875 | } | |
876 | *set++ = ch; | |
877 | } | |
878 | } | |
879 | *set = '\0'; | |
880 | ungetc (ch, f); | |
881 | return alloc; | |
882 | } | |
883 | ||
884 | /* This structure holds a linked list of file name maps, one per directory. */ | |
885 | ||
886 | struct file_name_map_list | |
887 | { | |
888 | struct file_name_map_list *map_list_next; | |
889 | char *map_list_name; | |
890 | struct file_name_map *map_list_map; | |
891 | }; | |
892 | ||
893 | /* Read the file name map file for DIRNAME. */ | |
894 | ||
895 | static struct file_name_map * | |
896 | read_name_map (pfile, dirname) | |
897 | cpp_reader *pfile; | |
898 | const char *dirname; | |
899 | { | |
900 | register struct file_name_map_list *map_list_ptr; | |
901 | char *name; | |
902 | FILE *f; | |
903 | ||
8767c894 | 904 | /* Check the cache of directories, and mappings in their remap file. */ |
c31a6508 ZW |
905 | for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr; |
906 | map_list_ptr = map_list_ptr->map_list_next) | |
907 | if (! strcmp (map_list_ptr->map_list_name, dirname)) | |
908 | return map_list_ptr->map_list_map; | |
909 | ||
910 | map_list_ptr = ((struct file_name_map_list *) | |
911 | xmalloc (sizeof (struct file_name_map_list))); | |
912 | map_list_ptr->map_list_name = xstrdup (dirname); | |
8767c894 NB |
913 | |
914 | /* The end of the list ends in NULL. */ | |
4b588241 | 915 | map_list_ptr->map_list_map = NULL; |
c31a6508 ZW |
916 | |
917 | name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2); | |
918 | strcpy (name, dirname); | |
919 | if (*dirname) | |
920 | strcat (name, "/"); | |
921 | strcat (name, FILE_NAME_MAP_FILE); | |
922 | f = fopen (name, "r"); | |
8767c894 NB |
923 | |
924 | /* Silently return NULL if we cannot open. */ | |
925 | if (f) | |
c31a6508 ZW |
926 | { |
927 | int ch; | |
928 | int dirlen = strlen (dirname); | |
929 | ||
930 | while ((ch = getc (f)) != EOF) | |
931 | { | |
932 | char *from, *to; | |
933 | struct file_name_map *ptr; | |
934 | ||
935 | if (is_space(ch)) | |
936 | continue; | |
937 | from = read_filename_string (ch, f); | |
938 | while ((ch = getc (f)) != EOF && is_hspace(ch)) | |
939 | ; | |
940 | to = read_filename_string (ch, f); | |
941 | ||
942 | ptr = ((struct file_name_map *) | |
943 | xmalloc (sizeof (struct file_name_map))); | |
944 | ptr->map_from = from; | |
945 | ||
946 | /* Make the real filename absolute. */ | |
05e81724 | 947 | if (IS_ABSOLUTE_PATHNAME (to)) |
c31a6508 ZW |
948 | ptr->map_to = to; |
949 | else | |
950 | { | |
951 | ptr->map_to = xmalloc (dirlen + strlen (to) + 2); | |
952 | strcpy (ptr->map_to, dirname); | |
953 | ptr->map_to[dirlen] = '/'; | |
954 | strcpy (ptr->map_to + dirlen + 1, to); | |
955 | free (to); | |
956 | } | |
957 | ||
958 | ptr->map_next = map_list_ptr->map_list_map; | |
959 | map_list_ptr->map_list_map = ptr; | |
960 | ||
961 | while ((ch = getc (f)) != '\n') | |
962 | if (ch == EOF) | |
963 | break; | |
964 | } | |
965 | fclose (f); | |
966 | } | |
967 | ||
8767c894 | 968 | /* Add this information to the cache. */ |
c31a6508 ZW |
969 | map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list); |
970 | CPP_OPTION (pfile, map_list) = map_list_ptr; | |
971 | ||
972 | return map_list_ptr->map_list_map; | |
973 | } | |
974 | ||
975 | /* Remap NAME based on the file_name_map (if any) for LOC. */ | |
976 | ||
977 | static char * | |
978 | remap_filename (pfile, name, loc) | |
979 | cpp_reader *pfile; | |
980 | char *name; | |
591e15a1 | 981 | struct search_path *loc; |
c31a6508 ZW |
982 | { |
983 | struct file_name_map *map; | |
8767c894 | 984 | const char *from, *p; |
591e15a1 NB |
985 | char *dir, *dname; |
986 | ||
987 | /* Get a null-terminated path. */ | |
988 | dname = alloca (loc->len + 1); | |
989 | memcpy (dname, loc->name, loc->len); | |
990 | dname[loc->len] = '\0'; | |
c31a6508 ZW |
991 | |
992 | if (! loc->name_map) | |
8767c894 | 993 | { |
591e15a1 | 994 | loc->name_map = read_name_map (pfile, dname); |
8767c894 NB |
995 | if (! loc->name_map) |
996 | return name; | |
997 | } | |
c31a6508 | 998 | |
591e15a1 NB |
999 | /* FIXME: this doesn't look right - NAME has been simplified. */ |
1000 | from = name + loc->len + 1; | |
c31a6508 ZW |
1001 | |
1002 | for (map = loc->name_map; map; map = map->map_next) | |
1003 | if (!strcmp (map->map_from, from)) | |
1004 | return map->map_to; | |
1005 | ||
1006 | /* Try to find a mapping file for the particular directory we are | |
1007 | looking in. Thus #include <sys/types.h> will look up sys/types.h | |
1008 | in /usr/include/header.gcc and look up types.h in | |
1009 | /usr/include/sys/header.gcc. */ | |
1010 | p = strrchr (name, '/'); | |
1011 | if (!p) | |
c31a6508 ZW |
1012 | return name; |
1013 | ||
8767c894 | 1014 | /* We know p != name as absolute paths don't call remap_filename. */ |
c31a6508 | 1015 | if (p == name) |
8767c894 NB |
1016 | cpp_ice (pfile, "absolute file name in remap_filename"); |
1017 | ||
1018 | dir = (char *) alloca (p - name + 1); | |
1019 | memcpy (dir, name, p - name); | |
1020 | dir[p - name] = '\0'; | |
1021 | from = p + 1; | |
c31a6508 ZW |
1022 | |
1023 | for (map = read_name_map (pfile, dir); map; map = map->map_next) | |
8767c894 | 1024 | if (! strcmp (map->map_from, from)) |
c31a6508 ZW |
1025 | return map->map_to; |
1026 | ||
1027 | return name; | |
1028 | } | |
1029 | ||
0b3d776a ZW |
1030 | /* Simplify a path name in place, deleting redundant components. This |
1031 | reduces OS overhead and guarantees that equivalent paths compare | |
1032 | the same (modulo symlinks). | |
1033 | ||
1034 | Transforms made: | |
1035 | foo/bar/../quux foo/quux | |
1036 | foo/./bar foo/bar | |
1037 | foo//bar foo/bar | |
1038 | /../quux /quux | |
1039 | //quux //quux (POSIX allows leading // as a namespace escape) | |
1040 | ||
1041 | Guarantees no trailing slashes. All transforms reduce the length | |
1042 | of the string. | |
1043 | */ | |
0b22d65c | 1044 | void |
b0699dad | 1045 | _cpp_simplify_pathname (path) |
21380ab0 | 1046 | char *path; |
0b3d776a ZW |
1047 | { |
1048 | char *from, *to; | |
1049 | char *base; | |
1050 | int absolute = 0; | |
1051 | ||
509781a4 | 1052 | #if defined (HAVE_DOS_BASED_FILE_SYSTEM) |
0b3d776a ZW |
1053 | /* Convert all backslashes to slashes. */ |
1054 | for (from = path; *from; from++) | |
1055 | if (*from == '\\') *from = '/'; | |
1056 | ||
1057 | /* Skip over leading drive letter if present. */ | |
1058 | if (ISALPHA (path[0]) && path[1] == ':') | |
1059 | from = to = &path[2]; | |
1060 | else | |
1061 | from = to = path; | |
1062 | #else | |
1063 | from = to = path; | |
1064 | #endif | |
1065 | ||
1066 | /* Remove redundant initial /s. */ | |
1067 | if (*from == '/') | |
1068 | { | |
1069 | absolute = 1; | |
1070 | to++; | |
1071 | from++; | |
1072 | if (*from == '/') | |
1073 | { | |
1074 | if (*++from == '/') | |
1075 | /* 3 or more initial /s are equivalent to 1 /. */ | |
1076 | while (*++from == '/'); | |
1077 | else | |
1078 | /* On some hosts // differs from /; Posix allows this. */ | |
1079 | to++; | |
1080 | } | |
1081 | } | |
1082 | base = to; | |
1083 | ||
1084 | for (;;) | |
1085 | { | |
1086 | while (*from == '/') | |
1087 | from++; | |
1088 | ||
1089 | if (from[0] == '.' && from[1] == '/') | |
1090 | from += 2; | |
1091 | else if (from[0] == '.' && from[1] == '\0') | |
1092 | goto done; | |
1093 | else if (from[0] == '.' && from[1] == '.' && from[2] == '/') | |
1094 | { | |
1095 | if (base == to) | |
1096 | { | |
1097 | if (absolute) | |
1098 | from += 3; | |
1099 | else | |
1100 | { | |
1101 | *to++ = *from++; | |
1102 | *to++ = *from++; | |
1103 | *to++ = *from++; | |
1104 | base = to; | |
1105 | } | |
1106 | } | |
1107 | else | |
1108 | { | |
1109 | to -= 2; | |
1110 | while (to > base && *to != '/') to--; | |
1111 | if (*to == '/') | |
1112 | to++; | |
1113 | from += 3; | |
1114 | } | |
1115 | } | |
1116 | else if (from[0] == '.' && from[1] == '.' && from[2] == '\0') | |
1117 | { | |
1118 | if (base == to) | |
1119 | { | |
1120 | if (!absolute) | |
1121 | { | |
1122 | *to++ = *from++; | |
1123 | *to++ = *from++; | |
1124 | } | |
1125 | } | |
1126 | else | |
1127 | { | |
1128 | to -= 2; | |
1129 | while (to > base && *to != '/') to--; | |
1130 | if (*to == '/') | |
1131 | to++; | |
1132 | } | |
1133 | goto done; | |
1134 | } | |
1135 | else | |
1136 | /* Copy this component and trailing /, if any. */ | |
1137 | while ((*to++ = *from++) != '/') | |
1138 | { | |
1139 | if (!to[-1]) | |
1140 | { | |
1141 | to--; | |
1142 | goto done; | |
1143 | } | |
1144 | } | |
1145 | ||
1146 | } | |
1147 | ||
1148 | done: | |
1149 | /* Trim trailing slash */ | |
1150 | if (to[0] == '/' && (!absolute || to > path+1)) | |
1151 | to--; | |
1152 | ||
1153 | /* Change the empty string to "." so that stat() on the result | |
1154 | will always work. */ | |
1155 | if (to == path) | |
1156 | *to++ = '.'; | |
1157 | ||
1158 | *to = '\0'; | |
1159 | ||
1160 | return; | |
1161 | } |