* lib/relocwrapper.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise.
[gnulib.git] / lib / relocwrapper.c
1 /* Relocating wrapper program.
2    Copyright (C) 2003, 2005-2007, 2009-2011 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2003.
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 /* Dependencies:
19    relocwrapper
20     -> progname
21     -> progreloc
22         -> areadlink
23            -> readlink
24         -> canonicalize-lgpl
25            -> malloca
26            -> readlink
27     -> relocatable
28     -> setenv
29        -> malloca
30     -> strerror
31     -> c-ctype
32
33    Macros that need to be set while compiling this file:
34      - ENABLE_RELOCATABLE 1
35      - INSTALLPREFIX the base installation directory
36      - INSTALLDIR the directory into which this program is installed
37      - LIBPATHVAR the platform dependent runtime library path variable
38      - LIBDIRS a comma-terminated list of strings representing the list of
39        directories that contain the libraries at installation time
40
41    We don't want to internationalize this wrapper because then it would
42    depend on libintl and therefore need relocation itself.  So use only
43    libc functions, no gettext(), no error(), no xmalloc(), no xsetenv().
44  */
45
46 #define _GL_USE_STDLIB_ALLOC 1
47 #include <config.h>
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <errno.h>
54
55 #include "progname.h"
56 #include "relocatable.h"
57 #include "c-ctype.h"
58 #include "verify.h"
59
60 /* Use the system functions, not the gnulib overrides in this file.  */
61 #undef fprintf
62
63 /* Return a copy of the filename, with an extra ".bin" at the end.
64    More generally, it replaces "${EXEEXT}" at the end with ".bin${EXEEXT}".  */
65 static char *
66 add_dotbin (const char *filename)
67 {
68   size_t filename_len = strlen (filename);
69   char *result = (char *) malloc (filename_len + 4 + 1);
70
71   if (result != NULL)
72     {
73       if (sizeof (EXEEXT) > sizeof (""))
74         {
75           /* EXEEXT handling.  */
76           const size_t exeext_len = sizeof (EXEEXT) - sizeof ("");
77           static const char exeext[] = EXEEXT;
78           if (filename_len > exeext_len)
79             {
80               /* Compare using an inlined copy of c_strncasecmp(), because
81                  the filenames may have undergone a case conversion since
82                  they were packaged.  In other words, EXEEXT may be ".exe"
83                  on one system and ".EXE" on another.  */
84               const char *s1 = filename + filename_len - exeext_len;
85               const char *s2 = exeext;
86               for (; *s1 != '\0'; s1++, s2++)
87                 {
88                   unsigned char c1 = *s1;
89                   unsigned char c2 = *s2;
90                   if (c_tolower (c1) != c_tolower (c2))
91                     goto simple_append;
92                 }
93               /* Insert ".bin" before EXEEXT or its equivalent.  */
94               memcpy (result, filename, filename_len - exeext_len);
95               memcpy (result + filename_len - exeext_len, ".bin", 4);
96               memcpy (result + filename_len - exeext_len + 4,
97                       filename + filename_len - exeext_len,
98                       exeext_len + 1);
99               return result;
100             }
101         }
102      simple_append:
103       /* Simply append ".bin".  */
104       memcpy (result, filename, filename_len);
105       memcpy (result + filename_len, ".bin", 4 + 1);
106       return result;
107     }
108   else
109     {
110       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
111       exit (1);
112     }
113 }
114
115 /* List of directories that contain the libraries.  */
116 static const char *libdirs[] = { LIBDIRS NULL };
117 /* Verify that at least one directory is given.  */
118 verify (sizeof (libdirs) / sizeof (libdirs[0]) > 1);
119
120 /* Relocate the list of directories that contain the libraries.  */
121 static void
122 relocate_libdirs ()
123 {
124   size_t i;
125
126   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
127     libdirs[i] = relocate (libdirs[i]);
128 }
129
130 /* Activate the list of directories in the LIBPATHVAR.  */
131 static void
132 activate_libdirs ()
133 {
134   const char *old_value;
135   size_t total;
136   size_t i;
137   char *value;
138   char *p;
139
140   old_value = getenv (LIBPATHVAR);
141   if (old_value == NULL)
142     old_value = "";
143
144   total = 0;
145   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
146     total += strlen (libdirs[i]) + 1;
147   total += strlen (old_value) + 1;
148
149   value = (char *) malloc (total);
150   if (value == NULL)
151     {
152       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
153       exit (1);
154     }
155   p = value;
156   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
157     {
158       size_t len = strlen (libdirs[i]);
159       memcpy (p, libdirs[i], len);
160       p += len;
161       *p++ = ':';
162     }
163   if (old_value[0] != '\0')
164     strcpy (p, old_value);
165   else
166     p[-1] = '\0';
167
168   if (setenv (LIBPATHVAR, value, 1) < 0)
169     {
170       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
171       exit (1);
172     }
173 }
174
175 int
176 main (int argc, char *argv[])
177 {
178   char *full_program_name;
179
180   /* Set the program name and perform preparations for
181      get_full_program_name() and relocate().  */
182   set_program_name_and_installdir (argv[0], INSTALLPREFIX, INSTALLDIR);
183
184   /* Get the full program path.  (Important if accessed through a symlink.)  */
185   full_program_name = get_full_program_name ();
186   if (full_program_name == NULL)
187     full_program_name = argv[0];
188
189   /* Invoke the real program, with suffix ".bin".  */
190   argv[0] = add_dotbin (full_program_name);
191   relocate_libdirs ();
192   activate_libdirs ();
193   execv (argv[0], argv);
194   fprintf (stderr, "%s: could not execute %s: %s\n",
195            program_name, argv[0], strerror (errno));
196   exit (127);
197 }