1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
31 #include <cppuhelper/findsofficepath.h>
32 #include <rtl/string.h>
33 #include <sal/types.h>
35 static char* getPath(void);
36 static char* createCommandName( char* argv0
);
38 static const int SEPARATOR
= '/';
39 static const char* PATHSEPARATOR
= ":";
43 * The main function implements a loader for applications which use UNO.
45 * <p>This code runs on the Unix/Linux platforms only.</p>
47 * <p>The main function detects a UNO installation on the system and adds the
48 * relevant directories of the installation to the LD_LIBRARY_PATH environment
49 * variable. After that, the application process is loaded and started, whereby
50 * the new process inherits the environment of the calling process, including
51 * the modified LD_LIBRARY_PATH environment variable. The application's
52 * executable name must be the same as the name of this executable, prefixed
54 * <p>On MACOSX DYLD_LIBRARY_PATH is used instead of LD_LIBRARY_PATH!<p>
56 * <p>A UNO installation can be specified by the user by setting the UNO_PATH
57 * environment variable to the program directory of the UNO installation.
58 * If no installation is specified by the user, the default installation on
59 * the system will be taken. The default installation is found from the
60 * PATH environment variable. This requires that the 'soffice' executable or
61 * a symbolic link is in one of the directories listed in the PATH environment
64 int main( int argc
, char *argv
[] )
69 (void) argc
; /* avoid warning about unused parameter */
71 /* get the path of the UNO installation */
77 static const char* ENVVARNAME
= "DYLD_LIBRARY_PATH";
79 static const char* ENVVARNAME
= "LD_LIBRARY_PATH";
86 size_t pathlen
= strlen(path
);
90 static char const unoinfoSuffix
[] = "/unoinfo";
91 char * unoinfo
= malloc(
92 pathlen
+ RTL_CONSTASCII_LENGTH(unoinfoSuffix
) + 1);
94 if (unoinfo
== NULL
) {
96 fprintf(stderr
, "Error: out of memory!\n");
99 strcpy(unoinfo
, path
);
102 unoinfoSuffix
+ (pathlen
== 0 || path
[pathlen
- 1] != '/' ? 0 : 1));
103 ret
= lstat(unoinfo
, &stats
);
108 2 * pathlen
+ RTL_CONSTASCII_LENGTH("/unoinfo c++") + 1);
116 fprintf(stderr
, "Error: out of memory!\n");
125 if (p
== path
|| p
[-1] != '/') {
128 strcpy(q
, "unoinfo c++");
133 fprintf(stderr
, "Error: calling unoinfo failed!\n");
139 libpath
= realloc(libpath
, n
);
140 if (libpath
== NULL
) {
143 "Error: out of memory reading unoinfo output!\n");
146 m
= fread(libpath
+ old
, 1, n
- old
- 1, f
);
147 if (m
!= n
- old
- 1) {
149 fprintf(stderr
, "Error: cannot read unoinfo output!\n");
152 libpath
[old
+ m
] = '\0';
155 if (n
>= SAL_MAX_SIZE
/ 2) {
158 "Error: out of memory reading unoinfo output!\n");
164 if (pclose(f
) != 0) {
165 fprintf(stderr
, "Error: executing unoinfo failed!\n");
172 /* Assume an old OOo 2.x installation without unoinfo: */
176 value
= getenv( ENVVARNAME
);
178 // workaround for finding wrong libsqlite3.dylib in the office installation
179 // For MacOS > 10.6 nss uses the system lib -> unresolved symbol _sqlite3_wal_checkpoint
181 size
= strlen( ENVVARNAME
) + strlen( "=/usr/lib:" ) + strlen( libpath
) + 1;
183 size
= strlen( ENVVARNAME
) + strlen( "=" ) + strlen( libpath
) + 1;
186 size
+= strlen( PATHSEPARATOR
) + strlen( value
);
187 envstr
= (char*) malloc( size
);
188 strcpy( envstr
, ENVVARNAME
);
190 strcat( envstr
, "=/usr/lib:" );
192 strcat( envstr
, "=" );
194 strcat( envstr
, libpath
);
198 strcat( envstr
, PATHSEPARATOR
);
199 strcat( envstr
, value
);
201 /* coverity[tainted_data : FALSE] */
206 fprintf( stderr
, "Warning: no office installation found!\n" );
210 /* set the executable name for the application process */
211 cmdname
= createCommandName( argv
[0] );
215 * create the application process;
216 * if successful, execvp doesn't return to the calling process
218 /* coverity[tainted_string] - createCommandName creates a safe string */
219 execvp( cmdname
, argv
);
220 fprintf( stderr
, "Error: execvp failed!\n" );
227 * Gets the path of a UNO installation.
229 * @return the installation path or NULL, if no installation was specified or
230 * found, or if an error occurred.
231 * Returned pointer must be released with free()
235 char* path
= cppuhelper_detail_findSofficePath();
239 fprintf( stderr
, "Warning: getting path from PATH environment "
240 "variable failed!\n" );
248 * Creates the application's executable file name.
250 * <p>The application's executable file name is the name of this executable
251 * prefixed by '_'.</p>
253 * @param argv0 specifies the argv[0] parameter of the main function
255 * @return the application's executable file name or NULL, if an error occurred
257 char* createCommandName( char* argv0
)
259 const char* CMDPREFIX
= "_";
260 const char* prgname
= NULL
;
262 char* cmdname
= NULL
;
266 /* get the executable file name from argv0 */
270 * if argv0 doesn't contain an absolute path name, try to get the absolute
271 * path name from dladdr; note that this only works for Solaris, not for
274 if ( argv0
!= NULL
&& *argv0
!= SEPARATOR
&&
275 dladdr( (void*) &createCommandName
, &dl_info
) &&
276 dl_info
.dli_fname
!= NULL
&& *dl_info
.dli_fname
== SEPARATOR
)
278 prgname
= dl_info
.dli_fname
;
281 /* prefix the executable file name by '_' */
282 if ( prgname
!= NULL
)
284 cmdname
= (char*) malloc( strlen( prgname
) + strlen( CMDPREFIX
) + 1 );
285 sep
= strrchr( prgname
, SEPARATOR
);
288 int pos
= ++sep
- prgname
;
289 strncpy( cmdname
, prgname
, pos
);
290 cmdname
[ pos
] = '\0';
291 strcat( cmdname
, CMDPREFIX
);
292 strcat( cmdname
, sep
);
296 strcpy( cmdname
, CMDPREFIX
);
297 strcat( cmdname
, prgname
);
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */