1 /* Java CLASSPATH handling.
2    Copyright (C) 2001-2003, 2006, 2009-2020 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4 
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.
9 
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.
14 
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/>.  */
17 
18 /* If CLASSPATHVAR is defined, this file is being #included, and config.h is
19    therefore already included.  */
20 #if !defined CLASSPATHVAR
21 # include <config.h>
22 #endif
23 
24 /* Specification.  */
25 #include "classpath.h"
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "xsetenv.h"
32 #include "xalloc.h"
33 
34 /* Name of environment variable.  */
35 #ifndef CLASSPATHVAR
36 # define CLASSPATHVAR "CLASSPATH"
37 #endif
38 
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 ';'
43 #else
44   /* Unix */
45 # define PATH_SEPARATOR ':'
46 #endif
47 
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.  */
51 char *
new_classpath(const char * const * classpaths,unsigned int classpaths_count,bool use_minimal_classpath)52 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
53                bool use_minimal_classpath)
54 {
55   const char *old_classpath;
56   unsigned int length;
57   unsigned int i;
58   char *result;
59   char *p;
60 
61   old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
62   if (old_classpath == NULL)
63     old_classpath = "";
64 
65   length = 0;
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')
70     length--;
71 
72   result = XNMALLOC (length + 1, char);
73   p = result;
74   for (i = 0; i < classpaths_count; i++)
75     {
76       memcpy (p, classpaths[i], strlen (classpaths[i]));
77       p += strlen (classpaths[i]);
78       *p++ = PATH_SEPARATOR;
79     }
80   if (old_classpath[0] != '\0')
81     {
82       memcpy (p, old_classpath, strlen (old_classpath));
83       p += strlen (old_classpath);
84     }
85   else
86     {
87       if (classpaths_count > 0)
88         p--;
89     }
90   *p = '\0';
91 
92   return result;
93 }
94 
95 /* Set CLASSPATH and returns a safe copy of its old value.  */
96 char *
set_classpath(const char * const * classpaths,unsigned int classpaths_count,bool use_minimal_classpath,bool verbose)97 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
98                bool use_minimal_classpath, bool verbose)
99 {
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);
104 
105   if (verbose)
106     printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
107 
108   xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
109 
110   free (new_CLASSPATH);
111 
112   return result;
113 }
114 
115 /* Restore CLASSPATH to its previous value.  */
116 void
reset_classpath(char * old_classpath)117 reset_classpath (char *old_classpath)
118 {
119   if (old_classpath != NULL)
120     {
121       xsetenv (CLASSPATHVAR, old_classpath, 1);
122       free (old_classpath);
123     }
124   else
125     unsetenv (CLASSPATHVAR);
126 }
127