LCOV - code coverage report
Current view: top level - cppuhelper/source - findsofficepath.c (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 44 0.0 %
Date: 2014-04-11 Functions: 0 2 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "sal/config.h"
      21             : 
      22             : #include <stdlib.h>
      23             : #include <string.h>
      24             : 
      25             : #include <cppuhelper/findsofficepath.h>
      26             : 
      27             : #if defined WNT
      28             : 
      29             : #define WIN32_LEAN_AND_MEAN
      30             : #include <windows.h>
      31             : 
      32             : /*
      33             :  * Gets the installation path from the Windows Registry for the specified
      34             :  * registry key.
      35             :  *
      36             :  * @param hroot       open handle to predefined root registry key
      37             :  * @param subKeyName  name of the subkey to open
      38             :  *
      39             :  * @return the installation path or NULL, if no installation was found or
      40             :  *         if an error occurred
      41             :  */
      42             : static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName )
      43             : {
      44             :     HKEY hkey;
      45             :     DWORD type;
      46             :     char* data = NULL;
      47             :     DWORD size;
      48             : 
      49             :     /* open the specified registry key */
      50             :     if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
      51             :     {
      52             :         return NULL;
      53             :     }
      54             : 
      55             :     /* find the type and size of the default value */
      56             :     if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
      57             :     {
      58             :         RegCloseKey( hkey );
      59             :         return NULL;
      60             :     }
      61             : 
      62             :     /* get memory to hold the default value */
      63             :     data = (char*) malloc( size );
      64             : 
      65             :     /* read the default value */
      66             :     if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
      67             :     {
      68             :         RegCloseKey( hkey );
      69             :         return NULL;
      70             :     }
      71             : 
      72             :     /* release registry key handle */
      73             :     RegCloseKey( hkey );
      74             : 
      75             :     return data;
      76             : }
      77             : 
      78             : /*
      79             :  * Gets the installation path from the Windows Registry.
      80             :  *
      81             :  * @return the installation path or NULL, if no installation was found or
      82             :  *         if an error occurred
      83             :  */
      84             : static char* platformSpecific()
      85             : {
      86             :     const char* SUBKEYNAME = "Software\\LibreOffice\\UNO\\InstallPath";
      87             : 
      88             :     char* path = NULL;
      89             : 
      90             :     /* read the key's default value from HKEY_CURRENT_USER */
      91             :     path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
      92             : 
      93             :     if ( path == NULL )
      94             :     {
      95             :         /* read the key's default value from HKEY_LOCAL_MACHINE */
      96             :         path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
      97             :     }
      98             : 
      99             :     return path;
     100             : }
     101             : 
     102             : #else
     103             : 
     104             : #include <unistd.h>
     105             : #include <limits.h>
     106             : 
     107             : /*
     108             :  * Gets the installation path from the PATH environment variable.
     109             :  *
     110             :  * <p>An installation is found, if the executable 'soffice' or a symbolic link
     111             :  * is in one of the directories listed in the PATH environment variable.</p>
     112             :  *
     113             :  * @return the installation path or NULL, if no installation was found or
     114             :  *         if an error occurred
     115             :  */
     116           0 : static char* platformSpecific()
     117             : {
     118           0 :     const int SEPARATOR = '/';
     119           0 :     const char* PATHSEPARATOR = ":";
     120           0 :     const char* PATHVARNAME = "PATH";
     121           0 :     const char* APPENDIX = "/libreoffice";
     122             : 
     123           0 :     char* path = NULL;
     124           0 :     char* env = NULL;
     125           0 :     char* str = NULL;
     126           0 :     char* dir = NULL;
     127           0 :     char* file = NULL;
     128           0 :     char* resolved = NULL;
     129           0 :     char* sep = NULL;
     130             : 
     131             :     char buffer[PATH_MAX];
     132             :     int pos;
     133             : 
     134             :     /* get the value of the PATH environment variable */
     135           0 :     env = getenv( PATHVARNAME );
     136           0 :     if (env == NULL)
     137           0 :         return NULL;
     138           0 :     str = (char*) malloc( strlen( env ) + 1 );
     139           0 :     strcpy( str, env );
     140             : 
     141             :     /* get the tokens separated by ':' */
     142           0 :     dir = strtok( str, PATHSEPARATOR );
     143             : 
     144           0 :     while ( dir )
     145             :     {
     146             :         /* construct soffice file path */
     147           0 :         file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
     148           0 :         strcpy( file, dir );
     149           0 :         strcat( file, APPENDIX );
     150             : 
     151             :         /* check existence of soffice file */
     152           0 :         if ( !access( file, F_OK ) )
     153             :         {
     154             :             /* resolve symbolic link */
     155           0 :             resolved = realpath( file, buffer );
     156             : 
     157           0 :             if ( resolved != NULL )
     158             :             {
     159             :                 /* get path to program directory */
     160           0 :                 sep = strrchr( resolved, SEPARATOR );
     161             : 
     162           0 :                 if ( sep != NULL )
     163             :                 {
     164           0 :                     pos = sep - resolved;
     165           0 :                     path = (char*) malloc( pos + 1 );
     166           0 :                     strncpy( path, resolved, pos );
     167           0 :                     path[ pos ] = '\0';
     168           0 :                     free( file );
     169           0 :                     break;
     170             :                 }
     171             :             }
     172             :         }
     173             : 
     174           0 :         dir = strtok( NULL, PATHSEPARATOR );
     175           0 :         free( file );
     176             :     }
     177             : 
     178           0 :     free( str );
     179             : 
     180           0 :     return path;
     181             : }
     182             : 
     183             : #endif
     184             : 
     185           0 : char const* cppuhelper_detail_findSofficePath()
     186             : {
     187           0 :     const char* UNOPATHVARNAME = "UNO_PATH";
     188             : 
     189           0 :     char* path = NULL;
     190             : 
     191             :     /* get the installation path from the UNO_PATH environment variable */
     192           0 :     path = getenv( UNOPATHVARNAME );
     193             : 
     194           0 :     if (!path || !path[0])
     195           0 :         path = platformSpecific();
     196             : 
     197           0 :     return path;
     198             : }
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10