update credits
[LibreOffice.git] / cppuhelper / source / findsofficepath.c
blob0f1245f33040a602498110a19e504cbdece5295a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "sal/config.h"
22 #include <stdlib.h>
23 #include <string.h>
25 #if defined WNT
27 #define WIN32_LEAN_AND_MEAN
28 #include <windows.h>
31 * Gets the installation path from the Windows Registry for the specified
32 * registry key.
34 * @param hroot open handle to predefined root registry key
35 * @param subKeyName name of the subkey to open
37 * @return the installation path or NULL, if no installation was found or
38 * if an error occurred
40 static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName )
42 HKEY hkey;
43 DWORD type;
44 char* data = NULL;
45 DWORD size;
47 /* open the specified registry key */
48 if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
50 return NULL;
53 /* find the type and size of the default value */
54 if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
56 RegCloseKey( hkey );
57 return NULL;
60 /* get memory to hold the default value */
61 data = (char*) malloc( size );
63 /* read the default value */
64 if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
66 RegCloseKey( hkey );
67 return NULL;
70 /* release registry key handle */
71 RegCloseKey( hkey );
73 return data;
77 * Gets the installation path from the Windows Registry.
79 * @return the installation path or NULL, if no installation was found or
80 * if an error occurred
82 static char* platformSpecific()
84 const char* SUBKEYNAME = "Software\\LibreOffice\\UNO\\InstallPath";
86 char* path = NULL;
88 /* read the key's default value from HKEY_CURRENT_USER */
89 path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
91 if ( path == NULL )
93 /* read the key's default value from HKEY_LOCAL_MACHINE */
94 path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
97 return path;
100 #else
102 #include <unistd.h>
103 #include <limits.h>
106 * Gets the installation path from the PATH environment variable.
108 * <p>An installation is found, if the executable 'soffice' or a symbolic link
109 * is in one of the directories listed in the PATH environment variable.</p>
111 * @return the installation path or NULL, if no installation was found or
112 * if an error occurred
114 static char* platformSpecific()
116 const int SEPARATOR = '/';
117 const char* PATHSEPARATOR = ":";
118 const char* PATHVARNAME = "PATH";
119 const char* APPENDIX = "/libreoffice";
121 char* path = NULL;
122 char* env = NULL;
123 char* str = NULL;
124 char* dir = NULL;
125 char* file = NULL;
126 char* resolved = NULL;
127 char* sep = NULL;
129 char buffer[PATH_MAX];
130 int pos;
132 /* get the value of the PATH environment variable */
133 env = getenv( PATHVARNAME );
134 str = (char*) malloc( strlen( env ) + 1 );
135 strcpy( str, env );
137 /* get the tokens separated by ':' */
138 dir = strtok( str, PATHSEPARATOR );
140 while ( dir )
142 /* construct soffice file path */
143 file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
144 strcpy( file, dir );
145 strcat( file, APPENDIX );
147 /* check existence of soffice file */
148 if ( !access( file, F_OK ) )
150 /* resolve symbolic link */
151 resolved = realpath( file, buffer );
153 if ( resolved != NULL )
155 /* get path to program directory */
156 sep = strrchr( resolved, SEPARATOR );
158 if ( sep != NULL )
160 pos = sep - resolved;
161 path = (char*) malloc( pos + 1 );
162 strncpy( path, resolved, pos );
163 path[ pos ] = '\0';
164 free( file );
165 break;
170 dir = strtok( NULL, PATHSEPARATOR );
171 free( file );
174 free( str );
176 return path;
179 #endif
181 char const* cppuhelper_detail_findSofficePath()
183 const char* UNOPATHVARNAME = "UNO_PATH";
185 char* path = NULL;
187 /* get the installation path from the UNO_PATH environment variable */
188 path = getenv( UNOPATHVARNAME );
190 if (!path || !path[0])
191 path = platformSpecific();
193 return path;
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */