xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/gnulib-lib/classpath.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Java CLASSPATH handling.
2*946379e7Schristos    Copyright (C) 2001-2003, 2006 Free Software Foundation, Inc.
3*946379e7Schristos    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4*946379e7Schristos 
5*946379e7Schristos    This program is free software; you can redistribute it and/or modify
6*946379e7Schristos    it under the terms of the GNU General Public License as published by
7*946379e7Schristos    the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos    any later version.
9*946379e7Schristos 
10*946379e7Schristos    This program is distributed in the hope that it will be useful,
11*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*946379e7Schristos    GNU General Public License for more details.
14*946379e7Schristos 
15*946379e7Schristos    You should have received a copy of the GNU General Public License
16*946379e7Schristos    along with this program; if not, write to the Free Software Foundation,
17*946379e7Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18*946379e7Schristos 
19*946379e7Schristos #include <config.h>
20*946379e7Schristos 
21*946379e7Schristos /* Specification.  */
22*946379e7Schristos #include "classpath.h"
23*946379e7Schristos 
24*946379e7Schristos #include <stdio.h>
25*946379e7Schristos #include <stdlib.h>
26*946379e7Schristos #include <string.h>
27*946379e7Schristos 
28*946379e7Schristos #include "xsetenv.h"
29*946379e7Schristos #include "xalloc.h"
30*946379e7Schristos 
31*946379e7Schristos /* Name of environment variable.  */
32*946379e7Schristos #ifndef CLASSPATHVAR
33*946379e7Schristos # define CLASSPATHVAR "CLASSPATH"
34*946379e7Schristos #endif
35*946379e7Schristos 
36*946379e7Schristos /* Separator in PATH like lists of pathnames.  */
37*946379e7Schristos #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
38*946379e7Schristos   /* Win32, OS/2, DOS */
39*946379e7Schristos # define PATH_SEPARATOR ';'
40*946379e7Schristos #else
41*946379e7Schristos   /* Unix */
42*946379e7Schristos # define PATH_SEPARATOR ':'
43*946379e7Schristos #endif
44*946379e7Schristos 
45*946379e7Schristos /* Return the new CLASSPATH value.  The given classpaths are prepended to
46*946379e7Schristos    the current CLASSPATH value.   If use_minimal_classpath, the current
47*946379e7Schristos    CLASSPATH is ignored.  */
48*946379e7Schristos char *
new_classpath(const char * const * classpaths,unsigned int classpaths_count,bool use_minimal_classpath)49*946379e7Schristos new_classpath (const char * const *classpaths, unsigned int classpaths_count,
50*946379e7Schristos 	       bool use_minimal_classpath)
51*946379e7Schristos {
52*946379e7Schristos   const char *old_classpath;
53*946379e7Schristos   unsigned int length;
54*946379e7Schristos   unsigned int i;
55*946379e7Schristos   char *result;
56*946379e7Schristos   char *p;
57*946379e7Schristos 
58*946379e7Schristos   old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
59*946379e7Schristos   if (old_classpath == NULL)
60*946379e7Schristos     old_classpath = "";
61*946379e7Schristos 
62*946379e7Schristos   length = 0;
63*946379e7Schristos   for (i = 0; i < classpaths_count; i++)
64*946379e7Schristos     length += strlen (classpaths[i]) + 1;
65*946379e7Schristos   length += strlen (old_classpath);
66*946379e7Schristos   if (classpaths_count > 0 && old_classpath[0] == '\0')
67*946379e7Schristos     length--;
68*946379e7Schristos 
69*946379e7Schristos   result = (char *) xmalloc (length + 1);
70*946379e7Schristos   p = result;
71*946379e7Schristos   for (i = 0; i < classpaths_count; i++)
72*946379e7Schristos     {
73*946379e7Schristos       memcpy (p, classpaths[i], strlen (classpaths[i]));
74*946379e7Schristos       p += strlen (classpaths[i]);
75*946379e7Schristos       *p++ = PATH_SEPARATOR;
76*946379e7Schristos     }
77*946379e7Schristos   if (old_classpath[0] != '\0')
78*946379e7Schristos     {
79*946379e7Schristos       memcpy (p, old_classpath, strlen (old_classpath));
80*946379e7Schristos       p += strlen (old_classpath);
81*946379e7Schristos     }
82*946379e7Schristos   else
83*946379e7Schristos     {
84*946379e7Schristos       if (classpaths_count > 0)
85*946379e7Schristos 	p--;
86*946379e7Schristos     }
87*946379e7Schristos   *p = '\0';
88*946379e7Schristos 
89*946379e7Schristos   return result;
90*946379e7Schristos }
91*946379e7Schristos 
92*946379e7Schristos /* Set CLASSPATH and returns a safe copy of its old value.  */
93*946379e7Schristos char *
set_classpath(const char * const * classpaths,unsigned int classpaths_count,bool use_minimal_classpath,bool verbose)94*946379e7Schristos set_classpath (const char * const *classpaths, unsigned int classpaths_count,
95*946379e7Schristos 	       bool use_minimal_classpath, bool verbose)
96*946379e7Schristos {
97*946379e7Schristos   const char *old_CLASSPATH = getenv (CLASSPATHVAR);
98*946379e7Schristos   char *result = (old_CLASSPATH != NULL ? xstrdup (old_CLASSPATH) : NULL);
99*946379e7Schristos   char *new_CLASSPATH =
100*946379e7Schristos     new_classpath (classpaths, classpaths_count, use_minimal_classpath);
101*946379e7Schristos 
102*946379e7Schristos   if (verbose)
103*946379e7Schristos     printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
104*946379e7Schristos 
105*946379e7Schristos   xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
106*946379e7Schristos 
107*946379e7Schristos   free (new_CLASSPATH);
108*946379e7Schristos 
109*946379e7Schristos   return result;
110*946379e7Schristos }
111*946379e7Schristos 
112*946379e7Schristos /* Restore CLASSPATH to its previous value.  */
113*946379e7Schristos void
reset_classpath(char * old_classpath)114*946379e7Schristos reset_classpath (char *old_classpath)
115*946379e7Schristos {
116*946379e7Schristos   if (old_classpath != NULL)
117*946379e7Schristos     {
118*946379e7Schristos       xsetenv (CLASSPATHVAR, old_classpath, 1);
119*946379e7Schristos       free (old_classpath);
120*946379e7Schristos     }
121*946379e7Schristos   else
122*946379e7Schristos     unsetenv (CLASSPATHVAR);
123*946379e7Schristos }
124