Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / classpath.c
blobf875d550839fba2480b7d27395fc12ea8560880c
1 /* Java CLASSPATH handling.
2 Copyright (C) 2001-2003 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 2, or (at your option)
8 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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 /* Specification. */
24 #include "classpath.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "xsetenv.h"
31 #include "xalloc.h"
33 /* Name of environment variable. */
34 #ifndef CLASSPATHVAR
35 # define CLASSPATHVAR "CLASSPATH"
36 #endif
38 /* Separator in PATH like lists of pathnames. */
39 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
40 /* Win32, OS/2, DOS */
41 # define PATH_SEPARATOR ';'
42 #else
43 /* Unix */
44 # define PATH_SEPARATOR ':'
45 #endif
47 /* Return the new CLASSPATH value. The given classpaths are prepended to
48 the current CLASSPATH value. If use_minimal_classpath, the current
49 CLASSPATH is ignored. */
50 char *
51 new_classpath (const char * const *classpaths, unsigned int classpaths_count,
52 bool use_minimal_classpath)
54 const char *old_classpath;
55 unsigned int length;
56 unsigned int i;
57 char *result;
58 char *p;
60 old_classpath = (use_minimal_classpath ? NULL : getenv (CLASSPATHVAR));
61 if (old_classpath == NULL)
62 old_classpath = "";
64 length = 0;
65 for (i = 0; i < classpaths_count; i++)
66 length += strlen (classpaths[i]) + 1;
67 length += strlen (old_classpath);
68 if (classpaths_count > 0 && old_classpath[0] == '\0')
69 length--;
71 result = (char *) xmalloc (length + 1);
72 p = result;
73 for (i = 0; i < classpaths_count; i++)
75 memcpy (p, classpaths[i], strlen (classpaths[i]));
76 p += strlen (classpaths[i]);
77 *p++ = PATH_SEPARATOR;
79 if (old_classpath[0] != '\0')
81 memcpy (p, old_classpath, strlen (old_classpath));
82 p += strlen (old_classpath);
84 else
86 if (classpaths_count > 0)
87 p--;
89 *p = '\0';
91 return result;
94 /* Set CLASSPATH and returns a safe copy of its old value. */
95 char *
96 set_classpath (const char * const *classpaths, unsigned int classpaths_count,
97 bool use_minimal_classpath, bool verbose)
99 const char *old_CLASSPATH = getenv (CLASSPATHVAR);
100 char *result = (old_CLASSPATH != NULL ? xstrdup (old_CLASSPATH) : NULL);
101 char *new_CLASSPATH =
102 new_classpath (classpaths, classpaths_count, use_minimal_classpath);
104 if (verbose)
105 printf (CLASSPATHVAR "=%s ", new_CLASSPATH);
107 xsetenv (CLASSPATHVAR, new_CLASSPATH, 1);
109 free (new_CLASSPATH);
111 return result;
114 /* Restore CLASSPATH to its previous value. */
115 void
116 reset_classpath (char *old_classpath)
118 if (old_classpath != NULL)
120 xsetenv (CLASSPATHVAR, old_classpath, 1);
121 free (old_classpath);
123 else
124 unsetenv (CLASSPATHVAR);