Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/classpath.c @ 17605:23cb5b2fd95b
relocatable-perl: like relocatable-script, but for Perl scripts
* build-aux/relocatable.pl.in: Add.
* doc/relocatable-maint.texi: Add documentation.
* modules/relocatable-perl: Add.
author | Reuben Thomas <rrt@sc3d.org> |
---|---|
date | Thu, 09 Jan 2014 22:31:42 +0000 (2014-01-09) |
parents | 344018b6e5d7 |
children | ab58d4870664 |
rev | line source |
---|---|
5608 | 1 /* Java CLASSPATH handling. |
17587 | 2 Copyright (C) 2001-2003, 2006, 2009-2014 Free Software Foundation, Inc. |
5608 | 3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5608 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
8 (at your option) any later version. |
5608 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5608 | 17 |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6792
diff
changeset
|
18 #include <config.h> |
5608 | 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. */ | |
6792 | 36 #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
37 /* Native Windows, OS/2, DOS */ |
5608 | 38 # define PATH_SEPARATOR ';' |
39 #else | |
40 /* Unix */ | |
5822
704da670783e
(PATH_SEPARATOR): Remove insignificant trailing blank.
Jim Meyering <jim@meyering.net>
parents:
5608
diff
changeset
|
41 # define PATH_SEPARATOR ':' |
5608 | 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, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
49 bool use_minimal_classpath) |
5608 | 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 | |
7603
23f14c284219
Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
68 result = XNMALLOC (length + 1, char); |
5608 | 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) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
84 p--; |
5608 | 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, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
94 bool use_minimal_classpath, bool verbose) |
5608 | 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 } |