exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / javaversion.c
blob60a59def76e473e2745ecc4ef40b69d832c1dd38
1 /* Determine the Java version supported by javaexec.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
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 <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "javaversion.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #if ENABLE_RELOCATABLE
28 # include "relocatable.h"
29 #else
30 # define relocate(pathname) (pathname)
31 # define relocate2(pathname,allocatedp) (*(allocatedp) = NULL, (pathname))
32 #endif
34 #include "javaexec.h"
35 #include "spawn-pipe.h"
36 #include "wait-process.h"
37 #include <error.h>
38 #include "gettext.h"
40 #define _(str) gettext (str)
42 /* Get PKGDATADIR. */
43 #include "configmake.h"
46 struct locals
48 /* OUT */
49 char *line;
52 static bool
53 execute_and_read_line (const char *progname,
54 const char *prog_path, const char * const *prog_argv,
55 void *private_data)
57 struct locals *l = (struct locals *) private_data;
58 pid_t child;
59 int fd[1];
60 FILE *fp;
61 char *line;
62 size_t linesize;
63 size_t linelen;
64 int exitstatus;
66 /* Open a pipe to the JVM. */
67 child = create_pipe_in (progname, prog_path, prog_argv, NULL,
68 DEV_NULL, false, true, false, fd);
70 if (child == -1)
71 return false;
73 /* Retrieve its result. */
74 fp = fdopen (fd[0], "r");
75 if (fp == NULL)
77 error (0, errno, _("fdopen() failed"));
78 return false;
81 line = NULL; linesize = 0;
82 linelen = getline (&line, &linesize, fp);
83 if (linelen == (size_t)(-1))
85 error (0, 0, _("%s subprocess I/O error"), progname);
86 return false;
88 if (linelen > 0 && line[linelen - 1] == '\n')
89 line[linelen - 1] = '\0';
91 fclose (fp);
93 /* Remove zombie process from process list, and retrieve exit status. */
94 exitstatus =
95 wait_subprocess (child, progname, true, false, true, false, NULL);
96 if (exitstatus != 0)
98 free (line);
99 return false;
102 l->line = line;
103 return false;
106 char *
107 javaexec_version (void)
109 const char *class_name = "javaversion";
110 char *malloc_pkgdatadir;
111 const char *pkgdatadir = relocate2 (PKGDATADIR, &malloc_pkgdatadir);
112 const char *args[1];
113 struct locals locals;
115 args[0] = NULL;
116 locals.line = NULL;
117 execute_java_class (class_name, &pkgdatadir, 1, true, NULL, args,
118 false, false, execute_and_read_line, &locals);
120 free (malloc_pkgdatadir);
121 return locals.line;