LCOV - code coverage report
Current view: top level - cppuhelper/source - findsofficepath.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 42 0.0 %
Date: 2012-08-25 Functions: 0 2 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 12 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #include "sal/config.h"
      30                 :            : 
      31                 :            : #include <stdlib.h>
      32                 :            : #include <string.h>
      33                 :            : 
      34                 :            : #if defined WNT
      35                 :            : 
      36                 :            : #define WIN32_LEAN_AND_MEAN
      37                 :            : #include <windows.h>
      38                 :            : 
      39                 :            : /*
      40                 :            :  * Gets the installation path from the Windows Registry for the specified
      41                 :            :  * registry key.
      42                 :            :  *
      43                 :            :  * @param hroot       open handle to predefined root registry key
      44                 :            :  * @param subKeyName  name of the subkey to open
      45                 :            :  *
      46                 :            :  * @return the installation path or NULL, if no installation was found or
      47                 :            :  *         if an error occurred
      48                 :            :  */
      49                 :            : static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName )
      50                 :            : {
      51                 :            :     HKEY hkey;
      52                 :            :     DWORD type;
      53                 :            :     char* data = NULL;
      54                 :            :     DWORD size;
      55                 :            : 
      56                 :            :     /* open the specified registry key */
      57                 :            :     if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
      58                 :            :     {
      59                 :            :         return NULL;
      60                 :            :     }
      61                 :            : 
      62                 :            :     /* find the type and size of the default value */
      63                 :            :     if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
      64                 :            :     {
      65                 :            :         RegCloseKey( hkey );
      66                 :            :         return NULL;
      67                 :            :     }
      68                 :            : 
      69                 :            :     /* get memory to hold the default value */
      70                 :            :     data = (char*) malloc( size );
      71                 :            : 
      72                 :            :     /* read the default value */
      73                 :            :     if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
      74                 :            :     {
      75                 :            :         RegCloseKey( hkey );
      76                 :            :         return NULL;
      77                 :            :     }
      78                 :            : 
      79                 :            :     /* release registry key handle */
      80                 :            :     RegCloseKey( hkey );
      81                 :            : 
      82                 :            :     return data;
      83                 :            : }
      84                 :            : 
      85                 :            : /*
      86                 :            :  * Gets the installation path from the Windows Registry.
      87                 :            :  *
      88                 :            :  * @return the installation path or NULL, if no installation was found or
      89                 :            :  *         if an error occurred
      90                 :            :  */
      91                 :            : static char* platformSpecific()
      92                 :            : {
      93                 :            :     const char* SUBKEYNAME = "Software\\LibreOffice\\UNO\\InstallPath";
      94                 :            : 
      95                 :            :     char* path = NULL;
      96                 :            : 
      97                 :            :     /* read the key's default value from HKEY_CURRENT_USER */
      98                 :            :     path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
      99                 :            : 
     100                 :            :     if ( path == NULL )
     101                 :            :     {
     102                 :            :         /* read the key's default value from HKEY_LOCAL_MACHINE */
     103                 :            :         path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
     104                 :            :     }
     105                 :            : 
     106                 :            :     return path;
     107                 :            : }
     108                 :            : 
     109                 :            : #else
     110                 :            : 
     111                 :            : #include <unistd.h>
     112                 :            : #include <limits.h>
     113                 :            : 
     114                 :            : /*
     115                 :            :  * Gets the installation path from the PATH environment variable.
     116                 :            :  *
     117                 :            :  * <p>An installation is found, if the executable 'soffice' or a symbolic link
     118                 :            :  * is in one of the directories listed in the PATH environment variable.</p>
     119                 :            :  *
     120                 :            :  * @return the installation path or NULL, if no installation was found or
     121                 :            :  *         if an error occurred
     122                 :            :  */
     123                 :          0 : static char* platformSpecific()
     124                 :            : {
     125                 :          0 :     const int SEPARATOR = '/';
     126                 :          0 :     const char* PATHSEPARATOR = ":";
     127                 :          0 :     const char* PATHVARNAME = "PATH";
     128                 :          0 :     const char* APPENDIX = "/libreoffice";
     129                 :            : 
     130                 :          0 :     char* path = NULL;
     131                 :          0 :     char* env = NULL;
     132                 :          0 :     char* str = NULL;
     133                 :          0 :     char* dir = NULL;
     134                 :          0 :     char* file = NULL;
     135                 :          0 :     char* resolved = NULL;
     136                 :          0 :     char* sep = NULL;
     137                 :            : 
     138                 :            :     char buffer[PATH_MAX];
     139                 :            :     int pos;
     140                 :            : 
     141                 :            :     /* get the value of the PATH environment variable */
     142                 :          0 :     env = getenv( PATHVARNAME );
     143                 :          0 :     str = (char*) malloc( strlen( env ) + 1 );
     144                 :          0 :     strcpy( str, env );
     145                 :            : 
     146                 :            :     /* get the tokens separated by ':' */
     147                 :          0 :     dir = strtok( str, PATHSEPARATOR );
     148                 :            : 
     149         [ #  # ]:          0 :     while ( dir )
     150                 :            :     {
     151                 :            :         /* construct soffice file path */
     152                 :          0 :         file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
     153                 :          0 :         strcpy( file, dir );
     154                 :          0 :         strcat( file, APPENDIX );
     155                 :            : 
     156                 :            :         /* check existence of soffice file */
     157         [ #  # ]:          0 :         if ( !access( file, F_OK ) )
     158                 :            :         {
     159                 :            :             /* resolve symbolic link */
     160                 :          0 :             resolved = realpath( file, buffer );
     161                 :            : 
     162         [ #  # ]:          0 :             if ( resolved != NULL )
     163                 :            :             {
     164                 :            :                 /* get path to program directory */
     165                 :          0 :                 sep = strrchr( resolved, SEPARATOR );
     166                 :            : 
     167         [ #  # ]:          0 :                 if ( sep != NULL )
     168                 :            :                 {
     169                 :          0 :                     pos = sep - resolved;
     170                 :          0 :                     path = (char*) malloc( pos + 1 );
     171                 :          0 :                     strncpy( path, resolved, pos );
     172                 :          0 :                     path[ pos ] = '\0';
     173                 :          0 :                     free( file );
     174                 :          0 :                     break;
     175                 :            :                 }
     176                 :            :             }
     177                 :            :         }
     178                 :            : 
     179                 :          0 :         dir = strtok( NULL, PATHSEPARATOR );
     180                 :          0 :         free( file );
     181                 :            :     }
     182                 :            : 
     183                 :          0 :     free( str );
     184                 :            : 
     185                 :          0 :     return path;
     186                 :            : }
     187                 :            : 
     188                 :            : #endif
     189                 :            : 
     190                 :          0 : char const* cppuhelper_detail_findSofficePath()
     191                 :            : {
     192                 :          0 :     const char* UNOPATHVARNAME = "UNO_PATH";
     193                 :            : 
     194                 :          0 :     char* path = NULL;
     195                 :            : 
     196                 :            :     /* get the installation path from the UNO_PATH environment variable */
     197                 :          0 :     path = getenv( UNOPATHVARNAME );
     198                 :            : 
     199 [ #  # ][ #  # ]:          0 :     if (!path || !path[0])
     200                 :          0 :         path = platformSpecific();
     201                 :            : 
     202                 :          0 :     return path;
     203                 :            : }
     204                 :            : 
     205                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10