*** empty log message ***
[gnulib.git] / lib / classpath.c
1 /* Java CLASSPATH handling.
2    Copyright (C) 2001-2003 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 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Specification.  */
24 #include "classpath.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "xsetenv.h"
31 #include "xalloc.h"
32
33 /* Name of environment variable.  */
34 #ifndef CLASSPATHVAR
35 # define CLASSPATHVAR "CLASSPATH"
36 #endif
37
38 /* Separator in PATH like lists of pathnames.  */
39 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
40   /* Win32, OS/2, DOS */
41 # define PATH_SEPARATOR ';'
42 #else
43   /* Unix */
44 # define PATH_SEPARATOR ':'
45 #endif
46
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.  */
50 char *
51 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
52                bool use_minimal_classpath)
53 {
54   const char *old_classpath;
55   unsigned int length;
56   unsigned int i;
57   char *result;
58   char *p;
59
60   old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
61   if (old_classpath == NULL)
62     old_classpath = "";
63
64   length = 0;
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')
69     length--;
70
71   result = (char *) xmalloc (length + 1);
72   p = result;
73   for (i = 0; i < classpaths_count; i++)
74     {
75       memcpy (p, classpaths[i], strlen (classpaths[i]));
76       p += strlen (classpaths[i]);
77       *p++ = PATH_SEPARATOR;
78     }
79   if (old_classpath[0] != '\0')
80     {
81       memcpy (p, old_classpath, strlen (old_classpath));
82       p += strlen (old_classpath);
83     }
84   else
85     {
86       if (classpaths_count > 0)
87         p--;
88     }
89   *p = '\0';
90
91   return result;
92 }
93
94 /* Set CLASSPATH and returns a safe copy of its old value.  */
95 char *
96 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
97                bool use_minimal_classpath, bool verbose)
98 {
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);
103
104   if (verbose)
105     printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
106
107   xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
108
109   free (new_CLASSPATH);
110
111   return result;
112 }
113
114 /* Restore CLASSPATH to its previous value.  */
115 void
116 reset_classpath (char *old_classpath)
117 {
118   if (old_classpath != NULL)
119     {
120       xsetenv (CLASSPATHVAR, old_classpath, 1);
121       free (old_classpath);
122     }
123   else
124     unsetenv (CLASSPATHVAR);
125 }