xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/gnulib-lib/classpath.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* Java CLASSPATH handling.
2    Copyright (C) 2001-2003, 2006 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18 
19 #include <config.h>
20 
21 /* Specification.  */
22 #include "classpath.h"
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "xsetenv.h"
29 #include "xalloc.h"
30 
31 /* Name of environment variable.  */
32 #ifndef CLASSPATHVAR
33 # define CLASSPATHVAR "CLASSPATH"
34 #endif
35 
36 /* Separator in PATH like lists of pathnames.  */
37 #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
38   /* Win32, OS/2, DOS */
39 # define PATH_SEPARATOR ';'
40 #else
41   /* Unix */
42 # define PATH_SEPARATOR ':'
43 #endif
44 
45 /* Return the new CLASSPATH value.  The given classpaths are prepended to
46    the current CLASSPATH value.   If use_minimal_classpath, the current
47    CLASSPATH is ignored.  */
48 char *
new_classpath(const char * const * classpaths,unsigned int classpaths_count,bool use_minimal_classpath)49 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
50 	       bool use_minimal_classpath)
51 {
52   const char *old_classpath;
53   unsigned int length;
54   unsigned int i;
55   char *result;
56   char *p;
57 
58   old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
59   if (old_classpath == NULL)
60     old_classpath = "";
61 
62   length = 0;
63   for (i = 0; i < classpaths_count; i++)
64     length += strlen (classpaths[i]) + 1;
65   length += strlen (old_classpath);
66   if (classpaths_count > 0 && old_classpath[0] == '\0')
67     length--;
68 
69   result = (char *) xmalloc (length + 1);
70   p = result;
71   for (i = 0; i < classpaths_count; i++)
72     {
73       memcpy (p, classpaths[i], strlen (classpaths[i]));
74       p += strlen (classpaths[i]);
75       *p++ = PATH_SEPARATOR;
76     }
77   if (old_classpath[0] != '\0')
78     {
79       memcpy (p, old_classpath, strlen (old_classpath));
80       p += strlen (old_classpath);
81     }
82   else
83     {
84       if (classpaths_count > 0)
85 	p--;
86     }
87   *p = '\0';
88 
89   return result;
90 }
91 
92 /* Set CLASSPATH and returns a safe copy of its old value.  */
93 char *
set_classpath(const char * const * classpaths,unsigned int classpaths_count,bool use_minimal_classpath,bool verbose)94 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
95 	       bool use_minimal_classpath, bool verbose)
96 {
97   const char *old_CLASSPATH = getenv (CLASSPATHVAR);
98   char *result = (old_CLASSPATH != NULL ? xstrdup (old_CLASSPATH) : NULL);
99   char *new_CLASSPATH =
100     new_classpath (classpaths, classpaths_count, use_minimal_classpath);
101 
102   if (verbose)
103     printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
104 
105   xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
106 
107   free (new_CLASSPATH);
108 
109   return result;
110 }
111 
112 /* Restore CLASSPATH to its previous value.  */
113 void
reset_classpath(char * old_classpath)114 reset_classpath (char *old_classpath)
115 {
116   if (old_classpath != NULL)
117     {
118       xsetenv (CLASSPATHVAR, old_classpath, 1);
119       free (old_classpath);
120     }
121   else
122     unsetenv (CLASSPATHVAR);
123 }
124