Include <config.h> unconditionally.
[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 #include "javaexec.h"
35 #include "pipe.h"
36 #include "wait-process.h"
37 #include "error.h"
38 #include "getline.h"
39 #include "gettext.h"
40
41 #define _(str) gettext (str)
42
43
44 struct locals
45 {
46   /* OUT */
47   char *line;
48 };
49
50 static bool
51 execute_and_read_line (const char *progname,
52                        const char *prog_path, char **prog_argv,
53                        void *private_data)
54 {
55   struct locals *l = (struct locals *) private_data;
56   pid_t child;
57   int fd[1];
58   FILE *fp;
59   char *line;
60   size_t linesize;
61   size_t linelen;
62   int exitstatus;
63
64   /* Open a pipe to the JVM.  */
65   child = create_pipe_in (progname, prog_path, prog_argv, DEV_NULL, false,
66                           true, false, fd);
67
68   if (child == -1)
69     return false;
70
71   /* Retrieve its result.  */
72   fp = fdopen (fd[0], "r");
73   if (fp == NULL)
74     {
75       error (0, errno, _("fdopen() failed"));
76       return false;
77     }
78
79   line = NULL; linesize = 0;
80   linelen = getline (&line, &linesize, fp);
81   if (linelen == (size_t)(-1))
82     {
83       error (0, 0, _("%s subprocess I/O error"), progname);
84       return false;
85     }
86   if (linelen > 0 && line[linelen - 1] == '\n')
87     line[linelen - 1] = '\0';
88
89   fclose (fp);
90
91   /* Remove zombie process from process list, and retrieve exit status.  */
92   exitstatus = wait_subprocess (child, progname, true, false, true, false);
93   if (exitstatus != 0)
94     {
95       free (line);
96       return false;
97     }
98
99   l->line = line;
100   return false;
101 }
102
103 char *
104 javaexec_version (void)
105 {
106   const char *class_name = "javaversion";
107   const char *pkgdatadir = relocate (PKGDATADIR);
108   const char *args[1];
109   struct locals locals;
110
111   args[0] = NULL;
112   locals.line = NULL;
113   execute_java_class (class_name, &pkgdatadir, 1, true, NULL, args,
114                       false, false, execute_and_read_line, &locals);
115
116   return locals.line;
117 }