sys_socket: Add support for OpenVMS.
[gnulib.git] / lib / classpath.c
blobc7868f6dc9c69c9451507e1e57bcf68b6e9357b0
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 /* If CLASSPATHVAR is defined, this file is being #included, and config.h is
19 therefore already included. */
20 #if !defined CLASSPATHVAR
21 # include <config.h>
22 #endif
24 /* Specification. */
25 #include "classpath.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "xsetenv.h"
32 #include "xalloc.h"
34 /* Name of environment variable. */
35 #ifndef CLASSPATHVAR
36 # define CLASSPATHVAR "CLASSPATH"
37 #endif
39 /* Separator in PATH like lists of pathnames. */
40 #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
41 /* Native Windows, OS/2, DOS */
42 # define PATH_SEPARATOR ';'
43 #else
44 /* Unix */
45 # define PATH_SEPARATOR ':'
46 #endif
48 /* Return the new CLASSPATH value. The given classpaths are prepended to
49 the current CLASSPATH value. If use_minimal_classpath, the current
50 CLASSPATH is ignored. */
51 char *
52 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
53 bool use_minimal_classpath)
55 const char *old_classpath;
56 unsigned int length;
57 unsigned int i;
58 char *result;
59 char *p;
61 old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
62 if (old_classpath == NULL)
63 old_classpath = "";
65 length = 0;
66 for (i = 0; i < classpaths_count; i++)
67 length += strlen (classpaths[i]) + 1;
68 length += strlen (old_classpath);
69 if (classpaths_count > 0 && old_classpath[0] == '\0')
70 length--;
72 result = XNMALLOC (length + 1, char);
73 p = result;
74 for (i = 0; i < classpaths_count; i++)
76 memcpy (p, classpaths[i], strlen (classpaths[i]));
77 p += strlen (classpaths[i]);
78 *p++ = PATH_SEPARATOR;
80 if (old_classpath[0] != '\0')
82 memcpy (p, old_classpath, strlen (old_classpath));
83 p += strlen (old_classpath);
85 else
87 if (classpaths_count > 0)
88 p--;
90 *p = '\0';
92 return result;
95 /* Set CLASSPATH and returns a safe copy of its old value. */
96 char *
97 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
98 bool use_minimal_classpath, bool verbose)
100 const char *old_CLASSPATH = getenv (CLASSPATHVAR);
101 char *result = (old_CLASSPATH != NULL ? xstrdup (old_CLASSPATH) : NULL);
102 char *new_CLASSPATH =
103 new_classpath (classpaths, classpaths_count, use_minimal_classpath);
105 if (verbose)
106 printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
108 xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
110 free (new_CLASSPATH);
112 return result;
115 /* Restore CLASSPATH to its previous value. */
116 void
117 reset_classpath (char *old_classpath)
119 if (old_classpath != NULL)
121 xsetenv (CLASSPATHVAR, old_classpath, 1);
122 free (old_classpath);
124 else
125 unsetenv (CLASSPATHVAR);