mkdeps.c, mkdeps.h: New files.
[gcc.git] / gcc / cppfiles.c
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
8
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding! */
26
27 #include "config.h"
28 #include "system.h"
29 #include "cpplib.h"
30 #include "intl.h"
31
32 /* The entry points to this file are: find_include_file,
33 cpp_read_file, finclude, include_hash, append_include_chain, and
34 file_cleanup. file_cleanup is only called through
35 CPP_BUFFER(pfile)->cleanup, so it's static anyway. */
36
37 static struct include_hash *redundant_include_p
38 PARAMS ((cpp_reader *,
39 struct include_hash *,
40 struct file_name_list *));
41 static struct file_name_map *read_name_map
42 PARAMS ((cpp_reader *, const char *));
43 static char *read_filename_string PARAMS ((int, FILE *));
44 static char *remap_filename PARAMS ((cpp_reader *, char *,
45 struct file_name_list *));
46 static long read_and_prescan PARAMS ((cpp_reader *, cpp_buffer *,
47 int, size_t));
48 static struct file_name_list *actual_directory
49 PARAMS ((cpp_reader *, const char *));
50 static void initialize_input_buffer PARAMS ((cpp_reader *, int,
51 struct stat *));
52 static int file_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
53 static U_CHAR *find_position PARAMS ((U_CHAR *, U_CHAR *,
54 unsigned long *));
55
56 #if 0
57 static void hack_vms_include_specification PARAMS ((char *));
58 #endif
59
60 /* Windows does not natively support inodes, and neither does MSDOS.
61 Cygwin's emulation can generate non-unique inodes, so don't use it.
62 VMS has non-numeric inodes. */
63 #ifdef VMS
64 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
65 #elif (defined _WIN32 && ! defined (_UWIN)) \
66 || defined __MSDOS__
67 #define INO_T_EQ(a, b) 0
68 #else
69 #define INO_T_EQ(a, b) ((a) == (b))
70 #endif
71
72 #ifndef INCLUDE_LEN_FUDGE
73 #define INCLUDE_LEN_FUDGE 0
74 #endif
75
76 /* Merge the four include chains together in the order quote, bracket,
77 system, after. Remove duplicate dirs (as determined by
78 INO_T_EQ()). The system_include and after_include chains are never
79 referred to again after this function; all access is through the
80 bracket_include path.
81
82 For the future: Check if the directory is empty (but
83 how?) and possibly preload the include hash. */
84
85 void
86 merge_include_chains (opts)
87 struct cpp_options *opts;
88 {
89 struct file_name_list *prev, *cur, *other;
90 struct file_name_list *quote, *brack, *systm, *after;
91 struct file_name_list *qtail, *btail, *stail, *atail;
92
93 qtail = opts->pending->quote_tail;
94 btail = opts->pending->brack_tail;
95 stail = opts->pending->systm_tail;
96 atail = opts->pending->after_tail;
97
98 quote = opts->pending->quote_head;
99 brack = opts->pending->brack_head;
100 systm = opts->pending->systm_head;
101 after = opts->pending->after_head;
102
103 /* Paste together bracket, system, and after include chains. */
104 if (stail)
105 stail->next = after;
106 else
107 systm = after;
108 if (btail)
109 btail->next = systm;
110 else
111 brack = systm;
112
113 /* This is a bit tricky.
114 First we drop dupes from the quote-include list.
115 Then we drop dupes from the bracket-include list.
116 Finally, if qtail and brack are the same directory,
117 we cut out qtail.
118
119 We can't just merge the lists and then uniquify them because
120 then we may lose directories from the <> search path that should
121 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
122 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
123 -Ibar -I- -Ifoo -Iquux.
124
125 Note that this algorithm is quadratic in the number of -I switches,
126 which is acceptable since there aren't usually that many of them. */
127
128 for (cur = quote, prev = NULL; cur; cur = cur->next)
129 {
130 for (other = quote; other != cur; other = other->next)
131 if (INO_T_EQ (cur->ino, other->ino)
132 && cur->dev == other->dev)
133 {
134 if (opts->verbose)
135 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
136 cur->name);
137
138 prev->next = cur->next;
139 free (cur->name);
140 free (cur);
141 cur = prev;
142 break;
143 }
144 prev = cur;
145 }
146 qtail = prev;
147
148 for (cur = brack; cur; cur = cur->next)
149 {
150 for (other = brack; other != cur; other = other->next)
151 if (INO_T_EQ (cur->ino, other->ino)
152 && cur->dev == other->dev)
153 {
154 if (opts->verbose)
155 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
156 cur->name);
157
158 prev->next = cur->next;
159 free (cur->name);
160 free (cur);
161 cur = prev;
162 break;
163 }
164 prev = cur;
165 }
166
167 if (quote)
168 {
169 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
170 {
171 if (quote == qtail)
172 {
173 if (opts->verbose)
174 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
175 quote->name);
176
177 free (quote->name);
178 free (quote);
179 quote = brack;
180 }
181 else
182 {
183 cur = quote;
184 while (cur->next != qtail)
185 cur = cur->next;
186 cur->next = brack;
187 if (opts->verbose)
188 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
189 qtail->name);
190
191 free (qtail->name);
192 free (qtail);
193 }
194 }
195 else
196 qtail->next = brack;
197 }
198 else
199 quote = brack;
200
201 opts->quote_include = quote;
202 opts->bracket_include = brack;
203 }
204
205 /* Look up or add an entry to the table of all includes. This table
206 is indexed by the name as it appears in the #include line. The
207 ->next_this_file chain stores all different files with the same
208 #include name (there are at least three ways this can happen). The
209 hash function could probably be improved a bit. */
210
211 struct include_hash *
212 include_hash (pfile, fname, add)
213 cpp_reader *pfile;
214 const char *fname;
215 int add;
216 {
217 unsigned int hash = 0;
218 struct include_hash *l, *m;
219 const char *f = fname;
220
221 while (*f)
222 hash += *f++;
223
224 l = pfile->all_include_files[hash % ALL_INCLUDE_HASHSIZE];
225 m = 0;
226 for (; l; m = l, l = l->next)
227 if (!strcmp (l->nshort, fname))
228 return l;
229
230 if (!add)
231 return 0;
232
233 l = (struct include_hash *) xmalloc (sizeof (struct include_hash));
234 l->next = NULL;
235 l->next_this_file = NULL;
236 l->foundhere = NULL;
237 l->buf = NULL;
238 l->limit = NULL;
239 if (m)
240 m->next = l;
241 else
242 pfile->all_include_files[hash % ALL_INCLUDE_HASHSIZE] = l;
243
244 return l;
245 }
246
247 /* Return 0 if the file pointed to by IHASH has never been included before,
248 -1 if it has been included before and need not be again,
249 or a pointer to an IHASH entry which is the file to be reread.
250 "Never before" is with respect to the position in ILIST.
251
252 This will not detect redundancies involving odd uses of the
253 `current directory' rule for "" includes. They aren't quite
254 pathological, but I think they are rare enough not to worry about.
255 The simplest example is:
256
257 top.c:
258 #include "a/a.h"
259 #include "b/b.h"
260
261 a/a.h:
262 #include "../b/b.h"
263
264 and the problem is that for `current directory' includes,
265 ihash->foundhere is not on any of the global include chains,
266 so the test below (i->foundhere == l) may be false even when
267 the directories are in fact the same. */
268
269 static struct include_hash *
270 redundant_include_p (pfile, ihash, ilist)
271 cpp_reader *pfile;
272 struct include_hash *ihash;
273 struct file_name_list *ilist;
274 {
275 struct file_name_list *l;
276 struct include_hash *i;
277
278 if (! ihash->foundhere)
279 return 0;
280
281 for (i = ihash; i; i = i->next_this_file)
282 for (l = ilist; l; l = l->next)
283 if (i->foundhere == l)
284 /* The control_macro works like this: If it's NULL, the file
285 is to be included again. If it's "", the file is never to
286 be included again. If it's a string, the file is not to be
287 included again if the string is the name of a defined macro. */
288 return (i->control_macro
289 && (i->control_macro[0] == '\0'
290 || cpp_defined (pfile, i->control_macro, -1)))
291 ? (struct include_hash *)-1 : i;
292
293 return 0;
294 }
295
296 static int
297 file_cleanup (pbuf, pfile)
298 cpp_buffer *pbuf;
299 cpp_reader *pfile;
300 {
301 if (pbuf->buf)
302 {
303 free (pbuf->buf);
304 pbuf->buf = 0;
305 }
306 if (pfile->system_include_depth)
307 pfile->system_include_depth--;
308 return 0;
309 }
310
311 /* Search for include file FNAME in the include chain starting at
312 SEARCH_START. Return -2 if this file doesn't need to be included
313 (because it was included already and it's marked idempotent),
314 -1 if an error occurred, or a file descriptor open on the file.
315 *IHASH is set to point to the include hash entry for this file, and
316 *BEFORE is 1 if the file was included before (but needs to be read
317 again). */
318 int
319 find_include_file (pfile, fname, search_start, ihash, before)
320 cpp_reader *pfile;
321 const char *fname;
322 struct file_name_list *search_start;
323 struct include_hash **ihash;
324 int *before;
325 {
326 struct file_name_list *l;
327 struct include_hash *ih, *jh;
328 int f, len;
329 char *name;
330
331 ih = include_hash (pfile, fname, 1);
332 jh = redundant_include_p (pfile, ih,
333 fname[0] == '/' ? ABSOLUTE_PATH : search_start);
334
335 if (jh != 0)
336 {
337 *before = 1;
338 *ihash = jh;
339
340 if (jh == (struct include_hash *)-1)
341 return -2;
342 else
343 return open (jh->name, O_RDONLY, 0666);
344 }
345
346 if (ih->foundhere)
347 /* A file is already known by this name, but it's not the same file.
348 Allocate another include_hash block and add it to the next_this_file
349 chain. */
350 {
351 jh = (struct include_hash *)xmalloc (sizeof (struct include_hash));
352 while (ih->next_this_file) ih = ih->next_this_file;
353
354 ih->next_this_file = jh;
355 jh = ih;
356 ih = ih->next_this_file;
357
358 ih->next = NULL;
359 ih->next_this_file = NULL;
360 ih->buf = NULL;
361 ih->limit = NULL;
362 }
363 *before = 0;
364 *ihash = ih;
365 ih->nshort = xstrdup (fname);
366 ih->control_macro = NULL;
367
368 /* If the pathname is absolute, just open it. */
369 if (fname[0] == '/')
370 {
371 ih->foundhere = ABSOLUTE_PATH;
372 ih->name = ih->nshort;
373 return open (ih->name, O_RDONLY, 0666);
374 }
375
376 /* Search directory path, trying to open the file. */
377
378 len = strlen (fname);
379 name = xmalloc (len + pfile->max_include_len + 2 + INCLUDE_LEN_FUDGE);
380
381 for (l = search_start; l; l = l->next)
382 {
383 bcopy (l->name, name, l->nlen);
384 name[l->nlen] = '/';
385 strcpy (&name[l->nlen+1], fname);
386 simplify_pathname (name);
387 if (CPP_OPTIONS (pfile)->remap)
388 name = remap_filename (pfile, name, l);
389
390 f = open (name, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
391 #ifdef EACCES
392 if (f == -1 && errno == EACCES)
393 {
394 cpp_error(pfile, "included file `%s' exists but is not readable",
395 name);
396 return -1;
397 }
398 #endif
399
400 if (f >= 0)
401 {
402 ih->foundhere = l;
403 ih->name = xrealloc (name, strlen (name)+1);
404 return f;
405 }
406 }
407
408 if (jh)
409 {
410 jh->next_this_file = NULL;
411 free (ih);
412 }
413 free (name);
414 *ihash = (struct include_hash *)-1;
415 return -1;
416 }
417
418 /* The file_name_map structure holds a mapping of file names for a
419 particular directory. This mapping is read from the file named
420 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
421 map filenames on a file system with severe filename restrictions,
422 such as DOS. The format of the file name map file is just a series
423 of lines with two tokens on each line. The first token is the name
424 to map, and the second token is the actual name to use. */
425
426 struct file_name_map
427 {
428 struct file_name_map *map_next;
429 char *map_from;
430 char *map_to;
431 };
432
433 #define FILE_NAME_MAP_FILE "header.gcc"
434
435 /* Read a space delimited string of unlimited length from a stdio
436 file. */
437
438 static char *
439 read_filename_string (ch, f)
440 int ch;
441 FILE *f;
442 {
443 char *alloc, *set;
444 int len;
445
446 len = 20;
447 set = alloc = xmalloc (len + 1);
448 if (! is_space(ch))
449 {
450 *set++ = ch;
451 while ((ch = getc (f)) != EOF && ! is_space(ch))
452 {
453 if (set - alloc == len)
454 {
455 len *= 2;
456 alloc = xrealloc (alloc, len + 1);
457 set = alloc + len / 2;
458 }
459 *set++ = ch;
460 }
461 }
462 *set = '\0';
463 ungetc (ch, f);
464 return alloc;
465 }
466
467 /* This structure holds a linked list of file name maps, one per directory. */
468
469 struct file_name_map_list
470 {
471 struct file_name_map_list *map_list_next;
472 char *map_list_name;
473 struct file_name_map *map_list_map;
474 };
475
476 /* Read the file name map file for DIRNAME. */
477
478 static struct file_name_map *
479 read_name_map (pfile, dirname)
480 cpp_reader *pfile;
481 const char *dirname;
482 {
483 register struct file_name_map_list *map_list_ptr;
484 char *name;
485 FILE *f;
486
487 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
488 map_list_ptr = map_list_ptr->map_list_next)
489 if (! strcmp (map_list_ptr->map_list_name, dirname))
490 return map_list_ptr->map_list_map;
491
492 map_list_ptr = ((struct file_name_map_list *)
493 xmalloc (sizeof (struct file_name_map_list)));
494 map_list_ptr->map_list_name = xstrdup (dirname);
495
496 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
497 strcpy (name, dirname);
498 if (*dirname)
499 strcat (name, "/");
500 strcat (name, FILE_NAME_MAP_FILE);
501 f = fopen (name, "r");
502 if (!f)
503 map_list_ptr->map_list_map = (struct file_name_map *)-1;
504 else
505 {
506 int ch;
507 int dirlen = strlen (dirname);
508
509 while ((ch = getc (f)) != EOF)
510 {
511 char *from, *to;
512 struct file_name_map *ptr;
513
514 if (is_space(ch))
515 continue;
516 from = read_filename_string (ch, f);
517 while ((ch = getc (f)) != EOF && is_hspace(ch))
518 ;
519 to = read_filename_string (ch, f);
520
521 ptr = ((struct file_name_map *)
522 xmalloc (sizeof (struct file_name_map)));
523 ptr->map_from = from;
524
525 /* Make the real filename absolute. */
526 if (*to == '/')
527 ptr->map_to = to;
528 else
529 {
530 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
531 strcpy (ptr->map_to, dirname);
532 ptr->map_to[dirlen] = '/';
533 strcpy (ptr->map_to + dirlen + 1, to);
534 free (to);
535 }
536
537 ptr->map_next = map_list_ptr->map_list_map;
538 map_list_ptr->map_list_map = ptr;
539
540 while ((ch = getc (f)) != '\n')
541 if (ch == EOF)
542 break;
543 }
544 fclose (f);
545 }
546
547 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
548 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
549
550 return map_list_ptr->map_list_map;
551 }
552
553 /* Remap NAME based on the file_name_map (if any) for LOC. */
554
555 static char *
556 remap_filename (pfile, name, loc)
557 cpp_reader *pfile;
558 char *name;
559 struct file_name_list *loc;
560 {
561 struct file_name_map *map;
562 const char *from, *p, *dir;
563
564 if (! loc->name_map)
565 loc->name_map = read_name_map (pfile,
566 loc->name
567 ? loc->name : ".");
568
569 if (loc->name_map == (struct file_name_map *)-1)
570 return name;
571
572 from = name + strlen (loc->name) + 1;
573
574 for (map = loc->name_map; map; map = map->map_next)
575 if (!strcmp (map->map_from, from))
576 return map->map_to;
577
578 /* Try to find a mapping file for the particular directory we are
579 looking in. Thus #include <sys/types.h> will look up sys/types.h
580 in /usr/include/header.gcc and look up types.h in
581 /usr/include/sys/header.gcc. */
582 p = rindex (name, '/');
583 if (!p)
584 p = name;
585 if (loc && loc->name
586 && strlen (loc->name) == (size_t) (p - name)
587 && !strncmp (loc->name, name, p - name))
588 /* FILENAME is in SEARCHPTR, which we've already checked. */
589 return name;
590
591 if (p == name)
592 {
593 dir = ".";
594 from = name;
595 }
596 else
597 {
598 char * newdir = (char *) alloca (p - name + 1);
599 bcopy (name, newdir, p - name);
600 newdir[p - name] = '\0';
601 dir = newdir;
602 from = p + 1;
603 }
604
605 for (map = read_name_map (pfile, dir); map; map = map->map_next)
606 if (! strcmp (map->map_from, name))
607 return map->map_to;
608
609 return name;
610 }
611
612 /* Push an input buffer and load it up with the contents of FNAME.
613 If FNAME is "" or NULL, read standard input. */
614 int
615 cpp_read_file (pfile, fname)
616 cpp_reader *pfile;
617 const char *fname;
618 {
619 struct include_hash *ih_fake;
620 int f;
621
622 if (fname == NULL || *fname == 0)
623 {
624 fname = "";
625 f = 0;
626 }
627
628 /* Open the file in nonblocking mode, so we don't get stuck if
629 someone clever has asked cpp to process /dev/rmt0. finclude()
630 will check that we have a real file to work with. Also take
631 care not to acquire a controlling terminal by mistake (this can't
632 happen on sane systems, but paranoia is a virtue). */
633 else if ((f = open (fname, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666)) < 0)
634 {
635 cpp_notice_from_errno (pfile, fname);
636 return 0;
637 }
638
639 /* Push the buffer. */
640 if (!cpp_push_buffer (pfile, NULL, 0))
641 goto failed_push;
642
643 /* Gin up an include_hash structure for this file and feed it
644 to finclude. */
645
646 ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
647 ih_fake->next = 0;
648 ih_fake->next_this_file = 0;
649 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
650 ih_fake->name = fname;
651 ih_fake->control_macro = 0;
652 ih_fake->buf = (char *)-1;
653 ih_fake->limit = 0;
654 if (!finclude (pfile, f, ih_fake))
655 goto failed_finclude;
656
657 return 1;
658
659 failed_finclude:
660 /* If finclude fails, it pops the buffer. */
661 free (ih_fake);
662 failed_push:
663 if (f)
664 close (f);
665 return 0;
666 }
667
668 /* Read the contents of FD into the buffer on the top of PFILE's stack.
669 IHASH points to the include hash entry for the file associated with
670 FD.
671
672 The caller is responsible for the cpp_push_buffer. */
673
674 int
675 finclude (pfile, fd, ihash)
676 cpp_reader *pfile;
677 int fd;
678 struct include_hash *ihash;
679 {
680 struct stat st;
681 size_t st_size;
682 long length;
683 cpp_buffer *fp;
684
685 if (fstat (fd, &st) < 0)
686 goto perror_fail;
687 if (fcntl (fd, F_SETFL, 0) == -1) /* turn off nonblocking mode */
688 goto perror_fail;
689
690 fp = CPP_BUFFER (pfile);
691
692 /* If fd points to a plain file, we know how big it is, so we can
693 allocate the buffer all at once. If fd is a pipe or terminal, we
694 can't. Most C source files are 4k or less, so we guess that. If
695 fd is something weird, like a block device or a directory, we
696 don't want to read it at all.
697
698 Unfortunately, different systems use different st.st_mode values
699 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
700 zero the entire struct stat except a couple fields. Hence the
701 mess below.
702
703 In all cases, read_and_prescan will resize the buffer if it
704 turns out there's more data than we thought. */
705
706 if (S_ISREG (st.st_mode))
707 {
708 /* off_t might have a wider range than size_t - in other words,
709 the max size of a file might be bigger than the address
710 space. We can't handle a file that large. (Anyone with
711 a single source file bigger than 4GB needs to rethink
712 their coding style.) */
713 st_size = (size_t) st.st_size;
714 if ((unsigned HOST_WIDEST_INT) st_size
715 != (unsigned HOST_WIDEST_INT) st.st_size)
716 {
717 cpp_error (pfile, "file `%s' is too large", ihash->name);
718 goto fail;
719 }
720 }
721 else if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)
722 /* Permit any kind of character device: the sensible ones are
723 ttys and /dev/null, but weeding out the others is too hard. */
724 || S_ISCHR (st.st_mode)
725 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
726 socket or pipe return a stat struct with most fields zeroed. */
727 || (st.st_mode == 0 && st.st_nlink == 0 && st.st_size == 0))
728 {
729 /* Cannot get its file size before reading. 4k is a decent
730 first guess. */
731 st_size = 4096;
732 }
733 else
734 {
735 cpp_error (pfile, "`%s' is not a file, pipe, or tty", ihash->name);
736 goto fail;
737 }
738
739 if (pfile->input_buffer == NULL)
740 initialize_input_buffer (pfile, fd, &st);
741
742 /* Read the file, converting end-of-line characters and trigraphs
743 (if enabled). */
744 fp->ihash = ihash;
745 fp->nominal_fname = fp->fname = ihash->name;
746 length = read_and_prescan (pfile, fp, fd, st_size);
747 if (length < 0)
748 goto fail;
749 if (length == 0)
750 ihash->control_macro = ""; /* never re-include */
751
752 close (fd);
753 fp->rlimit = fp->alimit = fp->buf + length;
754 fp->cur = fp->buf;
755 if (ihash->foundhere != ABSOLUTE_PATH)
756 fp->system_header_p = ihash->foundhere->sysp;
757 fp->lineno = 1;
758 fp->colno = 1;
759 fp->line_base = fp->buf;
760 fp->cleanup = file_cleanup;
761
762 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
763 see do_include */
764 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
765 fp->actual_dir = actual_directory (pfile, fp->fname);
766
767 pfile->input_stack_listing_current = 0;
768 return 1;
769
770 perror_fail:
771 cpp_error_from_errno (pfile, ihash->name);
772 fail:
773 cpp_pop_buffer (pfile);
774 close (fd);
775 return 0;
776 }
777
778 /* Given a path FNAME, extract the directory component and place it
779 onto the actual_dirs list. Return a pointer to the allocated
780 file_name_list structure. These structures are used to implement
781 current-directory "" include searching. */
782
783 static struct file_name_list *
784 actual_directory (pfile, fname)
785 cpp_reader *pfile;
786 const char *fname;
787 {
788 char *last_slash, *dir;
789 size_t dlen;
790 struct file_name_list *x;
791
792 dir = xstrdup (fname);
793 last_slash = rindex (dir, '/');
794 if (last_slash)
795 {
796 if (last_slash == dir)
797 {
798 dlen = 1;
799 last_slash[1] = '\0';
800 }
801 else
802 {
803 dlen = last_slash - dir;
804 *last_slash = '\0';
805 }
806 }
807 else
808 {
809 dir[0] = '.';
810 dir[1] = '\0';
811 dlen = 1;
812 }
813
814 if (dlen > pfile->max_include_len)
815 pfile->max_include_len = dlen;
816
817 for (x = pfile->actual_dirs; x; x = x->alloc)
818 if (!strcmp (x->name, dir))
819 {
820 free (dir);
821 return x;
822 }
823
824 /* Not found, make a new one. */
825 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
826 x->name = dir;
827 x->nlen = dlen;
828 x->next = CPP_OPTIONS (pfile)->quote_include;
829 x->alloc = pfile->actual_dirs;
830 x->sysp = CPP_BUFFER (pfile)->system_header_p;
831 x->name_map = NULL;
832
833 pfile->actual_dirs = x;
834 return x;
835 }
836
837 /* Determine the current line and column. Used only by read_and_prescan. */
838 static U_CHAR *
839 find_position (start, limit, linep)
840 U_CHAR *start;
841 U_CHAR *limit;
842 unsigned long *linep;
843 {
844 unsigned long line = *linep;
845 U_CHAR *lbase = start;
846 while (start < limit)
847 {
848 U_CHAR ch = *start++;
849 if (ch == '\n' || ch == '\r')
850 {
851 line++;
852 lbase = start;
853 }
854 }
855 *linep = line;
856 return lbase;
857 }
858
859 /* Read the entire contents of file DESC into buffer BUF. LEN is how
860 much memory to allocate initially; more will be allocated if
861 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
862 canonical form (\n). If enabled, convert and/or warn about
863 trigraphs. Convert backslash-newline to a one-character escape
864 (\r) and remove it from "embarrassing" places (i.e. the middle of a
865 token). If there is no newline at the end of the file, add one and
866 warn. Returns -1 on failure, or the actual length of the data to
867 be scanned.
868
869 This function does a lot of work, and can be a serious performance
870 bottleneck. It has been tuned heavily; make sure you understand it
871 before hacking. The common case - no trigraphs, Unix style line
872 breaks, backslash-newline set off by whitespace, newline at EOF -
873 has been optimized at the expense of the others. The performance
874 penalty for DOS style line breaks (\r\n) is about 15%.
875
876 Warnings lose particularly heavily since we have to determine the
877 line number, which involves scanning from the beginning of the file
878 or from the last warning. The penalty for the absence of a newline
879 at the end of reload1.c is about 60%. (reload1.c is 329k.)
880
881 If your file has more than one kind of end-of-line marker, you
882 will get messed-up line numbering. */
883
884 /* Table of characters that can't be handled in the inner loop.
885 Keep these contiguous to optimize the performance of the code generated
886 for the switch that uses them. */
887 #define SPECCASE_EMPTY 0
888 #define SPECCASE_NUL 1
889 #define SPECCASE_CR 2
890 #define SPECCASE_BACKSLASH 3
891 #define SPECCASE_QUESTION 4
892
893 static long
894 read_and_prescan (pfile, fp, desc, len)
895 cpp_reader *pfile;
896 cpp_buffer *fp;
897 int desc;
898 size_t len;
899 {
900 U_CHAR *buf = (U_CHAR *) xmalloc (len);
901 U_CHAR *ip, *op, *line_base;
902 U_CHAR *ibase;
903 U_CHAR *speccase = pfile->input_speccase;
904 unsigned long line;
905 unsigned int deferred_newlines;
906 int count;
907 size_t offset;
908
909 offset = 0;
910 op = buf;
911 line_base = buf;
912 line = 1;
913 ibase = pfile->input_buffer + 2;
914 deferred_newlines = 0;
915
916 for (;;)
917 {
918 read_next:
919
920 count = read (desc, pfile->input_buffer + 2, pfile->input_buffer_len);
921 if (count < 0)
922 goto error;
923 else if (count == 0)
924 break;
925
926 offset += count;
927 ip = ibase;
928 ibase = pfile->input_buffer + 2;
929 ibase[count] = ibase[count+1] = '\0';
930
931 if (offset > len)
932 {
933 size_t delta_op;
934 size_t delta_line_base;
935 len *= 2;
936 if (offset > len)
937 /* len overflowed.
938 This could happen if the file is larger than half the
939 maximum address space of the machine. */
940 goto too_big;
941
942 delta_op = op - buf;
943 delta_line_base = line_base - buf;
944 buf = (U_CHAR *) xrealloc (buf, len);
945 op = buf + delta_op;
946 line_base = buf + delta_line_base;
947 }
948
949 for (;;)
950 {
951 unsigned int span = 0;
952
953 /* Deal with \-newline in the middle of a token. */
954 if (deferred_newlines)
955 {
956 while (speccase[ip[span]] == SPECCASE_EMPTY
957 && ip[span] != '\n'
958 && ip[span] != '\t'
959 && ip[span] != ' ')
960 span++;
961 memcpy (op, ip, span);
962 op += span;
963 ip += span;
964 /* If ip[0] is SPECCASE_EMPTY, we have hit white space.
965 Dump out the remaining deferred \-newlines. */
966 if (speccase[ip[0]] == SPECCASE_EMPTY)
967 while (deferred_newlines)
968 deferred_newlines--, *op++ = '\r';
969 span = 0;
970 }
971
972 /* Copy as much as we can without special treatment. */
973 while (speccase[ip[span]] == SPECCASE_EMPTY) span++;
974 memcpy (op, ip, span);
975 op += span;
976 ip += span;
977
978 switch (speccase[*ip++])
979 {
980 case SPECCASE_NUL: /* \0 */
981 ibase[-1] = op[-1];
982 goto read_next;
983
984 case SPECCASE_CR: /* \r */
985 if (ip[-2] == '\n')
986 continue;
987 else if (*ip == '\n')
988 ip++;
989 else if (*ip == '\0')
990 {
991 *--ibase = '\r';
992 goto read_next;
993 }
994 *op++ = '\n';
995 break;
996
997 case SPECCASE_BACKSLASH: /* \ */
998 backslash:
999 {
1000 /* If we're at the end of the intermediate buffer,
1001 we have to shift the backslash down to the start
1002 and come back next pass. */
1003 if (*ip == '\0')
1004 {
1005 *--ibase = '\\';
1006 goto read_next;
1007 }
1008 else if (*ip == '\n')
1009 {
1010 ip++;
1011 if (*ip == '\r') ip++;
1012 if (*ip == '\n' || *ip == '\t' || *ip == ' ')
1013 *op++ = '\r';
1014 else if (op[-1] == '\t' || op[-1] == ' '
1015 || op[-1] == '\r' || op[-1] == '\n')
1016 *op++ = '\r';
1017 else
1018 deferred_newlines++;
1019 }
1020 else if (*ip == '\r')
1021 {
1022 ip++;
1023 if (*ip == '\n') ip++;
1024 else if (*ip == '\0')
1025 {
1026 *--ibase = '\r';
1027 *--ibase = '\\';
1028 goto read_next;
1029 }
1030 else if (*ip == '\r' || *ip == '\t' || *ip == ' ')
1031 *op++ = '\r';
1032 else
1033 deferred_newlines++;
1034 }
1035 else
1036 *op++ = '\\';
1037 }
1038 break;
1039
1040 case SPECCASE_QUESTION: /* ? */
1041 {
1042 unsigned int d, t;
1043 /* If we're at the end of the intermediate buffer,
1044 we have to shift the ?'s down to the start and
1045 come back next pass. */
1046 d = ip[0];
1047 if (d == '\0')
1048 {
1049 *--ibase = '?';
1050 goto read_next;
1051 }
1052 if (d != '?')
1053 {
1054 *op++ = '?';
1055 break;
1056 }
1057 d = ip[1];
1058 if (d == '\0')
1059 {
1060 *--ibase = '?';
1061 *--ibase = '?';
1062 goto read_next;
1063 }
1064
1065 /* Trigraph map:
1066 * from to from to from to
1067 * ?? = # ?? ) ] ?? ! |
1068 * ?? ( [ ?? ' ^ ?? > }
1069 * ?? / \ ?? < { ?? - ~
1070 */
1071 if (d == '=') t = '#';
1072 else if (d == ')') t = ']';
1073 else if (d == '!') t = '|';
1074 else if (d == '(') t = '[';
1075 else if (d == '\'') t = '^';
1076 else if (d == '>') t = '}';
1077 else if (d == '/') t = '\\';
1078 else if (d == '<') t = '{';
1079 else if (d == '-') t = '~';
1080 else
1081 {
1082 *op++ = '?';
1083 break;
1084 }
1085 ip += 2;
1086 if (CPP_OPTIONS (pfile)->warn_trigraphs)
1087 {
1088 unsigned long col;
1089 line_base = find_position (line_base, op, &line);
1090 col = op - line_base + 1;
1091 if (CPP_OPTIONS (pfile)->trigraphs)
1092 cpp_warning_with_line (pfile, line, col,
1093 "trigraph ??%c converted to %c", d, t);
1094 else
1095 cpp_warning_with_line (pfile, line, col,
1096 "trigraph ??%c ignored", d);
1097 }
1098 if (CPP_OPTIONS (pfile)->trigraphs)
1099 {
1100 if (t == '\\')
1101 goto backslash;
1102 else
1103 *op++ = t;
1104 }
1105 else
1106 {
1107 *op++ = '?';
1108 *op++ = '?';
1109 *op++ = d;
1110 }
1111 }
1112 }
1113 }
1114 }
1115
1116 if (offset == 0)
1117 return 0;
1118
1119 /* Deal with pushed-back chars at true EOF.
1120 This may be any of: ?? ? \ \r \n \\r \\n.
1121 \r must become \n, \\r or \\n must become \r.
1122 We know we have space already. */
1123 if (ibase == pfile->input_buffer)
1124 {
1125 if (*ibase == '?')
1126 {
1127 *op++ = '?';
1128 *op++ = '?';
1129 }
1130 else
1131 *op++ = '\r';
1132 }
1133 else if (ibase == pfile->input_buffer + 1)
1134 {
1135 if (*ibase == '\r')
1136 *op++ = '\n';
1137 else
1138 *op++ = *ibase;
1139 }
1140
1141 if (op[-1] != '\n')
1142 {
1143 unsigned long col;
1144 line_base = find_position (line_base, op, &line);
1145 col = op - line_base + 1;
1146 cpp_warning_with_line (pfile, line, col, "no newline at end of file\n");
1147 if (offset + 1 > len)
1148 {
1149 len += 1;
1150 if (offset + 1 > len)
1151 goto too_big;
1152 buf = (U_CHAR *) xrealloc (buf, len);
1153 op = buf + offset;
1154 }
1155 *op++ = '\n';
1156 }
1157
1158 fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
1159 return op - buf;
1160
1161 too_big:
1162 cpp_error (pfile, "file is too large (>%lu bytes)\n", (unsigned long)offset);
1163 free (buf);
1164 return -1;
1165
1166 error:
1167 cpp_error_from_errno (pfile, fp->fname);
1168 free (buf);
1169 return -1;
1170 }
1171
1172 /* Initialize the `input_buffer' and `input_speccase' tables.
1173 These are only used by read_and_prescan, but they're large and
1174 somewhat expensive to set up, so we want them allocated once for
1175 the duration of the cpp run. */
1176
1177 static void
1178 initialize_input_buffer (pfile, fd, st)
1179 cpp_reader *pfile;
1180 int fd;
1181 struct stat *st;
1182 {
1183 long pipe_buf;
1184 U_CHAR *tmp;
1185
1186 /* Table of characters that cannot be handled by the
1187 read_and_prescan inner loop. The number of non-EMPTY entries
1188 should be as small as humanly possible. */
1189
1190 tmp = (U_CHAR *) xmalloc (1 << CHAR_BIT);
1191 memset (tmp, SPECCASE_EMPTY, 1 << CHAR_BIT);
1192 tmp['\0'] = SPECCASE_NUL;
1193 tmp['\r'] = SPECCASE_CR;
1194 tmp['\\'] = SPECCASE_BACKSLASH;
1195 if (CPP_OPTIONS (pfile)->trigraphs || CPP_OPTIONS (pfile)->warn_trigraphs)
1196 tmp['?'] = SPECCASE_QUESTION;
1197
1198 pfile->input_speccase = tmp;
1199
1200 /* Determine the appropriate size for the input buffer. Normal C
1201 source files are smaller than eight K. If we are reading a pipe,
1202 we want to make sure the input buffer is bigger than the kernel's
1203 pipe buffer. */
1204 pipe_buf = -1;
1205
1206 if (! S_ISREG (st->st_mode))
1207 {
1208 #ifdef _PC_PIPE_BUF
1209 pipe_buf = fpathconf (fd, _PC_PIPE_BUF);
1210 #endif
1211 if (pipe_buf == -1)
1212 {
1213 #ifdef PIPE_BUF
1214 pipe_buf = PIPE_BUF;
1215 #else
1216 pipe_buf = 8192;
1217 #endif
1218 }
1219 }
1220
1221 if (pipe_buf < 8192)
1222 pipe_buf = 8192;
1223 /* PIPE_BUF bytes of buffer proper, 2 to detect running off the end
1224 without address arithmetic all the time, and 2 for pushback in
1225 the case there's a potential trigraph or end-of-line digraph at
1226 the end of a block. */
1227
1228 tmp = (U_CHAR *) xmalloc (pipe_buf + 2 + 2);
1229 pfile->input_buffer = tmp;
1230 pfile->input_buffer_len = pipe_buf;
1231 }
1232
1233 /* Simplify a path name in place, deleting redundant components. This
1234 reduces OS overhead and guarantees that equivalent paths compare
1235 the same (modulo symlinks).
1236
1237 Transforms made:
1238 foo/bar/../quux foo/quux
1239 foo/./bar foo/bar
1240 foo//bar foo/bar
1241 /../quux /quux
1242 //quux //quux (POSIX allows leading // as a namespace escape)
1243
1244 Guarantees no trailing slashes. All transforms reduce the length
1245 of the string.
1246 */
1247 void
1248 simplify_pathname (path)
1249 char *path;
1250 {
1251 char *from, *to;
1252 char *base;
1253 int absolute = 0;
1254
1255 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1256 /* Convert all backslashes to slashes. */
1257 for (from = path; *from; from++)
1258 if (*from == '\\') *from = '/';
1259
1260 /* Skip over leading drive letter if present. */
1261 if (ISALPHA (path[0]) && path[1] == ':')
1262 from = to = &path[2];
1263 else
1264 from = to = path;
1265 #else
1266 from = to = path;
1267 #endif
1268
1269 /* Remove redundant initial /s. */
1270 if (*from == '/')
1271 {
1272 absolute = 1;
1273 to++;
1274 from++;
1275 if (*from == '/')
1276 {
1277 if (*++from == '/')
1278 /* 3 or more initial /s are equivalent to 1 /. */
1279 while (*++from == '/');
1280 else
1281 /* On some hosts // differs from /; Posix allows this. */
1282 to++;
1283 }
1284 }
1285 base = to;
1286
1287 for (;;)
1288 {
1289 while (*from == '/')
1290 from++;
1291
1292 if (from[0] == '.' && from[1] == '/')
1293 from += 2;
1294 else if (from[0] == '.' && from[1] == '\0')
1295 goto done;
1296 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1297 {
1298 if (base == to)
1299 {
1300 if (absolute)
1301 from += 3;
1302 else
1303 {
1304 *to++ = *from++;
1305 *to++ = *from++;
1306 *to++ = *from++;
1307 base = to;
1308 }
1309 }
1310 else
1311 {
1312 to -= 2;
1313 while (to > base && *to != '/') to--;
1314 if (*to == '/')
1315 to++;
1316 from += 3;
1317 }
1318 }
1319 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1320 {
1321 if (base == to)
1322 {
1323 if (!absolute)
1324 {
1325 *to++ = *from++;
1326 *to++ = *from++;
1327 }
1328 }
1329 else
1330 {
1331 to -= 2;
1332 while (to > base && *to != '/') to--;
1333 if (*to == '/')
1334 to++;
1335 }
1336 goto done;
1337 }
1338 else
1339 /* Copy this component and trailing /, if any. */
1340 while ((*to++ = *from++) != '/')
1341 {
1342 if (!to[-1])
1343 {
1344 to--;
1345 goto done;
1346 }
1347 }
1348
1349 }
1350
1351 done:
1352 /* Trim trailing slash */
1353 if (to[0] == '/' && (!absolute || to > path+1))
1354 to--;
1355
1356 /* Change the empty string to "." so that stat() on the result
1357 will always work. */
1358 if (to == path)
1359 *to++ = '.';
1360
1361 *to = '\0';
1362
1363 return;
1364 }
1365
1366 /* It is not clear when this should be used if at all, so I've
1367 disabled it until someone who understands VMS can look at it. */
1368 #if 0
1369
1370 /* Under VMS we need to fix up the "include" specification filename.
1371
1372 Rules for possible conversions
1373
1374 fullname tried paths
1375
1376 name name
1377 ./dir/name [.dir]name
1378 /dir/name dir:name
1379 /name [000000]name, name
1380 dir/name dir:[000000]name, dir:name, dir/name
1381 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1382 path:/name path:[000000]name, path:name
1383 path:/dir/name path:[000000.dir]name, path:[dir]name
1384 path:dir/name path:[dir]name
1385 [path]:[dir]name [path.dir]name
1386 path/[dir]name [path.dir]name
1387
1388 The path:/name input is constructed when expanding <> includes. */
1389
1390
1391 static void
1392 hack_vms_include_specification (fullname)
1393 char *fullname;
1394 {
1395 register char *basename, *unixname, *local_ptr, *first_slash;
1396 int f, check_filename_before_returning, must_revert;
1397 char Local[512];
1398
1399 check_filename_before_returning = 0;
1400 must_revert = 0;
1401 /* See if we can find a 1st slash. If not, there's no path information. */
1402 first_slash = index (fullname, '/');
1403 if (first_slash == 0)
1404 return 0; /* Nothing to do!!! */
1405
1406 /* construct device spec if none given. */
1407
1408 if (index (fullname, ':') == 0)
1409 {
1410
1411 /* If fullname has a slash, take it as device spec. */
1412
1413 if (first_slash == fullname)
1414 {
1415 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
1416 if (first_slash)
1417 *first_slash = ':'; /* make device spec */
1418 for (basename = fullname; *basename != 0; basename++)
1419 *basename = *(basename+1); /* remove leading slash */
1420 }
1421 else if ((first_slash[-1] != '.') /* keep ':/', './' */
1422 && (first_slash[-1] != ':')
1423 && (first_slash[-1] != ']')) /* or a vms path */
1424 {
1425 *first_slash = ':';
1426 }
1427 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
1428 && (first_slash[-1] == '.'))
1429 fullname += 2;
1430 }
1431
1432 /* Get part after first ':' (basename[-1] == ':')
1433 or last '/' (basename[-1] == '/'). */
1434
1435 basename = base_name (fullname);
1436
1437 local_ptr = Local; /* initialize */
1438
1439 /* We are trying to do a number of things here. First of all, we are
1440 trying to hammer the filenames into a standard format, such that later
1441 processing can handle them.
1442
1443 If the file name contains something like [dir.], then it recognizes this
1444 as a root, and strips the ".]". Later processing will add whatever is
1445 needed to get things working properly.
1446
1447 If no device is specified, then the first directory name is taken to be
1448 a device name (or a rooted logical). */
1449
1450 /* Point to the UNIX filename part (which needs to be fixed!)
1451 but skip vms path information.
1452 [basename != fullname since first_slash != 0]. */
1453
1454 if ((basename[-1] == ':') /* vms path spec. */
1455 || (basename[-1] == ']')
1456 || (basename[-1] == '>'))
1457 unixname = basename;
1458 else
1459 unixname = fullname;
1460
1461 if (*unixname == '/')
1462 unixname++;
1463
1464 /* If the directory spec is not rooted, we can just copy
1465 the UNIX filename part and we are done. */
1466
1467 if (((basename - fullname) > 1)
1468 && ( (basename[-1] == ']')
1469 || (basename[-1] == '>')))
1470 {
1471 if (basename[-2] != '.')
1472 {
1473
1474 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1475 -> PATH]:/name (basename = '/name', unixname = 'name')
1476 We strip the `]', and then splice the two parts of the name in the
1477 usual way. Given the default locations for include files in cccp.c,
1478 we will only use this code if the user specifies alternate locations
1479 with the /include (-I) switch on the command line. */
1480
1481 basename -= 1; /* Strip "]" */
1482 unixname--; /* backspace */
1483 }
1484 else
1485 {
1486
1487 /* The VMS part has a ".]" at the end, and this will not do. Later
1488 processing will add a second directory spec, and this would be a syntax
1489 error. Thus we strip the ".]", and thus merge the directory specs.
1490 We also backspace unixname, so that it points to a '/'. This inhibits the
1491 generation of the 000000 root directory spec (which does not belong here
1492 in this case). */
1493
1494 basename -= 2; /* Strip ".]" */
1495 unixname--; /* backspace */
1496 }
1497 }
1498
1499 else
1500
1501 {
1502
1503 /* We drop in here if there is no VMS style directory specification yet.
1504 If there is no device specification either, we make the first dir a
1505 device and try that. If we do not do this, then we will be essentially
1506 searching the users default directory (as if they did a #include "asdf.h").
1507
1508 Then all we need to do is to push a '[' into the output string. Later
1509 processing will fill this in, and close the bracket. */
1510
1511 if ((unixname != fullname) /* vms path spec found. */
1512 && (basename[-1] != ':'))
1513 *local_ptr++ = ':'; /* dev not in spec. take first dir */
1514
1515 *local_ptr++ = '['; /* Open the directory specification */
1516 }
1517
1518 if (unixname == fullname) /* no vms dir spec. */
1519 {
1520 must_revert = 1;
1521 if ((first_slash != 0) /* unix dir spec. */
1522 && (*unixname != '/') /* not beginning with '/' */
1523 && (*unixname != '.')) /* or './' or '../' */
1524 *local_ptr++ = '.'; /* dir is local ! */
1525 }
1526
1527 /* at this point we assume that we have the device spec, and (at least
1528 the opening "[" for a directory specification. We may have directories
1529 specified already.
1530
1531 If there are no other slashes then the filename will be
1532 in the "root" directory. Otherwise, we need to add
1533 directory specifications. */
1534
1535 if (index (unixname, '/') == 0)
1536 {
1537 /* if no directories specified yet and none are following. */
1538 if (local_ptr[-1] == '[')
1539 {
1540 /* Just add "000000]" as the directory string */
1541 strcpy (local_ptr, "000000]");
1542 local_ptr += strlen (local_ptr);
1543 check_filename_before_returning = 1; /* we might need to fool with this later */
1544 }
1545 }
1546 else
1547 {
1548
1549 /* As long as there are still subdirectories to add, do them. */
1550 while (index (unixname, '/') != 0)
1551 {
1552 /* If this token is "." we can ignore it
1553 if it's not at the beginning of a path. */
1554 if ((unixname[0] == '.') && (unixname[1] == '/'))
1555 {
1556 /* remove it at beginning of path. */
1557 if ( ((unixname == fullname) /* no device spec */
1558 && (fullname+2 != basename)) /* starts with ./ */
1559 /* or */
1560 || ((basename[-1] == ':') /* device spec */
1561 && (unixname-1 == basename))) /* and ./ afterwards */
1562 *local_ptr++ = '.'; /* make '[.' start of path. */
1563 unixname += 2;
1564 continue;
1565 }
1566
1567 /* Add a subdirectory spec. Do not duplicate "." */
1568 if ( local_ptr[-1] != '.'
1569 && local_ptr[-1] != '['
1570 && local_ptr[-1] != '<')
1571 *local_ptr++ = '.';
1572
1573 /* If this is ".." then the spec becomes "-" */
1574 if ( (unixname[0] == '.')
1575 && (unixname[1] == '.')
1576 && (unixname[2] == '/'))
1577 {
1578 /* Add "-" and skip the ".." */
1579 if ((local_ptr[-1] == '.')
1580 && (local_ptr[-2] == '['))
1581 local_ptr--; /* prevent [.- */
1582 *local_ptr++ = '-';
1583 unixname += 3;
1584 continue;
1585 }
1586
1587 /* Copy the subdirectory */
1588 while (*unixname != '/')
1589 *local_ptr++= *unixname++;
1590
1591 unixname++; /* Skip the "/" */
1592 }
1593
1594 /* Close the directory specification */
1595 if (local_ptr[-1] == '.') /* no trailing periods */
1596 local_ptr--;
1597
1598 if (local_ptr[-1] == '[') /* no dir needed */
1599 local_ptr--;
1600 else
1601 *local_ptr++ = ']';
1602 }
1603
1604 /* Now add the filename. */
1605
1606 while (*unixname)
1607 *local_ptr++ = *unixname++;
1608 *local_ptr = 0;
1609
1610 /* Now append it to the original VMS spec. */
1611
1612 strcpy ((must_revert==1)?fullname:basename, Local);
1613
1614 /* If we put a [000000] in the filename, try to open it first. If this fails,
1615 remove the [000000], and return that name. This provides flexibility
1616 to the user in that they can use both rooted and non-rooted logical names
1617 to point to the location of the file. */
1618
1619 if (check_filename_before_returning)
1620 {
1621 f = open (fullname, O_RDONLY, 0666);
1622 if (f >= 0)
1623 {
1624 /* The file name is OK as it is, so return it as is. */
1625 close (f);
1626 return 1;
1627 }
1628
1629 /* The filename did not work. Try to remove the [000000] from the name,
1630 and return it. */
1631
1632 basename = index (fullname, '[');
1633 local_ptr = index (fullname, ']') + 1;
1634 strcpy (basename, local_ptr); /* this gets rid of it */
1635
1636 }
1637
1638 return 1;
1639 }
1640 #endif /* VMS */