1 /* Java CLASSPATH handling.
2 Copyright (C) 2001-2003, 2006, 2009-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* If CLASSPATHVAR is defined, this file is being #included, and config.h is
19 therefore already included. */
20 #if !defined CLASSPATHVAR
25 #include "classpath.h"
34 /* Name of environment variable. */
36 # define CLASSPATHVAR "CLASSPATH"
39 /* Separator in PATH like lists of pathnames. */
40 #if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
41 /* Native Windows, OS/2, DOS */
42 # define PATH_SEPARATOR ';'
45 # define PATH_SEPARATOR ':'
48 /* Return the new CLASSPATH value. The given classpaths are prepended to
49 the current CLASSPATH value. If use_minimal_classpath, the current
50 CLASSPATH is ignored. */
52 new_classpath (const char * const *classpaths
, unsigned int classpaths_count
,
53 bool use_minimal_classpath
)
55 const char *old_classpath
;
61 old_classpath
= (use_minimal_classpath
? NULL
: getenv (CLASSPATHVAR
));
62 if (old_classpath
== NULL
)
66 for (i
= 0; i
< classpaths_count
; i
++)
67 length
+= strlen (classpaths
[i
]) + 1;
68 length
+= strlen (old_classpath
);
69 if (classpaths_count
> 0 && old_classpath
[0] == '\0')
72 result
= XNMALLOC (length
+ 1, char);
74 for (i
= 0; i
< classpaths_count
; i
++)
76 memcpy (p
, classpaths
[i
], strlen (classpaths
[i
]));
77 p
+= strlen (classpaths
[i
]);
78 *p
++ = PATH_SEPARATOR
;
80 if (old_classpath
[0] != '\0')
82 memcpy (p
, old_classpath
, strlen (old_classpath
));
83 p
+= strlen (old_classpath
);
87 if (classpaths_count
> 0)
95 /* Set CLASSPATH and returns a safe copy of its old value. */
97 set_classpath (const char * const *classpaths
, unsigned int classpaths_count
,
98 bool use_minimal_classpath
, bool verbose
)
100 const char *old_CLASSPATH
= getenv (CLASSPATHVAR
);
101 char *result
= (old_CLASSPATH
!= NULL
? xstrdup (old_CLASSPATH
) : NULL
);
102 char *new_CLASSPATH
=
103 new_classpath (classpaths
, classpaths_count
, use_minimal_classpath
);
106 printf (CLASSPATHVAR
"=%s ", new_CLASSPATH
);
108 xsetenv (CLASSPATHVAR
, new_CLASSPATH
, 1);
110 free (new_CLASSPATH
);
115 /* Restore CLASSPATH to its previous value. */
117 reset_classpath (char *old_classpath
)
119 if (old_classpath
!= NULL
)
121 xsetenv (CLASSPATHVAR
, old_classpath
, 1);
122 free (old_classpath
);
125 unsetenv (CLASSPATHVAR
);