Use configmake.
[gnulib.git] / lib / javaversion.c
1 /* Determine the Java version supported by javaexec.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2006.
4
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.
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
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "javaversion.h"
23
24 #include <errno.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27
28 #if ENABLE_RELOCATABLE
29 # include "relocatable.h"
30 #else
31 # define relocate(pathname) (pathname)
32 #endif
33
34 /* Get PKGDATADIR.  */
35 #include "configmake.h"
36
37 #include "javaexec.h"
38 #include "pipe.h"
39 #include "wait-process.h"
40 #include "error.h"
41 #include "getline.h"
42 #include "gettext.h"
43
44 #define _(str) gettext (str)
45
46
47 struct locals
48 {
49   /* OUT */
50   char *line;
51 };
52
53 static bool
54 execute_and_read_line (const char *progname,
55                        const char *prog_path, char **prog_argv,
56                        void *private_data)
57 {
58   struct locals *l = (struct locals *) private_data;
59   pid_t child;
60   int fd[1];
61   FILE *fp;
62   char *line;
63   size_t linesize;
64   size_t linelen;
65   int exitstatus;
66
67   /* Open a pipe to the JVM.  */
68   child = create_pipe_in (progname, prog_path, prog_argv, DEV_NULL, false,
69                           true, false, fd);
70
71   if (child == -1)
72     return false;
73
74   /* Retrieve its result.  */
75   fp = fdopen (fd[0], "r");
76   if (fp == NULL)
77     {
78       error (0, errno, _("fdopen() failed"));
79       return false;
80     }
81
82   line = NULL; linesize = 0;
83   linelen = getline (&line, &linesize, fp);
84   if (linelen == (size_t)(-1))
85     {
86       error (0, 0, _("%s subprocess I/O error"), progname);
87       return false;
88     }
89   if (linelen > 0 && line[linelen - 1] == '\n')
90     line[linelen - 1] = '\0';
91
92   fclose (fp);
93
94   /* Remove zombie process from process list, and retrieve exit status.  */
95   exitstatus = wait_subprocess (child, progname, true, false, true, false);
96   if (exitstatus != 0)
97     {
98       free (line);
99       return false;
100     }
101
102   l->line = line;
103   return false;
104 }
105
106 char *
107 javaexec_version (void)
108 {
109   const char *class_name = "javaversion";
110   const char *pkgdatadir = relocate (PKGDATADIR);
111   const char *args[1];
112   struct locals locals;
113
114   args[0] = NULL;
115   locals.line = NULL;
116   execute_java_class (class_name, &pkgdatadir, 1, true, NULL, args,
117                       false, false, execute_and_read_line, &locals);
118
119   return locals.line;
120 }