1 /* Java CLASSPATH handling.
2 Copyright (C) 2001-2003, 2006 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "classpath.h"
33 /* Name of environment variable. */
35 # define CLASSPATHVAR "CLASSPATH"
38 /* Separator in PATH like lists of pathnames. */
39 #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
40 /* Win32, OS/2, DOS */
41 # define PATH_SEPARATOR ';'
44 # define PATH_SEPARATOR ':'
47 /* Return the new CLASSPATH value. The given classpaths are prepended to
48 the current CLASSPATH value. If use_minimal_classpath, the current
49 CLASSPATH is ignored. */
51 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
52 bool use_minimal_classpath)
54 const char *old_classpath;
60 old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
61 if (old_classpath == NULL)
65 for (i = 0; i < classpaths_count; i++)
66 length += strlen (classpaths[i]) + 1;
67 length += strlen (old_classpath);
68 if (classpaths_count > 0 && old_classpath[0] == '\0')
71 result = (char *) xmalloc (length + 1);
73 for (i = 0; i < classpaths_count; i++)
75 memcpy (p, classpaths[i], strlen (classpaths[i]));
76 p += strlen (classpaths[i]);
77 *p++ = PATH_SEPARATOR;
79 if (old_classpath[0] != '\0')
81 memcpy (p, old_classpath, strlen (old_classpath));
82 p += strlen (old_classpath);
86 if (classpaths_count > 0)
94 /* Set CLASSPATH and returns a safe copy of its old value. */
96 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
97 bool use_minimal_classpath, bool verbose)
99 const char *old_CLASSPATH = getenv (CLASSPATHVAR);
100 char *result = (old_CLASSPATH != NULL ? xstrdup (old_CLASSPATH) : NULL);
101 char *new_CLASSPATH =
102 new_classpath (classpaths, classpaths_count, use_minimal_classpath);
105 printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
107 xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
109 free (new_CLASSPATH);
114 /* Restore CLASSPATH to its previous value. */
116 reset_classpath (char *old_classpath)
118 if (old_classpath != NULL)
120 xsetenv (CLASSPATHVAR, old_classpath, 1);
121 free (old_classpath);
124 unsetenv (CLASSPATHVAR);