blob: dda7ac2e24ebb5b812a12fe7e996a4624e757ce0 [file] [log] [blame]
Zack Weinbergadd70911998-10-29 11:54:13 +00001/* Part of CPP library. (include file handling)
Neil Booth5793b272003-03-01 14:31:21 +00002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
Zack Weinbergadd70911998-10-29 11:54:13 +00004 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
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
Richard Kennere38992e2000-04-18 20:42:00 +000021Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
Zack Weinbergadd70911998-10-29 11:54:13 +000022
23#include "config.h"
24#include "system.h"
Geoffrey Keating17211ab2003-01-10 02:22:34 +000025#include <dirent.h>
Zack Weinberg4977bab2002-12-16 18:23:00 +000026#include "coretypes.h"
27#include "tm.h"
Zack Weinbergadd70911998-10-29 11:54:13 +000028#include "cpplib.h"
Zack Weinberg88ae23e2000-03-08 23:35:19 +000029#include "cpphash.h"
Zack Weinbergc1212d22000-02-06 23:46:18 +000030#include "intl.h"
Zack Weinberg168d3732000-03-14 06:34:11 +000031#include "mkdeps.h"
Zack Weinbergc31a6502000-06-21 18:33:51 +000032#include "splay-tree.h"
Hans-Peter Nilsson414d23a2002-11-20 19:49:53 +000033#ifdef ENABLE_VALGRIND_CHECKING
34#include <valgrind.h>
35#else
36/* Avoid #ifdef:s when we can help it. */
37#define VALGRIND_DISCARD(x)
38#endif
Zack Weinbergadd70911998-10-29 11:54:13 +000039
Zack Weinbergf8f769e2000-05-28 05:56:38 +000040#ifdef HAVE_MMAP_FILE
41# include <sys/mman.h>
42# ifndef MMAP_THRESHOLD
43# define MMAP_THRESHOLD 3 /* Minimum page count to mmap the file. */
44# endif
Christopher Faylor969815c2002-01-14 19:45:11 +000045# if MMAP_THRESHOLD
46# define TEST_THRESHOLD(size, pagesize) \
47 (size / pagesize >= MMAP_THRESHOLD && (size % pagesize) != 0)
48 /* Use mmap if the file is big enough to be worth it (controlled
49 by MMAP_THRESHOLD) and if we can safely count on there being
50 at least one readable NUL byte after the end of the file's
51 contents. This is true for all tested operating systems when
52 the file size is not an exact multiple of the page size. */
53# ifndef __CYGWIN__
54# define SHOULD_MMAP(size, pagesize) TEST_THRESHOLD (size, pagesize)
55# else
56# define WIN32_LEAN_AND_MEAN
57# include <windows.h>
58 /* Cygwin can't correctly emulate mmap under Windows 9x style systems so
59 disallow use of mmap on those systems. Windows 9x does not zero fill
60 memory at EOF and beyond, as required. */
61# define SHOULD_MMAP(size, pagesize) ((GetVersion() & 0x80000000) \
62 ? 0 : TEST_THRESHOLD (size, pagesize))
63# endif
64# endif
Zack Weinbergf8f769e2000-05-28 05:56:38 +000065
66#else /* No MMAP_FILE */
67# undef MMAP_THRESHOLD
68# define MMAP_THRESHOLD 0
69#endif
70
Zack Weinbergd7a2e0f2000-06-01 20:06:57 +000071#ifndef O_BINARY
72# define O_BINARY 0
73#endif
74
Zack Weinberga58d32c2000-09-12 03:42:30 +000075/* If errno is inspected immediately after a system call fails, it will be
76 nonzero, and no error number will ever be zero. */
77#ifndef ENOENT
78# define ENOENT 0
79#endif
80#ifndef ENOTDIR
81# define ENOTDIR 0
82#endif
Zack Weinberga58d32c2000-09-12 03:42:30 +000083
Zack Weinbergf9a0e962000-07-13 02:32:41 +000084/* Suppress warning about function macros used w/o arguments in traditional
85 C. It is unlikely that glibc's strcmp macro helps this file at all. */
86#undef strcmp
87
Neil Booth642ce432000-12-07 23:17:56 +000088/* This structure is used for the table of all includes. */
Kazu Hiratadc65cd62002-11-19 06:55:04 +000089struct include_file {
Neil Booth642ce432000-12-07 23:17:56 +000090 const char *name; /* actual path name of file */
Geoffrey Keating17211ab2003-01-10 02:22:34 +000091 const char *header_name; /* the original header found */
Neil Booth642ce432000-12-07 23:17:56 +000092 const cpp_hashnode *cmacro; /* macro, if any, preventing reinclusion. */
Neil Booth5793b272003-03-01 14:31:21 +000093 const struct cpp_path *foundhere;
Neil Booth642ce432000-12-07 23:17:56 +000094 /* location in search path where file was
95 found, for #include_next and sysp. */
96 const unsigned char *buffer; /* pointer to cached file contents */
97 struct stat st; /* copy of stat(2) data for file */
98 int fd; /* fd open on file (short term storage only) */
Neil Boothf277b5e2001-05-05 11:12:19 +000099 int err_no; /* errno obtained if opening a file failed */
Neil Booth642ce432000-12-07 23:17:56 +0000100 unsigned short include_count; /* number of times file has been read */
101 unsigned short refcnt; /* number of stacked buffers using this file */
102 unsigned char mapped; /* file buffer is mmapped */
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000103 unsigned char pch; /* 0: file not known to be a PCH.
104 1: file is a PCH
105 (on return from find_include_file).
106 2: file is not and never will be a valid
107 precompiled header.
108 3: file is always a valid precompiled
109 header. */
Neil Booth642ce432000-12-07 23:17:56 +0000110};
111
Douglas B Ruppae1139f2001-11-21 17:03:27 -0500112/* Variable length record files on VMS will have a stat size that includes
Kazu Hirata3ef42a02002-01-18 13:40:36 +0000113 record control characters that won't be included in the read size. */
Douglas B Ruppae1139f2001-11-21 17:03:27 -0500114#ifdef VMS
115# define FAB_C_VAR 2 /* variable length records (see Starlet fabdef.h) */
116# define STAT_SIZE_TOO_BIG(ST) ((ST).st_fab_rfm == FAB_C_VAR)
117#else
118# define STAT_SIZE_TOO_BIG(ST) 0
119#endif
120
Neil Booth28e0f042000-12-09 12:06:37 +0000121/* The cmacro works like this: If it's NULL, the file is to be
122 included again. If it's NEVER_REREAD, the file is never to be
123 included again. Otherwise it is a macro hashnode, and the file is
Neil Boothba133c92001-03-15 07:57:13 +0000124 to be included again if the macro is defined. */
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000125#define NEVER_REREAD ((const cpp_hashnode *) -1)
Neil Booth28e0f042000-12-09 12:06:37 +0000126#define DO_NOT_REREAD(inc) \
127((inc)->cmacro && ((inc)->cmacro == NEVER_REREAD \
Neil Boothba133c92001-03-15 07:57:13 +0000128 || (inc)->cmacro->type == NT_MACRO))
Neil Booth41947a52001-03-13 23:55:10 +0000129#define NO_INCLUDE_PATH ((struct include_file *) -1)
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000130#define INCLUDE_PCH_P(F) (((F)->pch & 1) != 0)
Neil Booth28e0f042000-12-09 12:06:37 +0000131
Zack Weinberga73ac7a2000-01-30 18:09:07 +0000132static struct file_name_map *read_name_map
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000133 PARAMS ((cpp_reader *, const char *));
134static char *read_filename_string PARAMS ((int, FILE *));
135static char *remap_filename PARAMS ((cpp_reader *, char *,
Neil Booth5793b272003-03-01 14:31:21 +0000136 struct cpp_path *));
137static struct cpp_path *search_from PARAMS ((cpp_reader *,
Neil Boothba133c92001-03-15 07:57:13 +0000138 enum include_type));
Neil Booth41947a52001-03-13 23:55:10 +0000139static struct include_file *
Neil Boothba133c92001-03-15 07:57:13 +0000140 find_include_file PARAMS ((cpp_reader *, const cpp_token *,
141 enum include_type));
Neil Booth2047e262000-09-21 18:01:22 +0000142static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000143static struct include_file *validate_pch PARAMS ((cpp_reader *,
144 const char *,
145 const char *));
146static struct include_file *open_file_pch PARAMS ((cpp_reader *,
147 const char *));
Neil Booth7c092712001-04-06 05:21:36 +0000148static int read_include_file PARAMS ((cpp_reader *, struct include_file *));
Neil Boothe5eba702001-08-21 19:23:24 +0000149static bool stack_include_file PARAMS ((cpp_reader *, struct include_file *));
Zack Weinberga58d32c2000-09-12 03:42:30 +0000150static void purge_cache PARAMS ((struct include_file *));
Neil Bootha36c54f2001-03-12 19:33:08 +0000151static void destroy_node PARAMS ((splay_tree_value));
Zack Weinbergc71f8352000-07-05 05:33:57 +0000152static int report_missing_guard PARAMS ((splay_tree_node, void *));
Neil Bootha36c54f2001-03-12 19:33:08 +0000153static splay_tree_node find_or_create_entry PARAMS ((cpp_reader *,
154 const char *));
155static void handle_missing_header PARAMS ((cpp_reader *, const char *, int));
Neil Booth986b1f12003-03-02 17:44:18 +0000156static int remove_component_p PARAMS ((const char *));
Zack Weinbergc31a6502000-06-21 18:33:51 +0000157
Neil Bootha36c54f2001-03-12 19:33:08 +0000158/* Set up the splay tree we use to store information about all the
159 file names seen in this compilation. We also have entries for each
160 file we tried to open but failed; this saves system calls since we
161 don't try to open it again in future.
Zack Weinbergc31a6502000-06-21 18:33:51 +0000162
Neil Bootha36c54f2001-03-12 19:33:08 +0000163 The key of each node is the file name, after processing by
Neil Booth986b1f12003-03-02 17:44:18 +0000164 cpp_simplify_path. The path name may or may not be absolute.
Neil Bootha36c54f2001-03-12 19:33:08 +0000165 The path string has been malloced, as is automatically freed by
166 registering free () as the splay tree key deletion function.
Neil Boothd6d52dd2001-01-13 18:39:26 +0000167
Neil Bootha36c54f2001-03-12 19:33:08 +0000168 A node's value is a pointer to a struct include_file, and is never
169 NULL. */
Zack Weinbergd35364d2000-03-12 23:46:05 +0000170void
Zack Weinbergc71f8352000-07-05 05:33:57 +0000171_cpp_init_includes (pfile)
Zack Weinbergd35364d2000-03-12 23:46:05 +0000172 cpp_reader *pfile;
173{
174 pfile->all_include_files
Zack Weinbergc31a6502000-06-21 18:33:51 +0000175 = splay_tree_new ((splay_tree_compare_fn) strcmp,
176 (splay_tree_delete_key_fn) free,
Neil Bootha36c54f2001-03-12 19:33:08 +0000177 destroy_node);
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000178}
179
Neil Bootha36c54f2001-03-12 19:33:08 +0000180/* Tear down the splay tree. */
Zack Weinbergc71f8352000-07-05 05:33:57 +0000181void
182_cpp_cleanup_includes (pfile)
183 cpp_reader *pfile;
184{
185 splay_tree_delete (pfile->all_include_files);
186}
187
Neil Bootha36c54f2001-03-12 19:33:08 +0000188/* Free a node. The path string is automatically freed. */
189static void
190destroy_node (v)
191 splay_tree_value v;
192{
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000193 struct include_file *f = (struct include_file *) v;
Neil Bootha36c54f2001-03-12 19:33:08 +0000194
195 if (f)
196 {
197 purge_cache (f);
198 free (f);
199 }
200}
201
Neil Booth642ce432000-12-07 23:17:56 +0000202/* Mark a file to not be reread (e.g. #import, read failure). */
203void
204_cpp_never_reread (file)
205 struct include_file *file;
206{
207 file->cmacro = NEVER_REREAD;
208}
209
Neil Boothba133c92001-03-15 07:57:13 +0000210/* Lookup a filename, which is simplified after making a copy, and
Neil Booth5793b272003-03-01 14:31:21 +0000211 create an entry if none exists. */
Neil Bootha36c54f2001-03-12 19:33:08 +0000212static splay_tree_node
213find_or_create_entry (pfile, fname)
214 cpp_reader *pfile;
215 const char *fname;
216{
217 splay_tree_node node;
218 struct include_file *file;
Neil Boothba133c92001-03-15 07:57:13 +0000219 char *name = xstrdup (fname);
Neil Bootha36c54f2001-03-12 19:33:08 +0000220
Neil Booth986b1f12003-03-02 17:44:18 +0000221 cpp_simplify_path (name);
Neil Boothba133c92001-03-15 07:57:13 +0000222 node = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
223 if (node)
224 free (name);
225 else
Neil Bootha36c54f2001-03-12 19:33:08 +0000226 {
227 file = xcnew (struct include_file);
Neil Boothba133c92001-03-15 07:57:13 +0000228 file->name = name;
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000229 file->header_name = name;
Neil Booth986b1f12003-03-02 17:44:18 +0000230 file->err_no = errno;
Neil Bootha36c54f2001-03-12 19:33:08 +0000231 node = splay_tree_insert (pfile->all_include_files,
232 (splay_tree_key) file->name,
233 (splay_tree_value) file);
234 }
235
236 return node;
237}
238
Neil Boothba133c92001-03-15 07:57:13 +0000239/* Enter a file name in the splay tree, for the sake of cpp_included. */
Neil Boothd6d52dd2001-01-13 18:39:26 +0000240void
241_cpp_fake_include (pfile, fname)
242 cpp_reader *pfile;
243 const char *fname;
244{
Neil Bootha36c54f2001-03-12 19:33:08 +0000245 find_or_create_entry (pfile, fname);
Neil Boothd6d52dd2001-01-13 18:39:26 +0000246}
247
Zack Weinberga58d32c2000-09-12 03:42:30 +0000248/* Given a file name, look it up in the cache; if there is no entry,
Neil Booth2047e262000-09-21 18:01:22 +0000249 create one with a non-NULL value (regardless of success in opening
250 the file). If the file doesn't exist or is inaccessible, this
251 entry is flagged so we don't attempt to open it again in the
Neil Booth373e2172001-02-21 07:29:56 +0000252 future. If the file isn't open, open it. The empty string is
253 interpreted as stdin.
Neil Booth2047e262000-09-21 18:01:22 +0000254
255 Returns an include_file structure with an open file descriptor on
256 success, or NULL on failure. */
Zack Weinbergc31a6502000-06-21 18:33:51 +0000257static struct include_file *
Neil Booth2047e262000-09-21 18:01:22 +0000258open_file (pfile, filename)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000259 cpp_reader *pfile;
260 const char *filename;
Neil Booth2047e262000-09-21 18:01:22 +0000261{
Neil Bootha36c54f2001-03-12 19:33:08 +0000262 splay_tree_node nd = find_or_create_entry (pfile, filename);
263 struct include_file *file = (struct include_file *) nd->value;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000264
Neil Boothf277b5e2001-05-05 11:12:19 +0000265 if (file->err_no)
266 {
267 /* Ugh. handle_missing_header () needs errno to be set. */
268 errno = file->err_no;
269 return 0;
270 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000271
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000272 /* Don't reopen an idempotent file. */
Neil Bootha36c54f2001-03-12 19:33:08 +0000273 if (DO_NOT_REREAD (file))
274 return file;
Kazu Hiratadf383482002-05-22 22:02:16 +0000275
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000276 /* Don't reopen one which is already loaded. */
Neil Bootha36c54f2001-03-12 19:33:08 +0000277 if (file->buffer != NULL)
278 return file;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000279
280 /* We used to open files in nonblocking mode, but that caused more
281 problems than it solved. Do take care not to acquire a
282 controlling terminal by mistake (this can't happen on sane
283 systems, but paranoia is a virtue).
284
285 Use the three-argument form of open even though we aren't
286 specifying O_CREAT, to defend against broken system headers.
287
288 O_BINARY tells some runtime libraries (notably DJGPP) not to do
289 newline translation; we can handle DOS line breaks just fine
290 ourselves.
291
292 Special case: the empty string is translated to stdin. */
Zack Weinbergd4506962000-06-28 19:03:08 +0000293
Zack Weinbergc31a6502000-06-21 18:33:51 +0000294 if (filename[0] == '\0')
Andris Pavenis85be8c22002-05-13 23:28:28 +0300295 {
296 file->fd = 0;
297#ifdef __DJGPP__
298 /* For DJGPP redirected input is opened in text mode. Change it
299 to binary mode. */
300 if (! isatty (file->fd))
Kazu Hiratadf383482002-05-22 22:02:16 +0000301 setmode (file->fd, O_BINARY);
Andris Pavenis85be8c22002-05-13 23:28:28 +0300302#endif
303 }
Zack Weinbergc31a6502000-06-21 18:33:51 +0000304 else
Neil Boothba133c92001-03-15 07:57:13 +0000305 file->fd = open (file->name, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
Zack Weinbergd4506962000-06-28 19:03:08 +0000306
Neil Booth2047e262000-09-21 18:01:22 +0000307 if (file->fd != -1 && fstat (file->fd, &file->st) == 0)
308 {
Neil Boothc0bfe992001-10-08 17:54:15 +0000309 if (!S_ISDIR (file->st.st_mode))
310 return file;
Zack Weinberg55485cd2001-12-06 16:41:18 +0000311
Neil Booth7c092712001-04-06 05:21:36 +0000312 /* If it's a directory, we return null and continue the search
313 as the file we're looking for may appear elsewhere in the
314 search path. */
Neil Boothc0bfe992001-10-08 17:54:15 +0000315 errno = ENOENT;
Zack Weinberg55485cd2001-12-06 16:41:18 +0000316 close (file->fd);
317 file->fd = -1;
Neil Booth2047e262000-09-21 18:01:22 +0000318 }
Zack Weinberga58d32c2000-09-12 03:42:30 +0000319
Neil Boothf277b5e2001-05-05 11:12:19 +0000320 file->err_no = errno;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000321 return 0;
322}
323
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000324static struct include_file *
325validate_pch (pfile, filename, pchname)
326 cpp_reader *pfile;
327 const char *filename;
328 const char *pchname;
329{
330 struct include_file * file;
331
332 file = open_file (pfile, pchname);
333 if (file == NULL)
334 return NULL;
335 if ((file->pch & 2) == 0)
336 file->pch = pfile->cb.valid_pch (pfile, pchname, file->fd);
337 if (INCLUDE_PCH_P (file))
338 {
Neil Booth5793b272003-03-01 14:31:21 +0000339 char *f = xstrdup (filename);
Neil Booth986b1f12003-03-02 17:44:18 +0000340 cpp_simplify_path (f);
Neil Booth5793b272003-03-01 14:31:21 +0000341 file->header_name = f;
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000342 return file;
343 }
344 close (file->fd);
345 file->fd = -1;
346 return NULL;
347}
348
349
350/* Like open_file, but also look for a precompiled header if (a) one exists
351 and (b) it is valid. */
352static struct include_file *
353open_file_pch (pfile, filename)
354 cpp_reader *pfile;
355 const char *filename;
356{
357 if (filename[0] != '\0'
358 && pfile->cb.valid_pch != NULL)
359 {
360 size_t namelen = strlen (filename);
361 char *pchname = alloca (namelen + 5);
362 struct include_file * file;
363 splay_tree_node nd;
364
365 memcpy (pchname, filename, namelen);
Geoffrey Keatingd8fad4e2003-02-28 23:06:10 +0000366 memcpy (pchname + namelen, ".gch", 5);
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000367
368 nd = find_or_create_entry (pfile, pchname);
369 file = (struct include_file *) nd->value;
370
371 if (file != NULL)
372 {
373 if (stat (file->name, &file->st) == 0 && S_ISDIR (file->st.st_mode))
374 {
375 DIR * thedir;
376 struct dirent *d;
377 size_t subname_len = namelen + 64;
378 char *subname = xmalloc (subname_len);
379
380 thedir = opendir (pchname);
381 if (thedir == NULL)
382 return NULL;
383 memcpy (subname, pchname, namelen + 4);
384 subname[namelen+4] = '/';
385 while ((d = readdir (thedir)) != NULL)
386 {
387 if (strlen (d->d_name) + namelen + 7 > subname_len)
388 {
389 subname_len = strlen (d->d_name) + namelen + 64;
390 subname = xrealloc (subname, subname_len);
391 }
392 strcpy (subname + namelen + 5, d->d_name);
393 file = validate_pch (pfile, filename, subname);
394 if (file)
395 break;
396 }
397 closedir (thedir);
398 free (subname);
399 }
400 else
401 file = validate_pch (pfile, filename, pchname);
402 if (file)
403 return file;
404 }
405 }
406 return open_file (pfile, filename);
407}
408
Neil Boothe5eba702001-08-21 19:23:24 +0000409/* Place the file referenced by INC into a new buffer on the buffer
410 stack, unless there are errors, or the file is not re-included
411 because of e.g. multiple-include guards. Returns true if a buffer
412 is stacked. */
Neil Boothe5eba702001-08-21 19:23:24 +0000413static bool
Zack Weinberga58d32c2000-09-12 03:42:30 +0000414stack_include_file (pfile, inc)
415 cpp_reader *pfile;
416 struct include_file *inc;
417{
418 cpp_buffer *fp;
Neil Boothe5eba702001-08-21 19:23:24 +0000419 int sysp;
Neil Boothbb74c962001-08-17 22:23:49 +0000420 const char *filename;
Neil Booth51d0f322001-02-19 19:50:21 +0000421
Neil Boothe5eba702001-08-21 19:23:24 +0000422 if (DO_NOT_REREAD (inc))
423 return false;
424
Neil Booth47d89cf2001-08-11 07:33:39 +0000425 sysp = MAX ((pfile->map ? pfile->map->sysp : 0),
Alexandre Oliva11bca302001-03-16 05:19:46 +0000426 (inc->foundhere ? inc->foundhere->sysp : 0));
Neil Booth51d0f322001-02-19 19:50:21 +0000427
Neil Booth182d89a2002-08-14 22:34:50 +0000428 /* Add the file to the dependencies on its first inclusion. */
Neil Boothf4ff5a62002-08-12 22:44:30 +0000429 if (CPP_OPTION (pfile, deps.style) > !!sysp && !inc->include_count)
Neil Booth182d89a2002-08-14 22:34:50 +0000430 {
431 if (pfile->buffer || CPP_OPTION (pfile, deps.ignore_main_file) == 0)
432 deps_add_dep (pfile->deps, inc->name);
433 }
Neil Booth51d0f322001-02-19 19:50:21 +0000434
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000435 /* PCH files get dealt with immediately. */
436 if (INCLUDE_PCH_P (inc))
437 {
438 pfile->cb.read_pch (pfile, inc->name, inc->fd, inc->header_name);
439 close (inc->fd);
440 inc->fd = -1;
441 return false;
442 }
443
Neil Booth7c092712001-04-06 05:21:36 +0000444 /* Not in cache? */
Neil Boothe5eba702001-08-21 19:23:24 +0000445 if (! inc->buffer)
Neil Booth7c092712001-04-06 05:21:36 +0000446 {
Neil Booth4d6baaf2001-11-26 23:44:54 +0000447 if (read_include_file (pfile, inc))
Neil Boothe5eba702001-08-21 19:23:24 +0000448 {
Neil Boothc0bfe992001-10-08 17:54:15 +0000449 /* If an error occurs, do not try to read this file again. */
Neil Boothe5eba702001-08-21 19:23:24 +0000450 _cpp_never_reread (inc);
451 return false;
452 }
Neil Booth4d6baaf2001-11-26 23:44:54 +0000453 /* Mark a regular, zero-length file never-reread. We read it,
454 NUL-terminate it, and stack it once, so preprocessing a main
455 file of zero length does not raise an error. */
456 if (S_ISREG (inc->st.st_mode) && inc->st.st_size == 0)
457 _cpp_never_reread (inc);
Neil Booth7c092712001-04-06 05:21:36 +0000458 close (inc->fd);
459 inc->fd = -1;
460 }
461
Neil Boothe5eba702001-08-21 19:23:24 +0000462 if (pfile->buffer)
Neil Booth59930192001-08-21 21:17:48 +0000463 /* We don't want MI guard advice for the main file. */
464 inc->include_count++;
Neil Bootheb1f4d92000-12-18 19:00:26 +0000465
466 /* Push a buffer. */
Neil Booth29401c32001-08-22 20:37:20 +0000467 fp = cpp_push_buffer (pfile, inc->buffer, inc->st.st_size,
468 /* from_stage3 */ CPP_OPTION (pfile, preprocessed), 0);
Neil Bootheb1f4d92000-12-18 19:00:26 +0000469 fp->inc = inc;
Neil Booth3cf35932000-12-05 23:42:43 +0000470 fp->inc->refcnt++;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000471
Kazu Hirata4912a072002-09-14 15:51:45 +0000472 /* Initialize controlling macro state. */
Neil Booth6d18adb2001-07-29 17:27:57 +0000473 pfile->mi_valid = true;
Neil Booth3cf35932000-12-05 23:42:43 +0000474 pfile->mi_cmacro = 0;
Neil Bootheb1f4d92000-12-18 19:00:26 +0000475
476 /* Generate the call back. */
Neil Boothbb74c962001-08-17 22:23:49 +0000477 filename = inc->name;
478 if (*filename == '\0')
Philipp Thomas0b264062001-12-28 00:09:47 +0000479 filename = "<stdin>";
Neil Boothbb74c962001-08-17 22:23:49 +0000480 _cpp_do_file_change (pfile, LC_ENTER, filename, 1, sysp);
Neil Boothe5eba702001-08-21 19:23:24 +0000481
482 return true;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000483}
484
485/* Read the file referenced by INC into the file cache.
486
487 If fd points to a plain file, we might be able to mmap it; we can
488 definitely allocate the buffer all at once. If fd is a pipe or
489 terminal, we can't do either. If fd is something weird, like a
Neil Booth7c092712001-04-06 05:21:36 +0000490 block device, we don't want to read it at all.
Zack Weinberga58d32c2000-09-12 03:42:30 +0000491
492 Unfortunately, different systems use different st.st_mode values
493 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
494 zero the entire struct stat except a couple fields. Hence we don't
Neil Booth7c092712001-04-06 05:21:36 +0000495 even try to figure out what something is, except for plain files
496 and block devices.
Zack Weinberga58d32c2000-09-12 03:42:30 +0000497
498 FIXME: Flush file cache and try again if we run out of memory. */
Neil Booth7c092712001-04-06 05:21:36 +0000499static int
Zack Weinberga58d32c2000-09-12 03:42:30 +0000500read_include_file (pfile, inc)
501 cpp_reader *pfile;
502 struct include_file *inc;
503{
504 ssize_t size, offset, count;
Neil Booth562a5c22002-04-21 18:46:42 +0000505 uchar *buf;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000506#if MMAP_THRESHOLD
507 static int pagesize = -1;
508#endif
509
510 if (S_ISREG (inc->st.st_mode))
511 {
512 /* off_t might have a wider range than ssize_t - in other words,
513 the max size of a file might be bigger than the address
514 space. We can't handle a file that large. (Anyone with
515 a single source file bigger than 2GB needs to rethink
516 their coding style.) Some systems (e.g. AIX 4.1) define
517 SSIZE_MAX to be much smaller than the actual range of the
518 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
519 does not bite us. */
520 if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t))
521 {
Neil Boothebef4e82002-04-14 18:42:47 +0000522 cpp_error (pfile, DL_ERROR, "%s is too large", inc->name);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000523 goto fail;
524 }
525 size = inc->st.st_size;
526
Laurynas Biveinisae0f4de2000-09-16 18:17:53 +0000527 inc->mapped = 0;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000528#if MMAP_THRESHOLD
529 if (pagesize == -1)
530 pagesize = getpagesize ();
531
Christopher Faylor969815c2002-01-14 19:45:11 +0000532 if (SHOULD_MMAP (size, pagesize))
Zack Weinberga58d32c2000-09-12 03:42:30 +0000533 {
Neil Booth562a5c22002-04-21 18:46:42 +0000534 buf = (uchar *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0);
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000535 if (buf == (uchar *) -1)
Zack Weinberga58d32c2000-09-12 03:42:30 +0000536 goto perror_fail;
Hans-Peter Nilsson414d23a2002-11-20 19:49:53 +0000537
538 /* We must tell Valgrind that the byte at buf[size] is actually
539 readable. Discard the handle to avoid handle leak. */
540 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (buf + size, 1));
541
Zack Weinberga58d32c2000-09-12 03:42:30 +0000542 inc->mapped = 1;
543 }
544 else
545#endif
546 {
Neil Booth562a5c22002-04-21 18:46:42 +0000547 buf = (uchar *) xmalloc (size + 1);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000548 offset = 0;
549 while (offset < size)
550 {
551 count = read (inc->fd, buf + offset, size - offset);
552 if (count < 0)
553 goto perror_fail;
554 if (count == 0)
555 {
Douglas B Ruppae1139f2001-11-21 17:03:27 -0500556 if (!STAT_SIZE_TOO_BIG (inc->st))
Neil Boothebef4e82002-04-14 18:42:47 +0000557 cpp_error (pfile, DL_WARNING,
558 "%s is shorter than expected", inc->name);
Douglas B Ruppcdb29052001-12-12 07:42:03 -0500559 size = offset;
560 buf = xrealloc (buf, size + 1);
561 inc->st.st_size = size;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000562 break;
563 }
564 offset += count;
565 }
Neil Booth4d6baaf2001-11-26 23:44:54 +0000566 /* The lexer requires that the buffer be NUL-terminated. */
567 buf[size] = '\0';
Zack Weinberga58d32c2000-09-12 03:42:30 +0000568 }
569 }
570 else if (S_ISBLK (inc->st.st_mode))
571 {
Neil Boothebef4e82002-04-14 18:42:47 +0000572 cpp_error (pfile, DL_ERROR, "%s is a block device", inc->name);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000573 goto fail;
574 }
Zack Weinberga58d32c2000-09-12 03:42:30 +0000575 else
576 {
577 /* 8 kilobytes is a sensible starting size. It ought to be
578 bigger than the kernel pipe buffer, and it's definitely
579 bigger than the majority of C source files. */
580 size = 8 * 1024;
581
Neil Booth562a5c22002-04-21 18:46:42 +0000582 buf = (uchar *) xmalloc (size + 1);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000583 offset = 0;
584 while ((count = read (inc->fd, buf + offset, size - offset)) > 0)
585 {
586 offset += count;
587 if (offset == size)
Neil Booth4d6baaf2001-11-26 23:44:54 +0000588 {
589 size *= 2;
590 buf = xrealloc (buf, size + 1);
591 }
Zack Weinberga58d32c2000-09-12 03:42:30 +0000592 }
593 if (count < 0)
594 goto perror_fail;
595
Neil Booth4d6baaf2001-11-26 23:44:54 +0000596 if (offset + 1 < size)
597 buf = xrealloc (buf, offset + 1);
598
599 /* The lexer requires that the buffer be NUL-terminated. */
600 buf[offset] = '\0';
Zack Weinberga58d32c2000-09-12 03:42:30 +0000601 inc->st.st_size = offset;
602 }
603
Zack Weinberga58d32c2000-09-12 03:42:30 +0000604 inc->buffer = buf;
Neil Booth7c092712001-04-06 05:21:36 +0000605 return 0;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000606
607 perror_fail:
Neil Boothebef4e82002-04-14 18:42:47 +0000608 cpp_errno (pfile, DL_ERROR, inc->name);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000609 fail:
Neil Booth7c092712001-04-06 05:21:36 +0000610 return 1;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000611}
612
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000613/* Drop INC's buffer from memory, if we are unlikely to need it again. */
Zack Weinberga58d32c2000-09-12 03:42:30 +0000614static void
615purge_cache (inc)
616 struct include_file *inc;
617{
618 if (inc->buffer)
619 {
Laurynas Biveinisae0f4de2000-09-16 18:17:53 +0000620#if MMAP_THRESHOLD
Zack Weinberga58d32c2000-09-12 03:42:30 +0000621 if (inc->mapped)
Hans-Peter Nilsson414d23a2002-11-20 19:49:53 +0000622 {
623 /* Undo the previous annotation for the
624 known-zero-byte-after-mmap. Discard the handle to avoid
625 handle leak. */
626 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (inc->buffer
627 + inc->st.st_size, 1));
628 munmap ((PTR) inc->buffer, inc->st.st_size);
629 }
Zack Weinberga58d32c2000-09-12 03:42:30 +0000630 else
Laurynas Biveinisae0f4de2000-09-16 18:17:53 +0000631#endif
Zack Weinberga58d32c2000-09-12 03:42:30 +0000632 free ((PTR) inc->buffer);
633 inc->buffer = NULL;
634 }
Zack Weinbergadd70911998-10-29 11:54:13 +0000635}
636
Zack Weinbergb0699da2000-03-07 20:58:47 +0000637/* Return 1 if the file named by FNAME has been included before in
638 any context, 0 otherwise. */
639int
640cpp_included (pfile, fname)
641 cpp_reader *pfile;
642 const char *fname;
643{
Neil Booth5793b272003-03-01 14:31:21 +0000644 struct cpp_path *path;
Neil Boothe7182662001-03-14 19:35:08 +0000645 char *name, *n;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000646 splay_tree_node nd;
Zack Weinbergb0699da2000-03-07 20:58:47 +0000647
Neil Booth05e81722001-01-11 21:30:16 +0000648 if (IS_ABSOLUTE_PATHNAME (fname))
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000649 {
Zack Weinbergc31a6502000-06-21 18:33:51 +0000650 /* Just look it up. */
651 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
652 return (nd && nd->value);
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000653 }
Kazu Hiratadf383482002-05-22 22:02:16 +0000654
Zack Weinbergc31a6502000-06-21 18:33:51 +0000655 /* Search directory path for the file. */
Neil Boothb6464a72001-03-11 14:54:56 +0000656 name = (char *) alloca (strlen (fname) + pfile->max_include_len + 2);
Neil Booth5793b272003-03-01 14:31:21 +0000657 for (path = pfile->quote_include; path; path = path->next)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000658 {
Neil Booth591e15a2001-03-02 07:35:12 +0000659 memcpy (name, path->name, path->len);
660 name[path->len] = '/';
661 strcpy (&name[path->len + 1], fname);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000662 if (CPP_OPTION (pfile, remap))
Neil Boothe7182662001-03-14 19:35:08 +0000663 n = remap_filename (pfile, name, path);
664 else
Neil Boothba133c92001-03-15 07:57:13 +0000665 n = name;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000666
Neil Boothe7182662001-03-14 19:35:08 +0000667 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) n);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000668 if (nd && nd->value)
669 return 1;
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000670 }
Zack Weinbergc31a6502000-06-21 18:33:51 +0000671 return 0;
Zack Weinberge576beb2000-03-15 22:03:37 +0000672}
673
Neil Booth41947a52001-03-13 23:55:10 +0000674/* Search for HEADER. Return 0 if there is no such file (or it's
675 un-openable), in which case an error code will be in errno. If
676 there is no include path to use it returns NO_INCLUDE_PATH,
677 otherwise an include_file structure. If this request originates
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000678 from a directive of TYPE #include_next, set INCLUDE_NEXT to true. */
Zack Weinbergc31a6502000-06-21 18:33:51 +0000679static struct include_file *
Neil Boothba133c92001-03-15 07:57:13 +0000680find_include_file (pfile, header, type)
Zack Weinbergadd70911998-10-29 11:54:13 +0000681 cpp_reader *pfile;
Neil Booth41947a52001-03-13 23:55:10 +0000682 const cpp_token *header;
Neil Boothba133c92001-03-15 07:57:13 +0000683 enum include_type type;
Zack Weinbergadd70911998-10-29 11:54:13 +0000684{
Neil Booth41947a52001-03-13 23:55:10 +0000685 const char *fname = (const char *) header->val.str.text;
Neil Booth5793b272003-03-01 14:31:21 +0000686 struct cpp_path *path;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000687 struct include_file *file;
Neil Boothe7182662001-03-14 19:35:08 +0000688 char *name, *n;
Zack Weinbergadd70911998-10-29 11:54:13 +0000689
Neil Booth05e81722001-01-11 21:30:16 +0000690 if (IS_ABSOLUTE_PATHNAME (fname))
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000691 return open_file_pch (pfile, fname);
Neil Booth41947a52001-03-13 23:55:10 +0000692
693 /* For #include_next, skip in the search path past the dir in which
Neil Boothe7182662001-03-14 19:35:08 +0000694 the current file was found, but if it was found via an absolute
695 path use the normal search logic. */
Neil Boothba133c92001-03-15 07:57:13 +0000696 if (type == IT_INCLUDE_NEXT && pfile->buffer->inc->foundhere)
Neil Booth41947a52001-03-13 23:55:10 +0000697 path = pfile->buffer->inc->foundhere->next;
698 else if (header->type == CPP_HEADER_NAME)
Neil Booth5793b272003-03-01 14:31:21 +0000699 path = pfile->bracket_include;
Neil Booth41947a52001-03-13 23:55:10 +0000700 else
Neil Boothba133c92001-03-15 07:57:13 +0000701 path = search_from (pfile, type);
Neil Booth41947a52001-03-13 23:55:10 +0000702
703 if (path == NULL)
704 {
Neil Boothebef4e82002-04-14 18:42:47 +0000705 cpp_error (pfile, DL_ERROR, "no include path in which to find %s",
706 fname);
Neil Booth41947a52001-03-13 23:55:10 +0000707 return NO_INCLUDE_PATH;
708 }
709
Zack Weinbergc31a6502000-06-21 18:33:51 +0000710 /* Search directory path for the file. */
Neil Boothb6464a72001-03-11 14:54:56 +0000711 name = (char *) alloca (strlen (fname) + pfile->max_include_len + 2);
Neil Booth41947a52001-03-13 23:55:10 +0000712 for (; path; path = path->next)
Zack Weinbergadd70911998-10-29 11:54:13 +0000713 {
Zack Weinberg55485cd2001-12-06 16:41:18 +0000714 int len = path->len;
715 memcpy (name, path->name, len);
716 /* Don't turn / into // or // into ///; // may be a namespace
717 escape. */
718 if (name[len-1] == '/')
719 len--;
720 name[len] = '/';
721 strcpy (&name[len + 1], fname);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000722 if (CPP_OPTION (pfile, remap))
Neil Boothe7182662001-03-14 19:35:08 +0000723 n = remap_filename (pfile, name, path);
724 else
Neil Boothba133c92001-03-15 07:57:13 +0000725 n = name;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000726
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000727 file = open_file_pch (pfile, n);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000728 if (file)
Zack Weinbergd35364d2000-03-12 23:46:05 +0000729 {
Zack Weinbergc31a6502000-06-21 18:33:51 +0000730 file->foundhere = path;
731 return file;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000732 }
Zack Weinbergd35364d2000-03-12 23:46:05 +0000733 }
Neil Booth591e15a2001-03-02 07:35:12 +0000734
Zack Weinbergc31a6502000-06-21 18:33:51 +0000735 return 0;
Zack Weinbergadd70911998-10-29 11:54:13 +0000736}
737
Zack Weinberge605b042000-06-21 23:08:17 +0000738/* Not everyone who wants to set system-header-ness on a buffer can
Neil Booth642ce432000-12-07 23:17:56 +0000739 see the details of a buffer. This is an exported interface because
740 fix-header needs it. */
Zack Weinberge605b042000-06-21 23:08:17 +0000741void
Neil Booth614c7d32000-12-04 07:32:04 +0000742cpp_make_system_header (pfile, syshdr, externc)
Zack Weinberge605b042000-06-21 23:08:17 +0000743 cpp_reader *pfile;
Neil Booth614c7d32000-12-04 07:32:04 +0000744 int syshdr, externc;
Zack Weinberge605b042000-06-21 23:08:17 +0000745{
Neil Booth614c7d32000-12-04 07:32:04 +0000746 int flags = 0;
747
748 /* 1 = system header, 2 = system header to be treated as C. */
749 if (syshdr)
750 flags = 1 + (externc != 0);
Neil Booth47d89cf2001-08-11 07:33:39 +0000751 _cpp_do_file_change (pfile, LC_RENAME, pfile->map->to_file,
752 SOURCE_LINE (pfile->map, pfile->line), flags);
Zack Weinberge605b042000-06-21 23:08:17 +0000753}
754
Zack Weinbergc71f8352000-07-05 05:33:57 +0000755/* Report on all files that might benefit from a multiple include guard.
756 Triggered by -H. */
757void
758_cpp_report_missing_guards (pfile)
759 cpp_reader *pfile;
760{
761 int banner = 0;
762 splay_tree_foreach (pfile->all_include_files, report_missing_guard,
763 (PTR) &banner);
764}
765
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000766/* Callback function for splay_tree_foreach(). */
Zack Weinbergc71f8352000-07-05 05:33:57 +0000767static int
768report_missing_guard (n, b)
769 splay_tree_node n;
770 void *b;
771{
772 struct include_file *f = (struct include_file *) n->value;
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000773 int *bannerp = (int *) b;
Zack Weinbergc71f8352000-07-05 05:33:57 +0000774
775 if (f && f->cmacro == 0 && f->include_count == 1)
776 {
777 if (*bannerp == 0)
778 {
779 fputs (_("Multiple include guards may be useful for:\n"), stderr);
780 *bannerp = 1;
781 }
782 fputs (f->name, stderr);
783 putc ('\n', stderr);
784 }
785 return 0;
786}
787
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000788/* Create a dependency for file FNAME, or issue an error message as
Kazu Hiratada7d8302002-09-22 02:03:17 +0000789 appropriate. ANGLE_BRACKETS is nonzero if the file was bracketed
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000790 like <..>. */
Neil Bootha36c54f2001-03-12 19:33:08 +0000791static void
792handle_missing_header (pfile, fname, angle_brackets)
793 cpp_reader *pfile;
794 const char *fname;
795 int angle_brackets;
796{
Neil Boothf4ff5a62002-08-12 22:44:30 +0000797 bool print_dep
798 = CPP_OPTION (pfile, deps.style) > (angle_brackets || pfile->map->sysp);
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000799
Neil Boothf4ff5a62002-08-12 22:44:30 +0000800 if (CPP_OPTION (pfile, deps.missing_files) && print_dep)
Robert Spier3f8ffc72002-05-08 21:21:19 +0000801 deps_add_dep (pfile->deps, fname);
Neil Boothe7182662001-03-14 19:35:08 +0000802 /* If -M was specified, then don't count this as an error, because
803 we can still produce correct output. Otherwise, we can't produce
804 correct output, because there may be dependencies we need inside
805 the missing file, and we don't know what directory this missing
Neil Boothebef4e82002-04-14 18:42:47 +0000806 file exists in. */
Neil Bootha36c54f2001-03-12 19:33:08 +0000807 else
Neil Boothf4ff5a62002-08-12 22:44:30 +0000808 cpp_errno (pfile, CPP_OPTION (pfile, deps.style) && ! print_dep
Neil Boothebef4e82002-04-14 18:42:47 +0000809 ? DL_WARNING: DL_ERROR, fname);
Neil Bootha36c54f2001-03-12 19:33:08 +0000810}
811
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000812/* Handles #include-family directives (distinguished by TYPE),
813 including HEADER, and the command line -imacros and -include.
814 Returns true if a buffer was stacked. */
Neil Boothe5eba702001-08-21 19:23:24 +0000815bool
Neil Boothba133c92001-03-15 07:57:13 +0000816_cpp_execute_include (pfile, header, type)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000817 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000818 const cpp_token *header;
Neil Boothba133c92001-03-15 07:57:13 +0000819 enum include_type type;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000820{
Neil Boothe5eba702001-08-21 19:23:24 +0000821 bool stacked = false;
Neil Boothba133c92001-03-15 07:57:13 +0000822 struct include_file *inc = find_include_file (pfile, header, type);
Neil Booth642ce432000-12-07 23:17:56 +0000823
Neil Booth41947a52001-03-13 23:55:10 +0000824 if (inc == 0)
825 handle_missing_header (pfile, (const char *) header->val.str.text,
826 header->type == CPP_HEADER_NAME);
827 else if (inc != NO_INCLUDE_PATH)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000828 {
Neil Boothe5eba702001-08-21 19:23:24 +0000829 stacked = stack_include_file (pfile, inc);
Neil Booth51d0f322001-02-19 19:50:21 +0000830
Neil Boothba133c92001-03-15 07:57:13 +0000831 if (type == IT_IMPORT)
Neil Boothe7182662001-03-14 19:35:08 +0000832 _cpp_never_reread (inc);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000833 }
Neil Boothba133c92001-03-15 07:57:13 +0000834
Neil Boothe5eba702001-08-21 19:23:24 +0000835 return stacked;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000836}
837
Neil Booth41947a52001-03-13 23:55:10 +0000838/* Locate HEADER, and determine whether it is newer than the current
839 file. If it cannot be located or dated, return -1, if it is newer
840 newer, return 1, otherwise 0. */
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000841int
Neil Booth41947a52001-03-13 23:55:10 +0000842_cpp_compare_file_date (pfile, header)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000843 cpp_reader *pfile;
Neil Booth41947a52001-03-13 23:55:10 +0000844 const cpp_token *header;
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000845{
Neil Booth41947a52001-03-13 23:55:10 +0000846 struct include_file *inc = find_include_file (pfile, header, 0);
Kazu Hiratadf383482002-05-22 22:02:16 +0000847
Neil Booth41947a52001-03-13 23:55:10 +0000848 if (inc == NULL || inc == NO_INCLUDE_PATH)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000849 return -1;
Neil Booth41947a52001-03-13 23:55:10 +0000850
Zack Weinberga58d32c2000-09-12 03:42:30 +0000851 if (inc->fd > 0)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000852 {
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000853 close (inc->fd);
854 inc->fd = -1;
855 }
Kazu Hiratadf383482002-05-22 22:02:16 +0000856
Neil Booth41947a52001-03-13 23:55:10 +0000857 return inc->st.st_mtime > pfile->buffer->inc->st.st_mtime;
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000858}
859
860
Neil Boothe5eba702001-08-21 19:23:24 +0000861/* Push an input buffer and load it up with the contents of FNAME. If
862 FNAME is "", read standard input. Return true if a buffer was
863 stacked. */
864bool
Neil Booth614c7d32000-12-04 07:32:04 +0000865_cpp_read_file (pfile, fname)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000866 cpp_reader *pfile;
867 const char *fname;
868{
Geoffrey Keating17211ab2003-01-10 02:22:34 +0000869 /* This uses open_file, because we don't allow a PCH to be used as
870 the toplevel compilation (that would prevent re-compiling an
871 existing PCH without deleting it first). */
Neil Booth373e2172001-02-21 07:29:56 +0000872 struct include_file *f = open_file (pfile, fname);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000873
Zack Weinberg041c3192000-07-04 01:58:21 +0000874 if (f == NULL)
Neil Boothc0bfe992001-10-08 17:54:15 +0000875 {
Neil Boothebef4e82002-04-14 18:42:47 +0000876 cpp_errno (pfile, DL_ERROR, fname);
Neil Boothc0bfe992001-10-08 17:54:15 +0000877 return false;
878 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000879
Neil Boothc0bfe992001-10-08 17:54:15 +0000880 return stack_include_file (pfile, f);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000881}
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000882
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000883/* Do appropriate cleanup when a file INC's buffer is popped off the
Neil Boothaf0d16c2002-04-22 17:48:02 +0000884 input stack. */
885void
Neil Booth29401c32001-08-22 20:37:20 +0000886_cpp_pop_file_buffer (pfile, inc)
Zack Weinbergf9a0e962000-07-13 02:32:41 +0000887 cpp_reader *pfile;
Neil Booth29401c32001-08-22 20:37:20 +0000888 struct include_file *inc;
Zack Weinbergf9a0e962000-07-13 02:32:41 +0000889{
Neil Boothba133c92001-03-15 07:57:13 +0000890 /* Record the inclusion-preventing macro, which could be NULL
Neil Booth6d18adb2001-07-29 17:27:57 +0000891 meaning no controlling macro. */
892 if (pfile->mi_valid && inc->cmacro == NULL)
Neil Boothba133c92001-03-15 07:57:13 +0000893 inc->cmacro = pfile->mi_cmacro;
Neil Booth93c803682000-10-28 17:59:06 +0000894
895 /* Invalidate control macros in the #including file. */
Neil Booth6d18adb2001-07-29 17:27:57 +0000896 pfile->mi_valid = false;
Neil Booth93c803682000-10-28 17:59:06 +0000897
Zack Weinberga58d32c2000-09-12 03:42:30 +0000898 inc->refcnt--;
899 if (inc->refcnt == 0 && DO_NOT_REREAD (inc))
900 purge_cache (inc);
Zack Weinbergf9a0e962000-07-13 02:32:41 +0000901}
902
Neil Booth591e15a2001-03-02 07:35:12 +0000903/* Returns the first place in the include chain to start searching for
904 "" includes. This involves stripping away the basename of the
Neil Boothba133c92001-03-15 07:57:13 +0000905 current file, unless -I- was specified.
906
907 If we're handling -include or -imacros, use the "" chain, but with
908 the preprocessor's cwd prepended. */
Neil Booth5793b272003-03-01 14:31:21 +0000909static struct cpp_path *
Neil Boothba133c92001-03-15 07:57:13 +0000910search_from (pfile, type)
Neil Booth591e15a2001-03-02 07:35:12 +0000911 cpp_reader *pfile;
Neil Boothba133c92001-03-15 07:57:13 +0000912 enum include_type type;
Neil Booth591e15a2001-03-02 07:35:12 +0000913{
914 cpp_buffer *buffer = pfile->buffer;
915 unsigned int dlen;
916
Neil Boothba133c92001-03-15 07:57:13 +0000917 /* Command line uses the cwd, and does not cache the result. */
918 if (type == IT_CMDLINE)
919 goto use_cwd;
920
Neil Booth5793b272003-03-01 14:31:21 +0000921 /* Ignore the current file's directory? */
922 if (pfile->quote_ignores_source_dir)
923 return pfile->quote_include;
Neil Booth591e15a2001-03-02 07:35:12 +0000924
Neil Boothba133c92001-03-15 07:57:13 +0000925 if (! buffer->search_cached)
Neil Booth591e15a2001-03-02 07:35:12 +0000926 {
Neil Boothba133c92001-03-15 07:57:13 +0000927 buffer->search_cached = 1;
Neil Booth591e15a2001-03-02 07:35:12 +0000928
Neil Boothba133c92001-03-15 07:57:13 +0000929 dlen = lbasename (buffer->inc->name) - buffer->inc->name;
Neil Booth591e15a2001-03-02 07:35:12 +0000930
Neil Boothba133c92001-03-15 07:57:13 +0000931 if (dlen)
932 {
933 /* We don't guarantee NAME is null-terminated. This saves
Neil Booth29401c32001-08-22 20:37:20 +0000934 allocating and freeing memory. Drop a trailing '/'. */
Neil Booth5793b272003-03-01 14:31:21 +0000935 buffer->dir.name = (char *) buffer->inc->name;
Neil Boothba133c92001-03-15 07:57:13 +0000936 if (dlen > 1)
937 dlen--;
938 }
939 else
940 {
941 use_cwd:
Neil Booth5793b272003-03-01 14:31:21 +0000942 buffer->dir.name = (char *) ".";
Neil Boothba133c92001-03-15 07:57:13 +0000943 dlen = 1;
944 }
945
946 if (dlen > pfile->max_include_len)
947 pfile->max_include_len = dlen;
948
949 buffer->dir.len = dlen;
Neil Booth5793b272003-03-01 14:31:21 +0000950 buffer->dir.next = pfile->quote_include;
Neil Booth47d89cf2001-08-11 07:33:39 +0000951 buffer->dir.sysp = pfile->map->sysp;
Neil Boothba133c92001-03-15 07:57:13 +0000952 }
Neil Booth591e15a2001-03-02 07:35:12 +0000953
954 return &buffer->dir;
955}
956
Zack Weinbergadd70911998-10-29 11:54:13 +0000957/* The file_name_map structure holds a mapping of file names for a
958 particular directory. This mapping is read from the file named
959 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
960 map filenames on a file system with severe filename restrictions,
961 such as DOS. The format of the file name map file is just a series
962 of lines with two tokens on each line. The first token is the name
963 to map, and the second token is the actual name to use. */
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000964struct file_name_map {
Zack Weinbergadd70911998-10-29 11:54:13 +0000965 struct file_name_map *map_next;
966 char *map_from;
967 char *map_to;
968};
969
970#define FILE_NAME_MAP_FILE "header.gcc"
971
972/* Read a space delimited string of unlimited length from a stdio
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000973 file F. */
Zack Weinbergadd70911998-10-29 11:54:13 +0000974static char *
975read_filename_string (ch, f)
976 int ch;
977 FILE *f;
978{
979 char *alloc, *set;
980 int len;
981
982 len = 20;
983 set = alloc = xmalloc (len + 1);
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000984 if (! is_space (ch))
Zack Weinbergadd70911998-10-29 11:54:13 +0000985 {
986 *set++ = ch;
Kazu Hiratadc65cd62002-11-19 06:55:04 +0000987 while ((ch = getc (f)) != EOF && ! is_space (ch))
Zack Weinbergadd70911998-10-29 11:54:13 +0000988 {
989 if (set - alloc == len)
990 {
991 len *= 2;
992 alloc = xrealloc (alloc, len + 1);
993 set = alloc + len / 2;
994 }
995 *set++ = ch;
996 }
997 }
998 *set = '\0';
999 ungetc (ch, f);
1000 return alloc;
1001}
1002
1003/* This structure holds a linked list of file name maps, one per directory. */
Kazu Hiratadc65cd62002-11-19 06:55:04 +00001004struct file_name_map_list {
Zack Weinbergadd70911998-10-29 11:54:13 +00001005 struct file_name_map_list *map_list_next;
1006 char *map_list_name;
1007 struct file_name_map *map_list_map;
1008};
1009
1010/* Read the file name map file for DIRNAME. */
Zack Weinbergadd70911998-10-29 11:54:13 +00001011static struct file_name_map *
1012read_name_map (pfile, dirname)
1013 cpp_reader *pfile;
Kaveh R. Ghazi460ee111999-01-05 19:11:22 +00001014 const char *dirname;
Zack Weinbergadd70911998-10-29 11:54:13 +00001015{
Stan Shebsb3694842001-10-11 03:16:15 +00001016 struct file_name_map_list *map_list_ptr;
Zack Weinbergadd70911998-10-29 11:54:13 +00001017 char *name;
1018 FILE *f;
1019
Neil Booth8767c892000-12-13 19:20:14 +00001020 /* Check the cache of directories, and mappings in their remap file. */
Zack Weinbergae796972000-03-31 23:16:11 +00001021 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
Zack Weinbergadd70911998-10-29 11:54:13 +00001022 map_list_ptr = map_list_ptr->map_list_next)
1023 if (! strcmp (map_list_ptr->map_list_name, dirname))
1024 return map_list_ptr->map_list_map;
1025
1026 map_list_ptr = ((struct file_name_map_list *)
1027 xmalloc (sizeof (struct file_name_map_list)));
Zack Weinbergc49445e1998-12-15 11:23:27 +00001028 map_list_ptr->map_list_name = xstrdup (dirname);
Neil Booth8767c892000-12-13 19:20:14 +00001029
1030 /* The end of the list ends in NULL. */
Laurynas Biveinis4b588242000-07-16 21:22:19 +00001031 map_list_ptr->map_list_map = NULL;
Zack Weinbergadd70911998-10-29 11:54:13 +00001032
1033 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
1034 strcpy (name, dirname);
1035 if (*dirname)
1036 strcat (name, "/");
1037 strcat (name, FILE_NAME_MAP_FILE);
1038 f = fopen (name, "r");
Neil Booth8767c892000-12-13 19:20:14 +00001039
1040 /* Silently return NULL if we cannot open. */
1041 if (f)
Zack Weinbergadd70911998-10-29 11:54:13 +00001042 {
1043 int ch;
Zack Weinbergadd70911998-10-29 11:54:13 +00001044
1045 while ((ch = getc (f)) != EOF)
1046 {
1047 char *from, *to;
1048 struct file_name_map *ptr;
1049
Kazu Hiratadc65cd62002-11-19 06:55:04 +00001050 if (is_space (ch))
Zack Weinbergadd70911998-10-29 11:54:13 +00001051 continue;
1052 from = read_filename_string (ch, f);
Kazu Hiratadc65cd62002-11-19 06:55:04 +00001053 while ((ch = getc (f)) != EOF && is_hspace (ch))
Zack Weinbergadd70911998-10-29 11:54:13 +00001054 ;
1055 to = read_filename_string (ch, f);
1056
1057 ptr = ((struct file_name_map *)
1058 xmalloc (sizeof (struct file_name_map)));
1059 ptr->map_from = from;
1060
1061 /* Make the real filename absolute. */
Neil Booth05e81722001-01-11 21:30:16 +00001062 if (IS_ABSOLUTE_PATHNAME (to))
Zack Weinbergadd70911998-10-29 11:54:13 +00001063 ptr->map_to = to;
1064 else
1065 {
Kaveh R. Ghazi1dcd4442002-07-30 02:24:17 +00001066 ptr->map_to = concat (dirname, "/", to, NULL);
Zack Weinbergadd70911998-10-29 11:54:13 +00001067 free (to);
Kazu Hiratadf383482002-05-22 22:02:16 +00001068 }
Zack Weinbergadd70911998-10-29 11:54:13 +00001069
1070 ptr->map_next = map_list_ptr->map_list_map;
1071 map_list_ptr->map_list_map = ptr;
1072
1073 while ((ch = getc (f)) != '\n')
1074 if (ch == EOF)
1075 break;
1076 }
1077 fclose (f);
1078 }
Kazu Hiratadf383482002-05-22 22:02:16 +00001079
Neil Booth8767c892000-12-13 19:20:14 +00001080 /* Add this information to the cache. */
Zack Weinbergae796972000-03-31 23:16:11 +00001081 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
1082 CPP_OPTION (pfile, map_list) = map_list_ptr;
Zack Weinbergadd70911998-10-29 11:54:13 +00001083
1084 return map_list_ptr->map_list_map;
Kazu Hiratadf383482002-05-22 22:02:16 +00001085}
Zack Weinbergadd70911998-10-29 11:54:13 +00001086
Neil Boothe7182662001-03-14 19:35:08 +00001087/* Remap an unsimplified path NAME based on the file_name_map (if any)
1088 for LOC. */
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001089static char *
1090remap_filename (pfile, name, loc)
Zack Weinbergadd70911998-10-29 11:54:13 +00001091 cpp_reader *pfile;
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001092 char *name;
Neil Booth5793b272003-03-01 14:31:21 +00001093 struct cpp_path *loc;
Zack Weinbergadd70911998-10-29 11:54:13 +00001094{
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001095 struct file_name_map *map;
Neil Booth8767c892000-12-13 19:20:14 +00001096 const char *from, *p;
Neil Boothe7182662001-03-14 19:35:08 +00001097 char *dir;
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001098
1099 if (! loc->name_map)
Neil Booth8767c892000-12-13 19:20:14 +00001100 {
Neil Boothe7182662001-03-14 19:35:08 +00001101 /* Get a null-terminated path. */
1102 char *dname = alloca (loc->len + 1);
1103 memcpy (dname, loc->name, loc->len);
1104 dname[loc->len] = '\0';
1105
Neil Booth591e15a2001-03-02 07:35:12 +00001106 loc->name_map = read_name_map (pfile, dname);
Neil Booth8767c892000-12-13 19:20:14 +00001107 if (! loc->name_map)
1108 return name;
1109 }
Kazu Hiratadf383482002-05-22 22:02:16 +00001110
Neil Boothe7182662001-03-14 19:35:08 +00001111 /* This works since NAME has not been simplified yet. */
Neil Booth591e15a2001-03-02 07:35:12 +00001112 from = name + loc->len + 1;
Kazu Hiratadf383482002-05-22 22:02:16 +00001113
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001114 for (map = loc->name_map; map; map = map->map_next)
1115 if (!strcmp (map->map_from, from))
1116 return map->map_to;
1117
1118 /* Try to find a mapping file for the particular directory we are
1119 looking in. Thus #include <sys/types.h> will look up sys/types.h
1120 in /usr/include/header.gcc and look up types.h in
1121 /usr/include/sys/header.gcc. */
Neil Booth7ceb3592000-03-11 00:49:44 +00001122 p = strrchr (name, '/');
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001123 if (!p)
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001124 return name;
1125
Neil Booth8767c892000-12-13 19:20:14 +00001126 /* We know p != name as absolute paths don't call remap_filename. */
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001127 if (p == name)
Neil Boothebef4e82002-04-14 18:42:47 +00001128 cpp_error (pfile, DL_ICE, "absolute file name in remap_filename");
Neil Booth8767c892000-12-13 19:20:14 +00001129
1130 dir = (char *) alloca (p - name + 1);
1131 memcpy (dir, name, p - name);
1132 dir[p - name] = '\0';
1133 from = p + 1;
Kazu Hiratadf383482002-05-22 22:02:16 +00001134
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001135 for (map = read_name_map (pfile, dir); map; map = map->map_next)
Neil Booth8767c892000-12-13 19:20:14 +00001136 if (! strcmp (map->map_from, from))
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001137 return map->map_to;
Zack Weinbergadd70911998-10-29 11:54:13 +00001138
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001139 return name;
Zack Weinbergadd70911998-10-29 11:54:13 +00001140}
1141
Neil Booth5793b272003-03-01 14:31:21 +00001142/* Set the include chain for "" to QUOTE, for <> to BRACKET. If
1143 QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the
1144 directory of the including file.
1145
1146 If BRACKET does not lie in the QUOTE chain, it is set to QUOTE. */
1147void
1148cpp_set_include_chains (pfile, quote, bracket, quote_ignores_source_dir)
1149 cpp_reader *pfile;
1150 cpp_path *quote, *bracket;
1151 int quote_ignores_source_dir;
Neil Boothf9200da2001-04-06 07:22:01 +00001152{
Neil Booth5793b272003-03-01 14:31:21 +00001153 pfile->quote_include = quote;
1154 pfile->bracket_include = quote;
1155 pfile->quote_ignores_source_dir = quote_ignores_source_dir;
1156 pfile->max_include_len = 0;
Neil Boothf9200da2001-04-06 07:22:01 +00001157
Neil Booth5793b272003-03-01 14:31:21 +00001158 for (; quote; quote = quote->next)
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001159 {
Neil Booth5793b272003-03-01 14:31:21 +00001160 quote->name_map = NULL;
1161 quote->len = strlen (quote->name);
1162 if (quote->len > pfile->max_include_len)
1163 pfile->max_include_len = quote->len;
1164 if (quote == bracket)
1165 pfile->bracket_include = bracket;
Neil Boothf9200da2001-04-06 07:22:01 +00001166 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001167}
Neil Booth986b1f12003-03-02 17:44:18 +00001168
1169/* Returns true if it is safe to remove the final component of path,
1170 when it is followed by a ".." component. We use lstat to avoid
1171 symlinks if we have it. If not, we can still catch errors with
1172 stat (). */
1173static int
1174remove_component_p (path)
1175 const char *path;
1176{
1177 struct stat s;
1178 int result;
1179
1180#ifdef HAVE_LSTAT
1181 result = lstat (path, &s);
1182#else
1183 result = stat (path, &s);
1184#endif
1185
1186 /* There's no guarantee that errno will be unchanged, even on
1187 success. Cygwin's lstat(), for example, will often set errno to
1188 ENOSYS. In case of success, reset errno to zero. */
1189 if (result == 0)
1190 errno = 0;
1191
1192 return result == 0 && S_ISDIR (s.st_mode);
1193}
1194
1195/* Simplify a path name in place, deleting redundant components. This
1196 reduces OS overhead and guarantees that equivalent paths compare
1197 the same (modulo symlinks).
1198
1199 Transforms made:
1200 foo/bar/../quux foo/quux
1201 foo/./bar foo/bar
1202 foo//bar foo/bar
1203 /../quux /quux
1204 //quux //quux (POSIX allows leading // as a namespace escape)
1205
1206 Guarantees no trailing slashes. All transforms reduce the length
1207 of the string. Returns PATH. errno is 0 if no error occurred;
1208 nonzero if an error occurred when using stat () or lstat (). */
1209void
1210cpp_simplify_path (path)
1211 char *path ATTRIBUTE_UNUSED;
1212{
1213#ifndef VMS
1214 char *from, *to;
1215 char *base, *orig_base;
1216 int absolute = 0;
1217
1218 errno = 0;
1219 /* Don't overflow the empty path by putting a '.' in it below. */
1220 if (*path == '\0')
1221 return;
1222
1223#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1224 /* Convert all backslashes to slashes. */
1225 for (from = path; *from; from++)
1226 if (*from == '\\') *from = '/';
1227
1228 /* Skip over leading drive letter if present. */
1229 if (ISALPHA (path[0]) && path[1] == ':')
1230 from = to = &path[2];
1231 else
1232 from = to = path;
1233#else
1234 from = to = path;
1235#endif
1236
1237 /* Remove redundant leading /s. */
1238 if (*from == '/')
1239 {
1240 absolute = 1;
1241 to++;
1242 from++;
1243 if (*from == '/')
1244 {
1245 if (*++from == '/')
1246 /* 3 or more initial /s are equivalent to 1 /. */
1247 while (*++from == '/');
1248 else
1249 /* On some hosts // differs from /; Posix allows this. */
1250 to++;
1251 }
1252 }
1253
1254 base = orig_base = to;
1255 for (;;)
1256 {
1257 int move_base = 0;
1258
1259 while (*from == '/')
1260 from++;
1261
1262 if (*from == '\0')
1263 break;
1264
1265 if (*from == '.')
1266 {
1267 if (from[1] == '\0')
1268 break;
1269 if (from[1] == '/')
1270 {
1271 from += 2;
1272 continue;
1273 }
1274 else if (from[1] == '.' && (from[2] == '/' || from[2] == '\0'))
1275 {
1276 /* Don't simplify if there was no previous component. */
1277 if (absolute && orig_base == to)
1278 {
1279 from += 2;
1280 continue;
1281 }
1282 /* Don't simplify if the previous component was "../",
1283 or if an error has already occurred with (l)stat. */
1284 if (base != to && errno == 0)
1285 {
1286 /* We don't back up if it's a symlink. */
1287 *to = '\0';
1288 if (remove_component_p (path))
1289 {
1290 while (to > base && *to != '/')
1291 to--;
1292 from += 2;
1293 continue;
1294 }
1295 }
1296 move_base = 1;
1297 }
1298 }
1299
1300 /* Add the component separator. */
1301 if (to > orig_base)
1302 *to++ = '/';
1303
1304 /* Copy this component until the trailing null or '/'. */
1305 while (*from != '\0' && *from != '/')
1306 *to++ = *from++;
1307
1308 if (move_base)
1309 base = to;
1310 }
1311
1312 /* Change the empty string to "." so that it is not treated as stdin.
1313 Null terminate. */
1314 if (to == path)
1315 *to++ = '.';
1316 *to = '\0';
1317#else /* VMS */
1318 errno = 0;
1319#endif /* !VMS */
1320}