1 /* Java CLASSPATH handling.
2 Copyright (C) 2001-2003, 2006, 2009-2011 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 <http://www.gnu.org/licenses/>. */
21 #include "classpath.h"
30 /* Name of environment variable. */
32 # define CLASSPATHVAR "CLASSPATH"
35 /* Separator in PATH like lists of pathnames. */
36 #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
37 /* Win32, OS/2, DOS */
38 # define PATH_SEPARATOR ';'
41 # define PATH_SEPARATOR ':'
44 /* Return the new CLASSPATH value. The given classpaths are prepended to
45 the current CLASSPATH value. If use_minimal_classpath, the current
46 CLASSPATH is ignored. */
48 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
49 bool use_minimal_classpath)
51 const char *old_classpath;
57 old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
58 if (old_classpath == NULL)
62 for (i = 0; i < classpaths_count; i++)
63 length += strlen (classpaths[i]) + 1;
64 length += strlen (old_classpath);
65 if (classpaths_count > 0 && old_classpath[0] == '\0')
68 result = XNMALLOC (length + 1, char);
70 for (i = 0; i < classpaths_count; i++)
72 memcpy (p, classpaths[i], strlen (classpaths[i]));
73 p += strlen (classpaths[i]);
74 *p++ = PATH_SEPARATOR;
76 if (old_classpath[0] != '\0')
78 memcpy (p, old_classpath, strlen (old_classpath));
79 p += strlen (old_classpath);
83 if (classpaths_count > 0)
91 /* Set CLASSPATH and returns a safe copy of its old value. */
93 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
94 bool use_minimal_classpath, bool verbose)
96 const char *old_CLASSPATH = getenv (CLASSPATHVAR);
97 char *result = (old_CLASSPATH != NULL ? xstrdup (old_CLASSPATH) : NULL);
99 new_classpath (classpaths, classpaths_count, use_minimal_classpath);
102 printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
104 xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
106 free (new_CLASSPATH);
111 /* Restore CLASSPATH to its previous value. */
113 reset_classpath (char *old_classpath)
115 if (old_classpath != NULL)
117 xsetenv (CLASSPATHVAR, old_classpath, 1);
118 free (old_classpath);
121 unsetenv (CLASSPATHVAR);