fstat: Fix module dependency conditions.
[gnulib/ericb.git] / lib / classpath.c
blobe56f23fce2b6c7b02e9942c01fc7788854274194
1 /* Java CLASSPATH handling.
2 Copyright (C) 2001-2003, 2006, 2009-2017 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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.
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.
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/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "classpath.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "xsetenv.h"
28 #include "xalloc.h"
30 /* Name of environment variable. */
31 #ifndef CLASSPATHVAR
32 # define CLASSPATHVAR "CLASSPATH"
33 #endif
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
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)
51 const char *old_classpath;
52 unsigned int length;
53 unsigned int i;
54 char *result;
55 char *p;
57 old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
58 if (old_classpath == NULL)
59 old_classpath = "";
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--;
68 result = XNMALLOC (length + 1, char);
69 p = result;
70 for (i = 0; i < classpaths_count; i++)
72 memcpy (p, classpaths[i], strlen (classpaths[i]));
73 p += strlen (classpaths[i]);
74 *p++ = PATH_SEPARATOR;
76 if (old_classpath[0] != '\0')
78 memcpy (p, old_classpath, strlen (old_classpath));
79 p += strlen (old_classpath);
81 else
83 if (classpaths_count > 0)
84 p--;
86 *p = '\0';
88 return result;
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)
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);
101 if (verbose)
102 printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
104 xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
106 free (new_CLASSPATH);
108 return result;
111 /* Restore CLASSPATH to its previous value. */
112 void
113 reset_classpath (char *old_classpath)
115 if (old_classpath != NULL)
117 xsetenv (CLASSPATHVAR, old_classpath, 1);
118 free (old_classpath);
120 else
121 unsetenv (CLASSPATHVAR);