1 /* Part of CPP library. (include file handling)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
3 1999, 2000 Free Software Foundation, Inc.
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
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
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.
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
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "splay-tree.h"
32 # include <sys/mman.h>
33 # ifndef MMAP_THRESHOLD
34 # define MMAP_THRESHOLD 3 /* Minimum page count to mmap the file. */
37 #else /* No MMAP_FILE */
38 # undef MMAP_THRESHOLD
39 # define MMAP_THRESHOLD 0
46 #ifndef INCLUDE_LEN_FUDGE
47 # define INCLUDE_LEN_FUDGE 0
50 /* If errno is inspected immediately after a system call fails, it will be
51 nonzero, and no error number will ever be zero. */
62 /* Suppress warning about function macros used w/o arguments in traditional
63 C. It is unlikely that glibc's strcmp macro helps this file at all. */
66 /* This structure is used for the table of all includes. */
69 const char *name; /* actual path name of file */
70 const cpp_hashnode *cmacro; /* macro, if any, preventing reinclusion. */
71 const struct file_name_list *foundhere;
72 /* location in search path where file was
73 found, for #include_next and sysp. */
74 const unsigned char *buffer; /* pointer to cached file contents */
75 struct stat st; /* copy of stat(2) data for file */
76 int fd; /* fd open on file (short term storage only) */
77 unsigned short include_count; /* number of times file has been read */
78 unsigned short refcnt; /* number of stacked buffers using this file */
79 unsigned char mapped; /* file buffer is mmapped */
80 unsigned char defined; /* cmacro prevents inclusion in this state */
83 /* The cmacro works like this: If it's NULL, the file is to be
84 included again. If it's NEVER_REREAD, the file is never to be
85 included again. Otherwise it is a macro hashnode, and the file is
86 to be included again if the macro is defined or not as specified by
88 #define NEVER_REREAD ((const cpp_hashnode *)-1)
89 #define DO_NOT_REREAD(inc) \
90 ((inc)->cmacro && ((inc)->cmacro == NEVER_REREAD \
91 || ((inc)->cmacro->type == NT_MACRO) == (inc)->defined))
93 static struct file_name_map *read_name_map
94 PARAMS ((cpp_reader *, const char *));
95 static char *read_filename_string PARAMS ((int, FILE *));
96 static char *remap_filename PARAMS ((cpp_reader *, char *,
97 struct file_name_list *));
98 static struct file_name_list *actual_directory
99 PARAMS ((cpp_reader *, const char *));
100 static struct include_file *find_include_file
101 PARAMS ((cpp_reader *, const char *,
102 struct file_name_list *));
103 static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
104 static void read_include_file PARAMS ((cpp_reader *, struct include_file *));
105 static void stack_include_file PARAMS ((cpp_reader *, struct include_file *));
106 static void purge_cache PARAMS ((struct include_file *));
107 static void destroy_include_file_node PARAMS ((splay_tree_value));
108 static int report_missing_guard PARAMS ((splay_tree_node, void *));
110 /* We use a splay tree to store information about all the include
111 files seen in this compilation. The key of each tree node is the
112 physical path to the file. The value is 0 if the file does not
113 exist, or a struct include_file pointer. */
116 destroy_include_file_node (v)
119 struct include_file *f = (struct include_file *)v;
123 free (f); /* The tree is registered with free to free f->name. */
128 _cpp_init_includes (pfile)
131 pfile->all_include_files
132 = splay_tree_new ((splay_tree_compare_fn) strcmp,
133 (splay_tree_delete_key_fn) free,
134 destroy_include_file_node);
138 _cpp_cleanup_includes (pfile)
141 splay_tree_delete (pfile->all_include_files);
144 /* Mark a file to not be reread (e.g. #import, read failure). */
146 _cpp_never_reread (file)
147 struct include_file *file;
149 file->cmacro = NEVER_REREAD;
152 /* Given a file name, look it up in the cache; if there is no entry,
153 create one with a non-NULL value (regardless of success in opening
154 the file). If the file doesn't exist or is inaccessible, this
155 entry is flagged so we don't attempt to open it again in the
156 future. If the file isn't open, open it.
158 Returns an include_file structure with an open file descriptor on
159 success, or NULL on failure. */
161 static struct include_file *
162 open_file (pfile, filename)
164 const char *filename;
167 struct include_file *file;
169 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) filename);
173 file = (struct include_file *) nd->value;
175 /* Don't retry opening if we failed previously. */
179 /* Don't reopen an idempotent file. */
180 if (DO_NOT_REREAD (file))
183 /* Don't reopen one which is already loaded. */
184 if (file->buffer != NULL)
189 /* In particular, this clears foundhere. */
190 file = xcnew (struct include_file);
191 file->name = xstrdup (filename);
192 splay_tree_insert (pfile->all_include_files,
193 (splay_tree_key) file->name,
194 (splay_tree_value) file);
197 /* We used to open files in nonblocking mode, but that caused more
198 problems than it solved. Do take care not to acquire a
199 controlling terminal by mistake (this can't happen on sane
200 systems, but paranoia is a virtue).
202 Use the three-argument form of open even though we aren't
203 specifying O_CREAT, to defend against broken system headers.
205 O_BINARY tells some runtime libraries (notably DJGPP) not to do
206 newline translation; we can handle DOS line breaks just fine
209 Special case: the empty string is translated to stdin. */
211 if (filename[0] == '\0')
214 file->fd = open (filename, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
216 if (file->fd != -1 && fstat (file->fd, &file->st) == 0)
218 /* Mark a regular, zero-length file never-reread now. */
219 if (S_ISREG (file->st.st_mode) && file->st.st_size == 0)
221 _cpp_never_reread (file);
229 /* Don't issue an error message if the file doesn't exist. */
230 if (errno != ENOENT && errno != ENOTDIR)
231 cpp_error_from_errno (pfile, filename);
233 /* Create a negative node for this path, and return null. */
239 /* Place the file referenced by INC into a new buffer on PFILE's
240 stack. If there are errors, or the file should not be re-included,
241 a null buffer is pushed. */
244 stack_include_file (pfile, inc)
246 struct include_file *inc;
253 read_include_file (pfile, inc);
255 if (! DO_NOT_REREAD (inc))
256 len = inc->st.st_size;
259 fp = cpp_push_buffer (pfile, inc->buffer, len, BUF_FILE, inc->name);
263 fp->sysp = inc->foundhere->sysp;
265 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
267 if (!CPP_OPTION (pfile, ignore_srcdir))
268 fp->actual_dir = actual_directory (pfile, inc->name);
270 /* Initialise controlling macro state. */
271 pfile->mi_state = MI_OUTSIDE;
272 pfile->mi_cmacro = 0;
273 pfile->include_depth++;
275 /* Generate the call back. */
277 _cpp_do_file_change (pfile, FC_ENTER, 0, 0);
281 /* Read the file referenced by INC into the file cache.
283 If fd points to a plain file, we might be able to mmap it; we can
284 definitely allocate the buffer all at once. If fd is a pipe or
285 terminal, we can't do either. If fd is something weird, like a
286 block device or a directory, we don't want to read it at all.
288 Unfortunately, different systems use different st.st_mode values
289 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
290 zero the entire struct stat except a couple fields. Hence we don't
291 even try to figure out what something is, except for plain files,
292 directories, and block devices.
294 FIXME: Flush file cache and try again if we run out of memory. */
297 read_include_file (pfile, inc)
299 struct include_file *inc;
301 ssize_t size, offset, count;
304 static int pagesize = -1;
307 if (DO_NOT_REREAD (inc))
310 if (S_ISREG (inc->st.st_mode))
312 /* off_t might have a wider range than ssize_t - in other words,
313 the max size of a file might be bigger than the address
314 space. We can't handle a file that large. (Anyone with
315 a single source file bigger than 2GB needs to rethink
316 their coding style.) Some systems (e.g. AIX 4.1) define
317 SSIZE_MAX to be much smaller than the actual range of the
318 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
320 if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t))
322 cpp_error (pfile, "%s is too large", inc->name);
325 size = inc->st.st_size;
330 pagesize = getpagesize ();
332 if (size / pagesize >= MMAP_THRESHOLD)
334 buf = (U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0);
335 if (buf == (U_CHAR *)-1)
342 buf = (U_CHAR *) xmalloc (size);
344 while (offset < size)
346 count = read (inc->fd, buf + offset, size - offset);
351 cpp_warning (pfile, "%s is shorter than expected", inc->name);
358 else if (S_ISBLK (inc->st.st_mode))
360 cpp_error (pfile, "%s is a block device", inc->name);
363 else if (S_ISDIR (inc->st.st_mode))
365 cpp_error (pfile, "%s is a directory", inc->name);
370 /* 8 kilobytes is a sensible starting size. It ought to be
371 bigger than the kernel pipe buffer, and it's definitely
372 bigger than the majority of C source files. */
375 buf = (U_CHAR *) xmalloc (size);
377 while ((count = read (inc->fd, buf + offset, size - offset)) > 0)
381 buf = xrealloc (buf, (size *= 2));
387 buf = xrealloc (buf, offset);
388 inc->st.st_size = offset;
397 cpp_error_from_errno (pfile, inc->name);
399 /* Do not try to read this file again. */
402 _cpp_never_reread (inc);
408 struct include_file *inc;
414 munmap ((PTR) inc->buffer, inc->st.st_size);
417 free ((PTR) inc->buffer);
422 /* Return 1 if the file named by FNAME has been included before in
423 any context, 0 otherwise. */
425 cpp_included (pfile, fname)
429 struct file_name_list *path;
435 /* Just look it up. */
436 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
437 return (nd && nd->value);
440 /* Search directory path for the file. */
441 name = (char *) alloca (strlen (fname) + pfile->max_include_len
442 + 2 + INCLUDE_LEN_FUDGE);
443 for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
445 memcpy (name, path->name, path->nlen);
446 name[path->nlen] = '/';
447 strcpy (&name[path->nlen+1], fname);
448 _cpp_simplify_pathname (name);
449 if (CPP_OPTION (pfile, remap))
450 name = remap_filename (pfile, name, path);
452 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
459 /* Search for include file FNAME in the include chain starting at
460 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
461 otherwise an include_file structure. */
463 static struct include_file *
464 find_include_file (pfile, fname, search_start)
467 struct file_name_list *search_start;
469 struct file_name_list *path;
471 struct include_file *file;
474 return open_file (pfile, fname);
476 /* Search directory path for the file. */
477 name = (char *) alloca (strlen (fname) + pfile->max_include_len
478 + 2 + INCLUDE_LEN_FUDGE);
479 for (path = search_start; path; path = path->next)
481 memcpy (name, path->name, path->nlen);
482 name[path->nlen] = '/';
483 strcpy (&name[path->nlen+1], fname);
484 _cpp_simplify_pathname (name);
485 if (CPP_OPTION (pfile, remap))
486 name = remap_filename (pfile, name, path);
488 file = open_file (pfile, name);
491 file->foundhere = path;
498 /* Not everyone who wants to set system-header-ness on a buffer can
499 see the details of a buffer. This is an exported interface because
500 fix-header needs it. */
502 cpp_make_system_header (pfile, syshdr, externc)
508 /* 1 = system header, 2 = system header to be treated as C. */
510 flags = 1 + (externc != 0);
511 pfile->buffer->sysp = flags;
514 /* Report on all files that might benefit from a multiple include guard.
517 _cpp_report_missing_guards (pfile)
521 splay_tree_foreach (pfile->all_include_files, report_missing_guard,
526 report_missing_guard (n, b)
530 struct include_file *f = (struct include_file *) n->value;
531 int *bannerp = (int *)b;
533 if (f && f->cmacro == 0 && f->include_count == 1)
537 fputs (_("Multiple include guards may be useful for:\n"), stderr);
540 fputs (f->name, stderr);
546 #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
548 _cpp_execute_include (pfile, header, no_reinclude, include_next)
550 const cpp_token *header;
554 struct file_name_list *search_start = 0;
555 unsigned int len = header->val.str.len;
556 unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
557 struct include_file *inc;
560 /* Help protect #include or similar from recursion. */
561 if (pfile->buffer_stack_depth >= CPP_STACK_MAX)
563 cpp_fatal (pfile, "#include nested too deeply");
567 /* Check we've tidied up #include before entering the buffer. */
568 if (pfile->context->prev)
570 cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
574 /* For #include_next, skip in the search path past the dir in which
575 the current file was found. If this is the last directory in the
576 search path, don't include anything. If the current file was
577 specified with an absolute path, use the normal search logic. If
578 this is the primary source file, use the normal search logic and
579 generate a warning. */
582 if (! pfile->buffer->prev)
583 cpp_warning (pfile, "#include_next in primary source file");
586 if (pfile->buffer->inc->foundhere)
588 search_start = pfile->buffer->inc->foundhere->next;
595 fname = alloca (len + 1);
596 memcpy (fname, header->val.str.text, len);
602 search_start = CPP_OPTION (pfile, bracket_include);
603 else if (CPP_OPTION (pfile, ignore_srcdir))
604 search_start = CPP_OPTION (pfile, quote_include);
606 search_start = CPP_BUFFER (pfile)->actual_dir;
610 cpp_error (pfile, "No include path in which to find %s", fname);
615 inc = find_include_file (pfile, fname, search_start);
619 /* For -M, add the file to the dependencies on its first inclusion. */
620 if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets))
621 deps_add_dep (pfile->deps, inc->name);
622 inc->include_count++;
624 /* Actually process the file. */
625 stack_include_file (pfile, inc);
628 pfile->system_include_depth++;
630 if (! DO_NOT_REREAD (inc))
633 _cpp_never_reread (inc);
635 /* Handle -H option. */
636 if (CPP_OPTION (pfile, print_include_names))
638 cpp_buffer *fp = CPP_BUFFER (pfile);
639 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
641 fprintf (stderr, " %s\n", inc->name);
648 if (CPP_OPTION (pfile, print_deps_missing_files)
649 && PRINT_THIS_DEP (pfile, angle_brackets))
651 if (!angle_brackets || *fname == '/')
652 deps_add_dep (pfile->deps, fname);
656 struct file_name_list *ptr;
659 /* If requested as a system header, assume it belongs in
660 the first system header directory. */
661 if (CPP_OPTION (pfile, bracket_include))
662 ptr = CPP_OPTION (pfile, bracket_include);
664 ptr = CPP_OPTION (pfile, quote_include);
666 len = strlen (ptr->name);
667 p = (char *) alloca (len + strlen (fname) + 2);
670 memcpy (p, ptr->name, len);
673 strcpy (p + len, fname);
674 _cpp_simplify_pathname (p);
675 deps_add_dep (pfile->deps, p);
678 /* If -M was specified, and this header file won't be added to
679 the dependency list, then don't count this as an error,
680 because we can still produce correct output. Otherwise, we
681 can't produce correct output, because there may be
682 dependencies we need inside the missing file, and we don't
683 know what directory this missing file exists in. */
684 else if (CPP_PRINT_DEPS (pfile)
685 && ! PRINT_THIS_DEP (pfile, angle_brackets))
686 cpp_warning (pfile, "No include path in which to find %s", fname);
688 cpp_error_from_errno (pfile, fname);
691 /* Locate file F, and determine whether it is newer than PFILE. Return -1,
692 if F cannot be located or dated, 1, if it is newer and 0 if older. */
694 _cpp_compare_file_date (pfile, f)
698 unsigned int len = f->val.str.len;
700 struct file_name_list *search_start;
701 struct include_file *inc;
703 if (f->type == CPP_HEADER_NAME)
704 search_start = CPP_OPTION (pfile, bracket_include);
705 else if (CPP_OPTION (pfile, ignore_srcdir))
706 search_start = CPP_OPTION (pfile, quote_include);
708 search_start = CPP_BUFFER (pfile)->actual_dir;
710 fname = alloca (len + 1);
711 memcpy (fname, f->val.str.text, len);
713 inc = find_include_file (pfile, fname, search_start);
723 return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime;
727 /* Push an input buffer and load it up with the contents of FNAME.
728 If FNAME is "" or NULL, read standard input. */
730 _cpp_read_file (pfile, fname)
734 struct include_file *f;
739 f = open_file (pfile, fname);
743 cpp_error_from_errno (pfile, fname);
747 stack_include_file (pfile, f);
751 /* Do appropriate cleanup when a file buffer is popped off the input
754 _cpp_pop_file_buffer (pfile, buf)
758 struct include_file *inc = buf->inc;
760 if (pfile->system_include_depth)
761 pfile->system_include_depth--;
762 if (pfile->include_depth)
763 pfile->include_depth--;
765 /* Record the inclusion-preventing macro and its definedness. */
766 if (pfile->mi_state == MI_OUTSIDE && inc->cmacro != NEVER_REREAD)
768 /* This could be NULL meaning no controlling macro. */
769 inc->cmacro = pfile->mi_cmacro;
773 /* Invalidate control macros in the #including file. */
774 pfile->mi_state = MI_FAILED;
777 if (inc->refcnt == 0 && DO_NOT_REREAD (inc))
781 /* The file_name_map structure holds a mapping of file names for a
782 particular directory. This mapping is read from the file named
783 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
784 map filenames on a file system with severe filename restrictions,
785 such as DOS. The format of the file name map file is just a series
786 of lines with two tokens on each line. The first token is the name
787 to map, and the second token is the actual name to use. */
791 struct file_name_map *map_next;
796 #define FILE_NAME_MAP_FILE "header.gcc"
798 /* Read a space delimited string of unlimited length from a stdio
802 read_filename_string (ch, f)
810 set = alloc = xmalloc (len + 1);
814 while ((ch = getc (f)) != EOF && ! is_space(ch))
816 if (set - alloc == len)
819 alloc = xrealloc (alloc, len + 1);
820 set = alloc + len / 2;
830 /* This structure holds a linked list of file name maps, one per directory. */
832 struct file_name_map_list
834 struct file_name_map_list *map_list_next;
836 struct file_name_map *map_list_map;
839 /* Read the file name map file for DIRNAME. */
841 static struct file_name_map *
842 read_name_map (pfile, dirname)
846 register struct file_name_map_list *map_list_ptr;
850 /* Check the cache of directories, and mappings in their remap file. */
851 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
852 map_list_ptr = map_list_ptr->map_list_next)
853 if (! strcmp (map_list_ptr->map_list_name, dirname))
854 return map_list_ptr->map_list_map;
856 map_list_ptr = ((struct file_name_map_list *)
857 xmalloc (sizeof (struct file_name_map_list)));
858 map_list_ptr->map_list_name = xstrdup (dirname);
860 /* The end of the list ends in NULL. */
861 map_list_ptr->map_list_map = NULL;
863 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
864 strcpy (name, dirname);
867 strcat (name, FILE_NAME_MAP_FILE);
868 f = fopen (name, "r");
870 /* Silently return NULL if we cannot open. */
874 int dirlen = strlen (dirname);
876 while ((ch = getc (f)) != EOF)
879 struct file_name_map *ptr;
883 from = read_filename_string (ch, f);
884 while ((ch = getc (f)) != EOF && is_hspace(ch))
886 to = read_filename_string (ch, f);
888 ptr = ((struct file_name_map *)
889 xmalloc (sizeof (struct file_name_map)));
890 ptr->map_from = from;
892 /* Make the real filename absolute. */
897 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
898 strcpy (ptr->map_to, dirname);
899 ptr->map_to[dirlen] = '/';
900 strcpy (ptr->map_to + dirlen + 1, to);
904 ptr->map_next = map_list_ptr->map_list_map;
905 map_list_ptr->map_list_map = ptr;
907 while ((ch = getc (f)) != '\n')
914 /* Add this information to the cache. */
915 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
916 CPP_OPTION (pfile, map_list) = map_list_ptr;
918 return map_list_ptr->map_list_map;
921 /* Remap NAME based on the file_name_map (if any) for LOC. */
924 remap_filename (pfile, name, loc)
927 struct file_name_list *loc;
929 struct file_name_map *map;
930 const char *from, *p;
935 loc->name_map = read_name_map (pfile, loc->name ? loc->name : ".");
940 from = name + strlen (loc->name) + 1;
942 for (map = loc->name_map; map; map = map->map_next)
943 if (!strcmp (map->map_from, from))
946 /* Try to find a mapping file for the particular directory we are
947 looking in. Thus #include <sys/types.h> will look up sys/types.h
948 in /usr/include/header.gcc and look up types.h in
949 /usr/include/sys/header.gcc. */
950 p = strrchr (name, '/');
954 /* We know p != name as absolute paths don't call remap_filename. */
956 cpp_ice (pfile, "absolute file name in remap_filename");
958 dir = (char *) alloca (p - name + 1);
959 memcpy (dir, name, p - name);
960 dir[p - name] = '\0';
963 for (map = read_name_map (pfile, dir); map; map = map->map_next)
964 if (! strcmp (map->map_from, from))
970 /* Given a path FNAME, extract the directory component and place it
971 onto the actual_dirs list. Return a pointer to the allocated
972 file_name_list structure. These structures are used to implement
973 current-directory "" include searching. */
975 static struct file_name_list *
976 actual_directory (pfile, fname)
980 char *last_slash, *dir;
982 struct file_name_list *x;
984 dir = xstrdup (fname);
985 last_slash = strrchr (dir, '/');
988 if (last_slash == dir)
991 last_slash[1] = '\0';
995 dlen = last_slash - dir;
1002 dir = xstrdup (".");
1006 if (dlen > pfile->max_include_len)
1007 pfile->max_include_len = dlen;
1009 for (x = pfile->actual_dirs; x; x = x->alloc)
1010 if (!strcmp (x->name, dir))
1016 /* Not found, make a new one. */
1017 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1020 x->next = CPP_OPTION (pfile, quote_include);
1021 x->alloc = pfile->actual_dirs;
1022 x->sysp = pfile->buffer->sysp;
1025 pfile->actual_dirs = x;
1029 /* Simplify a path name in place, deleting redundant components. This
1030 reduces OS overhead and guarantees that equivalent paths compare
1031 the same (modulo symlinks).
1034 foo/bar/../quux foo/quux
1038 //quux //quux (POSIX allows leading // as a namespace escape)
1040 Guarantees no trailing slashes. All transforms reduce the length
1044 _cpp_simplify_pathname (path)
1051 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1052 /* Convert all backslashes to slashes. */
1053 for (from = path; *from; from++)
1054 if (*from == '\\') *from = '/';
1056 /* Skip over leading drive letter if present. */
1057 if (ISALPHA (path[0]) && path[1] == ':')
1058 from = to = &path[2];
1065 /* Remove redundant initial /s. */
1074 /* 3 or more initial /s are equivalent to 1 /. */
1075 while (*++from == '/');
1077 /* On some hosts // differs from /; Posix allows this. */
1085 while (*from == '/')
1088 if (from[0] == '.' && from[1] == '/')
1090 else if (from[0] == '.' && from[1] == '\0')
1092 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1109 while (to > base && *to != '/') to--;
1115 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1128 while (to > base && *to != '/') to--;
1135 /* Copy this component and trailing /, if any. */
1136 while ((*to++ = *from++) != '/')
1148 /* Trim trailing slash */
1149 if (to[0] == '/' && (!absolute || to > path+1))
1152 /* Change the empty string to "." so that stat() on the result
1153 will always work. */