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