1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: findsofficepath.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_cppuhelper.hxx"
32 #include "sal/config.h"
39 #define WIN32_LEAN_AND_MEAN
43 * Gets the installation path from the Windows Registry for the specified
46 * @param hroot open handle to predefined root registry key
47 * @param subKeyName name of the subkey to open
49 * @return the installation path or NULL, if no installation was found or
52 static char* getPathFromRegistryKey( HKEY hroot
, const char* subKeyName
)
59 /* open the specified registry key */
60 if ( RegOpenKeyEx( hroot
, subKeyName
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
65 /* find the type and size of the default value */
66 if ( RegQueryValueEx( hkey
, NULL
, NULL
, &type
, NULL
, &size
) != ERROR_SUCCESS
)
72 /* get memory to hold the default value */
73 data
= (char*) malloc( size
);
75 /* read the default value */
76 if ( RegQueryValueEx( hkey
, NULL
, NULL
, &type
, (LPBYTE
) data
, &size
) != ERROR_SUCCESS
)
82 /* release registry key handle */
89 * Gets the installation path from the Windows Registry.
91 * @return the installation path or NULL, if no installation was found or
94 static char* platformSpecific()
96 const char* SUBKEYNAME
= "Software\\OpenOffice.org\\UNO\\InstallPath";
100 /* read the key's default value from HKEY_CURRENT_USER */
101 path
= getPathFromRegistryKey( HKEY_CURRENT_USER
, SUBKEYNAME
);
105 /* read the key's default value from HKEY_LOCAL_MACHINE */
106 path
= getPathFromRegistryKey( HKEY_LOCAL_MACHINE
, SUBKEYNAME
);
118 * Gets the installation path from the PATH environment variable.
120 * <p>An installation is found, if the executable 'soffice' or a symbolic link
121 * is in one of the directories listed in the PATH environment variable.</p>
123 * @return the installation path or NULL, if no installation was found or
124 * if an error occured
126 static char* platformSpecific()
128 const int SEPARATOR
= '/';
129 const char* PATHSEPARATOR
= ":";
130 const char* PATHVARNAME
= "PATH";
131 const char* APPENDIX
= "/soffice";
138 char* resolved
= NULL
;
141 char buffer
[PATH_MAX
];
144 /* get the value of the PATH environment variable */
145 env
= getenv( PATHVARNAME
);
146 str
= (char*) malloc( strlen( env
) + 1 );
149 /* get the tokens separated by ':' */
150 dir
= strtok( str
, PATHSEPARATOR
);
154 /* construct soffice file path */
155 file
= (char*) malloc( strlen( dir
) + strlen( APPENDIX
) + 1 );
157 strcat( file
, APPENDIX
);
159 /* check existence of soffice file */
160 if ( !access( file
, F_OK
) )
162 /* resolve symbolic link */
163 resolved
= realpath( file
, buffer
);
165 if ( resolved
!= NULL
)
167 /* get path to program directory */
168 sep
= strrchr( resolved
, SEPARATOR
);
172 pos
= sep
- resolved
;
173 path
= (char*) malloc( pos
+ 1 );
174 strncpy( path
, resolved
, pos
);
182 dir
= strtok( NULL
, PATHSEPARATOR
);
193 char const* cppuhelper_detail_findSofficePath()
195 const char* UNOPATHVARNAME
= "UNO_PATH";
199 /* get the installation path from the UNO_PATH environment variable */
200 path
= getenv( UNOPATHVARNAME
);
202 if ( path
== NULL
|| strlen( path
) == 0 )
204 path
= platformSpecific();