1 /* Part of CPP library. (include file handling)
2 Copyright (C) 1986, 87, 89, 92 - 95, 1998 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 Split out of cpplib.c, Zack Weinberg, Oct 1998
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
30 /* The entry points to this file are: find_include_file, finclude,
31 include_hash, append_include_chain, deps_output, and file_cleanup.
32 file_cleanup is only called through CPP_BUFFER(pfile)->cleanup,
33 so it's static anyway. */
35 static struct include_hash
*redundant_include_p
37 struct include_hash
*,
38 struct file_name_list
*));
39 static struct file_name_map
*read_name_map
PROTO ((cpp_reader
*, char *));
40 static char *read_filename_string
PROTO ((int, FILE *));
41 static char *remap_filename
PROTO ((cpp_reader
*, char *,
42 struct file_name_list
*));
43 static long safe_read
PROTO ((int, char *, int));
44 static void simplify_pathname
PROTO ((char *));
45 static struct file_name_list
*actual_directory
PROTO ((cpp_reader
*, char *));
48 static void hack_vms_include_specification
PROTO ((char *));
51 /* Windows does not natively support inodes, and neither does MSDOS.
52 VMS has non-numeric inodes. */
54 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
55 #elif (defined _WIN32 && !defined CYGWIN) || defined __MSDOS__
56 #define INO_T_EQ(a, b) 0
58 #define INO_T_EQ(a, b) ((a) == (b))
61 /* Append an entry for dir DIR to list LIST, simplifying it if
62 possible. SYS says whether this is a system include directory.
63 *** DIR is modified in place. It must be writable and permanently
64 allocated. LIST is a pointer to the head pointer, because we actually
65 *prepend* the dir, and reverse the list later (in merge_include_chains). */
67 append_include_chain (pfile
, list
, dir
, sysp
)
69 struct file_name_list
**list
;
73 struct file_name_list
*new;
78 simplify_pathname (dir
);
81 /* Dirs that don't exist are silently ignored. */
83 cpp_perror_with_name (pfile
, dir
);
87 if (!S_ISDIR (st
.st_mode
))
89 cpp_message (pfile
, 1, "%s: %s: Not a directory", progname
, dir
);
94 if (len
> pfile
->max_include_len
)
95 pfile
->max_include_len
= len
;
97 new = (struct file_name_list
*)xmalloc (sizeof (struct file_name_list
));
101 new->ino
= st
.st_ino
;
102 new->dev
= st
.st_dev
;
104 new->name_map
= NULL
;
109 /* Merge the four include chains together in the order quote, bracket,
110 system, after. Remove duplicate dirs (as determined by
111 INO_T_EQ()). The system_include and after_include chains are never
112 referred to again after this function; all access is through the
113 bracket_include path.
115 For the future: Check if the directory is empty (but
116 how?) and possibly preload the include hash. */
119 merge_include_chains (opts
)
120 struct cpp_options
*opts
;
122 struct file_name_list
*prev
, *next
, *cur
, *other
;
123 struct file_name_list
*quote
, *brack
, *systm
, *after
;
124 struct file_name_list
*qtail
, *btail
, *stail
, *atail
;
126 qtail
= opts
->quote_include
;
127 btail
= opts
->bracket_include
;
128 stail
= opts
->system_include
;
129 atail
= opts
->after_include
;
131 /* Nreverse the four lists. */
133 for (cur
= qtail
; cur
; cur
= next
)
142 for (cur
= btail
; cur
; cur
= next
)
151 for (cur
= stail
; cur
; cur
= next
)
160 for (cur
= atail
; cur
; cur
= next
)
168 /* Paste together bracket, system, and after include chains. */
178 /* This is a bit tricky.
179 First we drop dupes from the quote-include list.
180 Then we drop dupes from the bracket-include list.
181 Finally, if qtail and brack are the same directory,
184 We can't just merge the lists and then uniquify them because
185 then we may lose directories from the <> search path that should
186 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
187 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
188 -Ibar -I- -Ifoo -Iquux. */
190 for (cur
= quote
; cur
; cur
= cur
->next
)
192 for (other
= quote
; other
!= cur
; other
= other
->next
)
193 if (INO_T_EQ (cur
->ino
, other
->ino
)
194 && cur
->dev
== other
->dev
)
196 prev
->next
= cur
->next
;
206 for (cur
= brack
; cur
; cur
= cur
->next
)
208 for (other
= brack
; other
!= cur
; other
= other
->next
)
209 if (INO_T_EQ (cur
->ino
, other
->ino
)
210 && cur
->dev
== other
->dev
)
212 prev
->next
= cur
->next
;
223 if (INO_T_EQ (qtail
->ino
, brack
->ino
) && qtail
->dev
== brack
->dev
)
234 while (cur
->next
!= qtail
)
247 opts
->quote_include
= quote
;
248 opts
->bracket_include
= brack
;
249 opts
->system_include
= NULL
;
250 opts
->after_include
= NULL
;
253 /* Look up or add an entry to the table of all includes. This table
254 is indexed by the name as it appears in the #include line. The
255 ->next_this_file chain stores all different files with the same
256 #include name (there are at least three ways this can happen). The
257 hash function could probably be improved a bit. */
259 struct include_hash
*
260 include_hash (pfile
, fname
, add
)
265 unsigned int hash
= 0;
266 struct include_hash
*l
, *m
;
272 l
= pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
];
274 for (; l
; m
= l
, l
= l
->next
)
275 if (!strcmp (l
->nshort
, fname
))
281 l
= (struct include_hash
*) xmalloc (sizeof (struct include_hash
));
283 l
->next_this_file
= NULL
;
290 pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
] = l
;
295 /* Return 0 if the file pointed to by IHASH has never been included before,
296 -1 if it has been included before and need not be again,
297 or a pointer to an IHASH entry which is the file to be reread.
298 "Never before" is with respect to the position in ILIST.
300 This will not detect redundancies involving odd uses of the
301 `current directory' rule for "" includes. They aren't quite
302 pathological, but I think they are rare enough not to worry about.
303 The simplest example is:
312 and the problem is that for `current directory' includes,
313 ihash->foundhere is not on any of the global include chains,
314 so the test below (i->foundhere == l) may be false even when
315 the directories are in fact the same. */
317 static struct include_hash
*
318 redundant_include_p (pfile
, ihash
, ilist
)
320 struct include_hash
*ihash
;
321 struct file_name_list
*ilist
;
323 struct file_name_list
*l
;
324 struct include_hash
*i
;
326 if (! ihash
->foundhere
)
329 for (i
= ihash
; i
; i
= i
->next_this_file
)
330 for (l
= ilist
; l
; l
= l
->next
)
331 if (i
->foundhere
== l
)
332 /* The control_macro works like this: If it's NULL, the file
333 is to be included again. If it's "", the file is never to
334 be included again. If it's a string, the file is not to be
335 included again if the string is the name of a defined macro. */
336 return (i
->control_macro
337 && (i
->control_macro
[0] == '\0'
338 || cpp_lookup (pfile
, i
->control_macro
, -1, -1)))
339 ? (struct include_hash
*)-1 : i
;
345 file_cleanup (pbuf
, pfile
)
354 if (pfile
->system_include_depth
)
355 pfile
->system_include_depth
--;
359 /* Search for include file FNAME in the include chain starting at
360 SEARCH_START. Return -2 if this file doesn't need to be included
361 (because it was included already and it's marked idempotent),
362 -1 if an error occurred, or a file descriptor open on the file.
363 *IHASH is set to point to the include hash entry for this file, and
364 *BEFORE is 1 if the file was included before (but needs to be read
367 find_include_file (pfile
, fname
, search_start
, ihash
, before
)
370 struct file_name_list
*search_start
;
371 struct include_hash
**ihash
;
374 struct file_name_list
*l
;
375 struct include_hash
*ih
, *jh
;
379 ih
= include_hash (pfile
, fname
, 1);
380 jh
= redundant_include_p (pfile
, ih
,
381 fname
[0] == '/' ? ABSOLUTE_PATH
: search_start
);
388 if (jh
== (struct include_hash
*)-1)
391 return open (jh
->name
, O_RDONLY
, 0666);
395 /* A file is already known by this name, but it's not the same file.
396 Allocate another include_hash block and add it to the next_this_file
399 jh
= (struct include_hash
*)xmalloc (sizeof (struct include_hash
));
400 while (ih
->next_this_file
) ih
= ih
->next_this_file
;
402 ih
->next_this_file
= jh
;
404 ih
= ih
->next_this_file
;
407 ih
->next_this_file
= NULL
;
413 ih
->nshort
= xstrdup (fname
);
414 ih
->control_macro
= NULL
;
416 /* If the pathname is absolute, just open it. */
419 ih
->foundhere
= ABSOLUTE_PATH
;
420 ih
->name
= ih
->nshort
;
421 return open (ih
->name
, O_RDONLY
, 0666);
424 /* Search directory path, trying to open the file. */
426 len
= strlen (fname
);
427 name
= xmalloc (len
+ pfile
->max_include_len
+ 2 + INCLUDE_LEN_FUDGE
);
429 for (l
= search_start
; l
; l
= l
->next
)
431 bcopy (l
->name
, name
, l
->nlen
);
433 strcpy (&name
[l
->nlen
+1], fname
);
434 simplify_pathname (name
);
435 if (CPP_OPTIONS (pfile
)->remap
)
436 name
= remap_filename (pfile
, name
, l
);
438 f
= open (name
, O_RDONLY
, 0666);
440 if (f
== -1 && errno
== EACCES
)
442 cpp_error(pfile
, "included file `%s' exists but is not readable",
451 ih
->name
= xrealloc (name
, strlen (name
)+1);
458 jh
->next_this_file
= NULL
;
462 *ihash
= (struct include_hash
*)-1;
466 /* The file_name_map structure holds a mapping of file names for a
467 particular directory. This mapping is read from the file named
468 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
469 map filenames on a file system with severe filename restrictions,
470 such as DOS. The format of the file name map file is just a series
471 of lines with two tokens on each line. The first token is the name
472 to map, and the second token is the actual name to use. */
476 struct file_name_map
*map_next
;
481 #define FILE_NAME_MAP_FILE "header.gcc"
483 /* Read a space delimited string of unlimited length from a stdio
487 read_filename_string (ch
, f
)
495 set
= alloc
= xmalloc (len
+ 1);
499 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
501 if (set
- alloc
== len
)
504 alloc
= xrealloc (alloc
, len
+ 1);
505 set
= alloc
+ len
/ 2;
515 /* This structure holds a linked list of file name maps, one per directory. */
517 struct file_name_map_list
519 struct file_name_map_list
*map_list_next
;
521 struct file_name_map
*map_list_map
;
524 /* Read the file name map file for DIRNAME. */
526 static struct file_name_map
*
527 read_name_map (pfile
, dirname
)
531 register struct file_name_map_list
*map_list_ptr
;
535 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
536 map_list_ptr
= map_list_ptr
->map_list_next
)
537 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
538 return map_list_ptr
->map_list_map
;
540 map_list_ptr
= ((struct file_name_map_list
*)
541 xmalloc (sizeof (struct file_name_map_list
)));
542 map_list_ptr
->map_list_name
= xstrdup (dirname
);
544 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
545 strcpy (name
, dirname
);
548 strcat (name
, FILE_NAME_MAP_FILE
);
549 f
= fopen (name
, "r");
551 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
555 int dirlen
= strlen (dirname
);
557 while ((ch
= getc (f
)) != EOF
)
560 struct file_name_map
*ptr
;
564 from
= read_filename_string (ch
, f
);
565 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
567 to
= read_filename_string (ch
, f
);
569 ptr
= ((struct file_name_map
*)
570 xmalloc (sizeof (struct file_name_map
)));
571 ptr
->map_from
= from
;
573 /* Make the real filename absolute. */
578 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
579 strcpy (ptr
->map_to
, dirname
);
580 ptr
->map_to
[dirlen
] = '/';
581 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
585 ptr
->map_next
= map_list_ptr
->map_list_map
;
586 map_list_ptr
->map_list_map
= ptr
;
588 while ((ch
= getc (f
)) != '\n')
595 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
596 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
598 return map_list_ptr
->map_list_map
;
601 /* Remap NAME based on the file_name_map (if any) for LOC. */
604 remap_filename (pfile
, name
, loc
)
607 struct file_name_list
*loc
;
609 struct file_name_map
*map
;
614 loc
->name_map
= read_name_map (pfile
,
618 if (loc
->name_map
== (struct file_name_map
*)-1)
621 from
= name
+ strlen (loc
->name
) + 1;
623 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
624 if (!strcmp (map
->map_from
, from
))
627 /* Try to find a mapping file for the particular directory we are
628 looking in. Thus #include <sys/types.h> will look up sys/types.h
629 in /usr/include/header.gcc and look up types.h in
630 /usr/include/sys/header.gcc. */
631 p
= rindex (name
, '/');
635 && strlen (loc
->name
) == (size_t) (p
- name
)
636 && !strncmp (loc
->name
, name
, p
- name
))
637 /* FILENAME is in SEARCHPTR, which we've already checked. */
647 dir
= (char *) alloca (p
- name
+ 1);
648 bcopy (name
, dir
, p
- name
);
649 dir
[p
- name
] = '\0';
653 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
654 if (! strcmp (map
->map_from
, name
))
660 /* Read the contents of FD into the buffer on the top of PFILE's stack.
661 IHASH points to the include hash entry for the file associated with
664 The caller is responsible for the cpp_push_buffer. */
667 finclude (pfile
, fd
, ihash
)
670 struct include_hash
*ihash
;
677 if (fstat (fd
, &st
) < 0)
680 fp
= CPP_BUFFER (pfile
);
681 fp
->nominal_fname
= fp
->fname
= ihash
->name
;
683 fp
->system_header_p
= (ihash
->foundhere
!= ABSOLUTE_PATH
684 && ihash
->foundhere
->sysp
);
687 fp
->cleanup
= file_cleanup
;
689 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
691 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
692 fp
->actual_dir
= actual_directory (pfile
, fp
->fname
);
694 if (S_ISREG (st
.st_mode
))
696 st_size
= (size_t) st
.st_size
;
697 if (st_size
!= st
.st_size
|| st_size
+ 2 < st_size
)
699 cpp_error (pfile
, "file `%s' too large", ihash
->name
);
702 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
703 fp
->alimit
= fp
->buf
+ st_size
+ 2;
706 /* Read the file contents, knowing that st_size is an upper bound
707 on the number of bytes we can read. */
708 length
= safe_read (fd
, fp
->buf
, st_size
);
709 fp
->rlimit
= fp
->buf
+ length
;
713 else if (S_ISDIR (st
.st_mode
))
715 cpp_pop_buffer (pfile
);
716 cpp_error (pfile
, "directory `%s' specified in #include", ihash
->name
);
721 /* Cannot count its file size before reading.
722 First read the entire file into heap and
723 copy them into buffer on stack. */
728 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
732 i
= safe_read (fd
, fp
->buf
+ st_size
, bsize
- st_size
);
736 if (st_size
!= bsize
)
737 break; /* End of file */
739 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
745 /* FIXME: Broken in presence of trigraphs (consider ??/<EOF>)
746 and doesn't warn about a missing newline. */
747 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
748 || (length
> 1 && fp
->buf
[length
- 2] == '\\'))
749 fp
->buf
[length
++] = '\n';
751 fp
->buf
[length
] = '\0';
752 fp
->rlimit
= fp
->buf
+ length
;
755 pfile
->input_stack_listing_current
= 0;
759 cpp_pop_buffer (pfile
);
760 cpp_error_from_errno (pfile
, ihash
->name
);
766 static struct file_name_list
*
767 actual_directory (pfile
, fname
)
771 char *last_slash
, *dir
;
773 struct file_name_list
*x
;
775 dir
= xstrdup (fname
);
776 last_slash
= rindex (dir
, '/');
779 if (last_slash
== dir
)
782 last_slash
[1] = '\0';
786 dlen
= last_slash
- dir
;
797 if (dlen
> pfile
->max_include_len
)
798 pfile
->max_include_len
= dlen
;
800 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
801 if (!strcmp (x
->name
, dir
))
807 /* Not found, make a new one. */
808 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
811 x
->next
= CPP_OPTIONS (pfile
)->quote_include
;
812 x
->alloc
= pfile
->actual_dirs
;
816 pfile
->actual_dirs
= x
;
820 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
821 retrying if necessary. If MAX_READ_LEN is defined, read at most
822 that bytes at a time. Return a negative value if an error occurs,
823 otherwise return the actual number of bytes read,
824 which must be LEN unless end-of-file was reached. */
827 safe_read (desc
, ptr
, len
)
832 int left
, rcount
, nchars
;
838 if (rcount
> MAX_READ_LEN
)
839 rcount
= MAX_READ_LEN
;
841 nchars
= read (desc
, ptr
, rcount
);
858 /* Add output to `deps_buffer' for the -M switch.
859 STRING points to the text to be output.
860 SPACER is ':' for targets, ' ' for dependencies, zero for text
861 to be inserted literally. */
864 deps_output (pfile
, string
, spacer
)
875 size
= strlen (string
);
877 #ifndef MAX_OUTPUT_COLUMNS
878 #define MAX_OUTPUT_COLUMNS 72
880 if (pfile
->deps_column
> 0
881 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
885 pfile
->deps_column
= 0;
888 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
890 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
891 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
892 pfile
->deps_allocated_size
);
897 bcopy (" \\\n ", &pfile
->deps_buffer
[pfile
->deps_size
], 5);
898 pfile
->deps_size
+= 5;
901 if (spacer
== ' ' && pfile
->deps_column
> 0)
902 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
903 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
904 pfile
->deps_size
+= size
;
905 pfile
->deps_column
+= size
;
907 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
908 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
911 /* Simplify a path name in place, deleting redundant components. This
912 reduces OS overhead and guarantees that equivalent paths compare
913 the same (modulo symlinks).
916 foo/bar/../quux foo/quux
920 //quux //quux (POSIX allows leading // as a namespace escape)
922 Guarantees no trailing slashes. All transforms reduce the length
926 simplify_pathname (path
)
933 #if defined _WIN32 || defined __MSDOS__
934 /* Convert all backslashes to slashes. */
935 for (from
= path
; *from
; from
++)
936 if (*from
== '\\') *from
= '/';
938 /* Skip over leading drive letter if present. */
939 if (ISALPHA (path
[0]) && path
[1] == ':')
940 from
= to
= &path
[2];
947 /* Remove redundant initial /s. */
956 /* 3 or more initial /s are equivalent to 1 /. */
957 while (*++from
== '/');
959 /* On some hosts // differs from /; Posix allows this. */
970 if (from
[0] == '.' && from
[1] == '/')
972 else if (from
[0] == '.' && from
[1] == '\0')
974 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
991 while (to
> base
&& *to
!= '/') to
--;
997 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
1010 while (to
> base
&& *to
!= '/') to
--;
1017 /* Copy this component and trailing /, if any. */
1018 while ((*to
++ = *from
++) != '/')
1030 /* Trim trailing slash */
1031 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
1034 /* Change the empty string to "." so that stat() on the result
1035 will always work. */
1044 /* It is not clear when this should be used if at all, so I've
1045 disabled it until someone who understands VMS can look at it. */
1048 /* Under VMS we need to fix up the "include" specification filename.
1050 Rules for possible conversions
1052 fullname tried paths
1055 ./dir/name [.dir]name
1057 /name [000000]name, name
1058 dir/name dir:[000000]name, dir:name, dir/name
1059 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1060 path:/name path:[000000]name, path:name
1061 path:/dir/name path:[000000.dir]name, path:[dir]name
1062 path:dir/name path:[dir]name
1063 [path]:[dir]name [path.dir]name
1064 path/[dir]name [path.dir]name
1066 The path:/name input is constructed when expanding <> includes. */
1070 hack_vms_include_specification (fullname
)
1073 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1074 int f
, check_filename_before_returning
, must_revert
;
1077 check_filename_before_returning
= 0;
1079 /* See if we can find a 1st slash. If not, there's no path information. */
1080 first_slash
= index (fullname
, '/');
1081 if (first_slash
== 0)
1082 return 0; /* Nothing to do!!! */
1084 /* construct device spec if none given. */
1086 if (index (fullname
, ':') == 0)
1089 /* If fullname has a slash, take it as device spec. */
1091 if (first_slash
== fullname
)
1093 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
1095 *first_slash
= ':'; /* make device spec */
1096 for (basename
= fullname
; *basename
!= 0; basename
++)
1097 *basename
= *(basename
+1); /* remove leading slash */
1099 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1100 && (first_slash
[-1] != ':')
1101 && (first_slash
[-1] != ']')) /* or a vms path */
1105 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1106 && (first_slash
[-1] == '.'))
1110 /* Get part after first ':' (basename[-1] == ':')
1111 or last '/' (basename[-1] == '/'). */
1113 basename
= base_name (fullname
);
1115 local_ptr
= Local
; /* initialize */
1117 /* We are trying to do a number of things here. First of all, we are
1118 trying to hammer the filenames into a standard format, such that later
1119 processing can handle them.
1121 If the file name contains something like [dir.], then it recognizes this
1122 as a root, and strips the ".]". Later processing will add whatever is
1123 needed to get things working properly.
1125 If no device is specified, then the first directory name is taken to be
1126 a device name (or a rooted logical). */
1128 /* Point to the UNIX filename part (which needs to be fixed!)
1129 but skip vms path information.
1130 [basename != fullname since first_slash != 0]. */
1132 if ((basename
[-1] == ':') /* vms path spec. */
1133 || (basename
[-1] == ']')
1134 || (basename
[-1] == '>'))
1135 unixname
= basename
;
1137 unixname
= fullname
;
1139 if (*unixname
== '/')
1142 /* If the directory spec is not rooted, we can just copy
1143 the UNIX filename part and we are done. */
1145 if (((basename
- fullname
) > 1)
1146 && ( (basename
[-1] == ']')
1147 || (basename
[-1] == '>')))
1149 if (basename
[-2] != '.')
1152 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1153 -> PATH]:/name (basename = '/name', unixname = 'name')
1154 We strip the `]', and then splice the two parts of the name in the
1155 usual way. Given the default locations for include files in cccp.c,
1156 we will only use this code if the user specifies alternate locations
1157 with the /include (-I) switch on the command line. */
1159 basename
-= 1; /* Strip "]" */
1160 unixname
--; /* backspace */
1165 /* The VMS part has a ".]" at the end, and this will not do. Later
1166 processing will add a second directory spec, and this would be a syntax
1167 error. Thus we strip the ".]", and thus merge the directory specs.
1168 We also backspace unixname, so that it points to a '/'. This inhibits the
1169 generation of the 000000 root directory spec (which does not belong here
1172 basename
-= 2; /* Strip ".]" */
1173 unixname
--; /* backspace */
1181 /* We drop in here if there is no VMS style directory specification yet.
1182 If there is no device specification either, we make the first dir a
1183 device and try that. If we do not do this, then we will be essentially
1184 searching the users default directory (as if they did a #include "asdf.h").
1186 Then all we need to do is to push a '[' into the output string. Later
1187 processing will fill this in, and close the bracket. */
1189 if ((unixname
!= fullname
) /* vms path spec found. */
1190 && (basename
[-1] != ':'))
1191 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1193 *local_ptr
++ = '['; /* Open the directory specification */
1196 if (unixname
== fullname
) /* no vms dir spec. */
1199 if ((first_slash
!= 0) /* unix dir spec. */
1200 && (*unixname
!= '/') /* not beginning with '/' */
1201 && (*unixname
!= '.')) /* or './' or '../' */
1202 *local_ptr
++ = '.'; /* dir is local ! */
1205 /* at this point we assume that we have the device spec, and (at least
1206 the opening "[" for a directory specification. We may have directories
1209 If there are no other slashes then the filename will be
1210 in the "root" directory. Otherwise, we need to add
1211 directory specifications. */
1213 if (index (unixname
, '/') == 0)
1215 /* if no directories specified yet and none are following. */
1216 if (local_ptr
[-1] == '[')
1218 /* Just add "000000]" as the directory string */
1219 strcpy (local_ptr
, "000000]");
1220 local_ptr
+= strlen (local_ptr
);
1221 check_filename_before_returning
= 1; /* we might need to fool with this later */
1227 /* As long as there are still subdirectories to add, do them. */
1228 while (index (unixname
, '/') != 0)
1230 /* If this token is "." we can ignore it
1231 if it's not at the beginning of a path. */
1232 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1234 /* remove it at beginning of path. */
1235 if ( ((unixname
== fullname
) /* no device spec */
1236 && (fullname
+2 != basename
)) /* starts with ./ */
1238 || ((basename
[-1] == ':') /* device spec */
1239 && (unixname
-1 == basename
))) /* and ./ afterwards */
1240 *local_ptr
++ = '.'; /* make '[.' start of path. */
1245 /* Add a subdirectory spec. Do not duplicate "." */
1246 if ( local_ptr
[-1] != '.'
1247 && local_ptr
[-1] != '['
1248 && local_ptr
[-1] != '<')
1251 /* If this is ".." then the spec becomes "-" */
1252 if ( (unixname
[0] == '.')
1253 && (unixname
[1] == '.')
1254 && (unixname
[2] == '/'))
1256 /* Add "-" and skip the ".." */
1257 if ((local_ptr
[-1] == '.')
1258 && (local_ptr
[-2] == '['))
1259 local_ptr
--; /* prevent [.- */
1265 /* Copy the subdirectory */
1266 while (*unixname
!= '/')
1267 *local_ptr
++= *unixname
++;
1269 unixname
++; /* Skip the "/" */
1272 /* Close the directory specification */
1273 if (local_ptr
[-1] == '.') /* no trailing periods */
1276 if (local_ptr
[-1] == '[') /* no dir needed */
1282 /* Now add the filename. */
1285 *local_ptr
++ = *unixname
++;
1288 /* Now append it to the original VMS spec. */
1290 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1292 /* If we put a [000000] in the filename, try to open it first. If this fails,
1293 remove the [000000], and return that name. This provides flexibility
1294 to the user in that they can use both rooted and non-rooted logical names
1295 to point to the location of the file. */
1297 if (check_filename_before_returning
)
1299 f
= open (fullname
, O_RDONLY
, 0666);
1302 /* The file name is OK as it is, so return it as is. */
1307 /* The filename did not work. Try to remove the [000000] from the name,
1310 basename
= index (fullname
, '[');
1311 local_ptr
= index (fullname
, ']') + 1;
1312 strcpy (basename
, local_ptr
); /* this gets rid of it */