Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / javaversion.c
1 /* Determine the Java version supported by javaexec.
2    Copyright (C) 2006, 2007 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 /* Get PKGDATADIR.  */
34 #include "configmake.h"
35
36 #include "javaexec.h"
37 #include "pipe.h"
38 #include "wait-process.h"
39 #include "error.h"
40 #include "gettext.h"
41
42 #define _(str) gettext (str)
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 = wait_subprocess (child, progname, true, false, true, false);
94   if (exitstatus != 0)
95     {
96       free (line);
97       return false;
98     }
99
100   l->line = line;
101   return false;
102 }
103
104 char *
105 javaexec_version (void)
106 {
107   const char *class_name = "javaversion";
108   const char *pkgdatadir = relocate (PKGDATADIR);
109   const char *args[1];
110   struct locals locals;
111
112   args[0] = NULL;
113   locals.line = NULL;
114   execute_java_class (class_name, &pkgdatadir, 1, true, NULL, args,
115                       false, false, execute_and_read_line, &locals);
116
117   return locals.line;
118 }