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