1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
28 #include "coretypes.h"
40 /* Possible flag values. */
44 /* We keep linked lists of directory names. A ``directory'' can be
45 either an ordinary directory or a .zip file. */
53 static void free_entry (struct entry
**);
54 static void append_entry (struct entry
**, struct entry
*);
55 static void add_entry (struct entry
**, const char *, int);
56 static void add_path (struct entry
**, const char *, int);
58 /* We support several different ways to set the class path.
60 built-in system directory (only libgcj.jar)
61 CLASSPATH environment variable
62 -classpath option overrides $CLASSPATH
63 -CLASSPATH option is a synonym for -classpath (for compatibility)
64 -bootclasspath overrides built-in
65 -extdirs sets the extensions directory path (overrides built-in)
66 -I prepends path to list
68 We implement this by keeping several path lists, and then simply
69 ignoring the ones which are not relevant. */
71 /* This holds all the -I directories. */
72 static struct entry
*include_dirs
;
74 /* This holds the CLASSPATH environment variable. */
75 static struct entry
*classpath_env
;
77 /* This holds the -classpath command-line option. */
78 static struct entry
*classpath_user
;
80 /* This holds the default directories. Some of these will have the
82 static struct entry
*sys_dirs
;
84 /* This holds the extensions path entries. */
85 static struct entry
*extensions
;
87 /* This is the sealed list. It is just a combination of other lists. */
88 static struct entry
*sealed
;
90 /* We keep track of the longest path we've seen. */
91 static int longest_path
= 0;
96 free_entry (struct entry
**entp
)
100 for (e
= *entp
; e
; e
= n
)
110 append_entry (struct entry
**entp
, struct entry
*ent
)
112 /* It doesn't matter if this is slow, since it is run only at
113 startup, and then infrequently. */
116 /* Find end of list. */
117 for (e
= *entp
; e
&& e
->next
; e
= e
->next
)
127 add_entry (struct entry
**entp
, const char *filename
, int is_system
)
132 n
= XNEW (struct entry
);
133 n
->flags
= is_system
? FLAG_SYSTEM
: 0;
136 len
= strlen (filename
);
138 if (len
> 4 && (FILENAME_CMP (filename
+ len
- 4, ".zip") == 0
139 || FILENAME_CMP (filename
+ len
- 4, ".jar") == 0))
141 n
->flags
|= FLAG_ZIP
;
142 /* If the user uses -classpath then he'll have to include
143 libgcj.jar in the value. We check for this in a simplistic
144 way. Symlinks will fool this test. This is only used for
145 -MM and -MMD, so it probably isn't terribly important. */
146 if (! FILENAME_CMP (filename
, LIBGCJ_ZIP_FILE
))
147 n
->flags
|= FLAG_SYSTEM
;
150 /* Note that we add a trailing separator to `.zip' names as well.
151 This is a little hack that lets the searching code in jcf-io.c
152 work more easily. Eww. */
153 if (! IS_DIR_SEPARATOR (filename
[len
- 1]))
155 char *f2
= (char *) alloca (len
+ 2);
156 strcpy (f2
, filename
);
157 f2
[len
] = DIR_SEPARATOR
;
159 n
->name
= xstrdup (f2
);
163 n
->name
= xstrdup (filename
);
165 if (len
> longest_path
)
168 append_entry (entp
, n
);
172 add_path (struct entry
**entp
, const char *cp
, int is_system
)
174 const char *startp
, *endp
;
178 char *buf
= (char *) alloca (strlen (cp
) + 3);
182 if (! *endp
|| *endp
== PATH_SEPARATOR
)
187 buf
[1] = DIR_SEPARATOR
;
192 strncpy (buf
, startp
, endp
- startp
);
193 buf
[endp
- startp
] = '\0';
195 add_entry (entp
, buf
, is_system
);
207 static int init_done
= 0;
209 /* Initialize the path module. */
214 char *attempt
, sep
[2];
222 sep
[0] = DIR_SEPARATOR
;
225 cp
= getenv ("GCC_EXEC_PREFIX");
228 attempt
= (char *) alloca (strlen (cp
) + 50);
229 /* The exec prefix can be something like
230 /usr/local/bin/../lib/gcc-lib/. We want to change this
231 into a pointer to the share/java directory. We support two
232 configurations: one where prefix and exec-prefix are the
233 same, and one where exec-prefix is `prefix/SOMETHING'. */
234 strcpy (attempt
, cp
);
235 strcat (attempt
, DIR_UP
);
236 strcat (attempt
, sep
);
237 strcat (attempt
, DIR_UP
);
238 strcat (attempt
, sep
);
239 len
= strlen (attempt
);
241 strcpy (attempt
+ len
, "share");
242 strcat (attempt
, sep
);
243 strcat (attempt
, "java");
244 strcat (attempt
, sep
);
245 strcat (attempt
, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
246 if (! stat (attempt
, &stat_b
))
248 add_entry (&sys_dirs
, attempt
, 1);
250 strcpy (&attempt
[strlen (attempt
)
251 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
253 strcat (attempt
, "ext");
254 strcat (attempt
, sep
);
255 if (! stat (attempt
, &stat_b
))
256 jcf_path_extdirs_arg (attempt
);
260 strcpy (attempt
+ len
, DIR_UP
);
261 strcat (attempt
, sep
);
262 strcat (attempt
, "share");
263 strcat (attempt
, sep
);
264 strcat (attempt
, "java");
265 strcat (attempt
, sep
);
266 strcat (attempt
, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
267 if (! stat (attempt
, &stat_b
))
269 add_entry (&sys_dirs
, attempt
, 1);
271 strcpy (&attempt
[strlen (attempt
)
272 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
274 strcat (attempt
, "ext");
275 strcat (attempt
, sep
);
276 if (! stat (attempt
, &stat_b
))
277 jcf_path_extdirs_arg (attempt
);
283 /* Desperation: use the installed one. */
285 add_entry (&sys_dirs
, LIBGCJ_ZIP_FILE
, 1);
286 extdirs
= (char *) alloca (strlen (LIBGCJ_ZIP_FILE
) + 1);
287 strcpy (extdirs
, LIBGCJ_ZIP_FILE
);
288 strcpy (&extdirs
[strlen (LIBGCJ_ZIP_FILE
)
289 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
291 strcat (extdirs
, sep
);
292 if (! stat (extdirs
, &stat_b
))
293 jcf_path_extdirs_arg (extdirs
);
296 cp
= getenv ("CLASSPATH");
297 add_path (&classpath_env
, cp
, 0);
300 /* Call this when -classpath is seen on the command line.
301 This overrides only the $CLASSPATH environment variable.
304 jcf_path_classpath_arg (const char *path
)
306 free_entry (&classpath_user
);
307 add_path (&classpath_user
, path
, 0);
310 /* Call this when -bootclasspath is seen on the command line.
313 jcf_path_bootclasspath_arg (const char *path
)
315 free_entry (&sys_dirs
);
316 add_path (&sys_dirs
, path
, 1);
319 /* Call this when -extdirs is seen on the command line.
322 jcf_path_extdirs_arg (const char *cp
)
324 const char *startp
, *endp
;
326 free_entry (&extensions
);
330 char *buf
= (char *) alloca (strlen (cp
) + 3);
334 if (! *endp
|| *endp
== PATH_SEPARATOR
)
339 strncpy (buf
, startp
, endp
- startp
);
340 buf
[endp
- startp
] = '\0';
344 int dirname_length
= strlen (buf
);
346 dirp
= opendir (buf
);
352 struct dirent
*direntp
= readdir (dirp
);
357 if (direntp
->d_name
[0] != '.')
359 char *name
= (char *) alloca (dirname_length
360 + strlen (direntp
->d_name
) + 2);
362 if (! IS_DIR_SEPARATOR (name
[dirname_length
-1]))
364 name
[dirname_length
] = DIR_SEPARATOR
;
365 name
[dirname_length
+1] = 0;
367 strcat (name
, direntp
->d_name
);
368 add_entry (&extensions
, name
, 0);
386 /* Call this when -I is seen on the command line. */
388 jcf_path_include_arg (const char *path
)
390 add_entry (&include_dirs
, path
, 0);
393 /* We `seal' the path by linking everything into one big list. Then
394 we provide a way to iterate through the sealed list. If PRINT is
395 true then we print the final class path to stderr. */
397 jcf_path_seal (int print
)
399 struct entry
*secondary
;
401 sealed
= include_dirs
;
406 secondary
= classpath_user
;
407 classpath_user
= NULL
;
412 add_entry (&classpath_env
, ".", 0);
414 secondary
= classpath_env
;
415 classpath_env
= NULL
;
419 free_entry (&classpath_user
);
420 free_entry (&classpath_env
);
422 append_entry (&sealed
, secondary
);
423 append_entry (&sealed
, sys_dirs
);
424 append_entry (&sealed
, extensions
);
431 fprintf (stderr
, "Class path starts here:\n");
432 for (ent
= sealed
; ent
; ent
= ent
->next
)
434 fprintf (stderr
, " %s", ent
->name
);
435 if ((ent
->flags
& FLAG_SYSTEM
))
436 fprintf (stderr
, " (system)");
437 if ((ent
->flags
& FLAG_ZIP
))
438 fprintf (stderr
, " (zip)");
439 fprintf (stderr
, "\n");
445 jcf_path_start (void)
447 return (void *) sealed
;
451 jcf_path_next (void *x
)
453 struct entry
*ent
= (struct entry
*) x
;
454 return (void *) ent
->next
;
458 PATH_SEPARATOR_STR
[] = {PATH_SEPARATOR
, '\0'};
461 jcf_path_compute (const char *prefix
)
465 int length
= strlen (prefix
) + 1;
468 for (iter
= sealed
; iter
!= NULL
; iter
= iter
->next
)
469 length
+= strlen (iter
->name
) + 1;
471 result
= (char *) xmalloc (length
);
472 strcpy (result
, prefix
);
474 for (iter
= sealed
; iter
!= NULL
; iter
= iter
->next
)
477 strcat (result
, PATH_SEPARATOR_STR
);
479 strcat (result
, iter
->name
);
480 /* Ugly: we want to strip the '/' from zip entries when
481 computing a string classpath. */
482 if ((iter
->flags
& FLAG_ZIP
) != 0)
483 result
[strlen (result
) - 1] = '\0';
489 /* We guarantee that the return path will either be a zip file, or it
490 will end with a directory separator. */
492 jcf_path_name (void *x
)
494 struct entry
*ent
= (struct entry
*) x
;
499 jcf_path_is_zipfile (void *x
)
501 struct entry
*ent
= (struct entry
*) x
;
502 return (ent
->flags
& FLAG_ZIP
);
506 jcf_path_is_system (void *x
)
508 struct entry
*ent
= (struct entry
*) x
;
509 return (ent
->flags
& FLAG_SYSTEM
);
513 jcf_path_max_len (void)