This source file includes following definitions.
- new_classpath
- set_classpath
- reset_classpath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #if !defined CLASSPATHVAR
21 # include <config.h>
22 #endif
23
24
25 #include "classpath.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "xsetenv.h"
32 #include "xalloc.h"
33
34
35 #ifndef CLASSPATHVAR
36 # define CLASSPATHVAR "CLASSPATH"
37 #endif
38
39
40 #if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
41
42 # define PATH_SEPARATOR ';'
43 #else
44
45 # define PATH_SEPARATOR ':'
46 #endif
47
48
49
50
51 char *
52 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
53 bool use_minimal_classpath)
54 {
55 const char *old_classpath;
56 unsigned int length;
57 unsigned int i;
58 char *result;
59 char *p;
60
61 old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
62 if (old_classpath == NULL)
63 old_classpath = "";
64
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--;
71
72 result = XNMALLOC (length + 1, char);
73 p = result;
74 for (i = 0; i < classpaths_count; i++)
75 {
76 memcpy (p, classpaths[i], strlen (classpaths[i]));
77 p += strlen (classpaths[i]);
78 *p++ = PATH_SEPARATOR;
79 }
80 if (old_classpath[0] != '\0')
81 {
82 memcpy (p, old_classpath, strlen (old_classpath));
83 p += strlen (old_classpath);
84 }
85 else
86 {
87 if (classpaths_count > 0)
88 p--;
89 }
90 *p = '\0';
91
92 return result;
93 }
94
95
96 char *
97 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
98 bool use_minimal_classpath, bool verbose)
99 {
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);
104
105 if (verbose)
106 printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
107
108 xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
109
110 free (new_CLASSPATH);
111
112 return result;
113 }
114
115
116 void
117 reset_classpath (char *old_classpath)
118 {
119 if (old_classpath != NULL)
120 {
121 xsetenv (CLASSPATHVAR, old_classpath, 1);
122 free (old_classpath);
123 }
124 else
125 unsetenv (CLASSPATHVAR);
126 }