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 char const* getPath();
36 char* createCommandName( char* argv0
);
38 const int SEPARATOR
= '/';
39 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
= "LIBPATH";
81 static const char* ENVVARNAME
= "LD_LIBRARY_PATH";
90 size_t pathlen
= strlen(path
);
94 static char const unoinfoSuffix
[] = "/unoinfo";
95 char * unoinfo
= malloc(
96 pathlen
+ RTL_CONSTASCII_LENGTH(unoinfoSuffix
) + 1);
98 if (unoinfo
== NULL
) {
99 fprintf(stderr
, "Error: out of memory!\n");
102 strcpy(unoinfo
, path
);
105 unoinfoSuffix
+ (pathlen
== 0 || path
[pathlen
- 1] != '/' ? 0 : 1));
106 ret
= lstat(unoinfo
, &stats
);
111 2 * pathlen
+ RTL_CONSTASCII_LENGTH("/unoinfo c++") + 1);
119 fprintf(stderr
, "Error: out of memory!\n");
128 if (p
== path
|| p
[-1] != '/') {
131 strcpy(q
, "unoinfo c++");
136 fprintf(stderr
, "Error: calling unoinfo failed!\n");
142 libpath
= realloc(libpath
, n
);
143 if (libpath
== NULL
) {
146 "Error: out of memory reading unoinfo output!\n");
149 m
= fread(libpath
+ old
, 1, n
- old
- 1, f
);
150 if (m
!= n
- old
- 1) {
152 fprintf(stderr
, "Error: cannot read unoinfo output!\n");
155 libpath
[old
+ m
] = '\0';
158 if (n
>= SAL_MAX_SIZE
/ 2) {
161 "Error: out of memory reading unoinfo output!\n");
167 if (pclose(f
) != 0) {
168 fprintf(stderr
, "Error: executing unoinfo failed!\n");
175 /* Assume an old OOo 2.x installation without unoinfo: */
176 libpath
= (char *) path
;
180 value
= getenv( ENVVARNAME
);
182 size
= strlen( ENVVARNAME
) + strlen( "=" ) + strlen( libpath
) + 1;
184 size
+= strlen( PATHSEPARATOR
) + strlen( value
);
185 envstr
= (char*) malloc( size
);
186 strcpy( envstr
, ENVVARNAME
);
187 strcat( envstr
, "=" );
188 strcat( envstr
, libpath
);
189 if ( freeLibpath
!= 0 )
195 strcat( envstr
, PATHSEPARATOR
);
196 strcat( envstr
, value
);
202 fprintf( stderr
, "Warning: no UNO installation found!\n" );
206 /* set the executable name for the application process */
207 cmdname
= createCommandName( argv
[0] );
211 * create the application process;
212 * if successful, execvp doesn't return to the calling process
214 execvp( cmdname
, argv
);
215 fprintf( stderr
, "Error: execvp failed!\n" );
222 * Gets the path of a UNO installation.
224 * @return the installation path or NULL, if no installation was specified or
225 * found, or if an error occurred
227 char const* getPath()
229 char const* path
= cppuhelper_detail_findSofficePath();
233 fprintf( stderr
, "Warning: getting path from PATH environment "
234 "variable failed!\n" );
242 * Creates the application's executable file name.
244 * <p>The application's executable file name is the name of this executable
245 * prefixed by '_'.</p>
247 * @param argv0 specifies the argv[0] parameter of the main function
249 * @return the application's executable file name or NULL, if an error occurred
251 char* createCommandName( char* argv0
)
253 const char* CMDPREFIX
= "_";
254 const char* prgname
= NULL
;
256 char* cmdname
= NULL
;
262 /* get the executable file name from argv0 */
267 * if argv0 doesn't contain an absolute path name, try to get the absolute
268 * path name from dladdr; note that this only works for Solaris, not for
271 if ( argv0
!= NULL
&& *argv0
!= SEPARATOR
&&
272 dladdr( (void*) &createCommandName
, &dl_info
) &&
273 dl_info
.dli_fname
!= NULL
&& *dl_info
.dli_fname
== SEPARATOR
)
275 prgname
= dl_info
.dli_fname
;
279 /* prefix the executable file name by '_' */
280 if ( prgname
!= NULL
)
282 cmdname
= (char*) malloc( strlen( prgname
) + strlen( CMDPREFIX
) + 1 );
283 sep
= strrchr( prgname
, SEPARATOR
);
286 int pos
= ++sep
- prgname
;
287 strncpy( cmdname
, prgname
, pos
);
288 cmdname
[ pos
] = '\0';
289 strcat( cmdname
, CMDPREFIX
);
290 strcat( cmdname
, sep
);
294 strcpy( cmdname
, CMDPREFIX
);
295 strcat( cmdname
, prgname
);
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */